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

Add support for left-recursive rules #262

Closed
fgasperij opened this issue Jul 5, 2021 · 2 comments · Fixed by #266
Closed

Add support for left-recursive rules #262

fgasperij opened this issue Jul 5, 2021 · 2 comments · Fixed by #266
Labels
feature Something not supported that perhaps should be

Comments

@fgasperij
Copy link
Contributor

Hey, I'd like to start working on adding support for left-recursive rules, which are of the form:

rule sum() -> Expression = sum() + number()

What do you guys think? Would you be interested in a PR?

The main motivation behind this is to parse the new syntax introduced by CPython 3.9/3.10. Guido has already described an approach that looks promising, following that would be my first go.

@kevinmehall
Copy link
Owner

Have you looked at rust-peg's existing support for operator precedence via precedence climbing? Or are you hoping to be able to translate the python grammar directly?

I've only skimmed the linked post, but it looks like a better explanation or simpler version of the approach used by OMeta. The drawback is that it's tied to packrat parsing / memoization, which rust-peg has some support for with #[cache], but I've not found it to be a performance win in practice, and is not often enabled. I tend to write peg grammars as a shorthand for how I'd hand-code a recursive descent parser, and avoid code that needs to repeatedly re-parse the same input.

But if you can make straightforward extension to #[cache] (#[cache_left_rec]?), to enable left recursion, I'd accept that PR. The code that implements #[cache] is pretty gross, though...

@zsol
Copy link
Contributor

zsol commented Jul 7, 2021

Or are you hoping to be able to translate the python grammar directly?

We're trying to keep as much of the original grammar intact as possible to reduce maintenance burden further down the line.

I think the extension of cache -> cache_left_rec would be the path of least resistance here for us. Thanks for this awesome crate btw!

@kevinmehall kevinmehall added the feature Something not supported that perhaps should be label Jul 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Something not supported that perhaps should be
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants