生成启用增量 API 和检查 API 的解析器

Generate a parser enabling incremental API and inspection API

我有一个由 menhir 和传统 makefile 构建的大项目。首先,我想在我的项目中添加一个像 this project 这样的错误处理机制。

按照示例项目的dune,我设法生成了.mly.mli.ml.cmi.cmounitActionsParser_e.mly 通过以下命令:

$ menhir --only-preprocess-u parser_e.mly > unitActionsParser_e.mly
$ menhir --table --external-tokens Parser_e unitActionsParser_e.mly
$ ocamlfind ocamlc -package menhirLib -c unitActionsParser_e.mli

增量 API 和错误处理确实有效。

然后,我想添加像this project to my project. Then, items state raised an error Error: Unbound value items in my project. Based on the manual and the dune这样的错误恢复,我想我需要在某处添加--inspection

我尝试了 menhir --explain --inspection --table --dump --infer --external-tokens Parser_e unitActionsParser_e.mly,然后 camlfind ocamlc -package menhirLib -c unitActionsParser_e.mli 引发了错误 Unbound type constructor Parser_e.terminal

我还尝试直接在 parser_e.mly 上工作,而不是通过 menhir --explain --inspection --table --dump --infer parser_e.mly 使用 unitActionsParser_e,但它返回了一个错误 Unbound module Utility,其中 Utility 是一个parser_e.mly 所需的另一个文件夹中的模块。我手动复制utility.cm*到[=25=的文件夹后,返回错误Unbound module Sedlexing(这里是a fork where we can reproduce the error) (this is probably related to Interaction with build systems of the manual)。

有谁知道生成解析器(UnitActionsParser_eParser_e)的正确命令和标志是什么,以启用 menhir 的增量 API 和检查 API?

(* link 在 discuss.ocaml.org: https://discuss.ocaml.org/t/generate-a-parser-enabling-incremental-api-and-inspection-api/9380 *)

这个问题确实与interaction of menhir and build systems有关。准确地说,检查 API (--inspection) 需要知道 .mly 的类型信息(包括它的语义动作)。我选择直接在 parse_e.mly 而不是 unitActionsParser_e.mly 上工作,并遵循“在不调用 OCaml 编译器的情况下获取 OCaml 类型信息”的方法:

  • menhir --explain --inspection --table --dump --infer-write-query mockfile.ml parser_e.mly生成mockfile.ml

  • ocamlfind ocamlc -I lib -package sedlex -package menhirLib -i mockfile.ml > sigfile生成sigfile。注意-I lib指的是外部模块的目录,它们的.cm[io]文件应该可以使用了。

  • menhir --explain --inspection --table --dump --infer-read-reply sigfile parser_e.mly 生成特别是 parser_e.mlparser_e.mli.conflictsautomaton.

因此,parser_e.ml 包含更多类型信息,检查 API 在 Parser_e.MenhirInterpreter.