Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BNF for comma separated sequence?

Tags:

grammar

bnf

Can't come up with a BNF grammar for the sequence of characters (possibly empty), separated by comma, but not starting or ending with a comma,

So this is OK:

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

This is NOT ok:

A,
,A
A,,B
AB

The empty case throws me off. What I got so far is:

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

but this produces strings like A, :-(

like image 450
Andriy Drozdyuk Avatar asked Sep 19 '12 13:09

Andriy Drozdyuk


People also ask

What is the difference between BNF and EBNF?

BNF syntax can only represent a rule in one line, whereas in EBNF a terminating character, the semicolon, marks the end of a rule. Furthermore, EBNF includes mechanisms for enhancements, defining the number of repetitions, excluding alternatives, comments, etc.

What are BNF rules?

Rules For making BNF : A terminal could be a quoted literal (like “+”, “switch” or ” “<<=”) or the name of a category of literals (like integer). The name of a category of literals is typically defined by other means, like a daily expression or maybe prose.

What do brackets mean in BNF?

The right hand side of a production may be composed of any sequence of bracketed names and printable characters. Curly brackets ({...}) are used to delimit choices separated by vertical bars and square brackets ([...]) are used to indicate optional phrases.


2 Answers

The empty char sequence is what gives you the trouble. You need a rule that matches a non-empty sequence to be separate from the rule that matches both an empty and a non-empty one, like this:

<char-seq> ::= <empty> | <non-empty-char-seq>
<non-empty-char-seq> ::= <char> | <char> , <non-empty-char-seq>
like image 194
Sergey Kalinichenko Avatar answered Nov 11 '22 22:11

Sergey Kalinichenko


<char-seq> ::= <empty> | <chars>
<chars> ::= <char> | <char> , <chars>
like image 39
Istvan Neuwirth Avatar answered Nov 11 '22 22:11

Istvan Neuwirth