next next up down toc toc mail

10.4.  CFGs with Epsilon Productions

A CFG may have a production for a nonterminal in which the right hand side is the empty string (which we denote by epsilon). The effect of this production is to remove the nonterminal from the string being generated.

Here is a grammar for balanced parentheses that uses epsilon productions.

1.   P: ( P )
2.   P: P P
3.   P: epsilon

We begin with the string P. We can replace P with epsilon, in which case we have generated the empty string (which does have balanced parentheses). Alternatively, we can generate a string of balanced parentheses within a pair of balanced parentheses, which must result in a string of balanced parentheses. Alternatively, we can concatenate two strings of balanced parentheses, which again must result in a string of balanced parentheses.

This grammar is equivalent to:


P --> ( P ) | P P | epsilon

We use the notational shorthand '|', which can be read as "or", to represent multiple rewriting rules within a single line.

CFG Examples

Examples:

A CFG describing real numbers in Pascal:

<real>:           <sign> <digit> <digit*> <decimal part> <exp>
<digit*>:         <digit> <digit*> | epsilon
<decimal part>:   '.' <digit> <digit*> | epsilon
<exp>:            'E' <sign> <digit> <digit*> | epsilon
<sign>:           + | - | epsilon
<digit>:          0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

back next up down toc toc mail


Created by unroff & hp-tools. © by Hans-Peter Bischof. All Rights Reserved (1998).

Last modified: 27/July/98 (12:14)