Grammar-Based Code Representation: Is It a Worthy Pursuit for LLMs?
Abstract
Grammar serves as a cornerstone in programming languages and software engineering, providing frameworks to define the syntactic space and program structure. Existing research demonstrates the effectiveness of grammar-based code representations in small-scale models, showing their ability to reduce syntax errors and enhance performance. However, as language models scale to the billion level or beyond, syntax-level errors become rare, making it unclear whether grammar information still provides performance benefits. To explore this, we develop a series of billion-scale GrammarCoder models, incorporating grammar rules in the code generation process. Experiments on HumanEval (+) and MBPP (+) demonstrate a notable improvement in code generation accuracy. Further analysis shows that grammar-based representations enhance LLMs’ ability to discern subtle code differences, reducing semantic errors caused by minor variations. These findings suggest that grammar-based code representations remain valuable even in billion-scale models, not only by maintaining syntax correctness but also by improving semantic differentiation.
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},
}Ask Althea to put this paper to the test.
| GramTrans: A Better Code Representation Approach in Code Generation | arXiv · 2025 |
| Grammar-Based Patches Generation for Automated Program Repair | ACL · 2021 |
| Enforcing Grammar in Code Synthesis with Transformers | Proceedings of the ... International Florida Artificial Intelligence Research Society Conference · 2023 |
| Benchmarking Language Models for Code Syntax Understanding | arXiv · 2022 |
| TreeDiff: AST-Guided Code Generation with Diffusion LLMs | arXiv · 2025 |
| AST-T5: Structure-Aware Pretraining for Code Generation and Understanding | arXiv · 2024 |