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

Memoize / packrat support for reusing parse results when multiple alt branches use the same parser #480

Open
epage opened this issue Feb 19, 2024 · 2 comments
Labels
A-combinator Area: combinators C-enhancement Category: Raise on the bar on expectations E-help-wanted Call for participation: Help is requested to fix this issue.

Comments

@epage
Copy link
Collaborator

epage commented Feb 19, 2024

The following is a left-recursive grammar, where the parser tries to parse an and_expression (details not important) and some other expressions that could come after it (link goes to exact line 28):

https://gitlab.com/seloxidate/selector/-/blob/33febe8d2de29199b09a271fabac2097d1c1f161/src/parsers/conditional_expression.rs#L28

BUT, if it fails to parse "some other expressions that come after it", it must parse the and-expression again in the other alt branch (line 39):

https://gitlab.com/seloxidate/selector

One can see that this could easily lead to exponential parsing time, so here, using memorized on and_expression can be very helpful and would prevent exponential runtime (with additional memory needed - a lot more memory).

Hope this clarifies it a bit and where one would use memorized.

Also, for inspiration, you might want to look into nom-packrat. It's an extension to nom that allows packrat parsing

https://github.com/dalance/nom-packrat

From https://hachyderm.io/@[email protected]/111960492116311141

@epage epage added A-combinator Area: combinators E-help-wanted Call for participation: Help is requested to fix this issue. C-enhancement Category: Raise on the bar on expectations labels Feb 19, 2024
@epage
Copy link
Collaborator Author

epage commented Feb 19, 2024

@epage
Copy link
Collaborator Author

epage commented Feb 19, 2024

Need to look at how chumsky and nom-packrat deal with storage.

The question I have is whether the storage lives directly in the Memoized (requires multiple accesses of the same instance) instance or externally, like an Arc<Mutex<_>> (requires tracing back to the same instantiation) or TLS (always used but requires explicit initialization).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-combinator Area: combinators C-enhancement Category: Raise on the bar on expectations E-help-wanted Call for participation: Help is requested to fix this issue.
Projects
None yet
Development

No branches or pull requests

1 participant