I'm building a simple language parser, and having an issue with lower precedence prefix expressions. Here's an example grammar:
E = E5
E5 = E4 'OR' E4 | E4
E4 = E3 'AND' E3 | E3
E3 = 'NOT' E3 | E2
E2 = E1 '==' E1 | E1
E1 = '(' E ')' | 'true' | 'false'
However, this grammar doesn't work correctly for the NOT
, if it's used as the RHS of a higher precedence infix operator, i.e.:
true == NOT false
This is due to the ==
operator requiring E1 on the RHS, which cannot be a NOT operation.
I'm unsure the correct way to express this grammar? Is it still possible using this simplistic recursive descent approach, or will I need to move to a more featured algorithm (shunting yard or precedence climbing).
Assuming the following input and expected parses are correct:
true == NOT false
(true == (NOT false))
NOT true == false
(NOT (true == false))
NOT true == NOT false
(NOT (true == (NOT false)))
Here's an (ANTLR4) grammar that does the trick:
Parses ANTLR created:
1
2
3
Your language is also (unnecessarily) ambiguous. Fixing that helps you fix this problem, too.
Here,
D
is shorthand for "disjunction",C
for conjunction,N
for negation, andP
for primary,E
for equality.Maybe use polish notation?