一句话:提出一种面向即时场景的神经网络语法高亮方法,可处理合法与非法代码并复用语言解析策略生成标注。
在线协作开发中源码常被分享查看,但语法高亮常为响应速度牺牲质量。形式化语法分析昂贵,且对大量无效语言推导不可行;主流高亮器依赖正则表达式,与语言词法规范差距大,需频繁更新且难识别复杂构造。本文提出基于深度学习的即时语法高亮方法,可处理正确与错误的语言推导,面向代码查看器和代码片段等场景,并允许复用语言解析策略生成高亮规范以减轻开发者负担。作者在三种主流编程语言、不同语法覆盖水平下,与在线高亮工具和形式化方法比较准确率与执行时间。结果显示该方法预测准确率持续接近完美,优于正则表达式策略。
With the presence of online collaborative tools for software developers, source code is shared and consulted frequently, from code viewers to merge requests and code snippets. Typically, code highlighting quality in such scenarios is sacrificed in favor of system responsiveness. In these on-the-fly settings, performing a formal grammatical analysis of the source code is not only expensive, but also intractable for the many times the input is an invalid derivation of the language. Indeed, current popular highlighters heavily rely on a system of regular expressions, typically far from the specification of the language's lexer. Due to their complexity, regular expressions need to be periodically updated as more feedback is collected from the users and their design unwelcome the detection of more complex language formations. This paper delivers a deep learning-based approach suitable for on-the-fly grammatical code highlighting of correct and incorrect language derivations, such as code viewers and snippets. It focuses on alleviating the burden on the developers, who can reuse the language's parsing strategy to produce the desired highlighting specification. Moreover, this approach is compared to nowadays online syntax highlighting tools and formal methods in terms of accuracy and execution time, across different levels of grammatical coverage, for three mainstream programming languages. The results obtained show how the proposed approach can consistently achieve near-perfect accuracy in its predictions, thereby outperforming regular expression-based strategies.