-
-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Non-Associative associativity for Pratt Parsers. #600
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR, great work! Just a few comments.
match self { | ||
Self::Left(x) => *x as u32 * 3, | ||
Self::Right(x) => *x as u32 * 3, | ||
Self::Non(x) => *x as u32 * 3 - 1, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this to be valid, all powers should have 1 added to them such that *x as u32 * 3 - 1
can never result in underflow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this resolve the issue here?
Self::Left(x) => *x as u32 * 3 + 1,
Self::Right(x) => *x as u32 * 3 + 1,
Self::Non(x) => *x as u32 * 3 - 1,
assert!( | ||
parser.parse("§1+-~2!$*3").has_errors(), | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change looks a little suspicious. Does this addition change the behaviour of existing parsers?
In current Pratt parsers, only associativity type of
left
andright
are supported. However, according to the original designs of Pratt parsing, it should also support Non-Associative infix binary operators.For example, consider operators
==
as a non-associative operator. We should be able to identify it as non-associative and therefore rejecting1 == 2 == 3
as an expression with the numbers being atomic literals. Whilst the current design only allow==
to be either left-associative or right-associative.This PR added associativity
non
for infix binary operators, and changed the binding power calculation function slightly based on The Original Parsing Methodology and this article.