[Paper Review] LMs: Understanding Code Syntax and Semantics for Code Analysis
The study evaluates how well LLMs understand code syntax and semantics for static and dynamic analysis across multiple languages, finding strong syntax understanding but limited dynamic semantics, with notable data-shift and hallucination risks.
Large language models~(LLMs) demonstrate significant potential to revolutionize software engineering (SE) by exhibiting outstanding performance in SE tasks such as code and document generation. However, the high reliability and risk control requirements in software engineering raise concerns about the lack of interpretability of LLMs. To address this concern, we conducted a study to evaluate the capabilities of LLMs and their limitations for code analysis in SE. We break down the abilities needed for artificial intelligence~(AI) models to address SE tasks related to code analysis into three categories: 1) syntax understanding, 2) static behavior understanding, and 3) dynamic behavior understanding. Our investigation focused on the ability of LLMs to comprehend code syntax and semantic structures, which include abstract syntax trees (AST), control flow graphs (CFG), and call graphs (CG). We employed four state-of-the-art foundational models, GPT4, GPT3.5, StarCoder and CodeLlama-13b-instruct. We assessed the performance of LLMs on cross-language tasks involving C, Java, Python, and Solidity. Our findings revealed that while LLMs have a talent for understanding code syntax, they struggle with comprehending code semantics, particularly dynamic semantics. We conclude that LLMs possess capabilities similar to an Abstract Syntax Tree (AST) parser, demonstrating initial competencies in static code analysis. Furthermore, our study highlights that LLMs are susceptible to hallucinations when interpreting code semantic structures and fabricating nonexistent facts. These results indicate the need to explore methods to verify the correctness of LLM output to ensure its dependability in SE. More importantly, our study provides an initial answer to why the codes generated by LLM are usually syntax-correct but vulnerable.
Motivation & Objective
- Assess whether LLMs can understand code syntax (AST and expression roles).
- Evaluate LLMs' ability to comprehend static behaviors (CFG, CG, data dependency, taint, pointer analysis).
- Investigate LLMs' grasp of dynamic behaviors (equivalent mutants, flaky tests).
- Analyze prompts and data sources to determine reliability and limitations for code analysis tasks.
Proposed method
- Evaluate four state-of-the-art LLMs: GPT-4, GPT-3.5, StarCoder, CodeLlama-13b-instruct across 9 tasks.
- Design tasks to test syntax understanding (AST generation, expression matching).
- Test static analysis capabilities (CFG, CG, data dependency, taint analysis, pointer analysis).
- Assess dynamic behavior understanding via equivalent mutant detection and flaky test reasoning.
- Use curated datasets (2,560 data points, 151,633 LoC, 217 programs) and diverse languages (Python, Java, C, Solidity).
- Employ role-based and instruction prompts, with prompt optimization via GPT-based tooling; evaluate outputs against AST parsers, expert reviews, and ground-truth tools (Tree-Sitter, Slither, Frama-C).
Experimental results
Research questions
- RQ1RQ1: Can LLMs understand code syntax well?
- RQ2RQ2: Can LLMs understand code static behaviors?
- RQ3RQ3: Can LLMs understand code dynamic behaviors?
Key findings
- GPT-4 and GPT-3.5 show strong performance in code syntax understanding and can act as AST parsers.
- LLMs have partial ability for static analysis, capable of CFG and CG generation and data-flow like analyses at a beginner level.
- LLMs struggle with dynamic semantics tasks such as equivalent mutant detection and flaky test reasoning.
- Performance varies across projects (data-shift), with F1 ranging from 0 to 0.8 for data dependency and taint analysis.
- Open-source models (CodeLlama, StarCoder) underperform relative to OpenAI models on these tasks.
- LLMs can generate syntactically correct code but may hallucinate semantics; highlights need for output verification in SE contexts.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.