[英]bison precedence is useless? it doesn't work
I have declared such precedence for bison
: 我已经宣布了bison
优先权:
%left '+' '-'
%left '*' '/'
Recursive rules for arithmetic: 算术的递归规则:
exp: exp binary_op exp { .. }
| literal_exp { .. }
| ID { .. }
binary_op: '+' { .. }
| '-' { .. }
| '*' { .. }
| '/' { .. }
I have an arithmetic expression: 10 * 3 + 5
我有一个算术表达式: 10 * 3 + 5
My program calculates the sum, and it's 80! 我的程序计算总和,它是80! I still don't know why precedence doesn't work. 我仍然不知道为什么优先权不起作用。
It should work if you define the expressions like this: 如果你定义这样的表达式它应该工作:
exp: exp '+' exp { .. }
exp '-' exp { .. }
exp '*' exp { .. }
exp '/' exp { .. }
| literal_exp { .. }
| ID { .. }
The precedence only works when the operators are present as terminals in the rule. 优先级仅在运算符作为规则中的终端出现时才有效。
See the documentation on How precedence works : 请参阅有关优先级如何工作的文档:
each rule gets its precedence from the last terminal symbol mentioned in the components 每个规则的优先级都来自组件中提到的最后一个终端符号
Your rule for exp has no terminals, hence no precedence is applied. 您的exp规则没有终端,因此不会应用优先级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.