A Syntactic Neural Model for General-Purpose Code Generation

Pengcheng Yin, Graham Neubig

Abstract: We consider the problem of parsing natural language descriptions into source code written in a general-purpose programming language like Python. Existing data-driven methods treat this problem as a language generation task without considering the underlying syntax of the target programming language. Informed by previous work in semantic parsing, in this paper we propose a novel neural architecture powered by a grammar model to explicitly capture the target syntax as prior knowledge. Experiments find this an effective way to scale up to generation of complex programs from natural language descriptions, achieving state-of-the-art results that well outperform previous code generation and semantic parsing approaches.

1. Introduction

Generating general-purpose source code from natural language (NL) descriptions is a fundamental challenge in software engineering. While semantic parsing has succeeded in mapping NL to domain-specific logic, general-purpose programming languages (PLs) like Python present a steeper challenge due to their structural complexity.

Traditional data-driven methods often treat code generation as a standard machine translation task, using sequence-to-sequence models to output flat code strings. However, code is inherently hierarchical and governed by rigid grammar. Standard models frequently produce syntactically invalid code because they lack an understanding of these underlying rules. This paper addresses this by proposing a model that explicitly respects the syntax of the target language.

2. Methodology

The core innovation is a neural architecture that predicts a sequence of actions to construct an Abstract Syntax Tree (AST) rather than a flat string. Once the AST is generated, it is deterministically converted back into surface code.

2.1 The Grammar Model

The model constructs the AST in a depth-first, left-to-right order using two primary action types:

  • APPLYRULE[r]: Expands a non-terminal node using a production rule from the language's grammar (e.g., expanding a Call node into its arguments).
  • GENTOKEN[v]: Populates terminal nodes with specific values, such as variable names or constants.
Overview of the AST generation process (a) and the corresponding action sequence (b).

2.2 Neural Architecture

The system utilizes an attentional LSTM decoder with two critical enhancements designed for tree-structured data:

  • Parent Feeding: To maintain context within deep trees, the hidden state of the "parent" action (the rule that generated the current node) is fed directly into the current time step. This allows information to flow along the branches of the tree rather than just linearly.
  • Pointer Network (Copy Mechanism): To handle out-of-vocabulary variable names mentioned in the NL input, the model can "point" to the input string and copy tokens directly into the code.
The neural architecture featuring parent feeding and the pointer network mechanism for state computation.

3. Experimental Setup

The model was evaluated on three benchmarks representing different levels of complexity:

  1. HEARTHSTONE (HS): Long, complex Python classes (avg. 137 AST nodes) representing game cards.
  2. DJANGO: Single-line Python snippets from the Django framework paired with manual descriptions.
  3. IFTTT: A simpler domain-specific language for "If-This-Then-That" recipes.

To improve efficiency, the authors implemented unary closures, collapsing chains of single-child rules into single actions, which significantly reduced the number of steps required for complex tree generation.

4. Results and Discussion

The syntax-driven approach achieved state-of-the-art results across all datasets:

  • HearthStone: Accuracy jumped from 4.5% to 16.2%, with BLEU scores rising from 65.6 to 75.8. Ablation studies confirmed that parent feeding was vital for this dataset's structural complexity.
  • Django: Achieved 71.6% accuracy, surpassing the previous best of 62.3%. Here, the copy mechanism proved to be the most critical component.
  • IFTTT: The model performed on par with or better than specialized semantic parsers, demonstrating robustness across both general PLs and DSLs.

By constraining the search space to valid grammar rules, the model ensures that generated programs are always syntactically correct. However, challenges remain in handling highly nested logic and complex functional structures like lambda expressions.

5. Conclusion

This work demonstrates that incorporating the underlying syntax of a programming language as prior knowledge is essential for generating complex, general-purpose code. By combining a grammar-based action model with topological information flow (parent feeding), the proposed architecture achieves a significant performance leap over traditional sequence-to-sequence models.

Summary updated

BibTeX
@inproceedings{yin2017syntactic,
  author = {Pengcheng Yin and Graham Neubig},
  title = {{A Syntactic Neural Model for General-Purpose Code Generation}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2017},
  url = {https://openreview.net/forum?id=SkNcRnedZB},
}
Figure