Extended BNF

Before trying the quiz, please read section 3.3.2 of Sebesta.

BNF as originally formulated is very simple. Some ideas are dificult to express. Additions to BNF have been used over the last 30 years to express these ideas. EBNF has no real standard set of extensions, but some ideas occur again and again in the various versions. These are: repetition, grouping, and options. In addition, many forms use typographical conventions (bold face, italics etc.) to simply the language of BNF itself.

Repetition replaces most (but not all) use of recursion in BNF. Whereas a sequence in BNF is expressed through right or left recursion:

<binary-seq> ::= <binary-digit> | <binary-digit> <binary-sequence>

in EBNF, we replace this by syntax to indicate that a sequence is a repetition of an element:

<binary-seq> ::= { <binary-digit> }+

The braces: {} signify the repetition of the encosed non-terminal, and the + sign after the right brace signifies there should be at least one occurrence. Nesting, e.g. of expressions, cannot be replaced by repetition.

Grouping is very useful to eliminate alternatives. Whereas in BNF the possibility of addition or subtraction might be represented by:

<expr> ::= <expr> + <term> | <expr> - <term>

EBNF would write:

<expr> ::= <expr> (+|-) <expr>

where the parentheses group the alternatives in one place.

Options also involve alternatives. The optional else after an if in C is handled in BNF by:

<if_stmt> ::= if <expr> <stmt> | if <expr> <stmt> else <stmt>

whereas in EBNF we would write:

<if_stmt> ::= if <expr> <stmt> [ else <stmt> ]

where the brackets [] surround the optional elements.

The syntac of EBNF itself also has many variations. The production, which in BNF is ::= can be -> or just =. The diamond brackets aruond non-terminals are often dropped, and literals are then distinguished typographically, usually non-terminals in bold or italics, terminals in normal font. Sometimes reserved words are quoted. Some examples with these variations are:

binary-seq = { binary-digit }+
expr -> expr (+|-) expr
if_stmt = 'if' expr stmt [ 'else' stmt ]

Now try the self-test called EBNF by clicking on Self-Test in the Action Menu