[Paper Review] A Grammar-Based Structural CNN Decoder for Code Generation
This paper proposes a grammar-based structural CNN decoder for code generation that generates programs by predicting grammar rules rather than tokens, using tree-based and pre-order convolutional layers with attentive pooling to model AST structure. It achieves a 5-percentage-point improvement over prior state-of-the-art RNN-based methods on the HearthStone benchmark, demonstrating superior performance on long-form code generation and robustness on semantic parsing tasks.
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. We also conduct in-depth ablation test to better understand each component of our model.
Motivation & Objective
- To address the limitations of RNN-based decoders in handling long sequences typical in program generation.
- To improve code generation by explicitly modeling program structure through abstract syntax trees (ASTs).
- To design a CNN-based decoder that generates code via grammar rule prediction, ensuring syntactic correctness.
- To evaluate the model's performance on long-form code generation and semantic parsing tasks.
- To analyze the contribution of each component through ablation studies.
Proposed method
- The model generates code by predicting grammar rules (e.g., If → expr stmt* stmt*) instead of individual tokens, ensuring syntactic validity.
- It employs tree-based convolution to capture structural dependencies in the AST by sliding windows over parent-child node relationships.
- Pre-order convolution processes the sequence of nodes in the AST in pre-order traversal, modeling sequential dependencies within the tree structure.
- Ancestral convolution is applied to encode the path from the root to the current node, enhancing autoregressive generation.
- A dedicated attentive pooling mechanism aggregates features from multiple CNN modules, with scope names (e.g., function names) used as controllers for attention.
- The encoder processes the natural language description, while the decoder autoregressively generates grammar rules conditioned on the encoder and prior predictions.
Experimental results
Research questions
- RQ1Can a CNN-based decoder outperform RNN-based decoders in code generation, especially for long programs?
- RQ2How effective is grammar rule-based generation in maintaining syntactic correctness and improving performance?
- RQ3To what extent do structural CNN components (tree-based, pre-order, ancestral convolutions) contribute to model performance?
- RQ4Does the attentive pooling mechanism that incorporates scope names improve code generation accuracy?
- RQ5How generalizable is the proposed model across different tasks, such as semantic parsing with shorter logical forms?
Key findings
- The proposed grammar-based structural CNN decoder achieves a 5-percentage-point improvement over the previous state-of-the-art RNN-based method on the HearthStone benchmark for Python code generation.
- On the ATIS semantic parsing dataset, the model achieves 85.0% accuracy, comparable to state-of-the-art neural models.
- On the JOBS semantic parsing dataset, the model reaches 89.3% accuracy, outperforming most prior neural and traditional systems.
- Ablation studies confirm that each component—tree-based convolution, pre-order convolution, ancestral convolution, and attentive pooling—contributes significantly to performance.
- The model generalizes well to semantic parsing tasks despite being primarily designed for long-form code generation, indicating its flexibility and robustness.
- The model performs less impressively on short-form logical forms (e.g., semantic parsing) compared to long-form code, suggesting that the benefits of CNNs are most pronounced in longer sequences.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.