Bison Shift/reduce error for C-like language

前端 未结 1 1664
灰色年华
灰色年华 2020-12-21 13:23

I\'m having an issue with my bison grammar, it is giving me shift/reduce errors, and I have already defined precedence for the operators.

I know that it is cause by

相关标签:
1条回答
  • 2020-12-21 14:10

    Precedence only works if the productions directly include terminals named in the precedence declaration.

    In other words, you cannot use expr: expr binop expr because the precedence relations can't help bison decide whether to shift the reduced binop non-terminal or reduce the expr on the top of the parse stack. Bison would have to know what the terminal inside the binop was, but it doesn't know that any more. (That's why reductions are called "reductions".)

    To make it work, you have to write the productions out in full:

    expr: expr '+' expr
        | expr '-' expr
        | expr '*' expr
        |...
    
    0 讨论(0)
提交回复
热议问题