Bison loop for conflict

自闭症网瘾萝莉.ら 提交于 2019-12-02 18:18:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!