Abstract
Code generation maps a program description to executable source code in a programming language. Existing approaches mainly rely on a recurrent neural network (RNN) as the decoder. However, we find that a program contains significantly more tokens than a natural language sentence, and thus it may be inappropriate for RNN to capture such a long sequence. In this paper, we propose a grammar-based structural convolutional neural network (CNN) for code generation. Our model generates a program by predicting the grammar rules of the programming language; we design several CNN modules, including the tree-based convolution and pre-order convolution, whose information is further aggregated by dedicated attentive pooling layers. Experimental results on the HearthStone benchmark dataset show that our CNN code generator significantly outperforms the previous state-of-the-art method by 5 percentage points; additional experiments on several semantic parsing tasks demonstrate the robustness of our model.
Introduction
Generating source code from natural language descriptions is a core challenge in semantic parsing and software engineering. Traditional approaches have leaned heavily on Recurrent Neural Networks (RNNs) as decoders. While effective for short natural language sequences, RNNs face two significant hurdles in code generation:
- Vanishing Gradients in Long Sequences: Programs often consist of hundreds of tokens, far exceeding the typical length of natural language sentences. RNNs struggle to maintain long-term dependencies across these extended sequences.
- Structural Hierarchies: Code is inherently structured as an Abstract Syntax Tree (AST). Flat RNNs often fail to capture the complex, non-linear relationships between distant but logically connected nodes.
To address these issues, researchers from Peking University and AdeptMind proposed a Grammar-Based Structural CNN Decoder. By leveraging the local feature extraction capabilities of CNNs and a grammar-driven generation process, the model ensures both long-range coherence and syntactic correctness.
Methodology
The proposed architecture moves away from token-by-token prediction, instead predicting the sequence of grammar rules (e.g., stmt -> If-block) required to construct the program's AST. This approach guarantees that the generated output is syntactically valid.
The decoder integrates several specialized CNN modules to process the partial AST:
- Tree-Based CNN: Extracts local structural context by sliding a window over a node and its ancestors.
- Pre-Order Traversal CNN: Captures the sequential flow of tree construction, utilizing backtracking traces to maintain positional awareness within the hierarchy.
- Tree-Path CNN: Models the direct path from the root to the current node, providing a global "map" for the generation step.
- Attentive Pooling: Aggregates features from these modules using "controllers" such as the program description or function scope to prioritize relevant information at each step.
Experimental Evaluation
The model was primarily evaluated on the HearthStone (HS) benchmark, which requires converting card descriptions into Python code. This dataset is particularly challenging due to the length and structural complexity of the target code.
Key Results
- Performance Leap: On the HearthStone dataset, the model achieved a String Accuracy (StrAcc) of 27.3%, a 5-percentage-point improvement over the previous state-of-the-art (22.7%).
- Functional Accuracy: When manually evaluated for functional correctness (accounting for variable name variations), accuracy reached 30.3%.
- Robustness: The model remained competitive on semantic parsing tasks like ATIS and JOBS, demonstrating that the structural CNN approach is effective even for shorter programs.
- Ablation Success: Removing any of the structural CNN components (Tree-based, Pre-order, or Tree-path) led to a measurable performance decline, validating the necessity of each module.
Conclusion
The Grammar-Based Structural CNN Decoder demonstrates that CNNs, when combined with tree-structured priors and grammar constraints, can significantly outperform RNNs in code generation tasks. While the current model relies on string matching for evaluation and focuses on relatively small code snippets, it paves the way for more robust, structure-aware code synthesis. Future work may explore deeper tree-based convolutions and the incorporation of sibling node information to further enhance structural understanding.
Summary updated