冲突的野牛圈 [英] Bison loop for conflict

查看:142
本文介绍了冲突的野牛圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了解决悬空的其他问题,我使用了以下解决方案:

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循环的语法规则,我产生了一个冲突转移/减少同样的问题:

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
            ;

如何解决这个问题?

推荐答案

语句的所有都不匹配。例如,考虑

Not all for statements are matched. Consider, for example

 if (c) for (;;) if (d) ; else ;

所以有必要将除以语句进入 for_matched for_unmatched 。 (与其他复合语句类似,例如,而。)

So it is necessary to divide for statements into for_matched and for_unmatched. (And similarly with other compound statements such as while.)

这篇关于冲突的野牛圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆