Operand parsing right to left.
See original GitHub issueHello, I have a problem with a language parsing.
I have the following grammar:
?start: expression
?expression: "(" expression ")"
| expression operator expression
| negation expression
| string
?operator: "AND" -> andt
| "OR" -> ort
negation: "NOT" -> nott
string: /[0-9a-zA-Z_]\w*/
%import common.WS
%ignore WS
And a possible string would be the following: test AND NOT test2
However, for the following case: test AND test2 OR test3 the parsing is equivalent to: test AND (test2 OR test3) however the desired behaviour is: (test AND test2) OR test3
The first case makes sense, as the first operator
token is the AND, so it grabs that as the “pivot” and parses the left and right expressions individually. Is there a way to make the parsing from right to left, so that in this case, it finds the OR operator first?
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
What are the ramifications of right-to-left and ... - Stack Overflow
The prefix unary operators have right-to-left associativity, which makes sense because the operators closest to the operand are evaluated first, ...
Read more >Operator-precedence parser - Wikipedia
In computer science, an operator precedence parser is a bottom-up parser that interprets an ... correct precedence even when parsed with a linear,...
Read more >3.6. Parsing Operands - Embecosm
The ParseOperand function makes use of the two previously defined functions in order to parse an operand of which type is originally unknown....
Read more >Parsing Expressions - Crafting Interpreters
Then we call comparison() again to parse the right-hand operand. We combine the operator and its two operands into a new Expr.Binary syntax...
Read more >Parsing left associative operators using RLMeta
Parsing left associative operators using RLMeta · What is operator associativity? · Right associative calculator · Left associative calculator ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
scratches head
In fact, your example was exactly the opposite of what I needed, so now that I know how recursion can be defined, I changed
| expression operator expression
with| (expression)? operator expression
Keep in mind that for 1 AND 2 AND 3, it has to first calculate 1 AND 2 and then the result AND 3.
Thanks for the help!