Crafting Interpreters · Week 3

Crafting Interpreters 6: Parsing Expressions: reading guide

Read the chapterVerified by tests

A recursive descent parser for Lox expressions, with precedence and associativity encoded in the grammar, plus syntax error reporting and panic-mode recovery by synchronizing at statement boundaries.

Time About 6 hours this week

Why it matters

Recursive descent is how most production compilers (GCC, Clang, V8, Roslyn) parse. Once you can write one, you can parse almost anything.

How to approach it

Map each grammar rule to its parsing method and check them side by side. Trace the parse of an expression like 1 - 2 * 3 == 4 on paper through the call stack.

Challenges

3 challenges at the end of the chapter.

Challenges 1 (comma operator) and 2 (the ternary ?: operator) are great practice at adding grammar rules with the right precedence. Challenge 3 (error productions for binary operators missing a left operand) teaches good error messages. Do challenges that change Lox's behavior on a separate git branch. The cracked.guide check runs the official test suite for this chapter, and a challenge that changes semantics (new syntax, different errors) can make tests fail on main.

Traps

  • Getting associativity wrong by using recursion where the book uses a loop.
  • Error recovery that never resynchronizes, causing a cascade of bogus errors.

Verified by the book's chap06_parsing tests when you push your repo.

Links

Back to week 3