Parsing & ASTs
Why parse first
- tokens are linear
- parser defines shape and meaning
- later phases rely on stable AST
Grammar contract
- precedence and associativity are encoded in recursive descent layers
- each layer consumes one precedence level
Left associativity pattern
- parse left branch
- while operator matches, parse right and fold
- produces
(a-b)-cbehavior fora-b-c
AST shape
- Number / Ident / Unary / Binary / Call / Grouping
- span metadata on nodes makes errors actionable
Error model
- expected token + actual token
- include location
- sync on
;,}, and EOF
Quality goals
- deterministic AST for the same source
- parser errors are clear and specific
- bad input should recover when possible
Quick checks
1 + 2 * 3is postfix-style precedencef(1, a + 2)keeps arg order1 +yields missing-expression diagnostic- bad statement with recovery still parses subsequent statements
1 / 1