Abstract
A code generation system generates programming language code based on an input natural language description. State-of-the-art approaches rely on neural networks for code generation. However, these code generators suffer from two problems: long-range dependencies and structure modeling. In this paper, we propose TreeGen, a novel tree-based neural architecture for code generation. TreeGen uses the attention mechanism of Transformers to alleviate the long-dependency problem and introduces a novel AST reader to incorporate grammar rules and AST structures. Evaluated on Python (HearthStone) and semantic parsing (ATIS, GEO) benchmarks, TreeGen outperformed previous state-of-the-art approaches, achieving 33.3% accuracy on HearthStone and high accuracy on ATIS (89.1%) and GEO (89.6%).
Introduction
Generating code from natural language (NL) descriptions is a complex task that faces two primary hurdles. First, long-range dependencies create challenges; for instance, a variable reference may depend on a definition appearing many lines earlier. Traditional models often struggle to maintain these distant relationships. Second, structural complexity is inherent to code, which follows formal grammar rules represented as Abstract Syntax Trees (ASTs). Standard "flat" models like RNNs or vanilla Transformers often fail to capture these nested hierarchical structures.
TreeGen addresses these issues by combining the long-range dependency handling of Transformers with a specialized "AST Reader" designed to navigate the complexities of code structure.
Architecture and Methodology
The TreeGen architecture consists of three primary components designed to bridge the gap between natural language and structured code:
- NL Reader (Encoder): This component processes the natural language input using a stack of Transformer blocks. It is enhanced with character embeddings and word convolutions to better capture technical nuances in descriptions.
- AST Reader (Encoder): The core innovation of TreeGen, the AST Reader encodes the partially generated code. Rather than treating code as a flat sequence, it utilizes Tree Convolution to integrate the features of a node with its ancestors in the AST.
- Decoder: A Transformer-based decoder attends to both the NL and AST Readers to predict the next grammar rule. It incorporates a Pointer Network to allow the model to copy specific tokens, such as variable names, directly from the input.
A critical architectural insight is the placement of structural convolutions. The authors found that applying tree convolutions only to the initial blocks of the AST reader is more effective than applying them throughout, as it preserves local structural information before it is integrated into the global attention mechanism.
Key Insights and Ideas
TreeGen's success stems from several key design choices:
- Grammar-Based Generation: Instead of predicting raw text tokens, TreeGen predicts a sequence of grammar rules. This ensures the generated output is syntactically valid and adheres to the language's formal structure.
- Selective Structural Convolution: By applying convolutions only to early layers, the model maintains a clear representation of the code's hierarchy while leveraging the Transformer's global view in deeper layers.
- Efficiency: TreeGen is significantly faster to train than previous CNN or RNN-based architectures, reducing training time per epoch on the HearthStone dataset from 180 seconds to just 18 seconds.
Evaluation and Results
The model was evaluated on three established benchmarks:
- HearthStone: A dataset of 665 Python classes. TreeGen-B achieved 33.3% Acc+, outperforming the previous state-of-the-art by 4.5 percentage points.
- Semantic Parsing (ATIS & GEO): TreeGen achieved 89.1% and 89.6% accuracy, respectively, the best performance among neural network-based approaches.
Ablation tests confirmed that both the Tree Convolution and the Rule Definition Encoding are vital, as removing either led to significant performance drops.
Conclusion
While TreeGen sets new benchmarks for neural code generation, it still trails slightly behind traditional template-based parsers on small, specific datasets like ATIS. Future work will explore scaling this tree-based Transformer to more complex languages like C++ or Java and deeper ASTs in larger codebases.
Summary updated