Skip to content
Latchkey

dune menhir / ocamllex generation error in CI

dune runs menhir or ocamllex to generate parser/lexer code before compiling. A grammar conflict or a lexer syntax error stops generation, so the produced .ml never exists and the build fails.

What this error means

dune build fails inside a menhir or ocamllex rule with "Warning: N states have conflicts", "menhir: ... failed", or an ocamllex parse error, before the OCaml compiler runs.

menhir
File "src/parser.mly", line 42:
Error: 3 states have shift/reduce conflicts.
menhir: 1 error(s) encountered

Common causes

Grammar conflicts treated as errors

menhir reports shift/reduce or reduce/reduce conflicts; with strict settings these fail the build rather than warn.

A lexer or grammar syntax error

An ocamllex rule or menhir declaration has a syntax mistake, so generation cannot produce valid OCaml.

How to fix it

Resolve grammar conflicts

  1. Run menhir with --explain to get a conflict report.
  2. Add precedence declarations or refactor the ambiguous rules.
  3. Rebuild until menhir reports no conflicts.
Terminal
menhir --explain src/parser.mly

Fix the lexer/grammar syntax

Correct the ocamllex or menhir syntax the error points at, then let dune regenerate.

Terminal
dune build src/parser.ml src/lexer.ml

How to prevent it

  • Keep grammars conflict-free with explicit precedence.
  • Run menhir --explain locally before pushing grammar changes.
  • Regenerate parser/lexer code as part of local builds.

Frequently asked questions

What causes "menhir / ocamllex rule failure"?
menhir reports shift/reduce or reduce/reduce conflicts; with strict settings these fail the build rather than warn.
How do I fix menhir / ocamllex rule failure?
Resolve grammar conflicts

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card