yacc中的算术表达式的Shift减少冲突 [英] Shift Reduce Conflict for arithmetic expressions in yacc

查看:63
本文介绍了yacc中的算术表达式的Shift减少冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管指定了运算符的优先级,但该语法给我带来了冲突.即使在《龙书》中,它也已经以这种方式(下面的前7行实现)解决了,但仍然会发生冲突!下面是在yacc中实现的代码

This grammar has given me conflict despite specifying precedence of operators. Even in the Dragon book it's been resolved in such a way(the way implemented as first 7 lines below) but it still gets conflict! Below is the code implemented in 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中的

erro是:

erro in parser.output is :

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

有关其他州的更多信息:

more info about other states :

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)

推荐答案

由于执行冲突解决的方式,您不能像刚才那样考虑运算符.因为您将要指定规则和令牌之间的优先级,所以您需要告诉规则之间的区别,不能以相同的方式对待它们.而且您不想将 exp:exp"+" exp 等同于 exp:exp"*" exp .

Because of the way the conflict resolution is performed, you cannot factor the operators as you just did. Because you are going to specify precedences between rules and tokens, you need to tell the difference between rules that must not be treated the same way. And you do not want to treat exp: exp "+" exp as equivalent to exp: exp "*" exp.

所以要保留四个规则,每个运算符一个.

So keep four rules, one for each operator.

如果您真的要考虑因素,可以为每个优先级定义一个规则,但这将变得更加复杂,因为没有真正的增值恕我直言.

If you really want to factor something you could define one rule per precedence level, but that's going to be more complex for no real added value IMHO.

适当的工具应该告诉您,优先级指令(%right 等)在这里没有用.这表明冲突解决方案无法使用它们(因为您编写语法的方式).我冒险野牛会警告.

A proper tool should tell you that your precedence directives (%right, etc.) are useless here. That's an hint the conflict resolution cannot use them (because of the way you wrote the grammar). I venture Bison would warn.

您还应该在那看看:

这篇关于yacc中的算术表达式的Shift减少冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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