我有以下语法给了我三个班次减少冲突:
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/*}
时,一切正常。
答案 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上还可以找到更多有用的信息。