Papers
Topics
Authors
Recent
Search
2000 character limit reached

Grammar-Constrained Decoding (GCD)

Updated 6 July 2026
  • Grammar-Constrained Decoding (GCD) is a technique that restricts language-model generation to valid sequences defined by formal grammars using token-level masking.
  • It employs methods such as pushdown reachability, finite-state lexers, and template constraints to ensure outputs meet strict syntactic and structural requirements.
  • GCD enhances syntactic validity and execution accuracy in applications like Text-to-SQL, structured NLP, DSL generation, and robotics command understanding.

Grammar-Constrained Decoding (GCD) is an inference-time family of methods that restricts language-model generation to strings that remain within the prefix language of a formal specification, most often a context-free grammar, a regular language, or a compiled automaton, so that completed outputs satisfy structural constraints by construction (Jivani et al., 30 Apr 2026). In its canonical autoregressive form, GCD masks invalid next tokens and renormalizes the model distribution; more specialized variants couple decoding to pushdown reachability, finite-state lexers, template constraints, or speculative verification, and some work uses the term more broadly for post-hoc grammar-enforced canonicalization rather than token-level masking (Alpay et al., 4 Mar 2026, Huo et al., 5 Apr 2026).

1. Formal definition and problem setting

The standard formalization begins with a grammar G=(N,Σ,P,S)G = (N, \Sigma, P, S), a tokenizer mapping model vocabulary items to terminal strings, and an autoregressive model that would otherwise optimize unconstrained next-token likelihood. In the Text-to-SQL setting, this is written as

y=argmaxyL(G)P(yx),y^* = \arg\max_{y \in L(G)} P(y \mid x),

or, when a reusable template language TT is also imposed,

y=argmaxyL(G)TP(yx).y^* = \arg\max_{y \in L(G)\cap T} P(y \mid x).

At decoding step tt, grammar guidance transforms the model’s next-token distribution by zeroing invalid token ids and renormalizing over the admissible set (Jivani et al., 30 Apr 2026).

A more general formulation treats GCD as the coupling of autoregressive next-token probabilities with a reachability oracle over a pushdown system compiled from a CFG. With tokenizer homomorphism τ:VΣ\tau: V \to \Sigma^*, admissible next tokens after terminal prefix uu are

ΩG(u)={vV:CoReachG(uτ(v))},\Omega_G(u)=\{v\in V:\mathrm{CoReach}_G(u\tau(v))\neq\emptyset\},

and the masked next-token distribution is

qθ,A(vy<t)=exp(t(v))1[vA]wAexp(t(w)).q_{\theta,A}(v\mid y_{<t})=\frac{\exp(\ell_t(v))\mathbf{1}[v\in A]}{\sum_{w\in A}\exp(\ell_t(w))}.

In GCD, A=ΩG(τ(y<t))A=\Omega_G(\tau(y_{<t})); this guarantees soundness in the sense that generated sequences terminate only in y=argmaxyL(G)P(yx),y^* = \arg\max_{y \in L(G)} P(y \mid x),0 (Alpay et al., 4 Mar 2026).

A central refinement is the use of input-dependent grammars, written as y=argmaxyL(G)P(yx),y^* = \arg\max_{y \in L(G)} P(y \mid x),1, in which grammar rules are instantiated from the input. This is crucial when the valid output space depends on the particular example, as in entity disambiguation candidate sets or constituency parsing where the terminals must be exactly the input sentence tokens (Geng et al., 2023). A separate but related refinement appears in template-based systems such as TeCoD, where the grammar restriction is intersected with a reusable SQL template derived from prior labeled workloads (Jivani et al., 30 Apr 2026).

The term is not used uniformly across the literature. Most papers reserve GCD for token-level masking during generation. By contrast, the robotics command-understanding system built with a fine-tuned LLM, a Structured LLM, and a Lark-based canonicalizer explicitly states that it does not enforce y=argmaxyL(G)P(yx),y^* = \arg\max_{y \in L(G)} P(y \mid x),2 during token-level generation; instead, it enforces grammatical validity post hoc through parsing, canonicalization, ontology checks, and corrective prompting (Huo et al., 5 Apr 2026). This broader usage preserves the idea of grammar-enforced outputs while altering the operational locus of the constraint.

2. Parsing, automata, and token-level enforcement

In deployed systems, GCD is realized through incremental parsing or automaton execution over the current prefix. Guidance-style grammar masking uses an online Earley parser with prediction, scanning, and completion, together with a token trie and a grammar trie, to compute the set of next subword tokens whose byte sequences keep the parser state non-dead (Netz et al., 2024). Formatron similarly maintains Earley sets and derives valid tokens from postdot terminals, while GreatGramma composes a lexer transducer with a detokenizing transducer so that subword tokens can be related soundly to grammar terminals despite tokenizer–grammar misalignment (Park et al., 7 Feb 2025, Sun et al., 1 Jun 2025).

Tokenizer alignment is a recurrent technical bottleneck. GreatGramma models the grammar’s lexer as a finite-state transducer, models the tokenizer as a detokenizing transducer, composes them, and precomputes an inverse token spanner table that maps lexer states and realizable terminal sequences back to model tokens. This lets online masking operate over subword vocabularies without sacrificing soundness, and directly addresses the fact that a single subword token may cover multiple grammar terminals, part of a terminal, or cross token boundaries (Park et al., 7 Feb 2025). Earlier practical systems also emphasize trie-based synchronization between grammar terminals and model tokens; in Guidance, many terminals are bytes or byte ranges, and the grammar trie can auto-emit forced prefixes or suffixes without querying the model (Netz et al., 2024).

Although autoregressive left-to-right decoding is the canonical setting, the same structural objective appears in non-autoregressive or editing-oriented models. For diffusion LLMs, LAVE handles incomplete prefixes with internal [MASK] slots by sampling candidate fillings for masked positions and checking whether at least one witness completion remains extendable under a CFG parser. Its acceptance test is therefore existential rather than purely local, because the intermediate output is not a complete left prefix (Zhang et al., 31 Jan 2026). In editing, Copy-as-Decode recasts output as a program over <copy lines="i-j"/> and <gen>...</gen> primitives; a token-level finite-state machine guarantees syntactic validity of the edit program, while copied spans are inserted into the KV cache via parallel prefill rather than ordinary autoregressive steps (Liu, 20 Apr 2026).

These mechanisms share a common invariant: every emitted token must preserve membership in the prefix closure of the target language. What differs is the runtime object that certifies viability—an Earley chart, an LR or PDA state, a DFA over lexemes, a transducer composition, or, in diffusion settings, a witness-based extendability check over masked positions.

3. Grammar shape, computational cost, and acceleration

The computational behavior of GCD depends not only on the language being enforced but also on how that language is represented. The Oracle Invariance Theorem states that language-equivalent grammars induce identical admissible next-token sets for every prefix, hence identical logit masks, but compiled state spaces and online ambiguity costs can still differ substantially (Alpay et al., 4 Mar 2026). The same paper introduces the compiled control-state count

y=argmaxyL(G)P(yx),y^* = \arg\max_{y \in L(G)} P(y \mid x),3

shows a factor blowup of y=argmaxyL(G)P(yx),y^* = \arg\max_{y \in L(G)} P(y \mid x),4 for two equivalent grammars of y=argmaxyL(G)P(yx),y^* = \arg\max_{y \in L(G)} P(y \mid x),5, and defines Structural Ambiguity Cost (SAC), under which right-recursive and concatenative grammars with the same language can have y=argmaxyL(G)P(yx),y^* = \arg\max_{y \in L(G)} P(y \mid x),6 versus y=argmaxyL(G)P(yx),y^* = \arg\max_{y \in L(G)} P(y \mid x),7 per-token ambiguity growth (Alpay et al., 4 Mar 2026). This makes grammar refactoring a latency problem as much as a language-design problem.

The systems literature addresses these costs through pruning, offline compilation, token-class compression, and constraint decomposition. The following examples are representative.

Method Core mechanism Reported effect
TeCoD (Jivani et al., 30 Apr 2026) Partition the template into static and dynamic segments; apply GCD only to literal slots, with boundary-aware regexes and KV-cache reuse constrained decoding time roughly 0.4–0.6× of the library default; up to 2.2× lower latency on matched queries
GreatGramma (Park et al., 7 Feb 2025) Lexer–tokenizer transducer composition plus inverse token spanner tables and parser-side preclassification of always-accepted/context-dependent sequences 17.71× faster offline preprocessing than existing approaches
CFGzip (Sullivan et al., 28 May 2026) Compress token space into equivalence classes based on grammar-transition behavior latency reduction of up to two orders of magnitude in grammar-engine overhead and up to 7.5x speedup in total constrained generation time
Formatron with ZapFormat (Sun et al., 1 Jun 2025) Earley-driven dynamic pruning, rejected-prefix tracking, and state caching inference speed up to 2x compared to state-of-the-art implementations
wgrammar (Wang et al., 22 Jul 2025) Decompose constraints into static and dynamic parts; precompile static structure and instantiate dynamic snippets with operator graphs and mask caching up to 250x speedup over existing systems

These accelerations do not all target the same regime. TeCoD exploits recurring enterprise workloads and template recurrence; GreatGramma targets general CFG soundness with faster offline preprocessing; CFGzip is engine-agnostic token-class compression for complex static grammars; Formatron reduces Earley-state growth through dependency-based pruning; wgrammar sacrifices full CFG generality for a fast path over regular, decomposition-friendly structures (Jivani et al., 30 Apr 2026, Park et al., 7 Feb 2025, Sullivan et al., 28 May 2026, Sun et al., 1 Jun 2025, Wang et al., 22 Jul 2025).

A plausible implication is that “grammar-constrained decoding” should be understood as a stack of interacting costs: grammar compilation, parser-state evolution, token-mask construction, and tokenizer alignment. Oracle invariance makes clear that language equivalence does not imply runtime equivalence, and modern systems work primarily by reducing one or more of these four costs without changing the accepted language (Alpay et al., 4 Mar 2026).

4. Probabilistic distortion, future validity, and globally constrained sampling

Hard masking guarantees validity, but it generally does not sample from the model’s distribution conditioned on the full grammar constraint. One line of work formalizes the intended target as the grammar-conditional distribution and shows that ordinary local masking is biased because it ignores differences in future completion probability among locally valid tokens. Grammar-Aligned Decoding defines the desired stepwise law as

y=argmaxyL(G)P(yx),y^* = \arg\max_{y \in L(G)} P(y \mid x),8

where y=argmaxyL(G)P(yx),y^* = \arg\max_{y \in L(G)} P(y \mid x),9 is the expected future grammaticality of the prefix; the ASAp algorithm refines an overestimate of TT0 from sampled trajectories and provably converges in probability to the true conditional distribution under the grammar (Park et al., 2024).

A sharper formulation appears in speculative decoding. The locally projected distribution is

TT1

whereas the true grammar-conditional step distribution is

TT2

with TT3. The paper proves that any speculative decoder satisfying local mask access, Leviathan rejection, and rollback soundness samples from TT4 rather than TT5, and identifies future validity TT6 as the missing correction statistic (Nie et al., 8 May 2026). Local masking is exact if and only if TT7 is constant over locally valid tokens.

This diagnosis connects to a broader view in which hard masking is a Doob TT8-transform only when the survival or completion probabilities of admissible next tokens are equal. In the pushdown-reachability treatment, the same issue is expressed through survival-probability spread and one-step distortion bounds between hard-masked decoding and the true conditional sampler (Alpay et al., 4 Mar 2026). In finite-budget settings, the distinction also appears between Locally Constrained Decoding and Globally Constrained Decoding: the latter uses finite automata and backward reachability over a fixed horizon so that only prefixes completable within the budget survive, and P-GCD further multiplies the automaton with an HMM distilled from the LLM to incorporate probabilistic lookahead in Sequential Monte Carlo proposals (Dang et al., 1 Jun 2026).

The resulting picture is two-layered. Validity by construction is a formal-language property; faithfulness to the model’s own grammar-conditional distribution is a probabilistic property. Standard GCD solves the first problem directly. The second requires future-validity information, either exactly or approximately.

5. Applications and empirical record

GCD has been applied across Text-to-SQL, structured NLP, modeling DSLs, robot command understanding, editing, and diffusion-based code or chemistry generation. The common empirical pattern is strong improvement in syntactic validity, schema adherence, or execution validity, with runtime overhead that ranges from negligible to dominant depending on grammar complexity and engine design.

Domain Representative setup Reported outcome
Text-to-SQL (Jivani et al., 30 Apr 2026) TeCoD with template selection, flexible grammars, and partitioned constrained decoding up to 36% higher [execution accuracy](https://www.emergentmind.com/topics/execution-accuracy-ea) than ICL on matched queries and up to 2.2× lower latency
Structured NLP without finetuning (Geng et al., 2023) Input-dependent grammars for closed IE, entity disambiguation, and constituency parsing LLaMA-33B cIE 17.5 → 36.0 F1; ED 54.1 → 80.3 average accuracy; CP validity 64.2 → 100.0
DSL/model generation (Netz et al., 2024) Grammar masking with Guidance on MontiCore-based DSLs Llama 3 8B CD4A parsed 41.97% unconstrained versus 92.63% constrained
Robot command understanding (Huo et al., 5 Apr 2026) Fine-tuned LLM plus grammar-based canonicalizer and validation loop Hybrid system reached 48.48% EM and 88.07% average JSON Similarity
Editing (Liu, 20 Apr 2026) Copy-as-Decode with FSM-constrained edit programs and parallel-prefill copy spans copy kernel speedup 6.8×–303×; oracle programs round-trip on all 482 cases
Diffusion LLMs (Zhang et al., 31 Jan 2026) LAVE lookahead-then-verify under CFGs syntactic@k rises to ≈100% across tasks and models with negligible runtime overhead

The Text-to-SQL results are especially revealing because they combine grammar enforcement with workload structure. TeCoD first matches a new natural-language query to a reusable SQL template using embeddings and a fine-tuned NLI reranker, then constrains generation to the intersection of a flexible SQL grammar and the selected template language. On matched queries it consistently outperforms zero-shot, ICL-3, and soft template guidance, and specific gains include Granite-8B on Spider syn from 87.03% to 98.62%, Llama-8B on BIRD syn from 73.90% to 89.22%, and CodeS-15B on BIRD non-syn from 78.08% to 90.41% (Jivani et al., 30 Apr 2026).

In structured NLP without finetuning, GCD acts as a unified framework rather than a task-specific decoder. The same grammar-first machinery supports information extraction, entity disambiguation, and constituency parsing by changing the grammar, often making it input-dependent. The results show that validity guarantees can coexist with substantial task gains, although specialized supervised parsers still dominate in constituency parsing (Geng et al., 2023).

The DSL and robotics papers illustrate two different deployment styles. Grammar masking on MontiCore DSLs dramatically increases parse success but can introduce large slowdowns, such as Phi-3 Mini on CD4A from 4.63 s unconstrained to 138.29 s constrained (Netz et al., 2024). The robotics system, by contrast, places the grammar downstream of semantic inference: the LLM proposes action frames, then parsing, canonicalization, and iterative correction force a robot-readable JSON format and a safe empty fallback {"frames": []} when recovery fails (Huo et al., 5 Apr 2026).

6. Limitations, misconceptions, and security

A persistent misconception is that grammar validity implies overall correctness. Multiple papers state the opposite explicitly. Grammar masking ensures syntax, not semantics; syntactically valid outputs can still be semantically meaningless, structurally appropriate but task-wrong, or unsafe (Netz et al., 2024). In constituency parsing, for example, GCD ensures 100.0% valid trees under the input-dependent grammar, yet still remains far below specialized supervised parsers in F1 (Geng et al., 2023).

Another limitation is tokenizer and lexer mismatch. GreatGramma’s entire construction exists because subword tokens and grammar terminals are not naturally aligned; a token may straddle terminal boundaries, and context-sensitive lexing phenomena such as Java’s >> versus > > are outside the paper’s 1-lookahead maximal-munch model (Park et al., 7 Feb 2025). Even when the grammar is correct, style sensitivity can degrade performance. TeCoD reports that fixed-string grammars are brittle to casing and spacing changes, while left/right boundary context in literal-slot generation is crucial: removing context causes large Execution Match declines, such as Llama dropping from ~92% to ~79% (Jivani et al., 30 Apr 2026).

Editing introduces a different fragility: Copy-as-Decode is mechanism-level lossless when the edit program is correct, but line-level address noise is catastrophic. In the perturbation study, pooled exact match drops from 100% to 15.48% under off-by-one noise, which localizes the main failure mode to span selection rather than the constrained mechanism itself (Liu, 20 Apr 2026). This suggests that, in structured editing, the control language may be exact while the control policy remains brittle.

The safety literature adds a more counterintuitive limitation: GCD can itself become an attack surface. CodeSpear shows that constraining a model with a benign code grammar can suppress natural-language refusal modes and increase the attack success rate by more than 30 percentage points on average across 10 LLMs and 4 benchmarks (Zhang et al., 10 Jun 2026). The defense, CodeShield, aligns the model in the code modality by preferring diverse harmless honeypot code over harmful code under grammar constraints, while preserving natural-language refusals when that channel is available (Zhang et al., 10 Jun 2026). This makes clear that “reliability-oriented” syntax control is not safety-neutral.

Current research directions follow directly from these limitations. Theoretical work calls for automated grammar optimization under semantic invariants, low-SAC normal forms, efficient estimation of survival or future-validity functions, broader deterministic pushdown compilation under realistic tokenizers, and joint optimization of grammar and tokenizer to minimize both runtime cost and distributional distortion (Alpay et al., 4 Mar 2026). A plausible synthesis is that mature GCD systems will need to combine formal-language enforcement with semantic validators, probabilistic correction terms, and safety-aware policy layers rather than treating syntax alone as the decisive constraint.

Topic to Video (Beta)

No one has generated a video about this topic yet.

Whiteboard

No one has generated a whiteboard explanation for this topic yet.

Follow Topic

Get notified by email when new papers are published related to Grammar-Constrained Decoding (GCD).