BNF for comma separated sequence?

It’s not possible to create a BNF grammar for a sequence of characters (possibly empty) separated by a comma, but not start or end with a comma,

So this is normal:

  <--- Empty sequence is ok!
A
A,B
A,B,C

It is not normal:

A,
,A
A,,B
AB

An empty box throws me away. So far I have received:

<char-seq> ::= <empty> | <char> , <char-seq> | <char>

but this creates strings like A,: - (

+5
source share
2 answers

An empty char sequence is what gives you problems. You need a rule that matches a non-empty sequence, separate from a rule that matches both empty and non-empty, for example:

<char-seq> ::= <empty> | <non-empty-char-seq>
<non-empty-char-seq> ::= <char> | <char> , <non-empty-char-seq>
+8
source
<char-seq> ::= <empty> | <chars>
<chars> ::= <char> | <char> , <chars>
+2
source

All Articles