I'm working on a Bison grammar for arithmetic expressions, and I encountered an interesting behavior when parsing expressions with the MINUS operator.
Here’s a simplified version of my grammar:
%left MINUS
%%
Exp : Exp MINUS Exp
| MINUS Exp
| ID
;
%%
When parsing a - b, how does Bison decide between Exp -> MINUS Exp and Exp -> Exp MINUS Exp?Is it a reduce/reduce conflict?And why he choose the latter one?
I have set MINUS as left-associative using %left MINUS, but I thought that only affects multiple Exp MINUS Exp cases like a - b - c.
The Bison's manual says Bison resolves a reduce/reduce conflict by choosing to use the rule that appears first in the grammar.But I have exchanged the sequence of these two rules and the results have no difference.
Questions: Why does Bison prioritize Exp → Exp MINUS Exp over Exp → MINUS Exp when parsing a - b? Even if I switch the order of the two rules, Bison still chooses Exp MINUS Exp. Why? Does %left MINUS play a role in this decision, and if so, how? I suspect this has to do with shift/reduce behavior, but I want to clarify why Exp MINUS Exp wins over MINUS Exp. Any detailed explanation, preferably from a state-machine perspective, would be greatly appreciated!
Thanks in advance!