NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Flexible and Efficient Grammar-Constrained Decoding

Kanghee Park, Timothy Zhou, Loris D'Antoni

Large Language Models (LLMs) are increasingly used to generate structured outputs like Python code or JSON objects. However, ensuring these models adhere to strict syntactic rules remains a significant challenge. GREATGRAMMA is a novel grammar-constrained decoding (GCD) framework designed to eliminate the trade-off between slow generation and long setup times. It achieves a 17x reduction in offline preprocessing time compared to current state-of-the-art methods while maintaining the high-speed online generation required for real-time applications.

The Challenge: Token Misalignment

The primary obstacle in GCD is token misalignment. LLMs generate text using a vocabulary of "subword tokens" (e.g., py, th, on), whereas formal grammars and parsers operate on "terminals" (e.g., keywords like if, or operators like +). A single LLM token might span multiple terminals or only a fragment of one, making it difficult to determine if a token is syntactically valid mid-generation.

Existing solutions typically fall into two categories:

  1. Slow Online Generation: Tools like OUTLINES offer flexibility but significantly slow down generation because they must re-verify the grammar for every single token.
  2. Slow Offline Preprocessing: Tools like SYNCODE offer fast generation but require an extensive "preprocessing" phase—often taking 30 minutes or more for a single grammar. This makes them impractical for dynamic tasks where grammars change frequently.

Methods: The GREATGRAMMA Framework

GREATGRAMMA bridges this gap by mathematically composing the LLM's detokenizer with the grammar's lexer into a single formal structure. The core innovation is the Token Spanner Table.

Overview of the GREATGRAMMA framework, illustrating the construction of the Token Spanner Table from the lexing automaton and subword vocabulary.

Key Components:

  • Token-Level Lexing Transducer: GREATGRAMMA uses Finite State Transducers (FSTs) to map exactly which sequences of grammar terminals are produced when the LLM emits a specific token from a specific lexer state.
  • Realizability Analysis: Instead of analyzing every theoretical path in a grammar, the framework focuses only on "realizable" sequences—those that can actually be constructed using the specific tokens available in the LLM's vocabulary. This drastically narrows the search space during preprocessing.
  • Stack Invariance & FSA Over-approximation: To minimize overhead during generation, the algorithm identifies "safe" tokens that are valid regardless of the code's nesting level (the "stack"). It only performs deep context-dependent checks on ambiguous tokens.
  • Efficient Implementation: The entire tool is implemented in approximately 900 lines of Python, built upon the LARK parsing library, demonstrating that complex GCD does not require overly heavy infrastructure.

Key Results and Insights

The authors evaluated GREATGRAMMA against major tokenizers (Llama-3, Qwen-2) and complex grammars for Go, Java, and Python.

  • Massive Preprocessing Speedup: GREATGRAMMA is 17.71x faster at preprocessing than SYNCODE. For instance, while SYNCODE requires 46 minutes to process a Python grammar for Llama-3, GREATGRAMMA completes the task in under 3 minutes.
  • Elite Online Performance: The per-token masking overhead is just 5–32 milliseconds. This is 2.73x faster than SYNCODE and roughly 550x faster than the flexible OUTLINES approach.
  • Robustness and Soundness: A critical finding was that existing tools like SYNCODE and XGRAMMAR often suffer from "soundness bugs," leading to crashes or incorrect token masking. GREATGRAMMA’s formal automata-based approach avoids these pitfalls, successfully handling all tested grammars without failure.

Limitations and Future Work

While highly effective, GREATGRAMMA currently assumes "maximal munch" lexing (taking the longest possible match), which can lead to edge-case errors in languages like Java (e.g., confusing nested generics >> with bitwise operators). Future iterations may explore hardware acceleration via GPUs and support for non-deterministic parsers to further expand the framework's utility.

Conclusion

GREATGRAMMA proves that high preprocessing times are not an unavoidable tax for fast grammar-constrained generation. By focusing on realizable terminal sequences and formal transducer composition, it provides a flexible, fast, and robust solution for ensuring LLM reliability in structured data tasks.

Summary updated

BibTeX
@inproceedings{park2025flexible,
  author = {Kanghee Park and Timothy Zhou and Loris D'Antoni},
  title = {{Flexible and Efficient Grammar-Constrained Decoding}},
  booktitle = {International Conference on Machine Learning},
  year = {2025},
  url = {https://openreview.net/forum?id=L6CYAzpO1k},
}
Figure