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

Support for mutually recursive parsers #213

Open
aryx opened this issue Apr 8, 2021 · 2 comments
Open

Support for mutually recursive parsers #213

aryx opened this issue Apr 8, 2021 · 2 comments

Comments

@aryx
Copy link

aryx commented Apr 8, 2021

I see that for recursive parser, like when parsing JSON, you provide the fix combinator. What about mutually recursive
parsers, for example if I want to parse Java, there will be a parser for stmt, one for expr, but inside stmt you need to call expr and inside expr you also need to call stmt (for example Java support lambdas expressions, which themselves contain stmt).
Parsing a real programming language requires a ton of mutually recursive parsers.

The fastparse parser combinator library https://com-lihaoyi.github.io/fastparse/#GettingStarted allows for example to define those mutually recursive parsers.

BTW fastparse has also special support to skip whitespace by using Implicit. Is there something like that planned for angstrom too?
See https://com-lihaoyi.github.io/fastparse/#ExampleParsers and the NoWhitespace special import.

@aryx aryx changed the title Support for mutually recursive parser Support for mutually recursive parsers Apr 8, 2021
@Hakimba
Copy link

Hakimba commented Sep 13, 2021

Hi, i have the same problem, its possible to have a full example of a toy programming language parsing with angstrom ?

@thedufer
Copy link
Collaborator

thedufer commented May 28, 2022

The fix combinator is convenient, but not actually necessary. You can see in my comment here how you could implement a recursive parser without fix. You can do the same thing to handle mutual recursion. You can also do it a bit more cleanly by using a fix for each parser that needs to refer to itself, taking arguments as stubs for parsers that haven't been defined yet, and then stitching it together at the end (see here).

I think the idea of skipping whitespace doesn't actually make sense in angstrom in the way that you mean. angstrom is, as far as I can tell, a lot lower-level than fastparse, which makes conveniences like that not really possible, but also means it can be a lot faster, and angstrom is targeted primarily at very perf-sensitive applications.

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

3 participants