Turning bison is useless? he does not work

I declared such a priority for 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

My program calculates the amount and it is 80! I still don't know why priority doesn't work.

+7
c bison
source share
1 answer

It should work if you define expressions as follows:

 exp: exp '+' exp { .. } exp '-' exp { .. } exp '*' exp { .. } exp '/' exp { .. } | literal_exp { .. } | ID { .. } 

Priority only applies when operators are present as terminals in the rule.

See the documentation on How Priority Works :

each rule takes priority from the last mentioned terminal symbol in the components

Your exp rule has no terminals, so priority does not apply.

+8
source share

All Articles