Introduction
While Large Language Models (LLMs) have demonstrated remarkable proficiency in generating and explaining code, their underlying representation often treats programming languages (PL) as simple linear sequences of tokens, akin to natural language (NL). However, code is fundamentally structured by formal grammars and represented as Abstract Syntax Trees (ASTs). This research introduces CodeSyntax, a benchmark designed to evaluate whether state-of-the-art models actually "understand" these structural relationships or merely rely on local patterns.
Methodology: Attention Probing
To investigate internal representations without task-specific fine-tuning, the researchers employed attention probing. This zero-shot approach examines whether specific attention heads within pre-trained models consistently assign high weights to tokens sharing a syntactic relationship (e.g., a function call attending to its arguments).
Performance was measured using the Unlabeled Attachment Score (UAS) and compared against two primary baselines:
- Offset Baseline: A heuristic that selects a token at a fixed distance (e.g., $n$ tokens away).
- Keyword Baseline: A rule-based approach targeting specific reserved words.
The CodeSyntax Benchmark
The CodeSyntax dataset consists of 18,701 Python and 13,711 Java functions sourced from CodeSearchNet. Each sample is automatically annotated with syntactic relationships derived from its AST, covering 43 relation types for Python and 39 for Java. For comparative analysis, the researchers also utilized the English Penn Treebank and German Hamburg Dependency Treebank.
Key Findings
The "Offset" Embarrassment
A striking result of the study is that simple heuristics often outperform sophisticated models. In Python, a combined simple baseline achieved a top-1 score of 49.4%, significantly higher than the best-performing model, CuBERT, which reached only 39.2%. CodeBERT followed at 33.1%.
Natural Language vs. Programming Language
Models exhibit a much stronger grasp of English syntax compared to Python or Java. This discrepancy is largely attributed to the nature of the dependencies. In NL, syntactic relations are typically local, whereas in PL, they are frequently distant and hierarchical. For instance, an if statement and its corresponding else may be separated by dozens of lines of code.
Long-Distance Dependencies
The performance of attention heads drops sharply as the distance (offset) between related tokens increases. While the average offset for NL relations is generally under 10 tokens, PL relations often exceed 100 tokens, creating a significant challenge for standard attention mechanisms.
Reliance on Keywords and Delimiters
Models perform best on relations involving reserved keywords (e.g., if → else) but struggle with user-defined identifiers. Furthermore, in Java, models rely heavily on semicolons and newlines as structural anchors; removing these delimiters causes a significant drop in performance, suggesting models use them as "hints" rather than understanding the underlying logic.
Discussion and Future Work
The findings suggest that current "code-as-sequence" pre-training is insufficient for capturing the hierarchical logic of programming. Models pre-trained on both text and code (like CodeBERT) do not necessarily outperform text-only models (like RoBERTa) in syntax understanding.
The authors conclude that treating code as a sequence is a "leaky abstraction." Future research should prioritize structure-aware pre-training, where models are explicitly incentivized to learn AST structures or data-flow graphs, moving beyond simple next-token prediction to a deeper structural understanding of programs.
Summary updated