shnewto / bnf

Parse BNF grammar definitions
MIT License
256 stars 22 forks source link

Undefined nonterminal is accepted in bnf parsing #136

Open Dan-wanna-M opened 1 year ago

Dan-wanna-M commented 1 year ago

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):

CrockAgile commented 1 year ago

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.

Dan-wanna-M commented 1 year ago

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.

I think so. A quick fix would be add grammar.is_valid() that returns a bool to indicate whether the grammar is valid.