Week 9 of 125
Crafting Interpreters, chapters 16 and 17
Sign up or sign in to tick these off and give this week a due date.
-
A new scanner for clox that produces tokens on demand instead of all at once, pointing into the source string rather than copying lexemes, with a hand-written trie-like switch for keywords.
Time About 4 hours this week
Why it matters
Zero-copy scanning is how fast real-world parsers work. The keyword switch is a neat trick worth stealing.
How to approach it
Compare each function with your jlox scanner. Note how lexemes are pointers plus lengths, not strings.
Challenges
3 challenges at the end of the chapter.
Challenge 1 (string interpolation) is a great design problem; think through how the scanner would need to cooperate with the parser. Challenges 2 and 3 are design discussions.
Traps
- Token lexemes are not null-terminated. Never pass them to printf with %s; use %.*s.
- The source buffer must outlive all tokens.
-
A single-pass compiler that parses expressions with a Pratt parser (a table of prefix and infix parse functions with precedence) and emits bytecode directly, with no AST.
Time About 7 hours this week
Why it matters
Pratt parsing is an elegant, compact way to handle operator precedence and is used in many real compilers and tools.
How to approach it
This chapter is dense. Read the Pratt parser part twice. Trace the parse of a small expression through parsePrecedence and the rule table by hand, which is exactly what challenge 1 asks.
Challenges
3 challenges at the end of the chapter.
Challenge 1 (trace a tricky expression through the parse functions) is the most important; do it on paper. Challenge 3 (add the ternary operator) is a good stretch. 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 precedence levels off by one in the rule table.
- Forgetting that the compiler emits code as it parses, so order matters.
-
NeetCode LeetCode: 2 NeetCode 150 problems
Two problems from the NeetCode 150, in order. One box for the pair.