如何消除 yacc 语法中的 shift-reduce 冲突?
How to remove shift-reduce conflict in yacc grammar?
我有以下语法:
Expression
: SimpleExpression {$$ = ;};
| SimpleExpression LTnum SimpleExpression
{ MkLeftC(, ); $$ = MkRightC(, ); }
| SimpleExpression LEnum SimpleExpression
{ MkLeftC(, ); $$ = MkRightC(, ); }
| SimpleExpression EQnum SimpleExpression
{ MkLeftC(, ); $$ = MkRightC(, ); }
| SimpleExpression NEnum SimpleExpression
{ MkLeftC(, ); $$ = MkRightC(, ); }
| SimpleExpression GEnum SimpleExpression
{ MkLeftC(, ); $$ = MkRightC(, ); }
| SimpleExpression GTnum SimpleExpression
{ MkLeftC(, ); $$ = MkRightC(, ); }
;
SimpleExpression
: PLUSnum Term op_terms
{ $$ = MakeTree(AddOp,,); }
| MINUSnum Term op_terms
{ $$ = MakeTree(SubOp,,); }
;
op_terms
: PLUSnum Term
{ $$ = MakeTree(AddOp,NullExp(),); }
| PLUSnum Term op_terms
{ $$ = MakeTree(AddOp,,); }
| MINUSnum Term
{ $$ = MakeTree(SubOp,NullExp(),); }
| MINUSnum Term op_terms
{ $$ = MakeTree(SubOp,,); }
| ORnum Term
{ $$ = MakeTree(OrOp,NullExp(),); }
| ORnum Term op_terms
{ $$ = MakeTree(OrOp,,); }
;
我在 y.output 文件中得到以下 shift-reduce 冲突:
51: shift/reduce conflict (shift 74, reduce 57) on GTnum
51: shift/reduce conflict (shift 75, reduce 57) on NEnum
51: shift/reduce conflict (shift 76, reduce 57) on EQnum
51: shift/reduce conflict (shift 77, reduce 57) on GEnum
51: shift/reduce conflict (shift 78, reduce 57) on LEnum
51: shift/reduce conflict (shift 79, reduce 57) on LTnum
state 51
Expression : SimpleExpression . (57)
Expression : SimpleExpression . LTnum SimpleExpression (58)
Expression : SimpleExpression . LEnum SimpleExpression (59)
Expression : SimpleExpression . EQnum SimpleExpression (60)
Expression : SimpleExpression . NEnum SimpleExpression (61)
Expression : SimpleExpression . GEnum SimpleExpression (62)
Expression : SimpleExpression . GTnum SimpleExpression (63)
我需要帮助来消除这些冲突。我在这里做错了什么?我试图设置优先规则,但它们在这里似乎不起作用。有什么想法吗?
奇怪的语法。它应该是这样的形式:
Expression
: SimpleExpression {$$ = ;};
| Expression LTnum SimpleExpression
| Expression LEnum SimpleExpression
| Expression EQnum SimpleExpression
| Expression NEnum SimpleExpression
| Expression GEnum SimpleExpression
| Expression GTnum SimpleExpression
;
即使用左递归。
奇树也。它应该是一般形式:
$$ = MkBinaryNode(,,);
我有以下语法:
Expression
: SimpleExpression {$$ = ;};
| SimpleExpression LTnum SimpleExpression
{ MkLeftC(, ); $$ = MkRightC(, ); }
| SimpleExpression LEnum SimpleExpression
{ MkLeftC(, ); $$ = MkRightC(, ); }
| SimpleExpression EQnum SimpleExpression
{ MkLeftC(, ); $$ = MkRightC(, ); }
| SimpleExpression NEnum SimpleExpression
{ MkLeftC(, ); $$ = MkRightC(, ); }
| SimpleExpression GEnum SimpleExpression
{ MkLeftC(, ); $$ = MkRightC(, ); }
| SimpleExpression GTnum SimpleExpression
{ MkLeftC(, ); $$ = MkRightC(, ); }
;
SimpleExpression
: PLUSnum Term op_terms
{ $$ = MakeTree(AddOp,,); }
| MINUSnum Term op_terms
{ $$ = MakeTree(SubOp,,); }
;
op_terms
: PLUSnum Term
{ $$ = MakeTree(AddOp,NullExp(),); }
| PLUSnum Term op_terms
{ $$ = MakeTree(AddOp,,); }
| MINUSnum Term
{ $$ = MakeTree(SubOp,NullExp(),); }
| MINUSnum Term op_terms
{ $$ = MakeTree(SubOp,,); }
| ORnum Term
{ $$ = MakeTree(OrOp,NullExp(),); }
| ORnum Term op_terms
{ $$ = MakeTree(OrOp,,); }
;
我在 y.output 文件中得到以下 shift-reduce 冲突:
51: shift/reduce conflict (shift 74, reduce 57) on GTnum
51: shift/reduce conflict (shift 75, reduce 57) on NEnum
51: shift/reduce conflict (shift 76, reduce 57) on EQnum
51: shift/reduce conflict (shift 77, reduce 57) on GEnum
51: shift/reduce conflict (shift 78, reduce 57) on LEnum
51: shift/reduce conflict (shift 79, reduce 57) on LTnum
state 51
Expression : SimpleExpression . (57)
Expression : SimpleExpression . LTnum SimpleExpression (58)
Expression : SimpleExpression . LEnum SimpleExpression (59)
Expression : SimpleExpression . EQnum SimpleExpression (60)
Expression : SimpleExpression . NEnum SimpleExpression (61)
Expression : SimpleExpression . GEnum SimpleExpression (62)
Expression : SimpleExpression . GTnum SimpleExpression (63)
我需要帮助来消除这些冲突。我在这里做错了什么?我试图设置优先规则,但它们在这里似乎不起作用。有什么想法吗?
奇怪的语法。它应该是这样的形式:
Expression
: SimpleExpression {$$ = ;};
| Expression LTnum SimpleExpression
| Expression LEnum SimpleExpression
| Expression EQnum SimpleExpression
| Expression NEnum SimpleExpression
| Expression GEnum SimpleExpression
| Expression GTnum SimpleExpression
;
即使用左递归。
奇树也。它应该是一般形式:
$$ = MkBinaryNode(,,);