Menhir generates invalid code for parameterized parser

I’ve been trying to use menhir to write a parameterized parser but I am getting a compilation error, which I suspect is due to some faulty code generation. I provide a minimal working example below that replicates the problem.

Can anyone tell me whether I am missing something or suggest a workaround?

Update: I am aware that a better practice here would be to create a separate tokens.mly file as demonstrated here so that the lexer does not have to be a functor too. I tried this solution too but the problem persists.

Minimal Working Example

To replicate the problem, create a folder with the four files below. Then, run dune build. You should see the following error:

Error: Module Parser in directory _build/default depends on Mylib.
This doesn't make sense to me.

Mylib is the main module of the library and is the only module exposed
outside of the library. Consequently, it should be the one depending on all
the other modules in the library.
Done: 20/31 (jobs: 1)

I think the problem appears clearly when looking at _build/default/parser.ml. Indeed, the generated code features type Mylib.Formula.Make(Annot).t instead of Formula.Make(Annot).t.

Files

formula.ml

module type ANNOT = sig type t end
module Make (Annot: ANNOT) = struct type t = A end

parser.mly

%parameter<Annot: Formula.ANNOT>

%{ open Formula.Make(Annot) %}

%token EOF
%start formula
%type <Formula.Make(Annot).t> formula

%%

formula: EOF { A }

dune-project

(lang dune 2.7)
(using menhir 2.1)

dune

(library
  (name mylib)
  (libraries base menhirLib))

(menhir (modules parser))

This might be fixed by disabling the --infer flag. Dune has a syntax for this:

(menhir
 (modules parser)
 (infer false))
1 Like

This appears to work, thanks!
By the way, is there any drawback of not passing the --infer flag to menhir beyond not being able to do inspection and getting worse error messages?

Also, the problem here seems to be related to the following issues:

I could find some informations here (comment from fpottier) jbuilder doesn't support menhir with --infer · Issue #305 · ocaml/dune · GitHub

1 Like