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

Order of Rules Affects Parsing #137

Open
lambdaknight opened this issue Sep 26, 2023 · 2 comments
Open

Order of Rules Affects Parsing #137

lambdaknight opened this issue Sep 26, 2023 · 2 comments

Comments

@lambdaknight
Copy link

Describe the bug
BNF grammar should be independent of the order of the rules as long as it is otherwise well-formed.

To Reproduce
Take the dna_left_recursive test and reverse the order of the rules. The BNF parser can no longer successfully parse the input string.

#[test]
fn dna_left_recursive() {
    let grammar: Grammar = "<base> ::= 'A' | 'C' | 'G' | 'T'
        <dna> ::= <base> | <dna> <base>"
        .parse()
        .unwrap();

    let input = "GATTACA";

    let parses: Vec<_> = grammar.parse_input(input).map(|a| a.to_string()).collect();
    assert_snapshot!(parses.join("\n"));
}
@CrockAgile
Copy link
Collaborator

Thanks for bringing this up! I am not sure yet, but I have a guess.

The current bnf::Grammar::parse doesn't have a way to designate the "starting" term. So as is, it assumes the first rule begins with the "starting" term.

In this example, this means the "starting" term is <base>, which cannot parse "GATTACA".

Sorry if this turns out to be the reason! I meant to mark this strange implicit assumption in the API documentation, but I must have forgotten.

@lambdaknight
Copy link
Author

That's what I figured was happening, but I figured I'd file a bug so you have a nice reminder to add it to the documentation or whatever you end up doing. :) Thanks!

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