Using jbuilder with ocamllex/ocamlyacc

I’m trying to get a jbuilder project to use ocamllex and ocamlyacc to build a lexer and parser, but I can’t get things to work. I have files Project_Lexer.mll and Project_Parser.mly in my directory, and here is the jbuild file:

(jbuild_version 1)

(ocamlyacc (Project_Parser.mly))
(ocamllex (Project_Lexer.mll))

(library
  ((name project)
   (public_name project)
   (libraries (various_things))
   (preprocess (pps (ppx_jane)))))

With that jbuild file, I get an error saying that there is no Project_Lexer.ml (and indeed there is no such file anywhere in _build). I tried adding a dependency line to the library section:

(preprocessor_deps (Project_Lexer.ml Project_Parser.ml))

but then I get an error saying No rule found for _build/default/lib/Project_Lexer.ml. Reading the appropriate section of the jbuilder documentation, it seems like the ocamllex part above should have provided this rule.

I can’t tell what the proper incantations to get this working is from the jbuilder documentation. Any help?

Try removing the file extensions from the ocamlyacc and ocamllex rules in your example.

I think you need to use the file name without .mly, .mll extension, eg.

(ocamllex (project_lexer))
(ocamlyacc (project_parser))

B.

Removing the file extensions worked. I think I should submit a PR to the docs with an example so that this is clearer.

3 Likes