Topic 4 · Class activity 3 · Week 10 · Wednesday

break, and the continue You Are Not Implementing

A label stack, a depth counter, and a two-sentence argument for why no grammar could have caught it.

Format pairsTime 45 min

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

  1. Predict the target of each break below, then implement the label stack and check.
    while (a) {
        while (b) {
            break;        <- which loop does this leave?
        }
        break;            <- and this one?
    }
  2. Implement the semantic check. A break outside 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.
  3. Test the error with a top-level break; and confirm the message names the line.
  4. Argue from theory. Write two sentences explaining why no context-free grammar could have caught this, referring to what the production for break_stmt can and cannot see.
  5. 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.