Generate a parser enabling incremental API and inspection API

I have a big project built by menhir and traditional makefile. First, I wanted to add a mechanism of error handling like this project to my project.

By following the dune of the sample project, I managed to generate .mly, .mli, .ml, .cmi and .cmo of unitActionsParser_e.mly by the following commands:

$ 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

And the incremental API and error handling did work.

Then, I wanted to add error recovery like this project to my project. Then, items state raised an error Error: Unbound value items in my project. Based on the manual of menhir and the dune, I guess I need to add --inspection somewhere.

I tried menhir --explain --inspection --table --dump --infer --external-tokens Parser_e unitActionsParser_e.mly, then camlfind ocamlc -package menhirLib -c unitActionsParser_e.mli raised an error Unbound type constructor Parser_e.terminal.

I also tried to work on parser_e.mly directly rather than using unitActionsParser_e by menhir --explain --inspection --table --dump --infer parser_e.mly, but it returned an error Unbound module Utility where Utility is a module in another folder needed by parser_e.mly. After I manually copied utility.cm* to the folder of parser_e.mly, it returned an error Unbound module Sedlexing.

Does anyone know what are the correct commands and flags to generate a parser (either UnitActionsParser_e or Parser_e) enabling incremental API and inspection API of menhir?

(* link in StackOverflow: stackoverflow dot com slash questions slash 71193987 *)

This question is indeed related to the interaction of menhir and build systems. Precisely, the inspection API (--inspection) requires the type information of .mly (including its semantic actions) to be known. I chose to directly work on parse_e.mly rather than unitActionsParser_e.mly, and followed the approach of “Obtaining OCaml type information without calling the OCaml compiler”:

  • menhir --explain --inspection --table --dump --infer-write-query mockfile.ml parser_e.mly to generate mockfile.ml

  • ocamlfind ocamlc -I lib -package sedlex -package menhirLib -i mockfile.ml > sigfile to generate sigfile. Note that -I lib refers to the directory of external modules, their .cm[io] files should be ready to use.

  • menhir --explain --inspection --table --dump --infer-read-reply sigfile parser_e.mly to generate especially parser_e.ml, parser_e.mli, .conflicts and automaton.

As a result, parser_e.ml contains more type information and the inspection API is in Parser_e.MenhirInterpreter.