# Ch 4: Scanning

> Study guide to Crafting Interpreters chapter 4. You build the scanner for jlox: turning source text into a list of tokens, handling lexemes, literals…

Updated October 20, 2026.

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

## Key facts {#key-facts}

- Week 2 of 145 on the cracked.guide path, in [Crafting Interpreters](https://cracked.guide/crafting-interpreters).
- Time budget: about 6 hours.
- Part of the book: jlox (tree-walk interpreter, Java).
- Verified on cracked.guide when the official `chap04_scanning` tests pass on GitHub Actions.
- 4 challenges at the end of the chapter.
- Terms it teaches: [jlox](https://cracked.guide/glossary/jlox).

## Week 2 goals {#objectives}

By the end of week 2 you can:

- Implement a scanner that turns Lox source text into a list of tokens.
- Explain the difference between a lexeme and a token.
- Trace the start and current indices while scanning a multi-character lexeme.

## This week's steps {#steps}

- Read the chapter: Type the code yourself instead of copying; that is the whole point of the book.
- Write the code in your lox repo: Type it into jlox/ (Java) yourself as you read, and get it running.
- Do the challenges (1–4): Challenge 4 (C-style block comments) is the one to actually implement; consider supporting nesting as the challenge suggests.
- Push and let the tests verify it: Your repo runs the book's chap04_scanning tests on every push; passing marks this done.

## Why it matters {#why}

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 {#approach}

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

## Links {#links}

- [Read the chapter: Ch 4: Scanning](https://craftinginterpreters.com/scanning.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)
- [Lox implementations in other languages (official wiki)](https://github.com/munificent/craftinginterpreters/wiki/Lox-implementations)
- [Wikipedia: Lexical analysis](https://en.wikipedia.org/wiki/Lexical_analysis)

---

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