node-ebnf icon indicating copy to clipboard operation
node-ebnf copied to clipboard

Are empty constructions valid?

Open Azmisov opened this issue 2 years ago • 2 comments

const grammar = `
root ::= ws 'x'
ws ::= #x20*

`;
const parser = new Grammars.W3C.Parser(grammar, {debug:true});
console.log(parser.getAST('x'));
console.log(parser.getAST('  x'));

I'd expect both cases to work, but the first does not parse.

Azmisov avatar May 18 '23 03:05 Azmisov

Same issue for me.

test1				::= 'A' OWS '=' OWS 'B'
OWS				::= ( SP | HTAB )*
SP				::= ' '
HTAB				::= #x9

A = B works without issues. However if an Optional Whitespace (OWS) is 0, it stops working: A= B

If I change the EBNF to:

test1				::= 'A' ( SP | HTAB )* '=' ( SP | HTAB )* 'B'
SP				::= ' '
HTAB				::= #x9

it works as expected.

marvinside avatar Feb 07 '25 16:02 marvinside

From what I remember, there can NOT be empty constructions. Constructions must be at least one character in length. Otherwise if you had OWS+ being OWS ::= ' '? then when OWS+ finish? It would be valid to have an infinite amount of OWS not matching any character.

This may be the a good scenario for your parser:

{ws=explicit}
test1			::= 'A' OWS? '=' OWS? 'B' '\n'?
OWS			::= ( SP | HTAB )+
SP			::= ' '
HTAB			::= #x9

menduz avatar Feb 07 '25 16:02 menduz