v4 Parsing: for loops and short-circuit logic

Lesson, slides, and applied problem sets.

View Slides

Lesson

v4 Parsing: for loops and short-circuit logic

New grammar pieces

stmt        -> forStmt | breakStmt | continueStmt | ...

forStmt     -> "for" "(" forInit? ";" expr? ";" forPost? ")" block
forInit     -> varDeclNoSemi | assignOrExprNoSemi
forPost     -> assignOrExprNoSemi

breakStmt   -> "break" ";"
continueStmt-> "continue" ";"

expr        -> or
or          -> and ("||" and)*
and         -> equality ("&&" equality)*

Parsing tips

  • for uses semicolons inside the parentheses. Create a helper that parses a statement without consuming a trailing semicolon.
  • Add or() and and() parse functions to keep precedence clean.
  • break / continue are simple statements with a semicolon.

Notes

  • Keep the parser readable. Small functions, simple control flow.

Module Items