Machine-Checked Grammar-Constrained Decoding
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.
