[ANN] stele 0.1.0: syntax-error messages for Menhir grammars

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 on menhirLib, 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.

This is very cool!

My project already has quite an extensive hand-written .messages file, but I could still see us depending on the runtime library so that we could add extra locations with the <2>The opening brace was here functionality.

It seems from the outside like these kinds of extensions could be nice to have in menhir itself, is that something you’ve considered?

This looks nice :slight_smile:

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.

Are you aware of lrgrep (by @let-def )? It is trying to solve the “getting stale” part by providing a more robust way of writing the error messages. It seems combining both projects could be great: generate default messages with stele, and use lrgrep for more precise hints in a manageable way.