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

Reading budgets cover selected sections, not whole chapters. Link to the book; this site does not reproduce it.

Build Build or investigate

  1. Implement the grammar from day 4.
  2. Parse addition, subtraction, multiplication and parentheses.
  3. Add a let name = expression; statement.
  4. 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.

Required to complete day 05

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