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)-c behavior for a-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 * 3 is postfix-style precedence
  • f(1, a + 2) keeps arg order
  • 1 + yields missing-expression diagnostic
  • bad statement with recovery still parses subsequent statements
1 / 1
Use arrow keys or click edges to navigate. Press H to toggle help, F for fullscreen.