Bringing Structure to Naturalness: On the Naturalness of ASTs

Profir-Petru Pârtachi, Mahito Sugiyama

Introduction

For over a decade, the "Naturalness of Software" has been a cornerstone of AI for Software Engineering. The core premise is that although code is technically formal, human developers write it in highly repetitive, predictable ways—much like natural language. While most research has focused on the token level, treating code as a sequence of words, newer models leverage structural representations like Abstract Syntax Trees (ASTs). This paper formalizes the Structured Naturalness Hypothesis, arguing that the statistical predictability of source code extends beyond simple text tokens to its underlying hierarchical structure.

Methodology

The authors test this hypothesis using a two-step approach:

  1. Measuring Entropy: They employ a Child-Sum TreeLSTM to perform masked token prediction on ASTs. By calculating "self-cross-entropy," they quantify the bits of information required to describe the data. Lower entropy indicates higher predictability (naturalness).
  2. Downstream Validation: To evaluate the utility of this structural signal, embeddings learned by the TreeLSTM are fed into a Random Forest classifier for Just-In-Time (JIT) defect prediction, identifying commits likely to introduce bugs.

This approach inherently handles syntax tokens (e.g., semicolons, brackets) as part of the tree structure, avoiding the "stop-word" noise common in token-based models.

Empirical Analysis of AST Structure

A primary finding of the study is the Zipfian Nature of Trees. The labels within ASTs follow a clear Zipfian distribution, where a small number of structural patterns appear with high frequency, while the vast majority are rare. This confirms that the "grammar" of code is as repetitive as its vocabulary.

AST labels follow a Zipfian distribution across various programming languages, indicating structural regularity.

The study utilized the CodeSearchNet Corpus for the primary naturalness study across five languages: Go, Ruby, Python, JavaScript, and Java. Standardized ASTs were generated using ANTLR4 to ensure consistency across different language grammars.

Key Results: Predictability and Performance

The research reveals that not all languages are equally "natural" in their tree representations. Ruby proved exceptionally predictable, with a tree-based entropy of 8.5 bits, actually outperforming traditional token-based n-gram models (9.0 bits). In contrast, Java and Python exhibited higher entropy (13.1 and 14.0 bits, respectively)—significantly less predictable than typical English text (~12 bits)—suggesting their structural views are more complex to model.

Entropy measurements for different languages across varying sequence length limits.

Despite varying predictability, the learned representations proved powerful. In the JIT defect prediction task on the Apache Commons Lang project, the AST-based model achieved an F1 score of 0.236. This outperformed a state-of-the-art approach (0.199) that relied on complex, manually engineered features. Notably, these gains were achieved without human experts defining "buggy" structures; the model learned these regularities autonomously from the inherent structure of the code.

Conclusion and Future Work

The Structured Naturalness Hypothesis provides a formal basis for using structural code representations in machine learning. While the current work uses Child-Sum TreeLSTMs, future research could explore N-way TreeLSTMs or TreeTransformers to capture sibling node order. Additionally, extending this analysis to Control-Flow Graphs (CFGs) and Program Dependency Graphs (PDGs) could further deepen our understanding of code's inherent naturalness beyond the syntax tree.

Summary updated

BibTeX
@inproceedings{partachi2024bringing,
  author = {Profir-Petru Pârtachi and Mahito Sugiyama},
  title = {{Bringing Structure to Naturalness: On the Naturalness of ASTs}},
  booktitle = {ICSE Companion},
  year = {2024},
  doi = {10.1145/3639478.3643517},
}
Figure