Java Reference
In-Depth Information
Another left-most derivation of the same sentence is
E
)
E
*
E
)
E
+
E
*
E
)
id+
E
*
E
)
id+id*
E
)
id+id*id
Therefore, the grammar is ambiguous. It is also the case that the sentence has two
right-most derivations in the grammar:
E
) E
+
E
) E
+
E
*
E
) E
+
E
*id
)
E
+id*id
)
id+id*id
and
E
) E
*
E
)
E
*id
) E
+
E
*id
) E
+id*id
)
id+id*id
These two right-most derivations for the same sentence also show the grammar is am-
biguous. Finally, the two parse trees, illustrated in Figure 3.3, for the same sentence also
demonstrate ambiguity.
FIGURE 3.3 Two parse trees for
id+id*id
.
Clearly, we would rather have unambiguous grammars describe our programming lan-
guage, because ambiguity in the parsing can lead to ambiguity in assigning semantics (mean-
ing) to programs. For example, in
id+id*id
, which operation is applied first: addition
Search WWH ::
Custom Search