Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

YunkaiZhang233
Copy link

In current Pratt parsers, only associativity type of left and right 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 rejecting 1 == 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.

Copy link
Owner

@zesterer zesterer left a 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,
Copy link
Owner

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.

Comment on lines +762 to +764
assert!(
parser.parse("§1+-~2!$*3").has_errors(),
);
Copy link
Owner

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants