Manuscript Offprint

arXiv:2610.36105

AGDecode: Semantic Attribute Grammar Derivatives for Safety-Constrained Decoding of Large Language Models

Rajan Maghera, Asher Tempest Arya, Aren A. Babikian, Marsha Chechik, Maryam Mehri Dehnavi

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

RAJAN MAGHERA, University of Toronto, Canada

ASHER TEMPEST ARYA, University of Toronto, Canada

AREN A. BABIKIAN, McGill University, Canada

MARSHA CHECHIK, University of Toronto, Canada

MARYAM MEHRI DEHNAVI*, University of Toronto, Canada

Large language models (LLMs) are increasingly used for code generation, motivating techniques that guarantee the structural and semantic correctness of generated programs. Constrained decoding enforces correctness during generation by restricting token choices to those that admit at least one valid completion. While existing approaches can enforce syntactic constraints and selected semantic properties such as type safety, they either target specific properties or rely on operational pruning mechanisms that are difficult to generalize to data-dependent, inductive program properties. We present AGDECODE, a programmable framework for semantic constrained decoding based on least-fixed-point semantics. AGDECODE extends attribute grammars to partially constructed syntax trees, and reduces semantic constrained decoding to a realizability problem over such trees. The framework compiles semantic specifications into systems of relational constraints that are discharged by external verification oracles, such as SMT and CHC solvers, cleanly separating language semantics from constraint-solving machinery. We prove that our approach is sound and complete relative to an ideal verification oracle. In practice, solvers inevitably encounter uncertain queries; conservatively, our system can reject tokens for soundness or speculatively continue to generate until later queries resolve the entire generation. We mechanize the core metatheory in Rocq and evaluate the framework on semantic-constrained generation of C programs, enforcing path-sensitive safety properties including null-pointer safety, division-by-zero freedom, and initialized-variable usage. Our results demonstrate that solver-backed semantic constrained decoding can enforce rich semantic guarantees while incurring modest computational overhead at regular program boundaries.

1 Introduction

Recent advances in large language models (LLMs) have significantly expanded their applications in structured domains, most notably automated program generation. However, LLMs provide no native guarantees that their generated text conforms to structural or semantic invariants.

To mitigate this limitation, constrained decoding has emerged as a powerful paradigm for enforcing safety properties during generation [Beurer-Kellner et al. 2024; Poesia et al. 2022; Scholak et al. 2021; Willard and Louf 2023]. Constrained decoding ensures that the final output is correct-by-construction by restricting the decoder's token selection to continuations that admit at least one completion satisfying a given specification. While syntactic enforcement via context-free grammars is well-explored [Geng et al. 2023; Park et al. 2024; Ugare et al. 2024], syntax alone is insufficient for guaranteeing deeper properties. Semantic constrained decoding addresses this gap by enforcing static properties such as type safety and variable scoping [Mündler et al. 2025; Nagy et al. 2025].

Early attempts to enforce semantic constraints have largely relied on ad-hoc or single-property encodings. Most notably, Mündler et al. [2025] introduce a decoder tailored explicitly for type safety

∗^{*}Also with NVIDIA Research.

Authors' Contact Information: Rajan Maghera, University of Toronto, Department of Computer Science, Toronto, Canada, maghera@cs.toronto.edu; Asher Tempest Arya, University of Toronto, Department of Computer Science, Toronto, Canada, asher.arya@mail.utoronto.ca; Aren A. Babikian, McGill University, Department of Electrical and Computer Engineering, Montreal, Canada, aren.babikian@mcgill.ca; Marsha Chechik, University of Toronto, Department of Computer Science, Toronto, Canada, chechik@cs.toronto.edu; Maryam Mehri Dehnavi, University of Toronto, Department of Computer Science, Toronto, Canada, mmehride@cs.toronto.edu.

Rajan Maghera, Asher Tempest Arya, Aren A. Babikian, Marsha Chechik, and Maryam Mehri Dehnavi

by evaluating type-checking logic over a program’s incremental string representation. However, monolithic approaches tied to a single semantic property scale poorly to generalized constraints. Evaluating semantic properties directly on a generated prefix requires the decoder to look ahead and reason about every permissible decoder token continuation.

Motivating example. Consider the task of enforcing selected undefined-behaviour (UB) safety properties on a C program while an LLM generates the program. Consider an incomplete generation:

void foo(int num){int*x=malloc(sizeof(int));*x=16/num;\text{void foo(int num)\{int*x=malloc(sizeof(int));*x=16/num;}

This incomplete generation contains two potential UB violations, highlighted in red, that cannot be rectified by generating more of the program. The first UB violation is a null-pointer dereference if x == NULL, and the second is a divide-by-zero violation if num == 0. Semantic constrained decoding must therefore permit the dereference of x and the use of num as a divisor only when the required non-nullness and non-zero facts can be established.

Programmable semantic constrained decoding frameworks. To provide a generalized solution to semantic constrained decoding, Nagy et al. [2025] introduce CHOPCHOP, the first programmable framework for semantic constrained decoding. CHOPCHOP addresses the problem of expressivity and programmability of constrained decoders by allowing for a programmable set of semantics. The space of all possible abstract syntax tree (AST) continuations for a given prefix is represented compactly as regular codata, reducing constraint enforcement to a realizability problem solved via coinduction. A user specifies a language grammar and a system of corecursive semantic pruner functions to eliminate invalid ASTs from the program space. While highly expressive, managing program spaces via coinductive coterms and operational pruners introduces fundamental challenges for general program verification:

(1) Representational overhead in non-regular domains. Courcelle’s theorems [Courcelle 1983] show that coterms are highly effective for capturing infinite control-flow behaviours or behavioural bisimulations. However, they are ill-suited for modelling data theories, such as bit-precise arithmetic or path conditions. Forcing these complex data states into a structural tree requires a large number of unique symbols, resulting in a state-space explosion.

(2) Coupling of semantics and search. Because CHOPCHOP’s pruners are formulated as operational, total functions over a trace space, they simultaneously define the language’s semantics and the execution oracle. Consequently, these pruners operate as algorithmic black boxes, preventing the underlying framework from performing global logical optimizations.

Many safety properties are inductive. For example, assertion reachability and memory safety are defined over the set of reachable program states, a least fixed point capturing the variable-value tracking required in the motivating example. Traditional program verification uses this structure by reducing a program to a set of relational verification conditions (VCs) and discharging them to efficient Satisfiability Modulo Theories (SMT) and Constrained Horn Clause (CHC) solvers [Bjørner et al. 2015; de Moura and Bjørner 2008]. Proving safety then reduces to a reachability query over that least fixed point, which is the kind of query these solvers are designed to decide. CHOPCHOP instead represents the space of completions as coinductive coterms, whose semantics is a greatest fixed point. This fits inductive properties poorly. A data-dependent inductive invariant has no compact coterm representation, because distinguishing states by their bit-precise values requires a separate structural symbol for each valuation and causes a state-space explosion.

In this paper, we ask:

How can we design a programmable framework that leverages inductive least-fixed-point semantics to integrate automated verification oracles into constrained decoding?

AGDECODE: Semantic Attribute Grammar Derivatives for Safety-Constrained Decoding of Large Language Models

3

Our approach. To overcome these limitations, we formalize constraint evaluation using Attribute Grammars (AGs) [Knuth 1968]. While traditional AGs evaluate semantic equations over complete syntax trees, constrained decoding demands reasoning over an incomplete token prefix spanning an infinite space of possible completions. Our framework, AGDECODE, addresses this challenge via a relational AG formulation that introduces placeholder holes at ungenerated syntax nodes. Rather than executing an operational traversal over a partial tree, AGDECODE translates the structural grammar rules into a uniform system of relational constraints and defers the semantic realizability query to an external oracle (e.g., SMT solver) capable of solving recursive rules efficiently. This architecture cleanly decouples the language's semantic specification from its checking machinery. We prove that our framework is sound and complete with respect to ideal evaluation. In practice, oracles may emit uncertain results. Thus, we show in evaluation that speculatively-generated decoder tokens can be verified in retrospect to ensure soundness.

This paper makes the following contributions:

(1) a novel programmable framework for semantic constrained decoding based on least-fixed-point semantics (Sec. 3);

(2) a formalization for residual attribute grammars used to represent partially-constructed syntax trees (Sec. 4);

(3) a formal connection tying semantic constrained decoding to generating the partially-constructed syntax trees (Sec. 5);

(4) an embedding to compile residual attribute grammar semantics into systems of CHCs. (Sec. 6); and

(5) an evaluation of our framework showcasing the generation of semantically verified programs while incurring small computational overhead (Sec. 7).

A mechanization of the core meta-theorems of the framework in the Rocq proof assistant is provided as supplementary material 1^{1}.

References (17)