← All papers
First page of Machine-Checked Grammar-Constrained Decoding

Machine-Checked Grammar-Constrained Decoding

Manu Bhat, Arnav Dandu, Kanghee Park, Loris D'Antoni

cs.FL Nov 28, 2026 · v1
Formalizes the GreatGramma grammar-constrained decoding checker in Lean 4, proving it sound and complete and building verified automata components.
Grammar-constrained decoding (GCD) restricts a language model's next-token choices so that its output remains extendable to a string accepted by a user-provided grammar. GreatGramma is a compositional GCD algorithm built from automata-theoretic components. We formalize its executable checker in Lean 4 and prove it sound and complete under explicit assumptions on the vocabulary, lexer, parser, and whitespace. To our knowledge, this is the first machine-checked end-to-end correctness proof for a grammar-constrained decoding algorithm for LLMs. The formalization finds an end-of-stream bug in the published lexer construction and makes explicit a universal-assumption needed for the algorithm. The development also includes reusable verified components for finite-state automata and transducers, pushdown automata, parser overapproximations, and graph search.

Grammar-constrained decoding restricts LLM token choices so outputs stay extendable to strings in a user grammar. Token–terminal misalignment makes efficient checkers subtle, and published decoders have shown soundness and completeness bugs.

The executable checker of the GreatGramma algorithm is formalized in Lean 4. The formalization covers partial deterministic FSAs and FSTs, nondeterministic PDAs, a one-lookahead maximal-munch lexer specification (PartialLexRel) proved equivalent to an executable transducer, precomputed token-spanner tables, and a verified DFS for single-producible terminals. Parser preprocessing is justified by stack-invariance and NFA overapproximation lemmas, and these combine into an end-to-end correctness theorem by induction.

The checker is proved sound and complete under explicit assumptions on vocabulary, lexer, parser, and whitespace, including a universal-separator assumption on whitespace. The authors describe this as the first machine-checked end-to-end proof for an LLM grammar-constrained decoding algorithm. The proof also uncovered an end-of-stream bug in the published lexer construction, which is fixed by adding a distinguished start state.