I’m happy to announce the first release of stele, a tool that writes the syntax-error messages of a Menhir grammar for you.
A Menhir parser that meets bad input raises Error, which says where the parse failed but not why. Menhir can show a real message instead, but only if you write one for every error state (every point where the parse can get stuck). A realistic grammar has hundreds of them, and they go stale as the grammar changes.
stele generates them at build time, from menhir --list-errors and the grammar’s .cmly. With a few aliases and one %on_error_reduce annotation, the calculator from the tutorial reports:
Error: Assuming that the statements are complete, expecting '}'.
--> stray.calc:1:6
1 | { 1; ) }
· ^
· ^^ these statements
· ^ This '{' opens the enclosing construct.
What you get:
- A message for every error state, saying what could come next. Each list is checked against the parser’s tables, so a message never offers a token the parser would reject in that state.
- Location hints: the opening delimiter the input is still inside, and the construct the parser assumed complete. A small runtime library,
stele.runtime, turns these into source spans when the parser fails. It depends only onmenhirLib, so it works under js_of_ocaml and wasm_of_ocaml. stele leaves the rendering to you; Grace works well. - Better wording when you want it: a small config file gives readable names and groups tokens (“an operator”), and an overrides file holds hand-written messages for the few states that need one. Stale entries in either fail the build.
stele builds on François Pottier’s Reachability and Error Diagnosis in LR(1) Parsers (CC 2016), the paper behind --list-errors. It automates the work the paper did by hand for CompCert’s parser. It started inside the Wax toolchain, where it produces the error messages of both its grammars.
To try it:
opam install stele
It needs OCaml ≥ 4.14 and menhir ≥ 20260122. The tutorial takes a small calculator grammar from “Syntax error” to the full setup in ten stages. Its final state is a runnable example to copy.