0

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?

4

2 回答 2

0

As mentioned in gsg's answer, ocamlyacc generates a mli interface together with the ml 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:

  • modify the generated mli afterwards (I usually have a rule in my Makefile that simply rm it, but you might want to just add the necessary signatures instead).
  • use menhir as suggested in the bug report mentioned above.
于 2014-02-03T14:03:25.363 回答
0

是的,这很简单。您可以简单地将数据放在第三个 .ml 文件中并引用:

在 .mly 中:

%{ 
    open Data
%}

在 .mll 中:

{
    open Data
}

您将无法引用parse.mly其他文件中的内部定义。运行时ocamlyacc,它将生成一个parse.mli不会使它们可用的。

于 2014-02-03T04:35:24.767 回答