arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2211.04673cs.SEcs.AI
收藏

语法感知的即时代码补全

Syntax-Aware On-the-Fly Code Completion

Monash University(莫纳什大学)

Wannita Takerngsaksiri, Chakkrit Tantithamthavorn, Yuan-Fang Li

AI总结:

针对现有语法感知代码补全无法即时运行、即时补全缺乏语法信息的问题,提出利用轻量标记类型的PyCoder方法,通过多任务训练实现推理无额外输入,在CodeXGLUE基准上显著优于基线,且已开源。

摘要:

代码补全旨在通过根据给定上下文预测下一个代码标记来提升开发者的生产力。已有多种方法将抽象语法树(AST)信息融入模型训练,以确保代码补全能感知编程语言的语法。然而,现有的语法感知代码补全方法无法实现即时运行,因为我们发现开发者每输入三分之二的字符时,AST都无法提取——因为它需要语法正确的源代码,这限制了其在真实场景中的实用性。另一方面,现有的即时代码补全方法尚未考虑语法信息。本文提出PyCoder方法,利用标记类型(一种轻量级语法信息),该信息易于获取且与源代码的自然顺序一致。PyCoder采用多任务训练方式:在训练阶段通过学习预测标记类型的辅助任务,模型无需在推理阶段输入标记类型,就能在标记和代码行预测任务上取得更优性能。综合实验表明,PyCoder在CodeXGLUE排行榜上排名第一,标记级预测准确率达77.12%,比基线方法准确率高出0.43%-24.25%。此外,PyCoder在行级预测上的精确匹配率达43.37%,比基线方法准确率高出3.63%-84.73%。这些结果表明,过去很少使用的标记类型信息(语法信息的一种替代方案)能够大幅提升代码补全方法的性能,且无需像基于AST的方法那样要求源代码语法正确。PyCoder已在HuggingFace和GitHub上公开。

英文摘要:

Code completion aims to help improve developers' productivity by suggesting the next code tokens from a given context. Various approaches have been proposed to incorporate abstract syntax tree (AST) information for model training, ensuring that code completion is aware of the syntax of the programming languages. However, existing syntax-aware code completion approaches are not on-the-fly, as we found that for every two-thirds of characters that developers type, AST fails to be extracted because it requires the syntactically correct source code, limiting its practicality in real-world scenarios. On the other hand, existing on-the-fly code completion does not consider syntactic information yet. In this paper, we propose PyCoder to leverage token types, a kind of lightweight syntactic information, which is readily available and aligns with the natural order of source code. Our PyCoder is trained in a multi-task training manner so that by learning the supporting task of predicting token types during the training phase, the models achieve better performance on predicting tokens and lines of code without the need for token types in the inference phase. Comprehensive experiments show that PyCoder achieves the first rank on the CodeXGLUE leaderboard with an accuracy of 77.12% for the token-level predictions, which is 0.43%-24.25% more accurate than baselines. In addition, PyCoder achieves an exact match of 43.37% for the line-level predictions, which is 3.63%-84.73% more accurate than baselines. These results lead us to conclude that token type information (an alternative to syntactic information) that is rarely used in the past can greatly improve the performance of code completion approaches, without requiring the syntactically correct source code like AST-based approaches do. Our PyCoder is publicly available on HuggingFace and GitHub.

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

↑