Bison - operator precedence

我怕爱的太早我们不能终老 提交于 2019-12-17 16:40:06

问题


I have a question about operator precedence and associativity in Bison.

In every example I see the productions are like expr 'op' expr, for example :http://dinosaur.compilertools.net/bison/bison_8.html

But if I would use bison %leftand others associativity tools, and I would use grammar like:

  expr|     expr binop expr
      |     expr relop expr
      |     expr logical_op expr

and

 binop: '+' 
      | '-' 
      | '*' 
      | '/' 
      ;
 relop: EE
      | NE
      | LE
      | '<'
      | GE
      | '>'
      ;
 logical_op: AND
           | OR
           ;

would associativity and precedence rules be used?

Or do I need to write explicite expr 'op' expr for every operator?

I am asking, because when I try to use the grammar like the one I posted I get warnings about conflicts.

But when by hand I write productions like expr '+' expr I am not getting any warnings.


回答1:


For precedence rules to work, the terminal itself must appear in the ambiguous production. So you cannot group terminals into non-terminals and retain the ability to use precedence rules.




回答2:


I prefer to add grammar rules (productions) to account for operator precedence. See my answer here.



来源:https://stackoverflow.com/questions/13553837/bison-operator-precedence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!