一句话:提出基于下推自动机的语法约束 Seq2Seq 代码生成框架 CODEP,保证生成代码语法正确并提升多项基准指标。
通用代码生成需将自然语言描述转为 Python 等通用语言代码,但现有 Seq2Seq 模型忽略语法规则。本文首次提出面向通用代码生成的语法化 Seq2Seq 方法:基于 PL 是 PDA 可识别语言子集、PDA 接受代码即合语法的原理,构建 PDA 模块并设计约束生成算法;进一步提出 CODEP,将 PDA 推导状态(状态表示、状态预测与联合预测)融入深度学习。作者为 Python 构建 PDA,在四个公开基准上实验,CODEP 达到 100% 语法正确率;相比基模型,CONALA 的 CodeBLEU 相对提升 17%,DJANGO 的 EM 提升 8%,JUICE-10K 的 CodeBLEU 提升 15%,对预训练模型亦有显著提升。
General-purpose code generation aims to automatically convert the natural language description to code snippets in a general-purpose programming language (GPL) such as Python. In the process of code generation, it is essential to guarantee the generated code satisfies grammar constraints of GPL. However, existing sequence-to-sequence (Seq2Seq) approaches neglect grammar rules when generating GPL code. In this paper, we devise a pushdown automaton (PDA)-based methodology to make the first attempt to consider grammatical Seq2Seq models for general-purpose code generation, exploiting the principle that PL is a subset of PDA recognizable language and code accepted by PDA is grammatical. Specifically, we construct a PDA module and design an algorithm to constrain the generation of Seq2Seq models to ensure grammatical correctness. Guided by this methodology, we further propose CODEP, a code generation framework equipped with a PDA module, to integrate the deduction of PDA into deep learning. This framework leverages the state of PDA deduction (including state representation, state prediction task, and joint prediction with state) to assist models in learning PDA deduction. To comprehensively evaluate CODEP, we construct a PDA for Python and conduct extensive experiments on four public benchmark datasets. CODEP can employ existing sequence-based models as base models, and we show that it achieves 100% grammatical correctness percentage on these benchmark datasets. Consequently, CODEP relatively improves 17% CodeBLEU on CONALA, 8% EM on DJANGO, and 15% CodeBLEU on JUICE-10K compared to base models. Moreover, PDA module also achieves significant improvements on the pre-trained models.