arXiv:2609.39869cs.AISep 2026

Manuscript Offprint

arXiv:2609.39869

GrammarRL: Effective Grammar-Constrained Decoding via Reinforcement Learning

Gabriele Tuccio, Antonino Furnari, Aldo Gangemi, Misael Mongiovì

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

ABSTRACT

Grammar-constrained generation guarantees syntactic validity, but can substantially degrade semantic quality when the model's preferred outputs are poorly aligned with the imposed grammar. This trade-off is particularly severe when the prompt is underspecified or the model has limited instruction-following ability. Beam search can partially mitigate these failures by exploring multiple valid sequences, but its computational cost grows with beam width, while sequence-level probability is only an imperfect proxy for semantic quality.

We introduce GrammarRL, a label-free reinforcement learning method that adapts language models to grammar constraints without requiring annotated data. GrammarRL optimizes the model using two complementary self-supervised rewards derived from its own likelihoods: a direct reward, measuring how likely the constrained output is given the input, and a reverse reward, measuring how well the input can be reconstructed from the generated output. We optimize these rewards with a Reinforce Leave-One-Out (RLOO) objective over groups of grammar-constrained rollouts, augmented with the top-1 beam-search hypothesis and regularized towards a frozen base model.

We evaluate GrammarRL on sign language gloss translation, hierarchical text classification, and named entity recognition using Llama models ranging from 1B to 8B parameters. GrammarRL consistently outperforms constrained greedy decoding, with an average improvement of 9.8 points and gains of up to 22.8 BLEU. It matches or outperforms beam search on two of the three tasks while preserving greedy-decoding inference cost. Ablations further show that the two rewards are complementary: either reward alone can underperform the untrained baseline, whereas their combination consistently improves upon it.

1 INTRODUCTION

Grammar-constrained decoding is widely used to guarantee the formal correctness of generated outputs (Geng et al., 2023; Dong et al., 2025; Tuccio et al., 2025; Beurer-Kellner et al., 2024; Ugare et al., 2025; Willard & Louf, 2023). By masking invalid tokens at every generation step, a grammar restricts the model to a valid subset of the output space and prevents syntactically malformed sequences from being produced. However, formal validity alone does not guarantee that the generated sequence expresses the desired semantics (Tam et al., 2024; Banerjee et al., 2025). A model may assign high probability to a valid token that leads to an undesirable continuation, creating a “trap” in which subsequent greedy decisions remain syntactically valid but semantically wrong (Park et al., 2024; Biagiola et al., 2026). As illustrated in the top half of Figure 1, a base model might assign most of its probability mass to tokens outside the grammar. When forced to select from the remaining valid options in a low-probability region, a greedy choice can commit the model to a syntactically valid but contextually incorrect continuation.

This problem is particularly important in data-scarce settings, where annotated target sequences are limited or unavailable to guide model generation. The standard remedy is inference-time search. Beam search maintains several candidate prefixes and can recover sequences that greedy decoding misses (Lu et al., 2022), but it is primarily a mode-seeking strategy, and high sequence-level probability does not necessarily translate into high-quality outputs (Holtzman et al., 2019). Posterior-approximation methods instead target the grammar-conditioned distribution over valid sequences, for instance by exploring multiple valid continuations and reweighting them with importance sam-

Figure

Figure 1: Illustration of grammar-constrained decoding pitfalls and the GrammarRL solution. Top: The base model assigns most of its probability mass to invalid tokens (e.g., “great”, “huge”). Constrained to a low-probability region where differences are narrowed, a greedy step chooses “real”, leading to the valid but incorrect sequence “real disaster”. Bottom: Unsupervised RL fine-tuning with GrammarRL reshapes the distribution on unlabeled task inputs. The model directly assigns high probability to valid and correct continuations (“major success”), bypassing the need for complex inference-time search.

pling (Lew et al., 2023; Park et al., 2024; Loula et al., 2025). Both families, however, obtain their improvements through additional computation at every inference call.

We instead ask whether useful sequence-level preferences can be learned before inference in the data-scarce, label-free setting, using only signals available without annotated target outputs. We introduce GrammarRL, a reinforcement learning framework that uses grammar-constrained rollouts and two complementary rewards derived from a frozen pretrained model to shift probability mass toward continuations that are both grammatical and correct (see the bottom half of Figure 1). The direct reward measures the conditional log-likelihood of a candidate output under the frozen reference model, and encourages the policy to discover valid sequences that are likely under the reference model. We also define a reverse reward, since high output likelihood alone does not necessarily identify task-useful outputs. In particular, among valid candidates with similar likelihood, some may progressively discard information from the input and converge toward generic or degenerate sequences. We therefore complement the direct reward with a reverse reward, which favors outputs that retain sufficient information about the source for the reference model to recover it. The two rewards thus capture complementary properties of a desirable output: output plausibility under the frozen reference model and preservation of source-relevant information. Both signals are available without reference outputs, providing sequence-level supervision in the label-free setting. Because these rewards are computed from discrete sequences generated under the grammar constraint, they cannot be optimized by backpropagating through the decoding decisions. GrammarRL therefore treats constrained generation as a policy and optimizes the expected reward with reinforcement learning. Specifically, we use a Reinforce Leave-One-Out (RLOO) objective over groups of grammar-constrained rollouts, using the rewards of the other samples in each group to form a relative baseline for each candidate. We additionally include the top-1 beam-search hypothesis as a candidate during training, allowing beam search to provide useful trajectories without turning the objective into beam-search imitation. The policy is trained to increase the probability of high-reward sequences while remaining regularized toward the frozen reference model.

This reinforcement learning formulation is central to GrammarRL. Rather than improving posterior approximation at inference time or explicitly maximizing the sequence-level probability, we use model-derived sequence-level rewards to transfer sequence-level preferences into the model parameters, amortizing part of the inference-time correction during a label-free tuning phase. After training, these preferences can be exploited with greedy constrained decoding, without maintaining multiple hypotheses or performing iterative correction.

We evaluate GrammarRL on three structured generation tasks—Sign Language Translation (Gloss), Hierarchical Classification (WoS), and Named Entity Recognition (CoNLL)—and compare it with beam search and with sampling-based posterior approximation. The results suggest that sequence-level preferences can be learned from unlabeled inputs and internal model signals, shaping the model toward informative and task-relevant constrained outputs without costly search at inference time. Our contributions are as follows:

• We introduce GrammarRL, a reinforcement learning method that adapts language models to grammar constraints without annotated target outputs, significantly improving the output quality without increasing inference cost.

• We propose a bidirectional reward computed by a frozen reference model, which combines the likelihood of the output given the input with the likelihood of reconstructing the input from the output. We show that the two terms are complementary: each one alone favors a different kind of degenerate output, whereas their combination improves over greedy decoding of the frozen baseline in every setting.

• We show on three structured-generation tasks and three model sizes that GrammarRL improves constrained greedy decoding by 9.8 points on average and matches or exceeds beam search on two of the three tasks, at greedy-decoding inference cost. On Gloss and CoNLL it also matches or outperforms sampling-based posterior approximation (importance sampling and SMC) that uses ten times more generation.

References (32)