Shift/reduce conflict with C-like grammar under Bison

后端 未结 1 1598
不知归路
不知归路 2020-12-21 05:16

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.

<
相关标签:
1条回答
  • 2020-12-21 05:48

    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.

    0 讨论(0)
提交回复
热议问题