v4 Parsing: for loops and short-circuit logic
Lesson, slides, and applied problem sets.
View SlidesLesson
v4 Parsing: for loops and short-circuit logic
This module adds parsing support for:
forstatement clauses- loop control statements (
break,continue) &&/||precedence
New grammar
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)*
Important:
forclause pieces are optional except semicolons.postis parsed without a trailing semicolon.- clause parse errors should be pinned to header structure, not delayed into later phases.
AST shape and parser responsibilities
Use explicit nodes:
ForStmt { init, condition, post, body }BreakStmtContinueStmtBinaryExprwith logical operators
This separation allows the semantic stage to enforce:
- loop-legal
break/continue - scope behavior inside loop headers/body
- clean nesting
Clause parsing implementation notes
parseFor
Expected flow:
- consume
forand( - parse optional init (declaration or expression assignment) until
; - parse optional condition until second
; - parse optional post until
) - parse required
{block
Common pitfall: the post clause must not consume final ; since loop header already owns it.
Supporting parsers
parseVarDeclNoSemi: declaration parser without consuming;parseAssignOrExprNoSemi: assignment/expression parser without trailing statement terminator- both are scoped only to header slots
Logical precedence and associativity
Keep or and and explicit:
parseExprstarts atparseOrparseOrparses repeated||parseAndparses repeated&&
This yields expected associativity and precedence:
a || b && c->a || (b && c)a && b || c->(a && b) || c
Loop-control statement parsing
break and continue are dedicated statements, not expressions:
- require semicolon
- no condition/expression afterward
They should only be valid in statement position inside loop bodies.
Parser diagnostics and synchronization
To avoid avalanche failures:
- report missing expected punctuation (
;,),{) with context - avoid accepting impossible loop headers with permissive recovery
- synchronize at statement boundaries after a loop-header error
Stable diagnostics are more valuable than “best-effort parse forever.”
Practice checkpoints (almost solved)
1) Optional clauses
All should parse:
for (;;) {}for (let i = 0;;) {}for (; i < 10;) {}for (; ; i = i + 1) {}
2) Precedence check
Input: a || b && c || d
Expected shape: ((a || (b && c)) || d).
3) Assignment target validation
Input: for (1 = 2; ; ) {}
Expected: invalid assignment target during init parse.
4) For-post parsing
Input: for (let i = 0; i < 10; i = i + 1) {}
Expected:
- init: declaration
- condition: expression
- post: assignment expression, no extra semicolon consumed
5) Control statement tokens
break / continue should:
- be statement nodes
- consume required trailing semicolon
- fail if parsed with trailing expression
6) Scope and legality
for (let i = 0; ; ) { continue; break; }
Expected: both statements valid in loop body context.
Module Items
Program Parser v4