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

ParserPEG with multiple root rules #88

Open
Mortal opened this issue Nov 24, 2020 · 1 comment
Open

ParserPEG with multiple root rules #88

Mortal opened this issue Nov 24, 2020 · 1 comment
Labels

Comments

@Mortal
Copy link

Mortal commented Nov 24, 2020

I have a grammar with multiple top-level non-terminals that I want to use at different points in my program. Currently I have to specify ONE root rule when loading a grammar with ParserPEG. That means I need to parse the same grammar multiple times with different root rule names if I want to use different root rules throughout my program.

Could Arpeggio add support for specifying the root rule name when calling parse(), instead of once and for all for the parser when instantiating it? Or is there another way of achieving this?

E.g. currently I have to do this:

from arpeggio.cleanpeg import ParserPEG

grammar = r"""
assignment = name "=" expr EOF
expression = expr EOF
name = r"\b[a-z]+\b"
expr = name (("+" / "-") name)*
"""
parser_e = ParserPEG(grammar, root_rule_name="expression")
parser_a = ParserPEG(grammar, root_rule_name="assignment")  # <-- BAD! Parsing the same grammar twice

def run(condition, action):
    parser_e.parse(condition)
    parser_a.parse(action)

run("a + b", "c = a + a + b")

I would like to be able to do this instead:

parser2 = ParserPEG(grammar)

def run2(condition, action):
    parser2.parse(condition, root_rule_name="expression")
    parser2.parse(action, root_rule_name="assignment")  # GOOD! One grammar, multiple root rules
@igordejanovic
Copy link
Member

I think it should be relatively straight forward to support this although it would require first implementing the same feature in ParserPython. I don't see any conceptual difficulty as the recursive descend parsers can naturally start from any rule.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants