转变减少了规则中期行动野牛的冲突

时间:2016-11-26 06:57:41

标签: compiler-construction grammar bison bnf shift-reduce-conflict

我有以下语法给了我三个班次减少冲突:

boolexpression:         boolexpression OR boolterm 
                        | boolterm ;

boolterm:              boolterm AND boolfact
                        | boolfact;

boolfact:               "!" "(" boolexpression ")"
                        | "(" boolexpression ")"
                        | BOOLLITERAL
                        | expression boolop expression

boolop:                 "<"| ">"| BOOLOPLEQ /* <= */ | BOOLOPGEQ /* >= */ | BOOLOPEQ /* == */ | BOOLOPNEQ /* != */; 

expression:             sum ;

sum:                    sum "+" term         
                        | sum "-" term      
                        | term ;

term:                 term "*" factor
                        | term "/" factor 
                        | factor;

factor:                 ID        
                        | NUMBER       
                        | "(" {/* rules to generate IR/*} expression ")";

当我删除{/* rules to generate IR/*}时,一切正常。

1 个答案:

答案 0 :(得分:0)

此冲突是因为您在"("规则factor之后立即执行了中间规则操作。在这种情况下,您无法执行此操作,因为缩减boolfact --> "(" boolexpression ")"factor --> "(" {/* rules to generate IR/*} expression ")"都包含"("标记,因为它们的第一个标记已被识别。因此,当野牛识别"("时,它不知道要减少哪个规则。

一种解决方案是在{/* rules to generate IR/} expression "(" {/之后放置 rules to generate IR/*} expression ")"; 。在https://www.gnu.org/software/bison/manual/html_node/Mid_002dRule-Conflicts.html#Mid_002dRule-Conflicts上还可以找到更多有用的信息。