You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
when using left recursive grammars with operator precedence, I am unable to split my left recursive rule into multiple ones while maintaining the operator precedence.
An input string of !a && a results in a parse tree is the equivalent to !(a && a), ignoring the precedence. I would have expected a result such as (!a) && (a) instead.
The reason being, that operator precedence is only used with ordered alts and reset to 0 for references.
This is a snippet from the generated (Java) parser of this grammar:
The reason for hiding productions behind another is that we/I derive grammars from other grammars, resulting in ... larger grammars.
Due reusing productions (such as expressions) and the need to parse productions stand alone (e.g., a not expression), the generated java parsers are reaching the limits allowed in Java.
I spend some time to think about a possible solution in ANTLR:
My suggesting is to introduce a "delegated/indirect operator precedence" parameter to some methods,
which passes the operator precedence through (such as for the not expression in the example above).
An input string of !a && a results in a parse tree is the equivalent to !(a && a), ignoring the precedence. I would have expected a result such as (!a) && (a) instead.
From my perspective it looks correct because notexpr has higher priority. You can try to use the following grammar:
Hey there,
when using left recursive grammars with operator precedence, I am unable to split my left recursive rule into multiple ones while maintaining the operator precedence.
For example:
An input string of
!a && a
results in a parse tree is the equivalent to!(a && a)
, ignoring the precedence. I would have expected a result such as(!a) && (a)
instead.The reason being, that operator precedence is only used with ordered alts and reset to 0 for references.
This is a snippet from the generated (Java) parser of this grammar:
And here, the same part for a grammar where notexpressions are inlined/not hidden behind another production.
The reason for hiding productions behind another is that we/I derive grammars from other grammars, resulting in ... larger grammars.
Due reusing productions (such as expressions) and the need to parse productions stand alone (e.g., a not expression), the generated java parsers are reaching the limits allowed in Java.
I spend some time to think about a possible solution in ANTLR:
My suggesting is to introduce a "delegated/indirect operator precedence" parameter to some methods,
which passes the operator precedence through (such as for the not expression in the example above).
I've only implemented it for Java right now to receive some feedback:
dev...MisterErwin:antlr4:ind-operator-prec
(It contains three smaller tests, but I also ran some larger tests with the root cause of this issue 😄 )
In case the change seems okay, I would go ahead and prepare a PR for all languages (and not just a prototype in Java).
Thanks
Alex
P.S.: @ kaby76, thanks for helping me with this over on stackoverflow - sorry for not being able to upvote the answers.
The text was updated successfully, but these errors were encountered: