Topic 5 · Class activity 1 · Week 12 · Wednesday

Create the Dangling-Else Conflict, Then Fix It Three Ways

Build the conflict on purpose, read the items in parser.output, fix it with precedence and again with a matched/unmatched grammar, then swap the declarations and watch it break.

Format pairs, at a laptopTime 45 min

The point

Create the dangling-else conflict deliberately, read the parser's own report on it, then fix it three different ways and watch one of them break.

Do this

  1. Add the two if rules with no precedence declarations. Build with bison -d -v parser.y and read the warning.
  2. Open parser.output and find the conflicted state. It lists the items — productions with a dot — exactly as in the lecture notes. Copy it into your notes and identify which item wanted to shift and which wanted to reduce, and on what lookahead.
  3. Test which way bison resolved it. Compile and run
    int main() {
        int x; int y;
        x = 1; y = 0;
        if (x > 0) if (y > 0) print(1); else print(2);
        return 0;
    }
    It prints 2 if the else bound to the inner if, and nothing at all if it bound to the outer one. Which happened?
  4. Fix it properly with the two %nonassoc declarations. Rebuild — the warning should be gone and the behavior unchanged.
  5. Swap the two declarations. Predict what the test prints now, then run it. Explain the result in terms of positional precedence.
  6. Fix it a third way, on paper. Write the matched/unmatched grammar from Dragon §4.3.2 for our language. Count how many rules mentioning stmt you had to touch, and say whether you would ship it.

Deliverable

The parser.output excerpt with the two competing items identified; what the test printed at each stage; your matched/unmatched grammar with a rule count; and a one-paragraph judgement. This is Lab Question 25.