Undefined nonterminal is accepted in bnf parsing
Describe the bug A nonterminal that is not defined is not rejected during parsing.
To Reproduce For example,
let input = "<dna> ::= <base> | <base> <dna>";
let grammar: Grammar = input.parse().unwrap();
does not panic, while 'base' is not defined in the input.
Desktop (please complete the following information):
- Windows 11
This is a good question! Should an undefined nonterminal be an error?
In a similar way, should this code cause an error?:
let mut grammar = bnf::Grammar::new();
let production_with_undefined_nonterminal: bnf::Production = "<start> ::= <end>".parse().unwrap();
grammar.add_production(production_with_undefined_nonterminal);
The root question seems to be "Is a bnf::Grammar always valid / fully defined?"
The current bnf version does not enforce anything. But maybe as this issue suggests, this should be changed.
This is a good question! Should an undefined nonterminal be an error?
In a similar way, should this code cause an error?:
let mut grammar = bnf::Grammar::new(); let production_with_undefined_nonterminal: bnf::Production = "<start> ::= <end>".parse().unwrap(); grammar.add_production(production_with_undefined_nonterminal);The root question seems to be "Is a
bnf::Grammaralways valid / fully defined?"The current
bnfversion does not enforce anything. But maybe as this issue suggests, this should be changed.
I think so. A quick fix would be add grammar.is_valid() that returns a bool to indicate whether the grammar is valid.