Correctness-Guaranteed Code Generation via Constrained Decoding

Lingxiao Li, salar rahili, Yiwei Zhao

TL;DR

This paper introduces a novel constrained decoding algorithm that uses a dynamic "Tree of Parsers" (ToP) to generate semantically and even runtime-correct code. This approach is crucial for domains like game development where human oversight is absent, ensuring generated code functions as intended on the first try.

What Problem Does This Paper Address?

Large Language Models (LLMs) are revolutionizing code generation, but ensuring the correctness of their output remains a significant hurdle. While errors might be tolerable in early development, critical applications like video game logic or robotics demand one-shot correctness. Traditional constrained decoding methods, often based on regular expressions or context-free grammars, fall short of guaranteeing semantic correctness. The core challenges are: 1) LLMs need immediate feedback for each token, unlike compilers that check complete programs, and 2) aligning LLM's subword tokens with traditional parsing units is difficult.

Methods

The authors propose a constrained decoding algorithm that integrates a context-sensitive parser with an LLM. The key innovation is the dynamic Tree of Parsers (ToP). Each node in this tree represents a modular context-free grammar (CFG) enriched with contextual information like variable scopes and types.

Here's how it works:

  • Tree of Parsers (ToP): Instead of a single parser, a tree structure is used. Each node handles a specific language construct and can spawn child parsers for nested structures or ambiguities. This allows for a more granular and context-aware parsing process.
  • Context-Sensitive Parsing: The ToP incorporates semantic information (e.g., variable types, scopes) directly into the parsing process, going beyond simple syntactic rules.
  • Regular Expression Guidance: At each generation step, the ToP outputs a regular expression that guides the LLM to generate the next sequence of tokens. This regex is designed to satisfy a "non-extensible match" property, ensuring that once a valid segment is matched, further extensions won't also match the same regex, thus preventing ambiguity.
  • Token Healing: A procedure is introduced to handle cases where LLM tokens might span across the boundaries of the generated regexes, ensuring seamless integration.
  • sLua: A strongly-typed variant of Lua was developed and used to demonstrate the approach. Its strong typing simplifies the ToP construction and reduces ambiguity.

Datasets

The paper does not explicitly mention training datasets for the LLM. However, it mentions that LLMs are often trained on substantial amounts of Lua code, which helps them generalize to sLua. The evaluation was performed on generated game mechanics for a roguelike video game.

Key Insights and Ideas

  • Dynamic Tree of Parsers (ToP) for Context: The ToP is a powerful abstraction for handling complex language structures and contextual information. It allows for modularity and dynamic adaptation during parsing.
  • Non-Extensible Regexes for Guiding LLMs: The "non-extensible match" property for regexes is a clever way to ensure that the LLM is guided towards completing a valid segment without overshooting or creating ambiguity.
  • Semantic Guarantees Extend to Runtime: By carefully restricting the language features (sLua) and designing the API, the authors show that semantic correctness can translate to provable runtime correctness, a significant achievement for critical applications.
  • Adaptive Rejection Sampling Speedup: The use of adaptive rejection sampling for generating tokens guided by regexes is shown to be significantly faster than traditional regex-compilation methods.

Key Results

  • Semantic Correctness: The proposed method successfully generates semantically correct sLua code that conforms to prescribed scripting APIs.
  • Runtime Correctness: For the roguelike game "Dungeon Crawl Infinite," generated game mechanics scripts are guaranteed to terminate and execute without runtime errors.
  • Speedup: The adaptive rejection sampling approach achieved a speedup of over three times compared to alternative regex-compilation methods.
  • Quality: In experiments generating talent categories for a game, the authors' method achieved the highest success rate and quality scores, especially when combined with reflection. For instance, Claude-3.5-Sonnet with reflection achieved 38 data points in the 90-100 score range, while the proposed method with reflection achieved 33 data points.

Limitations and Future Work

  • Nontermination: While the generated code is guaranteed to be correct and terminate, the constrained decoding algorithm itself is not guaranteed to terminate. This is a known issue in constrained decoding, where the LM might get constraints slightly wrong, leading to infinite repetition or junk text.
  • Distribution Distortion: Auto-regressive constrained decoding can distort the probability distribution of the LM, potentially leading to nontermination or sub-par quality.
  • Future Work: The authors suggest exploring post-training techniques that leverage parser hints to combat distribution distortion and improve generation quality. They also note that their context-sensitive parser can provide additional context (like error types or expected next tokens) that could be used for this purpose.

Summary updated

BibTeX
@inproceedings{li2025correctness,
  author = {Lingxiao Li and salar rahili and Yiwei Zhao},
  title = {{Correctness-Guaranteed Code Generation via Constrained Decoding}},
  booktitle = {COLM},
  year = {2025},
  url = {https://openreview.net/forum?id=CYiXNIQegF},
}
Figure