Grammar-Based Code Representation: Is It a Worthy Pursuit for LLMs?

Qingyuan Liang, Zhao Zhang, Zeyu Sun, Zheng Lin, Qi Luo, Yueyi Xiao, Yizhou Chen, Yuqun Zhang, Haotian Zhang, Lu Zhang, Chenbin Chenbin, Yingfei Xiong

Introduction

While billion-scale Large Language Models (LLMs) have largely mastered programming syntax through sheer scale, recent research suggests that explicitly incorporating formal grammar rules still provides a substantial performance boost. In the early stages of AI-driven code generation, small models frequently produced syntactically invalid code. While modern models like GPT-4 or DeepSeek-Coder have largely solved the "syntax error" problem, they still struggle with semantic differentiation. A minor character change can fundamentally alter a program's logic, a nuance often overlooked by standard token-based LLMs. This paper introduces GrammarCoder, demonstrating that representing code as a sequence of Abstract Syntax Tree (AST) rules helps models better distinguish between subtle, logic-altering code variations.

Methodology

The authors propose a hybrid representation that shifts the model's objective from predicting text tokens to predicting structural grammar rules.

Overview of the grammar-based representation, showing the mapping from Python code to an Abstract Syntax Tree (AST) and its corresponding grammar rules.

The GrammarCoder framework utilizes three primary components:

  1. AST-Based Representation: Code is represented as a sequence of grammar rules derived from a preorder traversal of its AST. For instance, a function definition begins with the rule module -> function_definition.
  2. Hybrid Vocabulary: The model's vocabulary is expanded to include unique identity tokens for formal grammar rules alongside standard BPE tokens for "terminals" such as variable names and literals.
  3. Training and Guidance: Models (1.3B, 1.5B, and 7B parameters) underwent Continued Pre-training (CPT) on 10B tokens of Python code. During generation, the model effectively constructs the tree structure, ensuring structural coherence.

Key Insights: Semantic Amplification

The core advantage of grammar-based representation is "semantic amplification." In standard text, a logic error might differ from the correct code by only a few characters. However, in a grammar-based representation, that same error often results in a vastly different AST structure. This increases the representational "distance" between correct and incorrect code by over 90%, making it significantly easier for the model's loss function to identify and correct logical flaws. This suggests that the benefit of grammar is not merely syntax enforcement—since even baseline models achieve near 100% syntax correctness—but rather enhanced semantic accuracy.

Key Results

Evaluation on standard benchmarks reveals that GrammarCoder significantly outperforms its token-based counterparts.

Performance comparison of GrammarCoder models against standard token-based baselines on HumanEval and MBPP benchmarks.

As shown in the results, GrammarCoder-7B achieved 76.8% on HumanEval, a notable improvement over the 57.9% of the base Qwen2.5-7B model. Even at smaller scales, the efficiency is evident: the 1.3B model outperformed DeepSeek-Coder-1.3B by nearly 7 percentage points on MBPP. Furthermore, in semantic classification tasks—distinguishing buggy code from fixes—grammar-based models showed up to an 18.25% improvement in F1 scores.

Discussion and Limitations

Despite these gains, grammar-based models face challenges. They rely heavily on successful AST parsing; "dirty" or truncated data that cannot be parsed becomes unusable. Additionally, the reliance on formal structures makes these models less flexible than token-based ones when handling mixed natural language and code. Future work will need to explore whether this "semantic amplification" effect persists at the 70B+ parameter scale and address the inference overhead associated with tree-traversal processing.

Conclusion

GrammarCoder demonstrates that formal grammar remains a powerful tool for LLMs. By forcing structural awareness and amplifying the representational difference between logic states, grammar-based models achieve a level of semantic precision that scale alone has yet to provide for standard token-based architectures.

Summary updated

BibTeX
@inproceedings{liang2025grammar,
  author = {Qingyuan Liang and Zhao Zhang and Zeyu Sun and Zheng Lin and Qi Luo and Yueyi Xiao and Yizhou Chen and Yuqun Zhang and Haotian Zhang and Lu Zhang and Chenbin Chenbin and Yingfei Xiong},
  title = {{Grammar-Based Code Representation: Is It a Worthy Pursuit for LLMs?}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
}
Figure