C++ AST & Parsing
Explore how C++ code is parsed into an Abstract Syntax Tree with interactive visualizations.
Best viewed on desktop for optimal interactive experience
Abstract Syntax Tree (AST)
The AST is a tree representation of your code's syntactic structure. Each node represents a construct in the source code, from functions to expressions.
Abstract Syntax Tree Explorer
AST Structure
TranslationUnit
FunctionDecl: fibonacci
BuiltinType: int
ParmVarDecl: n(int)
CompoundStmt
IfStmt
ReturnStmt
Source Code
int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
🎯 Tip: Click on any node to see its source location highlighted. The AST represents the hierarchical structure of your code, with each node containing type information and relationships.
Parsing Phases
1. Lexical Analysis (Tokenization)
Breaks code into tokens:
- Keywords:
int
,if
,return
- Identifiers: variable and function names
- Literals:
42
,"string"
,3.14
- Operators:
+
,->
,::
2. Syntax Analysis
Builds the AST according to grammar rules:
function_declaration ├── return_type ├── function_name ├── parameter_list └── compound_statement └── statements...
3. Semantic Analysis
- Type checking
- Name resolution
- Template instantiation
- Overload resolution
Why AST Matters
- Enables optimization: Compilers analyze and transform the tree
- Powers tooling: IDEs use AST for refactoring and analysis
- Template processing: AST manipulation for template instantiation
- Error detection: Semantic errors found through tree analysis
Viewing the AST
# Clang AST dump clang++ -Xclang -ast-dump main.cpp # GCC tree dump g++ -fdump-tree-original main.cpp
Common AST Nodes
- FunctionDecl: Function declarations
- CompoundStmt: Block statements
{...}
- IfStmt: Conditional statements
- CallExpr: Function calls
- BinaryOperator: Binary operations (+, -, *, /)
- DeclRefExpr: Variable references
Next Steps
- Learn about Compiler Optimization
- Understand Symbol Tables
- Explore the Preprocessor