Shift/reduce conflict with LALR grammar

101 Views Asked by At

I am writing a grammar where I'd like certain functions to be only at the top-level of the expression while arithmetic operations can be anywhere. E.g. 4 + (5 * 9) is correct, FUNC(2 + 4) * 8 - FUNC(4) is also correct, but FUNC(2 * FUNC(8)) is incorrect cause, nested FUNC is not top-level. Here's my grammar:

start : F

F : F + F
F : F - F
F : F * F
F : F / F
F : ( F )
F : FUNC( E )
F : E

E : E + E
E : E - E
E : E * E
E : E / E
E : ( E )
E : num

FUNC : MAX
FUNC : MIN

I am using YACC, and I'm getting shift/reduce conflict on above grammar. Seems like grammar has no ambiguities but still something is not right, since there are conflicts. I am wondering what would be the correct way to write such grammar?

0

There are 0 best solutions below