Source-linked AI summary
A Syntactic Neural Model for General-Purpose Code Generation
Pengcheng Yin, Graham Neubig
TL;DR
Programmers may know what they want to do but lack the syntax to implement it, and searching the web for code is time-consuming. This paper generates ASTs with a grammar model that encodes programming-language syntax, reporting 11.7% and 9.3% absolute accuracy improvements over Ling et al. (2016) on two Python code-generation tasks.
Problem
Method
The model generates an AST from a natural-language description through sequential grammar actions, encoding the target programming language's syntax as prior knowledge.
Source: §1 · pp. 1–2, §2 · p. 2Results
11.7% and 9.3% absolute accuracy improvements over the state-of-the-art system (Ling et al., 2016) on two Python code-generation tasks; the model also performs competitively on a standard semantic-parsing benchmark.
Source: §1 · p. 2Takeaways & Limitations
Experiments support the approach's effectiveness on both code-generation and semantic-parsing tasks.
Source: §7 · p. 9Accuracy measures exact match, BLEU may not proxy code semantics well, and the paper leaves more sophisticated metrics such as static-code-analysis measures for future work.
Source: §5.3 · p. 6
Abstract
from arXiv · showhide
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
The paper proposes syntax-aware neural generation for general-purpose code, representing programs as ASTs and using a grammar model to guide generation. Experiments report substantial accuracy gains on two Python code-generation tasks.
1 Introduction
- Earlier data-driven code generation treated the task as sequence-to-sequence modeling without explicitly accounting for target-language syntax, while semantic-parsing targets often have simpler syntax than programming languages.The introduction frames direct generation as an alternative to time-consuming searches for code examples.Source: §1 · p. 1
- The model generates an AST from a natural-language statement, then deterministically converts it to surface code, using standard parsers to obtain syntax information with minimal engineering.The authors hypothesize that this structure constrains generation to well-formed code and helps model information flow through recursive code structure.Source: §1 · pp. 1–2
- 11.7% and 9.3% absolute improvements in accuracy over the state-of-the-art system on two Python code-generation tasks.The comparison is against Ling et al. (2016).Source: §1 · p. 2
2 The Code Generation Problem
Given a natural-language description, the task is to produce a code snippet in a modern programming language. The paper models this as generating an AST and converting the selected tree into code.
2 The Code Generation Problem
- The task is to generate a code snippet in a modern programming language from the intent of a natural-language description.The paper uses Python examples but describes the method as programming-language agnostic.Source: §2 · p. 2
- A probabilistic grammar model assigns p(y|x) to an AST y given description x, and selects the best-possible AST.The selected AST is deterministically converted to surface code.Source: §2 · p. 2
- The target AST is introduced as an intermediate representation for generating the corresponding code.The paper describes Python ASTs as built from production rules, with non-terminal nodes sketching code structure and terminal nodes holding operations, variables, or constants.Source: §2 · p. 2
3 Grammar Model
The grammar model generates a derivation AST through a sequence of actions. Production-rule actions build structure, and token-generation actions fill variable terminals.
3 Grammar Model
- The grammar model factorizes AST generation into sequential actions, with APPLYRULE selecting production rules and GENTOKEN supplying terminal tokens.The example generation proceeds depth-first and left-to-right; Figure 1 links the AST nodes to the action sequence that generates them.Source: §3 · pp. 2–3, Figure 1 · p. 3
3.1 APPLYRULE Actions
- APPLYRULE expands the current frontier node with a production whose head matches that node’s type; unary closure can combine chains of unary productions into one action.Unary closure reduces the number of actions but can increase grammar size.Source: §3.1 · p. 3
3.2 GENTOKEN Actions
- GENTOKEN fills variable-terminal nodes by appending one or more tokens, which may come from a predefined vocabulary or be copied from the input description.A special end token closes the node before generation proceeds to the next frontier node.Source: §3.2 · pp. 3–4
4 Estimating Action Probabilities
The model estimates action probabilities with an attentional encoder–decoder whose information flow is structured by syntax trees. Its decoder tracks AST generation and chooses grammar rules or token actions at successive steps.
4 Estimating Action Probabilities
- Action probabilities are estimated with an attentional encoder–decoder whose information flow follows syntax-tree structure.Source: §4 · p. 4
4.1 Encoder
- A bidirectional LSTM computes a context-sensitive embedding for each word in the natural-language description.Source: §4.1 · p. 4
4.2 Decoder
- Each grammar action corresponds to a decoder RNN step that tracks AST generation before computing the next action probability.Source: §4.2 · p. 4
4.2.1 Tracking Generation States
- Each decoder state combines the previous action embedding, input-attention context, parent-action information, and frontier-node type through an LSTM update.Source: §4.2.1 · p. 4
4.2.2 Calculating Action Probabilities
- APPLYRULE selects grammar expansions, while GENTOKEN generates variable-terminal values from a vocabulary or by copying input tokens.Source: §4.2.2 · p. 5
4.3 Training and Inference
- Training converts target code into ASTs and oracle action sequences, maximizes their log-likelihood, and uses beam search at inference.Source: §4.3 · p. 5
5 Experimental Evaluation
The evaluation tests Python code generation on HS and DJANGO, with IFTTT as a domain-specific comparison. The model improves accuracy over the prior state-of-the-art system on both Python tasks, alongside analyses of decoding, AST size, and evaluation limits.
5.1 Datasets and Metrics
- HS contains complex Python classes averaging 137 AST nodes, while DJANGO spans diverse real-world code; evaluation uses accuracy and token-level BLEU-4.The IFTTT benchmark is a noisy domain-specific dataset evaluated on a filtered test set verified by at least three annotators.Source: §5.1 · pp. 5–6
5.2 Setup
- The model uses 128-dimensional embeddings except 64-dimensional node-type embeddings, 256-dimensional RNN states and 50-dimensional hidden layers, recurrent and standard decoder dropout, and beam-size-15 decoding.Unary closures are extracted above frequency thresholds of 30 for HS and 50 for DJANGO.Source: §5.2 · p. 6
5.3 Results
- Accuracy misses semantically equivalent code, and BLEU may not measure code semantics; functional equivalence remains an evaluation challenge.About 2% of sampled failed examples across DJANGO and HS were judged semantically equivalent.Source: §5.3 · pp. 6, 8
- Accuracy improves over LPN by 11.7% on HS and 9.3% on DJANGO.Results are averaged over three runs.Source: Table 3 · p. 6, §5.3 · p. 6
- On HS, SEQ2TREE takes over 300 decoding steps on average versus 170 for this model; grammar actions guarantee grammatical ASTs, unlike SEQ2TREE.Removing parent feeding significantly reduces HS accuracy, and removing the pointer network yields poor results, indicating the value of copying names and values from inputs.Source: §5.3 · p. 7
- Performance drops as reference ASTs grow; unary closure helps HS but not DJANGO, where it expands the grammar and increases sparsity.On HS, AST BLEU remains around 50 at 200 nodes; unary closure reduces actions from 173 to 142 on HS, while DJANGO has 237 productions versus HS's 100.Source: §5.3 · p. 7, Figure 3 · p. 7, Figure 4 · p. 7
- On IFTTT, the model reaches 82.0% full-tree accuracy, outperforming other neural models and nearing the best classical method.The comparison uses a noise-filtered test set requiring more than three annotators to agree with gold annotations.Source: §5.3 · p. 7, Table 4 · p. 7
- HS predictions usually fill learned templates, but an unseen block shows some generalization; failures include 30% copy-position errors on DJANGO and partial implementations on HS.In the sampled failures, all failed HS cards involved partial implementation errors.Source: §5.3 · p. 8
6 Related Work
Related work spans code generation, code retrieval, and semantic parsing, with grammar-informed neural parsing providing a close connection to this model. The paper distinguishes its approach by jointly modeling production-rule application and terminal-symbol generation for general-purpose code.
Code Generation and Analysis
- General-purpose code generation has used language- and task-specific rules, while another line retrieves code from natural-language queries.Allamanis et al. (2015) measures semantic relatedness between natural language and ASTs for code retrieval; this paper addresses generation.Source: §6 · pp. 8–9
- Most existing works on code generation focus on generating code for domain-specific languages (DSLs), with neural network-based approaches recently explored (Parisotto et al., 2016; Balog et al., 2016).Related tasks include natural-language queries for code retrieval and generating natural-language summaries from source code.Source: §6 · p. 8
Semantic Parsing
- The closest related structured-prediction approach also uses grammar as prior knowledge, while this model jointly captures production-rule application and terminal-symbol generation for general-purpose code.The cited structured-prediction approach constrains generation of derivation trees.Source: §6 · p. 9
- Neural semantic parsers and grammar-guided methods include data augmentation and constrained next-token decoding (Misra and Artzi, 2016; Dong and Lapata, 2016; Neelakantan et al., 2016; Yin et al., 2016; Kocisk´y et al., 2016; Jia and Liang, 2016; Liang et al., 2016).Semantic parsing targets logical forms that can be viewed as domain-specific languages.Source: §6 · p. 9
7 Conclusion
The paper concludes that its syntax-driven neural approach generates ASTs through sequential grammar-model actions. Experiments on code-generation and semantic-parsing tasks demonstrate the approach's effectiveness.
- The approach generates an abstract syntax tree by sequentially applying actions from a grammar model, and experiments on code-generation and semantic-parsing tasks demonstrate its effectiveness.Source: §7 · p. 9
A Encoder LSTM Equations
The encoder processes the input description in both directions and combines the resulting word representations. Inference uses beam search to expand AST hypotheses with grammar rules or terminal-token choices, retaining the top-scoring candidates.
A Encoder LSTM Equations
- Two LSTMs process the natural-language input in forward and backward order, and each word's representation concatenates the two directional hidden states.Source: A Encoder LSTM Equations · p. 12
B Inference Algorithm
- Beam search expands each AST hypothesis at its frontier node using grammar rules for non-terminals or vocabulary and input-description tokens for variable terminals, then keeps the top K hypotheses.The top-scored complete AST is converted into surface code; inference can expand hypotheses simultaneously using GPU mini-batching.Source: B Inference Algorithm · p. 12
C Dataset Preprocessing
Preprocessing replaces infrequent word types with an unknown token and canonicalizes some Django descriptions. Training action sequences are generated by parsing reference code into ASTs and traversing them in pre-order.
- Word types below frequency thresholds are replaced by <unk>: d = 3 for DJANGO and HS, and d = 2 for IFTTT.Source: C Dataset Preprocessing · p. 12
- For DJANGO, quoted string literals become indexed placeholders later restored to their values, and cascading variable references are split into tokens to allow partial or whole-name copying.Source: C Dataset Preprocessing · p. 12
- Gold action sequences come from pre-order AST traversal; Python examples are parsed with the standard ast module, while IFTTT uses its official ASTs.Variable-terminal values are tokenized by spaces and camel case.Source: C Dataset Preprocessing · p. 12
D Additional Decoding Examples
The additional predictions expose dataset-specific error patterns, including pointer-copy mistakes and examples deemed incorrect despite being semantically correct.
D Additional Decoding Examples
- DJANGO errors commonly involve missing arguments or incorrectly copied words, while HS errors mostly involve partially or incorrectly implemented effects.The error sources differ across the two datasets.Source: D Additional Decoding Examples · p. 12
- The model relies heavily on its pointer network to copy variable names and constants from input descriptions.Source: D Additional Decoding Examples · p. 12
- One Table 6 example is semantically correct but counted as incorrect by the exact-match metric, motivating evaluation that considers execution results in future work.Source: D Additional Decoding Examples · p. 12
- Tables 6 and 7 show predicted examples from DJANGO and HS, respectively, with copied contents above 0.9 copy probability highlighted.Source: Table 6 · p. 13, Table 7 · p. 14