问题描述
我有几个词法分析器:lexer_1.mll
,lexer_2.mll
,...
I have several lexers: lexer_1.mll
, lexer_2.mll
, ...
ident
(let ident = regexp
)的某些定义是常见的,并且在这些文件中重复.例如,INTEGER
,FLOAT
,...
Some definitions of ident
(let ident = regexp
) are common and repeated in these files. For instance, the definition of INTEGER
, FLOAT
, ...
有人知道是否有可能在某个地方为它们一次定义一次,然后让.mll
文件调用吗?
Does anyone know if it is possible to define them the once for all somewhere, and let the .mll
files call it?
推荐答案
恐怕没有解决方案,因为ident
似乎已被ocamllex
系统内联.您仍然可以将regexp定义放在文件中,并在需要的地方使用cpp
(或任何其他C预处理程序)将其#include
用作它.当然,这是对cpp
的滥用,因为您没有将其与.c
文件一起使用,但是您将不是第一个这样做的人.
I'm afraid there's no "pure OCaml" solution, as ident
seems to be systematically inlined by ocamllex
. You can still put your regexp definition in a file, and use cpp
(or any other C pre-processor) to #include
it wherever needed though. This is of course an abuse of cpp
, as you're not feeding it with .c
file, but you wouldn't be the first one to do that.
事实上,我碰到了 cppo ,这是一种类似于cpp的实用程序您可能会发现有用的OCaml程序.
As a matter of fact, I've just come across cppo, a cpp-like utility targetting OCaml programs, that you might find useful.
这篇关于是否可以让几个词法分析器共享相同的ident定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!