Day 04 of 30 · Compiler foundations
Represent syntax with trees
- Time
- About 150 minutes: 45 reading, 75 lab, 20 testing, 10 notes.
- Prerequisites
- Day 03: Turn characters into tokens
Toolchain: your host C++ compiler
Today’s lab is the toy expression frontend. Any working C++ compiler builds it; no LLVM libraries are needed yet. Setup guide
Learn
- Grammar, precedence, associativity and AST ownership.
Read Reading
- R1 · Learn LLVM 17, Second Edition
Chapter 2: syntax analysis · 25 min - R1 · Learn LLVM 17, Second Edition
Chapter 3: AST and parser discussion · 20 min
Reading budgets cover selected sections, not whole chapters. Link to the book; this site does not reproduce it.
Build Build or investigate
- Write a small grammar and C++ AST nodes for literals, names and binary expressions.
- Use explicit ownership such as
unique_ptr. - Hand-draw trees for 2 + 3 * 4 and (2 + 3) * 4, then add an AST printer.
Done when
An AST printer and a written grammar in the learning repository.
Recall question
Why are the trees for those two expressions different?
Optional
- Add a unary-minus AST node.
Optional work never counts toward completion.
Completion checks
Progress tracking needs JavaScript. The lesson above is complete without it.
Notes
Saved only in this browser. Keep it short enough to finish in ten minutes; none of the prompts are required. All notes, export and import