Crafting Interpreters · Week 2

Ch 4: Scanning

Updated

About 6 hoursVerified by tests

Summary

Crafting Interpreters chapter 4, Scanning: You build the scanner for jlox: turning source text into a list of tokens, handling lexemes, literals, keywords and identifiers, line numbers for errors, and the first bits of error reporting. Budget about 6 hours; the book's chap04_scanning tests check your code.

Flashcards 11 cards for Anki and AnkiMobile · all decks

Anki deck.tsv

Step through at your own pace; the arrow keys work too.

Crafting Interpreters · Ch 4 Scanning: characters into tokens Watch start and current walk the source, the lexeme grow, and each token appear the moment its lexeme ends.

start 6current 8lexeme >=

Tokens

  1. IFif
  2. LEFT_PAREN(
  3. IDENTIFIERn
  4. GREATER_EQUAL>=

The next character is '=', so match() consumes it: one GREATER_EQUAL token, not two.

This week's steps

4 steps
  1. Read the chapter

    Type the code yourself instead of copying; that is the whole point of the book.

  2. Write the code in your lox repo

    Type it into jlox/ (Java) yourself as you read, and get it running.

  3. Do the challenges (1–4)

    Challenge 4 (C-style block comments) is the one to actually implement; consider supporting nesting as the challenge suggests.

  4. Push and let the tests verify it

    Your repo runs the book's chap04_scanning tests on every push; passing marks this done.

Sign in to track this: tick each step off and it counts toward your week.

Try it in the browser

Optional

jlox as it stands at the end of chapter 4: what this chapter adds works, and anything from a later chapter is the error your own jlox would give. Compare its output with yours. The full playground

Chapter 4: Scanning

Scanning only: Run prints every token as type, lexeme and literal.

Output
Run the code to see what it prints.

jlox from Crafting Interpreters by Robert Nystrom, ported to TypeScript (MIT).

Your notes

Only you

Sign in to keep private notes on this guide, in Markdown. They stay yours: never on your profile, and you can export them all.

Discussion

0 posts

Questions about this chapter, tips, and what tripped you up. Hints over answers: full solutions go in a spoiler.

No posts yet. Ask a question or share a tip.

Sign in to postAnyone can read. Posting needs an account with a confirmed email.

Reference

Why it matters

Every tool that reads text (config parsers, linters, syntax highlighters) starts with a scanner. It is the cleanest first step into the codebase.

How to approach it

Type the code yourself instead of copying; that is the whole point of the book. Run it on small inputs after each step. Set up the official test runner this week so every later chapter is checked automatically.

Challenges

4 challenges at the end of the chapter.

Challenge 4 (C-style block comments) is the one to actually implement; consider supporting nesting as the challenge suggests. Challenges 1 to 3 are discussion questions; think about them briefly. 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
  • Off-by-one errors in current/start indices when extracting lexemes.
  • Forgetting to increment the line counter inside multi-line strings.
  • Writing jlox in a language other than Java: totally fine, but you translate every snippet yourself and the Visitor-heavy code in later chapters is more work in some languages. Check the wiki of other implementations for your language.
  • Using a Java version too old for the code. Any modern JDK (17 or 21) works.

Spot a mistake? Report it

Back to week 2