That grammar is ambiguous, although it's heading in the right direction :)
Here's an ambiguity:
IF c1 THEN IF c2 THEN s2 ELSE IF c3 THEN s3 ELSE s4
Since IF c2 THEN s2 ELSE IF c3 THEN s3
can be reduced to:
IF c2 THEN matchedStmt ELSE stmt
which is a matchedStmt
. So it's ambiguous whether ELSE s4
belongs to IF c3
or IF c1
.
What you need is for matchedStmt
to be completely matched on both sides of the ELSE
, something like this:
stmt -> matchedStmt | unmatchedStmt
matchedStmt -> IF expr THEN matchedStmt ELSE matchedStmt
| other
unmatchedStmt -> IF expr THEN stmt
| IF expr THEN matchedStmt ELSE unmatchedStmt