NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Generative Code Modeling with Graphs

Marc Brockschmidt, Miltiadis Allamanis, Alexander L. Gaunt, Oleksandr Polozov

Generating source code presents a unique challenge for machine learning. Unlike natural language, code must satisfy strict "hard" constraints: it requires rigorous adherence to grammar (syntax) and logical consistency (semantics), such as utilizing variables that are currently in scope and of the correct type.

Existing approaches typically fall into two distinct camps that struggle to balance these needs. Program synthesis relies on formal logic and search, excelling at satisfying specifications but often producing "unnatural" code. Conversely, deep learning models like Seq2Seq or Tree-LSTMs capture the stylistic nuances of code but frequently fail on semantics—generating realistic-looking lines that reference non-existent variables or perform operations on incorrect data types.

To bridge this gap, researchers have introduced a novel generative model that combines the structural rigor of grammar-based trees with the expressive power of Graph Neural Networks (GNNs). By understanding the deep, non-linear relationships within a program, this model produces code that is both syntactically correct and semantically consistent.

Neural Attribute Grammars and Interleaved Generation

The proposed framework, called Neural Attribute Grammars (NAG), treats code generation as a step-by-step expansion of an Abstract Syntax Tree (AST) powered by a graph-based neural network. This approach is heavily inspired by "Attribute Grammars" from compiler theory, where nodes in a syntax tree carry attributes that flow up and down the tree. NAGs replace manually defined flow rules with learned neural message passing.

Instead of merely viewing the code as a tree, the model deterministically adds extra edges to represent hidden relationships, providing a powerful inductive bias. These include Parent edges for bottom-up information, NextSib for information flow between siblings, and NextUse to connect different occurrences of the same variable. By treating variables as first-class citizens via NextUse edges, the model ensures consistent variable usage.

The generation process operates in a continuous loop of expansion and message passing:

  1. Expand: Select a production rule from the grammar to grow the tree (e.g., expanding an Expression node).
  2. Augment: Deterministically add graph edges (like NextSib or NextUse) to the newly created nodes.
  3. Message Passing: Run a Gated Graph Neural Network (GGNN) over the current partial graph, allowing information to flow across the structural shortcuts.
  4. Update: Utilize the resulting neural embeddings to inform the next expansion step.
Step-by-step interleaved generation and graph augmentation process.

The ExprGen Task and Dataset

To evaluate this architecture, the authors introduced the ExprGen task, a "hole completion" challenge where the model must generate a complex expression to fill a gap in an existing C# program based on surrounding context.

They curated a dataset of approximately 344,000 samples from 593 high-star open-source C# projects on GitHub. The dataset focuses on Boolean, arithmetic, and string expressions, intentionally excluding project-specific APIs to maintain a manageable vocabulary while preserving high semantic complexity. A strict "test-only" split from entirely unseen projects was used to evaluate generalization.

Key Results and Future Directions

The NAG model demonstrated significant improvements over standard sequence and tree-based baselines:

  • Well-Typedness: The NAG model produced code that compiles and makes semantic sense in context 86.4% of the time, drastically outperforming the standard Seq2Seq model's 32.4%.
  • Accuracy: For exact ground-truth expression matching (Acc@1), the NAG model achieved 52.3%, outperforming the next best baseline (Syn at 50.5%).
  • Generalization: The model maintained its performance lead on the test-only set of unseen projects, proving it learned general C# programming patterns rather than memorizing training data.

While highly effective, the current model is restricted to specific expression types and struggles with generating unseen literals (hard-coded strings or numbers). Future work aims to scale this approach to generate entire functions or classes, integrate more sophisticated data-flow analysis into the graph augmentation, and apply the model to automated program repair and intelligent code review tools.

Summary updated

BibTeX
@inproceedings{brockschmidt2019generative,
  author = {Marc Brockschmidt and Miltiadis Allamanis and Alexander L. Gaunt and Oleksandr Polozov},
  title = {{Generative Code Modeling with Graphs}},
  booktitle = {International Conference on Learning Representations},
  year = {2019},
  url = {https://openreview.net/forum?id=dbbvaGYRBy},
}
Figure