【发布时间】:2017-11-22 11:27:35
【问题描述】:
尽管指定了运算符的优先级,但这个语法给了我冲突。即使在 Dragon book 中,它也以这种方式解决(实现方式为下面的前 7 行),但它仍然会发生冲突! 下面是yacc中实现的代码
%right THEN_KW
%right ELSE_KW
%left XOR_KW OR_KW
%right '='
%left AND_KW ALSO_KW
%left EQ_KW LT_KW GT_KW LE_KW GE_KW
%left PLUS_KW MINUS_KW
%left MULT_KW DIV_KW MOD_KW
%right NOT_KW
arthlogicexpr -> operand | arthlogicexpr arthop arthlogicexpr
arthop -> '+' | '-' | '*' | '/' |'%'
operand -> variable
variable -> IDENTIFIER
parser.output 中的错误是:
state 141
78 arthlogicexpr: arthlogicexpr . arthop arthlogicexpr
78 | arthlogicexpr arthop arthlogicexpr .
'+' shift, and go to state 103
'-' shift, and go to state 104
'*' shift, and go to state 105
'/' shift, and go to state 106
'%' shift, and go to state 107
'+' [reduce using rule 78 (arthlogicexpr)]
'-' [reduce using rule 78 (arthlogicexpr)]
'*' [reduce using rule 78 (arthlogicexpr)]
'/' [reduce using rule 78 (arthlogicexpr)]
'%' [reduce using rule 78 (arthlogicexpr)]
$default reduce using rule 78 (arthlogicexpr)
arthop go to state 109
关于其他州的更多信息:
state 103
79 arthop: '+' .
$default reduce using rule 79 (arthop)
state 104
80 arthop: '-' .
$default reduce using rule 80 (arthop)
state 105
81 arthop: '*' .
$default reduce using rule 81 (arthop)
state 106
82 arthop: '/' .
$default reduce using rule 82 (arthop)
state 107
83 arthop: '%' .
$default reduce using rule 83 (arthop)
【问题讨论】:
标签: compiler-construction bison yacc lex