I have the following ambiguous operator grammar:
E->E+E*E | E-E*E
E->E+E | E-E | E+E | E*E | E/E
E->(E) | x
I must convert it to an unambiguous one which is also SLR(1). The following rules regarding precedence and associativity must hold:
- Operators * and / have the highest precedence (and precedence between them must be equal)
- Operators - and + have the same precedence (lower than * and /)
- All operators are left associative.
- Reducing with the first 2 rules is preferred if it does not break the precedence and associativity rules.
My problem is the first line of the grammar (E+E*E , E-E*E)
. I can't find i way to convert it and the new grammar will be SLR(1).
Edit: The closest i came is this grammar, but it has 2 SLR conflicts:
E->E+F | E-F | T
T->T+F | T-F | F
F-> F*C | F/C | C
C-> (E) | x