问题
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 %left
and 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