Bison loop for conflict

2019-03-06 08:12发布

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条回答
ゆ 、 Hurt°
2楼-- · 2019-03-06 08:42

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.)

查看更多
登录 后发表回答