一句话:提出 ASTNN,将大型 AST 拆分为语句树序列并用双向 RNN 编码,显著提升代码分类与克隆检测效果。
利用机器学习分析程序的关键在于如何良好地表示代码片段。传统信息检索方法把程序当作自然语言文本,会丢失重要语义信息;而基于抽象语法树(AST)的神经模型虽表现更好,却因 AST 规模庞大而受困于长程依赖问题。本文提出 ASTNN:不再对整个大 AST 直接建模,而是将其切分为一系列小的语句树,先通过捕获语句的词法与句法知识把每棵语句树编码为向量,再由双向 RNN 对语句向量序列建模,利用语句的自然性最终生成代码片段的向量表示。作者将该表示应用于源代码分类与代码克隆检测两个常见的程序理解任务,实验结果表明其性能优于当前最先进的方法。
Exploiting machine learning techniques for analyzing programs has attracted much attention. One key problem is how to represent code fragments well for follow-up analysis. Traditional information retrieval based methods often treat programs as natural language texts, which could miss important semantic information of source code. Recently, state-of-the-art studies demonstrate that abstract syntax tree (AST) based neural models can better represent source code. However, the sizes of ASTs are usually large and the existing models are prone to the long-term dependency problem. In this paper, we propose a novel AST-based Neural Network (ASTNN) for source code representation. Unlike existing models that work on entire ASTs, ASTNN splits each large AST into a sequence of small statement trees, and encodes the statement trees to vectors by capturing the lexical and syntactical knowledge of statements. Based on the sequence of statement vectors, a bidirectional RNN model is used to leverage the naturalness of statements and finally produce the vector representation of a code fragment. We have applied our neural network based source code representation method to two common program comprehension tasks: source code classification and code clone detection. Experimental results on the two tasks indicate that our model is superior to state-of-the-art approaches.
@inproceedings{ZhangWZ0WL19,
title = {A Novel Neural Source Code Representation Based on Abstract Syntax Tree},
author = {Jian Zhang and Xu Wang and Hongyu Zhang and Hailong Sun and Kaixuan Wang and Xudong Liu},
booktitle = {ICSE},
year = {2019},
doi = {10.1109/icse.2019.00086}
}