Abstract
Large Language Models (LLMs) for code are increasingly integrated into developer tools, yet explaining their predictions remains difficult. This paper introduces ASTxplainer, a framework that aligns token predictions with Abstract Syntax Tree (AST) nodes. By aggregating model logits within AST structures, ASTxplainer enables fine-grained evaluation of programming concepts and provides visualizations to aid user understanding. An empirical study of 12 LLMs and a user study demonstrate the framework's utility in providing insights into model effectiveness and explainability.
Introduction
The rapid adoption of LLMs like GitHub Copilot has transformed software engineering, but these models often function as "black boxes." Traditional metrics like BLEU or functional correctness (e.g., HumanEval) indicate if a model works but fail to explain why. A significant "representation gap" exists: models predict token sequences, while developers reason using programming language (PL) concepts like loops or scope. ASTxplainer bridges this gap by grounding model predictions in the formal syntactic structure of code.
Methodology: The ASTxplainer Framework
ASTxplainer consists of three core components designed to evaluate and explain LLM behavior through syntactic structures.
- AsC-Eval (Evaluation): This component defines Abstract Syntax Concepts (AsC)—groups of AST nodes representing meaningful PL categories (e.g., Iteration, Exceptions). It uses an alignment function to map predicted tokens to AST nodes and an aggregation function to calculate confidence scores for specific concepts.
- AsC-Causal (Causal Analysis): To verify that syntactic concepts drive performance, the authors employ Causal Inference. A Structural Causal Model (SCM) estimates the Average Treatment Effect of specific concepts on model loss, controlling for confounders like code length.
- AsC-Viz (Visualization): This tool overlays confidence scores onto the code's AST, using color-coding to highlight areas of model uncertainty, thereby assisting developers in interpreting suggestions.
Empirical Evaluation
The framework was tested using Galeras, a dataset of 50,000 unique Python snippets from GitHub commits (Jan 2022 – Jan 2023) to avoid data contamination. The study benchmarked 12 LLMs, including the GPT-Neo family and Salesforce’s CodeGen, ranging from 110M to 2.7B parameters.
Key Results and Insights
The evaluation revealed that code models benefit significantly from syntactic grounding.
- Model Performance: The
codegen-2B-monomodel outperformed others with a global AsC-Eval score of 0.84. Mono-language models (fine-tuned on Python) showed higher confidence and lower variability than general-purpose models. - Syntactic Strengths and Weaknesses: Models generally excel at "Scope" and "Iterations" due to rigid rules but struggle with "Natural Language" elements like identifiers and strings.
- Causal Impact: Causal analysis identified "identifiers" as having the most significant impact on overall loss; errors in naming often correlate with poor global performance metrics.
- User Utility: A study with 40 practitioners found that 57% preferred a "Partial AST" visualization for its balance of detail and readability.
Conclusion
ASTxplainer provides a robust method for interpreting LLM predictions by leveraging the inherent structure of programming languages. While currently focused on Python, the methodology is generalizable. Future work includes integrating these "uncertainty heatmaps" into IDEs and expanding the framework to encompass more complex semantic concepts.
Summary updated