# Ch 17: Compiling Expressions

> Study guide to Crafting Interpreters chapter 17. A single-pass compiler that parses expressions with a Pratt parser (a table of prefix and infix parse…

Updated October 20, 2026.

## Summary {#summary}

Crafting Interpreters chapter 17, Compiling Expressions: 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. Budget about 7 hours; the book's `chap17_compiling` tests check your code.

## Key facts {#key-facts}

- Week 15 of 145 on the cracked.guide path, in [Crafting Interpreters](https://cracked.guide/crafting-interpreters).
- Time budget: about 7 hours.
- Part of the book: clox (bytecode VM, C).
- Verified on cracked.guide when the official `chap17_compiling` tests pass on GitHub Actions.
- 3 challenges at the end of the chapter.
- Terms it teaches: [clox](https://cracked.guide/glossary/clox), [Pratt parser](https://cracked.guide/glossary/pratt-parser).

## Week 15 goals {#objectives}

By the end of week 15 you can:

- Implement a Pratt parser with prefix and infix functions per token type.
- Trace parsePrecedence on a small expression through the rule table.
- Explain how a single-pass compiler emits bytecode while it parses.

## This week's steps {#steps}

- Read the chapter: This chapter is dense.
- Write the code in your lox repo: Type it into clox/ (C) yourself as you read, and get it running.
- Do the challenges (1–3): Challenge 1 (trace a tricky expression through the parse functions) is the most important; do it on paper.
- Push and let the tests verify it: Your repo runs the book's chap17_compiling tests on every push; passing marks this done.

## Why it matters {#why}

Pratt parsing is an elegant, compact way to handle operator precedence and is used in many real compilers and tools.

## How to approach it {#approach}

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 {#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.

## Common traps {#traps}

- Getting precedence levels off by one in the rule table.
- Forgetting that the compiler emits code as it parses, so order matters.

## Links {#links}

- [Read the chapter: Ch 17: Compiling Expressions](https://craftinginterpreters.com/compiling-expressions.html)
- [Official repo: book source, both interpreters, test suite](https://github.com/munificent/craftinginterpreters)
- [Running the official test suite against your own interpreter (repo README)](https://github.com/munificent/craftinginterpreters#testing-your-implementation)
- [Wikipedia: Pratt parsing](https://en.wikipedia.org/wiki/Operator-precedence_parser#Pratt_parsing)
- [Hacker News: Intuiting Pratt Parsing](https://news.ycombinator.com/item?id=47573450)

---

Source: https://cracked.guide/guide/ci-17
cracked.guide: A free, dated 145-week path to getting genuinely cracked at programming.
