野牛-运算符优先级 [英] Bison - operator precedence
问题描述
我对Bison中的运算符优先级和关联性有疑问.
I have a question about operator precedence and associativity in Bison.
在每个示例中,我看到的作品都类似于expr 'op' expr
,例如:http://dinosaur.compilertools.net/bison/bison_8.html
In every example I see the productions are like expr 'op' expr
, for example :http://dinosaur.compilertools.net/bison/bison_8.html
但是,如果我要使用野牛%left
和其他关联性工具,那么我将使用如下语法:
But if I would use bison %left
and others associativity tools, and I would use grammar like:
expr| expr binop expr
| expr relop expr
| expr logical_op expr
和
binop: '+'
| '-'
| '*'
| '/'
;
relop: EE
| NE
| LE
| '<'
| GE
| '>'
;
logical_op: AND
| OR
;
会使用关联性和优先级规则吗?
would associativity and precedence rules be used?
还是我需要为每个运算符写显式expr 'op' expr
?
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.
但是当我手工编写类似expr '+' expr
的作品时,我没有得到任何警告.
But when by hand I write productions like expr '+' expr
I am not getting any warnings.
推荐答案
要使优先级规则起作用,终端本身必须出现在模棱两可的产品中.因此,您不能将终端分组为非终端,也不能保留使用优先级规则的能力.
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.
这篇关于野牛-运算符优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!