1 / 13

Parse Trees

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 .

jbacon
Download Presentation

Parse Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Parse Trees Section 3.2 Wed, Oct 20, 2004

  2. 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.

  3. 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.

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

  5. 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.

  6. 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, wV* and A and B are nonterminals. • One derivation next replaces A and then B, while the other derivation next replaces B and then A.

  7. 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 D1D2. • Now consider the reflexive, symmetric, transitive closure of  and call this new relation similarity. • Clearly, similarity is an equivalence relation.

  8. 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.

  9. Ambiguous Grammars • If there exists wL(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?

  10. 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.

  11. 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?

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

  13. 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?

More Related