arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2510.02887cs.SE
收藏

GramTrans:一种更优的代码生成中的代码表示方法

GramTrans: A Better Code Representation Approach in Code Generation

Peking University(北京大学);Institute of Software, Chinese Academy of Sciences(中国科学院软件研究所)

Zhao Zhang, Qingyuan Liang, Zeyu Sun, Yizhou Chen, Guoqing Wang, Yican Sun, Lu Zhang, Ge Li, Yingfei Xiong

AI总结:

该研究提出代码表示越易解析模型性能越好的猜想,据此开发出可将上下文无关语言转为LL(1)类表示的GramTrans方法,经多模型多语言基准验证其能显著提升代码生成效果。

摘要:

代码生成在辅助软件开发方面展现出巨大潜力。一个基础但尚未被充分探究的问题是,代码表示方式的选择会如何影响模型性能。现有研究采用了多种表示方式,例如将代码视为纯文本、语法规则序列或语法树序列,但它们对解析难度与模型有效性之间的关系缺乏系统性的理解。本文提出一个猜想:一种表示越容易解析,模型取得的性能就越好。我们利用文法类将这一观点形式化,其中属于更简单类别的表示(例如LL(1))更易于解析。通过在基于Python的领域特定语言(DSL)上开展的对照实验,我们发现解析难度与模型性能强相关。受这一发现启发,我们提出了GramTrans,这是一种通用方法,可自动将上下文无关语言转换为LL(1)类内的表示。GramTrans引入了一种新颖的分层冲突消除算法,能够在语法简洁性和词元效率之间实现灵活权衡。我们使用三种代码生成模型(StarCoder 1B、DeepSeek-Coder 1.3B和Qwen2.5 1.5B)在Python和Java上对GramTrans进行了评估。在多个基准测试中,GramTrans相较于基线表示始终带来显著提升。此外,我们对现有表示的分析再次证实了解析难度与模型性能之间的强一致性,为该猜想提供了额外支持。

英文摘要:

Code generation has shown great promise in assisting software development. A fundamental yet underexplored question is how the choice of code representation affects model performance. While existing studies employ various representations, such as treating code as plain text, grammar rule sequences, or syntax tree sequences, they lack a principled understanding of the relationship between parsing difficulty and model effectiveness. This paper proposes a conjecture: the easier a representation is to parse, the better performance the model achieves. We formalize this idea using grammar classes, where representations in simpler classes (e.g., LL(1)) are easier to parse. Through a controlled experiment on a Python-based DSL, we show that parsing difficulty strongly correlates with model performance. Motivated by this finding, we present GramTrans, a general approach that automatically transforms a context-free language into a representation within the LL(1) class. GramTrans introduces a novel hierarchical conflict elimination algorithm, enabling a flexible trade-off between syntactic simplicity and token efficiency. We evaluate GramTrans on both Python and Java using three code generation models: StarCoder 1B, DeepSeek-Coder 1.3B, and Qwen2.5 1.5B. Across multiple benchmarks, GramTrans consistently delivers significant improvements over baseline representations. Furthermore, our analysis of existing representations reconfirms the strong alignment between parsing difficulty and model performance, providing additional support for the conjecture.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑