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?