Is it possible to let several lexers share same id

2019-08-26 17:12发布

问题:

I have several lexers: lexer_1.mll, lexer_2.mll, ...

Some definitions of ident (let ident = regexp) are common and repeated in these files. For instance, the definition of INTEGER, FLOAT, ...

Does anyone know if it is possible to define them the once for all somewhere, and let the .mll files call it?

回答1:

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.

As a matter of fact, I've just come across cppo, a cpp-like utility targetting OCaml programs, that you might find useful.