Writing a Compiler from Scratch

claude-opus-47/30/2026@demo4 turns
1
U
I want to understand how compilers work by building a simple one that compiles arithmetic expressions like 3 plus 4 times 5 to machine code. Where do I start?
2
A
Perfect project to learn compilers! Let's build this in stages: STAGE 1: LEXER (tokenizer). Converts text to tokens. Input: 3 plus 4 times 5 Output: NUMBER(3), PLUS, NUMBER(4), MULTIPLY, NUMBER(5) The lexer scans character by character, building tokens: digits combine into numbers, operators become tokens, whitespace is skipped. STAGE 2: PARSER. Converts tokens to AST (Abstract Syntax Tree). The key challenge: operator precedence. How do we know to multiply before adding? There are two main approaches: 1. PRATT PARSING: Each operator has a binding power (precedence). Higher precedence operators grab tighter. 2. Recursive descent: Build grammar rules that encode precedence. Want me to implement the parser and show how it handles precedence?
3
U
Yes! But I'm confused about how the parser handles operator precedence. How does it know to multiply before adding?
4
A
THIS IS THE BRILLIANT PART OF PARSING! There are two main approaches: 1. PRATT PARSING (my favorite for expressions): Each operator has a binding power (precedence). How it handles 3 plus 4 times 5: 1. Parse 3 (left side) 2. See plus (precedence 10) 3. Recursively parse right side with minPrecedence equals 10 4. In recursive call, parse 4, see times (precedence 20 greater than 10) 5. Parse 5 first - (4 times 5) completes 6. Then plus combines: 3 plus (4 times 5) Higher precedence operators grab tighter! The parse tree reflects this: Plus(3, Times(4, 5)). Want to see how we actually generate code from this tree?