The strings (a + b)*c and a*b + c are in L(G). It is easy to see that this grammar
generates a restricted subset of arithmetic expressions for C-like programming
languages. The grammar is ambiguous. For instance, the string a + b*c has two
different derivation trees, as shown in Figure 5.5.
Figure 5.5
Two derivation trees for a + b*c.
One way to resolve the ambiguity is, as is done in programming manuals, to
associate precedence rules with the operators + and . Since normally has higher
precedence than +, we would take Figure 5.5(a) as the correct parsing as it
indicates that b*c is a subexpression to be evaluated before performing the
addition. However, this resolution is completely outside the grammar. It is better
Précédent

- 182/532

Suivant