I would like to define a keyword_table
which maps some strings to some tokens, and I would like to make this table visible for both parser.mly
and lexer.mll
.
It seems that the table has to be defined in parser.mly
,
%{
open Utility (* where hash_table is defined to make a table from a list *)
let keyword_table = hash_table [
"Call", CALL; "Case", CASE; "Close", CLOSE; "Const", CONST;
"Declare", DECLARE; "DefBool", DEFBOOL; "DefByte", DEFBYTE ]
%}
However, I could NOT use it in lexer.mll
, for instance
{
open Parser
let x = keyword_table (* doesn't work *)
let x = Parser.keyword_table (* doesn't work *)
let x = Parsing.keyword_table (* doesn't work *)
}
Could anyone tell me where is the problem? Isn't it possible to make a data visible for both parser.mly
and lexer.mll
?
Yes, it is fairly straightforward. You could simply place the data in a third .ml file and reference that:
In the .mly:
In the .mll:
You will not be able to refer to the internal definitions of
parse.mly
in other files. Whenocamlyacc
runs, it will generate aparse.mli
that won't make them available.As mentioned in gsg's answer,
ocamlyacc
generates amli
interface together with theml
implementation of the parser, and only exports the type of tokens and the entry points. According to http://caml.inria.fr/mantis/view.php?id=1703, this is unlikely to change, so you basically have two solutions:mli
afterwards (I usually have a rule in myMakefile
that simplyrm
it, but you might want to just add the necessary signatures instead).menhir
as suggested in the bug report mentioned above.