Abstract
Automated Program Repair (APR) aims to reduce the manual effort of debugging by automatically generating code fixes. While neural machine translation (NMT) models have shown promise, they often generate syntactically invalid code because they treat programming languages as flat sequences of tokens. This paper presents a grammar-based "rule-to-rule" transformation model that operates on Abstract Syntax Trees (ASTs). By enforcing grammatical constraints during inference and utilizing a dual-encoder architecture, the proposed model achieves state-of-the-art results on Java benchmarks while ensuring 100% syntactic validity.
Introduction
Most modern APR tools leverage deep learning to "translate" buggy code into fixed versions. However, code possesses a rigid, hierarchical structure defined by formal grammars that standard sequence-to-sequence models often ignore. This leads to two primary failures: grammatical errors (e.g., uncompilable code) and structural blindness, where the model fails to capture long-range dependencies within nested logic.
To address these issues, researchers from the Harbin Institute of Technology and Microsoft Research Asia propose shifting the paradigm from token-level translation to grammar-rule transformation. Their approach ensures that every generated patch is a valid derivation within the language's formal grammar.
Methodology
The core innovation is a Grammar-Driven Model based on the Transformer architecture, designed to predict production rules rather than individual tokens.
Dual-Encoder Architecture
The model employs two distinct encoders: one for raw token sequences and another for grammar rules. To capture the hierarchical nature of code, the researchers implemented Tree-Masked Self-Attention, which restricts a node's attention to its immediate structural neighbors (parents or children) in the AST. Additionally, Depth Embeddings are integrated to provide the model with explicit information regarding a rule's position within the tree hierarchy.
Grammar-Constrained Inference
During the decoding phase, the model utilizes a Cascade Strategy. It first performs cross-attention with the token encoder to understand the context, then queries the grammar encoder to determine the appropriate structural transformation. Crucially, the inference engine maintains a partial AST and masks out any production rules that would violate Java's grammar at a given position, guaranteeing that the output is always syntactically correct.
Experimental Results
The model was evaluated using the Big Fixes Pairs (BFPs) dataset, consisting of 58,000 Java bug-fix pairs. The performance was measured using Exact Match (XMatch) accuracy and a novel metric, XBug, which counts grammatical errors in generated patches.
As shown in the results table, the proposed model significantly outperforms traditional NMT and token-based Transformer models. At a beam size of 5, it achieved a Top Match accuracy of 13.42% and an All Match accuracy of 28.03%. Most notably, while baseline models produced dozens of uncompilable patches, the grammar-constrained model achieved an XBug score of 0, confirming its ability to eliminate syntax errors entirely.
Discussion and Conclusion
The success of this approach highlights that structural constraints are a powerful asset rather than a limitation. By narrowing the search space to grammatically valid derivations, the model becomes both more accurate and more reliable.
Future work remains to address semantic correctness—ensuring the code not only compiles but also passes functional unit tests. Additionally, expanding the model to support other programming languages would require the extraction of language-specific grammar rules and robust parsers. Nonetheless, this grammar-based framework represents a significant step toward truly automated and reliable software maintenance.
Summary updated