Day 05 of 30 · Compiler foundations
Write a recursive descent parser
- Time
- About 150 minutes: 45 reading, 75 lab, 20 testing, 10 notes.
- Prerequisites
- Day 04: Represent syntax with trees
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
- Parsing expressions, consuming tokens and reporting syntax errors.
Read Reading
- R1 · Learn LLVM 17, Second Edition
Chapter 3: recursive descent parser · 35 min - R3 · Crafting Interpreters
Expression parsing, if precedence is unclear · 10 min
Reading budgets cover selected sections, not whole chapters. Link to the book; this site does not reproduce it.
Build Build or investigate
- Implement the grammar from day 4.
- Parse addition, subtraction, multiplication and parentheses.
- Add a
let name = expression;statement. - Fail with a useful location for a missing closing parenthesis or missing operand.
Done when
At least eight cases covering precedence, associativity, nested parentheses and syntax errors.
Recall question
How does your parser prevent an infinite loop on invalid input?
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