The point
break is the first construct whose legality depends on context rather than on its own text. That makes it a small, concrete example of why phase 3 exists.
Do this
- Predict the target of each
breakbelow, then implement the label stack and check.while (a) { while (b) { break; <- which loop does this leave? } break; <- and this one? } - Implement the semantic check. A
breakoutside any loop must be an error with a line number. Use a depth counter incremented around loop bodies only — and explain in one sentence why the condition is excluded. - Test the error with a top-level
break;and confirm the message names the line. - Argue from theory. Write two sentences explaining why no context-free grammar could have caught this, referring to what the production for
break_stmtcan and cannot see. - Design labeled break. Java has
break outer;. What would that require of your label stack? Two sentences.
Deliverable
Your predictions; a working break with a working error message; your two-sentence theory answer; and your design for labeled break.