..
expression -> term + term + term + term ...+ term
So, the EBNF notation says more simply:
expression -> term { (+ | -) term }
An expression is a term followed by zero, one, or many additive terms.
Here is an example of EBNF used to represent an optional element in a production:
if-statement -> if( expression ) statement [else statement]
This production says that an if-statement consists of the key word if, followed by an open parenthesis, followed by
an expression, followed by a closed parenthesis, followed by a program statement, optionally followed by the
key word else and another program statement.
62 SOFTWARE [CHAP. 4
A very important requirement for a programming language grammar is that it be unambiguous.
Given an expression in the language, there must be one and only one valid derivation in the language.
To illustrate an ambiguous grammar, consider this simplification of the grammar for mathematical
expressions:
1 expression -> expression operator expression | identifier |
number | - expression | ( expression )
2 operator -> + | - | * | /
We can again parse the expression (X * 3 + 4) proceeding from the left to the right, and the result will
be the same parse tree we derived from the more complex grammar.
Pages:
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180