I’m writing a simple grammar that always resolve to a single line binary expression.
E.g.: variable1 < variable2 - variable3
Here is my .grammar
:
@top Program { expression* }
expression { (expression | ExpressionComponent) Operator ExpressionComponent }
ExpressionComponent { UnaryOperator* Variable }
@tokens {
Variable { $[a-z]+ $[a-zA-Z_0-9]* }
Operator { ("|" | "&" | "=" | "!=" | ">" | ">=" | "<" | "<=" | "-" | "+" | "/" | "*") }
UnaryOperator { ("-" | "+") }
space { $[ ]+ }
}
@skip { space }
That works well except for unary operators. The generator complains that overlapping tokens appear in the same context. I understand why, but not how to resolve it.
There can be any number of unary operators modifying the sign of the variable. E.g.: variable1 < --variable2 - -+-variable3
It’s simple to read (the first symbol between two variables is a binary operator, the (optional) following symbols are unary operators), but I’m not sure how to write it. Also, there are no increment (++
) or decrement (--
) operators nor parenthesis.
I tried modifying precedence without success. What am I missing?
Thanks!