Added Non-Associative associativity for Pratt Parsers.#600
Added Non-Associative associativity for Pratt Parsers.#600YunkaiZhang233 wants to merge 3 commits into
Conversation
zesterer
left a comment
There was a problem hiding this comment.
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.
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.
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.
This change looks a little suspicious. Does this addition change the behaviour of existing parsers?
|
Any progress? |
Another PR has been opened, but discussion is stale (and probably needs rebasing on upstream). If you're interested in picking it up again, I'd really appreciate it! |
38bb88c to
02a1373
Compare
0f2b61a to
e350fc6
Compare
In current Pratt parsers, only associativity type of
leftandrightare 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 == 3as 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
nonfor infix binary operators, and changed the binding power calculation function slightly based on The Original Parsing Methodology and this article.