If I understand you correctly, you did the following:
%left '+' '-'
%right '?'
%%
arithmetic_expression : expression + expression
| expression - expression
;
ternary : expression ? expression : expression
;
That won't work, because you haven't told bison what the precedence of the ternary
production is.
To resolve shift-reduce conflicts, bison compares the precedence of the production which might be reduced with the precedence of the terminal which might be shifted. You've declared the precedence of the terminals '+', '-' and '?', and the precedence of both productions in arithmetic_expression
, but not the precedence of the production in ternary
.
Quoting from the bison manual, emphasis added:
each rule gets its precedence from the last terminal symbol mentioned in the components.
That's very subtle and you won't be the first person who didn't notice.
Solutions: either declare the precedence of the terminal :
or add %prec '?'
to the production in ternary
.