question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Operand parsing right to left.

See original GitHub issue

Hello, 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:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
erezshcommented, May 13, 2020

scratches head

0reactions
PabloMK7commented, May 13, 2020

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found