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

Left-Recursive Expressions #4

Open
j-mie6 opened this issue Aug 23, 2021 · 1 comment
Open

Left-Recursive Expressions #4

j-mie6 opened this issue Aug 23, 2021 · 1 comment
Labels
pitfall A problem that could be encountered when using combinators

Comments

@j-mie6
Copy link
Owner

j-mie6 commented Aug 23, 2021

Parser combinator libraries, like our very own miniparsec, are often vulnerable to left-recursion:

Expressions with left recursion cannot be encoded by recursive descent parsers and will diverge.

Let's suppose we had the parser:

bad = bad *> char 'a' <|> char 'b'

This could be interpreted as matching a regex like ba*, but using a recursive descent algorithm it will evaluate bad by immediately evaluating... bad. Oops! In order for parsers to be productive, they have to have consumed some input before they recurse again.

Commonly, the grammar can be factored to remove left-recursion, but this exposes implementation details and complicates the grammar, so ideally we could avoid that... I'll have a think.

@j-mie6 j-mie6 added the pitfall A problem that could be encountered when using combinators label Aug 23, 2021
@j-mie6
Copy link
Owner Author

j-mie6 commented Aug 25, 2021

Most parser combinator libraries support some form of "chain" combinators that abstract away the repeated application of a parser to operators. They handle the recursion properly, and apply the operators with the correct associativity.

I should add these to the library at some point...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pitfall A problem that could be encountered when using combinators
Projects
None yet
Development

No branches or pull requests

1 participant