C++ AST & Parsing

7 min

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

  1. Enables optimization: Compilers analyze and transform the tree
  2. Powers tooling: IDEs use AST for refactoring and analysis
  3. Template processing: AST manipulation for template instantiation
  4. 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

If you found this explanation helpful, consider sharing it with others.

Mastodon