140 likes | 175 Views
Parse Trees. Section 3.2 Wed, Oct 20, 2004. Parse Trees. Given a derivable string, we may represent the derivation by a parse tree . A parse tree is a tree with the following properties. At the root node is the start symbol S .
E N D
Parse Trees Section 3.2 Wed, Oct 20, 2004
Parse Trees • Given a derivable string, we may represent the derivation by a parse tree. • A parse tree is a tree with the following properties. • At the root node is the start symbol S. • At each node, the children (from left to right) are the symbols on the right hand side (from left to right) of the production rule that was used. • Two derivations are equivalent if they have the same parse tree.
Example of a Parse Tree • Let the grammar G be • S → AB • A → aAb | e • B → bB | e • Let the string be w = aabbbb. • Write the parse tree of w.
S A B a A b b B a A b b B e e Parse Tree of aabbbb
Derivations vs. Parse Trees • This is the parse tree of many different, but equivalent, derivations of aabbbb. • For example, • S AB aAbB aaAbbB aabbB aabbbB aabbbbB aabbbb. • S AB AbB AbbB Abb aAbbb aaAbbbb aabbbb.
Similar Derivations • The most minor difference between two derivations is that two consecutive steps in one of them are reversed in the other, and all other steps are the same. • That is, at some point both derivations have derived a string uAvBw, where u, v, wV* and A and B are nonterminals. • One derivation next replaces A and then B, while the other derivation next replaces B and then A.
Similar Derivations • Let D1 be the derivation that replaces A, then B, and let D2 be the derivation that replaces B, then A, and D1 and D2 are identical in all other steps. • Then we say that D1precedesD2, and we write D1D2. • Now consider the reflexive, symmetric, transitive closure of and call this new relation similarity. • Clearly, similarity is an equivalence relation.
Leftmost and Rightmost Derivations • A leftmost derivation of a string is a derivation that is not preceded by any other derivation. • That is, each production rule is applied to the leftmost nonterminal in the string. • A rightmost derivation of a string is a derivation that does not precede any other derivation. • That is, each production rule is applied to the rightmost nonterminal in the string. • For example, see the previous two derivations.
Ambiguous Grammars • If there exists wL(G) such that w has two different parse trees, then G is ambiguous. • For example, define G as • S → AB | aSb | Sb | e • A → aAb | e • B → bB | e • Then the string aabbbb has two different parse trees, that is, two derivations that are not similar. • What are they?
Inherently Ambiguous Grammars • It is obvious how to remove the ambiguity in the last example. • However, if the ambiguity cannot be removed, then the language is called inherently ambiguous.
Example: An Inherently Ambiguous Grammar • Consider the following grammar of expressions using addition and multiplication. • E E + E • E E * E • E (E) • E id • The string E + E * E has two parse trees. • What are they? • Is there a difference in their “meaning” (semantics)? • Can this ambiguity be removed?
Example: An Inherently Ambiguous Grammar • Consider a grammar for if statements without braces. • The language includes both if(condition)statement and if(condition)statementelsestatement
Example: An Inherently Ambiguous Grammar • Production rules • S if(c)S if(c)S else S SS s e • There are two different parse trees for the string if(c1)if(c2)s1elses2 • What are they? • Is there a difference in meaning? • Can this ambiguity be removed?