2021-02-28

Parsing a function call (e.g. `exp '(' exp ')'`) in Bison: results in shift/reduce errors (precedence issue)

I'm trying to parse a function call (currently just one argument, but I'll allow for several when I get it working).

Suppose exp is defined as

%left '+'
%precedence CALL
exp:
    exp '+' exp { ... }
|   exp '(' exp ')' %prec CALL { ... }
|   LITERAL { ... }
;

This creates an ambiguity. If I use -Wcounterexamples then it says that exp '+' exp · '(' exp ')' could be parsed in 2 ways, either shifting or reducing at the '('.

calc.y: warning: shift/reduce conflict on token '(' [-Wcounterexamples]
  Example: exp '+' exp • '(' exp ')'
  Shift derivation
    exp
    ↳ exp '+' exp
              ↳ exp • '(' exp ')'
  Example: exp '+' exp • '(' exp ')'
  Reduce derivation
    exp
    ↳ exp             '(' exp ')'
      ↳ exp '+' exp •

It appears that it doesn't know that a call should have a precedence that is higher or lower than +. What can I do to make sure that it knows that x+y(z) should be equivalent to x+(y(z)) and not (x+y)(z)?

Bison: strange shift-reduce conflict seems related.



from Recent Questions - Stack Overflow https://ift.tt/2PanSEk
https://ift.tt/eA8V8J

No comments:

Post a Comment