Code Completion by Modeling Flattened Abstract Syntax Trees as Graphs

Yanlin Wang, Hui Li

Abstract

Code completion is a critical task in modern software development, traditionally approached through either sequential modeling (treating code as text) or structural modeling (treating code as trees). This paper introduces CCAG (Code Completion as Graphs), a novel framework that bridges these paradigms. By flattening Abstract Syntax Trees (ASTs) into sequences and then reconstructing them as graphs with merged identical nodes, CCAG captures both the chronological flow of coding and the inherent hierarchical structure. We introduce the AST Graph Attention Block (ASTGab) and an uncertainty-based multi-task learning strategy to optimize next-node type and value prediction. Experimental results on large-scale JavaScript and Python benchmarks demonstrate that CCAG significantly outperforms state-of-the-art models, including Transformer-XL and Pointer Networks.

1. Introduction

Modern code completion tools face a dichotomy: sequential models like LSTMs and Transformers excel at capturing the "flow" of coding but often neglect hierarchical syntax. Conversely, vanilla graph models capture structure but struggle with the sequential and repetitive patterns inherent in human coding—such as the high likelihood (~60%) that a variable used previously will be reused shortly. Furthermore, code completion requires predicting both the type of a node (e.g., NameStore) and its value (e.g., variable name x). Balancing these sub-tasks usually requires manual weight tuning, which is often suboptimal.

2. Methodology

CCAG addresses these limitations through a three-step pipeline that integrates sequential order with structural depth.

The CCAG architecture pipeline, from AST flattening to multi-task learning.

2.1 Flattened AST as a Graph

The process begins by flattening the AST using a pre-order depth-first traversal, reflecting the actual order in which code is written. This sequence is then converted into a graph where duplicated nodes are merged into a single node. This transformation is pivotal; it converts long-distance sequential dependencies into direct neighbor relationships in the graph, allowing the model to "see" repetitions directly.

Detailed view of graph construction where repeated nodes are merged to capture long-range dependencies.

2.2 AST Graph Attention Block (ASTGab)

To process the resulting graph, CCAG employs a stack of specialized attention layers:

  • NGAT (Neighbor Graph Attention): Captures local dependencies between adjacent tokens in the sequence.
  • GSAT (Global Self-attention): Provides a global context of the entire partial AST.
  • PCAT (Parent-child Attention): Reintroduces the hierarchical tree structure by explicitly modeling parent-child relationships.

2.3 Uncertainty-based Multi-task Learning

To solve the task imbalance between type and value prediction, CCAG treats the weights of the respective losses as learnable parameters based on aleatoric uncertainty. The model automatically adjusts its focus based on which task is "noisier" during specific training phases, leading to more stable convergence.

3. Evaluation

The model was evaluated on standard benchmarks consisting of 150,000 program files each for JavaScript (JS) and Python (PY). To test robustness, the authors created variations (e.g., JS1k to JS50k) based on vocabulary size.

3.1 Key Results

CCAG consistently outperformed all baselines, including industry standards like Vanilla LSTMs and advanced models like Transformer-XL:

  • Accuracy: CCAG improved value prediction accuracy by 2.68% to 12.32% and type prediction by 3.32% to 5.88% over the strongest baselines.
  • Industry Comparison: It outperformed Vanilla LSTM (used in early versions of IntelliCode) by up to 23.86% in value prediction.
  • Ablation Studies: Removing any component (PCAT edges or GSAT) resulted in performance degradation, validating the multi-layered attention approach.

4. Conclusion and Future Work

CCAG demonstrates that treating repetition as a structural feature rather than a sequential burden significantly enhances code completion. By merging nodes in a flattened AST graph, the model respects both the chronological and logical structure of code. Future work may involve incorporating data flow graphs to provide deeper semantic context and exploring alternative tree linearization methods to further mitigate vocabulary constraints and the "Out of Vocabulary" problem.

Summary updated

BibTeX
@inproceedings{wang2021code,
  author = {Yanlin Wang and Hui Li},
  title = {{Code Completion by Modeling Flattened Abstract Syntax Trees as Graphs}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2021},
  doi = {10.1609/aaai.v35i16.17650},
}
Figure