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

Feature Request: Composing Grammars #18

Open
quad opened this issue Sep 6, 2023 · 3 comments
Open

Feature Request: Composing Grammars #18

quad opened this issue Sep 6, 2023 · 3 comments

Comments

@quad
Copy link

quad commented Sep 6, 2023

Context: I'm porting an existing handwritten parser to use parsus.

Right now, parsus is built on the presumption that a Grammar is complete. It would be helpful if Grammars could be composed.

Because it doesn't support this natively, I've resorted to this hack:

internal abstract class C<T> : Grammar<T>() {
   // ... grammar
}

val extendedGrammar = object : C<Expression>() {
    // Additional grammar
    override val root by parser_in_C_class
}
@alllex
Copy link
Owner

alllex commented Sep 9, 2023

Do you have a concrete use-case for composing grammars?

The main problem with the naive approach here is that grammar represent a grouping of tokens that split the initial input stream into lexemes. Since the ordering/priority of tokens matter, it is not immediately clear how composing tokens sets from different grammars would behave or be intuitive for the users.

Grammars also serve as a grouping for parsers, but this grouping is loose and does not have a runtime effect. For instance, nothing prevents from using parsers from other grammars, as long as they agree on the token set. However, this could lead to hard-to-debug problems if the tokens do diverge.

@quad
Copy link
Author

quad commented Nov 27, 2023

Do you have a concrete use-case for composing grammars?

I was porting an existing hand-written recursive decent parser into a Parsus grammar. The existing test suite tested different leaves of the grammar tree; whereas Parsus' API made it difficult (impossible?) to test a piece of the grammar in isolation.

My solution was to isolate the units of the grammar and expose them via their own roots.

@alllex
Copy link
Owner

alllex commented Nov 27, 2023

If you need that entirely for testing, then you can rely on the Grammar.parse method overload that allows passing a different parser (than root) to perform parsing.

https://javadoc.io/doc/me.alllex.parsus/parsus-jvm/latest/parsus/me.alllex.parsus.parser/-grammar/parse.html

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