I\'ve been working on a C-like grammar for my personal amusement. However, I\'ve been running into shift/reduce conflicts, and I\'m quite sure they could be resolved.
<You need to add %left '('
to your precedence rules (or %nonassoc '('
might be better).
The way that precedence works to resolve shift/reduce conflicts in yacc/bison is that it compares the precedence of the rule to be reduced with the precedence of the token to be shifted. In your example, the conflict is between reducing expr: expr '+' expr
and shifting a '('
, so to resolve it, you need a precedence on '('
(and you want it to be higher than the rule, which comes from '+'
)
The %prec
directive just sets the precedence of a rule, overriding its default precedence which comes from the first token on its rhs. It doesn't affect the precedence of tokens that appear in the rule in any way.