04 BNF Parse Trees
04 BNF Parse Trees
Usually, derivations are more useful if they are done as parse trees.
The same derivation of “the flower blooms”, expressed as a parse tree, is:
<sentence>
<noun-phrase> <predicate>
the flower
<exp> <exp>
6 3 3 4
Note that the parentheses now act as a mechanism for overriding operator
precedence, as is customary. However, our grammar is still ambiguous!
Notice that, using this grammar, there are two parse trees for 6-3-2
<diff> <diff
>
<diff> - <diff> <diff> - <diff>
6 3 3 2
The left tree implies a result of (6-3)-2 which is 1.
The right tree implies a result of 6-(3-2) which is 5.
Thus the above specification is ambiguous, and therefore is an inadequate
specification of the subtraction operator. (note that the previous grammar
contained the same ambiguity in the + and * operators). The solution is to
rewrite it so that it limits recursion to one side or the other, but not both:
<diff> ::= <diff> - <digit> | <digit>
<digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9