arXiv:2609.35425cs.PLSep 2026

Manuscript Offprint

arXiv:2609.35425

Semantic Prefix Oracles for LLM Decoding: Contracts and Differential Validation

Paul Kronlund-Drouault

Abstract

Constrained decoding can enforce regular or context-free output formats, but many program-generation failures are semantic: scope, typing, and declaration effects depend on context. We present semantic grammar specifications, a declarative formalism that attaches such constraints to a context-free surface and executes them during Earley descent. Our implementation enforces safe pruning: it rejects only prefixes whose semantic contradictions cannot be repaired by any continuation. A separate, grammar-dependent, dead-end freedom property guarantees the existence of a realizable witness for each remaining branch. We give simple sufficient conditions based on surface productivity, type coverage, and left-to-right constraint flow. Our finite-lambda, core ML, and C-like fragments satisfy them, while the STLC instance used in our experiments does not: plain STLC can violate type coverage, and we show how restricting its type universe recovers it. A tokenizer-lifting lemma carries character-level witnesses to token sequences under an explicit vocabulary-coverage hypothesis.

We validate the implementation differentially against production compilers (ocamlc, cc). Across every prefix of 65 compiler-valid programs we observe zero false prunes. The semantic oracle localizes 25/30 invalid programs mid-stream, against 0/30 for a syntax-only oracle, and agrees on 42/42 recursion probes. A twelve-model generation study, including a matched semantic-versus-syntactic ablation for nine models, finds nonnegative observed semantic-minus-syntactic point estimates for every model-language pair, with maxima of +15.2 points on STLC task correctness and +14.3 points on ML validity.

CCS Concepts: • Theory of computation → Grammars and context-free languages; • Software and its engineering → Formal language definitions; Software testing and debugging; • Computing methodologies → Natural language processing.

Keywords: constrained decoding, semantic prefix grammars, attribute grammars, differential testing, large language models

ACM Reference Format:

Paul Kronlund-Drouault. 2026. Semantic Prefix Oracles for LLM Decoding: Contracts and Differential Validation. In Proceedings of the 2nd ACM SIGPLAN International Workshop on Language Models and Programming Languages (LMPL '26), October 4–9, 2026, Oakland, CA, USA. ACM, New York, NY, USA, 11 pages. https://doi.org/10.1145/3843750.3843841

1 Introduction

Grammar-constrained decoding has by now entered inference infrastructure, with libraries such as Outlines [25], DOMINO [2], and Formatron [21] which mask a model's vocabulary so that every emitted prefix extends to output in a regular or context-free language, giving a structural guarantee of completability. The constraints they express are nonetheless weak relative to how models actually fail.

Syntax-only constraints do not address consequential program-generation failures such as scope, type compatibility, and declaration effects, none of which a grammar mask can express. Lifting the guarantee to semantic constraints gives two distinct properties:

  1. safe pruning: no prefix with a semantically valid completion is rejected

  2. dead-end freedom: every retained prefix still has such a completion

The first protects the model’s valid continuations while the second protects the decoding loop from being walked into a state where no token is admissible. They are different theorems with different hypotheses.

We structure this paper around the following question:

When may a semantic prefix oracle reject an LLM continuation without hiding a valid program, and

Figure

This work is licensed under a Creative Commons Attribution 4.0 International License.

LMPL '26, Oakland, CA, USA

© 2026 Copyright held by the owner/author(s).

ACM ISBN 979-8-4007-2986-7/2026/10

https://doi.org/10.1145/3843750.3843841

Paul Kronlund-Drouault

how do we validate that the implementation actually respects that contract?

As a working example, take the finite lambda instance of §3.2. Under its constraints, a model that has decoded let x : Int = may still emit a digit, an open parenthesis, or any Int in scope, but the token true is masked out: the annotation has fixed the demanded type and the boolean literal's type is fixed too. This is where semantics is needed, since let x : Int = true; is a perfectly good parse in the underlying context-free grammar.

Our declarative format attaches typing rules to a context-free surface. The rules compile to a language-parametric IR over first-order terms, pattern ascription, syntactic unification, and finite right-bound context effects. AUFBAU executes this IR during Earley descent and rejects a path only after a stable semantic contradiction. The kernel has no built-in notion of type, function, or arrow. Those meanings come from each grammar's constructors and shared holes. Safe pruning is therefore a property of the oracle. Dead-end freedom is a stronger property of particular grammars. In §2.3 we give three sufficient conditions, show that the ML and C-like instances satisfy them, and show why the STLC instance used in our experiments does not.

Because a proof of the rejection rule does not rule out implementation bugs, we also validate Aufbau differentially against production compilers sharing none of our code. P7 then measures what the semantic layer buys over the same mask with typing removed.

This paper contributes:

  • A semantic-prefix formalism that separates safe pruning from dead-end freedom, with a safe-pruning theorem, simple sufficient conditions for dead-end freedom, and a tokenizer-lifting lemma under an explicit vocabulary-coverage hypothesis ( §2§2, §3§3)

  • A language-parametric implementation (AUFBAU/P7, with STLC, finite-lambda, core ML, C-like, and typed tool-DSL instances in three surface dialects) and prefix-by-prefix differential validation against ocamlc and cc: zero false prunes over every prefix of 65 in-fragment valid programs, 42/42 on a recursion probe, mid-stream error localization at 25/30 vs. 0/30 for a syntax-only oracle ( §3−§4.1§3-§4.1)

  • A twelve-model generation study containing a matched semantic-versus-syntactic ablation for the nine models that ran both arms, 3,693 graded attempts in total, holding parser, sampler, tokenizer bridge, and surface grammar fixed, with per-language validity and correctness metrics reported separately, together with exploratory observations on reasoning-then-constrained scheduling, rejection-uncertainty association, and multi-turn tool episodes ( §4.2§4.2).

The artifact contains the implementation, specifications, data, and commands used for the reported measurements.

References (14)