Abstract
Large Language Models (LLMs) have revolutionized code generation but frequently produce syntactically invalid code, particularly in "Fill-in-the-Middle" (FIM) tasks where the generated content must bridge a gap between an existing prefix and suffix. This paper introduces a novel incremental parser that ensures syntactic correctness by extending the Earley algorithm to handle "right quotients"—the set of strings that allow a specific suffix to complete a valid program. By incorporating a branching lexer and specialized logic for context-sensitive features like Python's indentation, the proposed method significantly reduces syntax errors in AI-generated code. Evaluation on real-world datasets demonstrates that this approach effectively "fixes" broken model outputs with constant-time per-token overhead.
Introduction
While LLMs like SantaCoder and StarCoder are proficient at suggesting code snippets, they lack an inherent understanding of formal grammar. This often leads to "hallucinated" syntax or code that is valid in isolation but fails to integrate with the surrounding context. In modern IDEs, the most common use case is FIM, where a developer requests a completion between a prefix (left context) and a suffix (right context).
Existing constrained decoding methods typically focus on left-to-right generation. However, FIM presents a unique challenge: neither the left nor the right context may be a complete, valid program on its own. Furthermore, real-world programming languages like Python introduce context-sensitive complexities, such as indentation levels and "maximal munch" lexing, where the interpretation of a character depends on whether it terminates the current token or begins the next.
Methodology
The core of the proposed solution is an incremental parser based on a modified Earley algorithm. The system architecture coordinates between the LLM, a specialized lexer, and the Earley chart collection to filter invalid token candidates in real-time.
Right Quotienting and Earley Extension
The authors formalize the FIM problem as a language quotient problem. If $L$ represents the language of all valid programs and $\beta$ is the right context (the code following the cursor), the goal is to generate a string from the right quotient $L/L(\beta)$. This quotient contains all strings that, when concatenated with $\beta$, form a valid member of $L$.
To implement this, the Earley algorithm was extended to operate over Nondeterministic Finite Automata (NFAs) representing the right context. This allows the parser to "look ahead" and verify if a proposed token can eventually lead to a state that matches the existing suffix.
Branching Lexer and Whitespace Handling
To address the ambiguity inherent in lexing—such as whether a character belongs to the current token or the next—the authors implemented a branching lexer. This lexer tracks multiple potential interpretations (branches) until they can be disambiguated by the grammar.
For context-sensitive languages like Python, the parser must also handle indentation. The authors introduced a "quotient grammar" that accounts for the unknown number of DEDENT tokens required to bridge the gap to the right context. While this approach prioritizes completeness (accepting all valid programs) over absolute soundness (potentially accepting some invalid ones in highly ambiguous cases), it provides a practical engineering solution for real-time performance.
Evaluation and Results
The method was evaluated using two synthetic datasets derived from The Stack:
- STACK-BOUNDARY: Simulates standard auto-completion by cutting code at symbol boundaries.
- STACK-RANDSPAN: Simulates "messy" real-world editing by deleting random spans, often cutting through the middle of symbols.
Using the SantaCoder model, the results showed a dramatic improvement in reliability. On the STACK-BOUNDARY dataset, constrained decoding produced valid code in 90,665 cases, a significant increase over the 65,353 valid cases produced by unconstrained generation. Crucially, the constrained decoder forced the model to find valid syntactic paths in nearly 25,000 instances where the base LLM would have otherwise failed.
From a performance standpoint, the incremental nature of the parser ensures that the per-token overhead remains nearly constant regardless of the file's total length. This is a major improvement over "checked unconstrained generation," where re-parsing the entire file leads to linear-time costs that grow with context size.
Discussion and Conclusion
While the proposed method ensures syntactic well-formedness, it does not guarantee semantic or logical coherence. A model might still "escape" the intended context by generating valid but irrelevant code (e.g., closing a function prematurely to start a new one). Future work aims to address this through "steering" (actively guiding the model toward desirable grammar branches) and incorporating static analysis to ensure type safety and security. By bridging the gap between probabilistic generation and formal syntax, this work provides a robust foundation for more reliable AI-assisted programming.
Summary updated