Parse Trees Section 3 2 Wed Oct 20

  • Slides: 13
Download presentation
Parse Trees Section 3. 2 Wed, Oct 20, 2004

Parse Trees Section 3. 2 Wed, Oct 20, 2004

Parse Trees Given a derivable string, we may represent the derivation by a parse

Parse Trees Given a derivable string, we may represent the derivation by a parse tree. p A parse tree is a tree with the following properties. p n n p 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 p Let the grammar G be n n n

Example of a Parse Tree p Let the grammar G be n n n S → AB A → a. Ab | e B → b. B | e Let the string be w = aabbbb. p Write the parse tree of w. p

Parse Tree of aabbbb S A a a B A b e b B

Parse Tree of aabbbb S A a a B A b e b B e

Derivations vs. Parse Trees This is the parse tree of many different, but equivalent,

Derivations vs. Parse Trees This is the parse tree of many different, but equivalent, derivations of aabbbb. p For example, p n n S AB a. Ab. B aa. Abb. B aabbbb. S AB Abb a. Abbb aa. Abbbb aabbbb.

Similar Derivations The most minor difference between two derivations is that two consecutive steps

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. p That is, at some point both derivations have derived a string u. Av. Bw, where u, v, w V* and A and B are nonterminals. p One derivation next replaces A and then B, while the other derivation next replaces B and then A. p

Similar Derivations Let D 1 be the derivation that replaces A, then B, and

Similar Derivations Let D 1 be the derivation that replaces A, then B, and let D 2 be the derivation that replaces B, then A, and D 1 and D 2 are identical in all other steps. p Then we say that D 1 precedes D 2, and we write D 1 D 2. p Now consider the reflexive, symmetric, transitive closure of and call this new relation similarity. p Clearly, similarity is an equivalence relation. p

Leftmost and Rightmost Derivations A leftmost derivation of a string is a derivation that

Leftmost and Rightmost Derivations A leftmost derivation of a string is a derivation that is not preceded by any other derivation. p That is, each production rule is applied to the leftmost nonterminal in the string. p A rightmost derivation of a string is a derivation that does not precede any other derivation. p That is, each production rule is applied to the rightmost nonterminal in the string. p For example, see the previous two derivations. p

Ambiguous Grammars If there exists w L(G) such that w has two different parse

Ambiguous Grammars If there exists w L(G) such that w has two different parse trees, then G is ambiguous. p For example, define G as p n n n S → AB | a. Sb | e A → a. Ab | e B → b. B | e Then the string aabbbb has two different parse trees, that is, two derivations that are not similar. p What are they? p

Inherently Ambiguous Grammars It is obvious how to remove the ambiguity in the last

Inherently Ambiguous Grammars It is obvious how to remove the ambiguity in the last example. p However, if the ambiguity cannot be removed, then the language is called inherently ambiguous. p

Example: An Inherently Ambiguous Grammar p Consider the following grammar of expressions using addition

Example: An Inherently Ambiguous Grammar p Consider the following grammar of expressions using addition and multiplication. n n E E+E E E*E E (E) E id The string E + E * E has two parse trees. p What are they? p Is there a difference in their “meaning” (semantics)? p Can this ambiguity be removed? p

Example: An Inherently Ambiguous Grammar Consider a grammar for if statements without braces. p

Example: An Inherently Ambiguous Grammar Consider a grammar for if statements without braces. p The language includes both if (condition) statement and if (condition) statement else statement p

Example: An Inherently Ambiguous Grammar p Production rules n S if(c)S else S SS

Example: An Inherently Ambiguous Grammar p Production rules n S if(c)S else S SS s e There are two different parse trees for the string if (c 1) if (c 2) s 1 else s 2 p What are they? p Is there a difference in meaning? p Can this ambiguity be removed? p