技术报告:通过机器学习实现通用代码格式化
Technical Report: Towards a Universal Code Formatter through Machine Learning
摘要:
有许多声明性框架允许我们相对容易地为任何特定语言实现代码格式化程序,但是构造它们很麻烦。第一个问题是,“每个人”都希望以不同的方式格式化他们的代码,这导致了许多格式化程序变体或荒谬的配置选项。第二,每个实现的大小与语言的语法大小成比例,导致数百条规则。在本文中,我们使用一种新的方法来解决格式化程序的构造问题,该方法可以自动导出任何给定语言的格式化程序,而无需语言专家的干预。我们介绍了一个代码格式称为CodeBuff,使用机器学习抽象的格式规则,从一个代表性的语料库,使用精心设计的功能集。我们在Java,SQL和ANTLR语法上的实验表明,CodeBuff是高效的,具有出色的准确性,并且对于给定的语言是语法不变的。它还推广到在手稿准备期间测试的第四语言。
英文摘要:
There are many declarative frameworks that allow us to implement code formatters relatively easily for any specific language, but constructing them is cumbersome. The first problem is that "everybody" wants to format their code differently, leading to either many formatter variants or a ridiculous number of configuration options. Second, the size of each implementation scales with a language's grammar size, leading to hundreds of rules. In this paper, we solve the formatter construction problem using a novel approach, one that automatically derives formatters for any given language without intervention from a language expert. We introduce a code formatter called CodeBuff that uses machine learning to abstract formatting rules from a representative corpus, using a carefully designed feature set. Our experiments on Java, SQL, and ANTLR grammars show that CodeBuff is efficient, has excellent accuracy, and is grammar invariant for a given language. It also generalizes to a 4th language tested during manuscript preparation.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。