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

Implement Pratt parsing or precedence climbing #358

Closed
stefnotch opened this issue Aug 22, 2023 · 4 comments
Closed

Implement Pratt parsing or precedence climbing #358

stefnotch opened this issue Aug 22, 2023 · 4 comments

Comments

@stefnotch
Copy link

I believe that implementing pratt parsing (or precedence climbing, which is supposedly the same algorithm) could be useful. It makes parsing mathematical expressions much simpler, since one no longer has to bend their parsing rules to fit, and can instead simply specify the binding power and associativity.

There are other libraries that have implemented this, for example

@Marwes
Copy link
Owner

Marwes commented Aug 22, 2023

Parsing precedence is done in the combine-language extension crate https://docs.rs/combine-language/latest/combine_language/fn.expression_parser.html (via shunting yard).

@stefnotch
Copy link
Author

Thank you!

This does seem a bit less powerful than Pratt parsing, since it doesn't handle prefix or postfix operators.

For example, if I want lim to be a prefix operator, with a precedence between addition and multiplication, then I'd have to put in more work.
$\lim 3 \cdot x + 5$ is typically parsed as $(\lim (3 \cdot 5)) + 5$

@Marwes
Copy link
Owner

Marwes commented Aug 23, 2023

I think pratt and shunting yard are the same algorithm (https://matklad.github.io/2020/04/15/from-pratt-to-dijkstra.html) the only difference is that pratt is generally presented by using the normal program stack whereas shunting yard uses an explicit stack.

In the case of https://docs.rs/combine-language/latest/combine_language/fn.expression_parser.html it may be closer to pratt parsing in that case since it uses the program stack. Thus it may just be a case of extending it to support pre and postfix operators.

@stefnotch
Copy link
Author

Sounds good, I'll move this issue to the combine-language repository then.

I don't think I have an immediate need for it, so I'm fine with the issue over there remaining open. If I ever need it, I might contribute a PR. Otherwise I'll leave implementing it to other people who need it.

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

No branches or pull requests

2 participants