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
- Add the two if rules with no precedence declarations. Build with
bison -d -v parser.yand read the warning. - Open
parser.outputand 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. - Test which way bison resolved it. Compile and run
It prints 2 if the else bound to the inner if, and nothing at all if it bound to the outer one. Which happened?int main() { int x; int y; x = 1; y = 0; if (x > 0) if (y > 0) print(1); else print(2); return 0; } - Fix it properly with the two
%nonassocdeclarations. Rebuild — the warning should be gone and the behavior unchanged. - Swap the two declarations. Predict what the test prints now, then run it. Explain the result in terms of positional precedence.
- 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
stmtyou 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.