Introduction
Standard code completion models often struggle when they hit a "hard" token—like a specific string literal or a rare variable name. This frequently results in "hallucinations" or the model stopping prematurely. To address this, researchers introduce GRAMMFORMER, a grammar-guided Transformer that generates code "sketches." By inserting placeholders (holes) where it is uncertain, GRAMMFORMER can skip the hard parts and successfully predict the rest of the statement.
The Problem with Left-to-Right Completion
Most modern code completion tools treat code as a simple sequence of text, predicting the next token from left to right. However, code is highly structured. It often contains "low-entropy" segments (predictable logic and boilerplate) interrupted by "high-entropy" segments (specific variable names or values that are impossible to guess without broader context).
When a standard model encounters a high-entropy segment, it faces a dilemma: it can guess (and likely be wrong, leading to hallucinations) or it can stop entirely. If it stops, the user misses out on the predictable code that might have followed the difficult part. A more useful assistant should be able to say, "I don't know what goes here, but I know what the rest of the line looks like."
Methods: Grammar-Guided Sketch Generation
To solve this, the authors propose GRAMMFORMER, a model that generates code by following the programming language's context-free grammar (CFG). Unlike standard models that move strictly left-to-right, GRAMMFORMER expands a partial syntax tree.
Key components of this approach include:
- Two-Step Generation:
- A Selector model identifies which part of the current code (a "non-terminal" symbol) should be expanded next. Crucially, it can choose to stop expanding a specific branch, effectively turning that part of the code into a "hole" (represented by a placeholder token).
- An Expansion model determines the actual code tokens to fill into the selected slot.
- Grammar Flattening: To make generation more efficient, the language grammar is simplified, collapsing tedious multi-step expansions into single steps.
- Reinforcement Learning (RL): Since there is no ground truth dataset dictating where holes should be placed, the model uses a self-critical policy gradient approach. It is rewarded for making long predictions that remain accurate when the holes are treated as wildcards.
- REGEXACC Metric: The authors introduce a new evaluation metric that converts a sketch into a regular expression (replacing holes with wildcards) and checks if it matches the ground truth. The score is scaled by how much of the code was actually predicted versus left as a hole.
Datasets
The researchers curated a massive dataset of C# and Python code from non-fork GitHub repositories with more than 20 stars. After deduplication and parsing with Tree-sitter to extract concrete syntax trees, the final test sets included approximately 318,000 examples for C# and 362,000 for Python.
Key Insights
- Grammar as a Constraint: By forcing the model to operate within the rules of the language's grammar, GRAMMFORMER ensures that the generated sketches are structurally sound, even if they are incomplete.
- Non-Linearity is Key: Standard language models are trapped in a left-to-right cage. By using a selector model, GRAMMFORMER can jump to the "easy" parts of a statement first, leaving the "hard" parts for the user to fill in later.
- The "Conservative" Trade-off: Through RL, the model learns a delicate balance. If it is too "brave," it makes mistakes; if it is too "conservative," it puts holes everywhere, rendering the suggestion useless. The reward function helps it find the sweet spot of being as helpful as possible without being incorrect.
Key Results
GRAMMFORMER demonstrates significant improvements over traditional generative models across multiple metrics.
- Higher Accuracy: GRAMMFORMER generated completions that were 10-50% more accurate (measured by REGEXACC) than traditional generative models.
- Longer Sketches: Compared to other "sketching" baselines that only allow holes at the end of a prefix, GRAMMFORMER produced sketches that were 37-50% longer.
- Qualitative Success: In practical examples, the model correctly predicted complex structures—like an entire
Assert.AreEqualstatement—while accurately placing a hole for a specific index or value it couldn't possibly know from the local context.
Limitations and Future Work
While promising, the approach has a few limitations:
- Computational Overhead: Because the model must re-run the encoder/decoder for every expansion step in the syntax tree, it is computationally more expensive than simple causal language models.
- Lack of Global Context: The model currently only looks at the local code prefix. Incorporating static analysis (like type checking or symbol tables from the rest of the project) could help the model fill in many of the holes it currently leaves blank.
- Human Factors: While the metrics look good on paper, human studies are needed to determine if developers actually find "sketches with holes" more helpful or more distracting than traditional completions in a real-world IDE environment.
Summary updated