v4 Parsing: for loops and short-circuit logic
Lesson, slides, and applied problem sets.
View SlidesLesson
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
foruses semicolons inside the parentheses. Create a helper that parses a statement without consuming a trailing semicolon.- Add
or()andand()parse functions to keep precedence clean. break/continueare simple statements with a semicolon.
Notes
- Keep the parser readable. Small functions, simple control flow.