3 ms·
IMO the "not equals" is easy to misinterpret as permitting the tree. I think something like this would clearer: Expr = 'number' | '(' Expr '
by armchairhacker 2y ago
IMO the "not equals" is easy to misinterpret as permitting the tree.
I think something like this would clearer:
Expr =
'number'
| '(' Expr ')'
| Expr '+' Expr (bind-order: 2, assoc: left)
| Expr '-' Expr (bind-order: 2, assoc: left)
| Expr '*' Expr (bind-order: 1, assoc: left)
| Expr '/' Expr (bind-order: 1, assoc: left)
or if you want relative precedences:
Expr =
'number'
| '(' Expr ')'
| Expr '+' Expr (add, assoc: left)
| Expr '-' Expr (sub, assoc: left)
| Expr '*' Expr (mul, assoc: left)
| Expr '/' Expr (div, assoc: left)
bind-order:
mul equals div
add equals sub
mul before add
or if you want to completely separate conflict resolution (and shorter bind-order syntax, and raise a syntax error if certain operators are mixed...this one I like the most):
Expr =
'number'
| '(' Expr ')'
| Expr '+' Expr (add)
| Expr '-' Expr (sub)
| Expr '*' Expr (mul)
| Expr '/' Expr (div)
| Expr '&&' Expr (land)
| Expr '||' Expr (lor)
| Expr '==' Expr (eq)
| Expr '!=' Expr (ne)
| Expr '.' Expr (haskellDot)
| Expr '..=' Expr (range)
assoc:
left: add, sub, mul, div, land, lor, eq, ne
right: haskellDot
forbid: range
bind-order:
mul | div
add | sub
range
eq | ne
land | lor
haskellDot
forbid:
eq | ne
land | lor