arXiv:2608.28229cs.AIAug 2026Cited by 1 paper on PaperVantage

Manuscript Offprint

arXiv:2608.28229

Stay Within Your Bounds: Distance-Guided Decoding for Guaranteed Context-Free Grammar Compliance

Vincenzo Collura, Karim Tit, Eleonora Giunchiglia, Mike Papadakis, Maxime Cordy

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original. This text is shared under the same licence.

Abstract

Grammar-constrained decoding helps large language models produce syntactically valid structured outputs, such as code, JSON, and SQL. For context-free grammars, many practical decoders enforce local prefix feasibility: each token must keep the current prefix extendable to some valid completion. Yet, under tokenizer–grammar mismatch and finite token budgets, feasible prefixes may still fail to reach acceptance. We propose a lookahead-guided decoding framework for context-free grammars based on pushdown automata. Offline, we compute bounded pushdown summaries with reachability labels and upper-bound distances to acceptance. Online, these estimates guide horizon-aware pruning and beam search. The resulting decoder is syntactically sound: every output is accepted by the target grammar. Experiments on JSON, SQL, and Linear Temporal Logic (LTL) show both consistent syntactic validity and improved completion quality over existing baselines.

1 Introduction

Large language models (LLMs) are increasingly used to generate structured objects such as programs, queries, and data records. In these settings, syntactic well-formedness is not merely a formatting preference: invalid outputs may be rejected by downstream parsers, violate interface contracts, or fail to execute. This has motivated a growing line of work on constrained and guided decoding, including grammar-constrained generation, type-constrained code generation, and structured generation engines for LLMs (Willard and Louf, 2023; Beurer-Kellner et al., 2024; Ugare et al., 2025; Dong et al., 2025; Park et al., 2025).

For context-free grammar constraints, many practical decoders enforce validity through local prefix feasibility. At each step, they compute which tokens keep the current prefix extendable to at least one valid completion and mask the remaining tokens. This mechanism is effective for preventing immediate syntax violations and has been implemented in several recent systems for structured generation (Willard and Louf, 2023; Beurer-Kellner et al., 2024; Ugare et al., 2025; Dong et al., 2025; Park et al., 2025; Chen et al., 2025). However, prefix feasibility is weaker than completed-output validity under a finite token budget. A prefix may remain locally admissible while moving toward configurations that require too many remaining tokens, additional structural commitments, or low-probability continuations. Thus, local masking preserves prefix validity, but it does not quantify progress toward acceptance.

A complementary line of work studies global control of autoregressive generation using automata, dynamic programming, probabilistic steering, or sampling-based inference (Zhang et al., 2023, 2024; Albinhassan et al., 2026; Loula et al., 2025; Lipkin et al., 2025). These methods show that constrained generation can benefit from reasoning about future completions. For context-free grammar constraints, however, such lookahead is difficult: the natural operational model is a pushdown automaton (PDA), whose configurations include an unbounded stack. This yields an infinite configuration space, making direct finite-state value computation lookahead inapplicable.

In this paper, we introduce SWYB, a lookahead-guided decoding framework for LLM generation under context-free grammar constraints. SWYB augments local grammar masking with distance-to-acceptance estimates computed over a pushdown representation. Offline, we construct bounded stack summaries using ideas from the reachability analysis of pushdown systems (Bouajjani et al., 1997)

The code to run all our experiments is available on GitHub: https://github.com/Enzossssss/SWYB.

Figure

Figure 1: Overview of SWYB: Model top-K tokens are validated by tokenizer-aware PDA execution, filtered using reachability and distance-to-acceptance estimates, and combined with PDA-proposed candidates. Surviving tokens are reranked and used to update the beam state.

and weighted pushdown systems (Reps et al., 2003). These summaries associate abstract PDA configurations with reachability labels and token-level upper-bound distances to acceptance. Online, the decoder uses these estimates for pruning and reranking in the beam search.

A practical difficulty is that LLM vocabularies are defined over subword tokens, whereas grammars are usually specified over characters, terminals, or lexical units. SWYB therefore includes a tokenizer-aware consumption procedure for matching model tokens against PDA transitions. At each decoding step, it combines model top-k tokens with automaton-proposed backup tokens, validates the resulting candidates through budgeted successor exploration, and uses distance information to favor continuations that make structural progress. The resulting decoder is syntactically sound: every completed output returned by the algorithm is accepted by the target PDA.

In summary, our contributions are:

• A sound pushdown lookahead mechanism based on bounded reachability and token-distances to acceptance.

• A tokenizer-aware decoding algorithm combining model top-k, automaton proposals, and budgeted PDA exploration.

• Experiments on JSON, SQL and LTL grammars showing consistent syntactic correctness and improved completion quality.

References (14)