问题
to solve the dangling else problem, I used the following solution:
stmt : stmt_matched
| stmt_unmatched
;
stmt_unmatched : IF '(' exp ')' stmt
| IF '(' exp ')' stmt_matched ELSE stmt_unmatched
;
stmt_matched : IF '(' exp ')' stmt_matched ELSE stmt_matched
| stmt_for
| ...
;
For defining the rules of grammar about the for loop, I produce a conflict shift/reduce due to the same problem:
stmt_for : FOR '(' exp ';' exp ';' exp ')' stmt
;
How can I solve this problem?
回答1:
Not all for
statements are matched. Consider, for example
if (c) for (;;) if (d) ; else ;
So it is necessary to divide for
statements into for_matched
and for_unmatched
. (And similarly with other compound statements such as while
.)
来源:https://stackoverflow.com/questions/41088400/bison-loop-for-conflict