Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Parse Trees Definitions Relationship to lm and rm Derivations Ambiguity in Grammars.

Similar presentations


Presentation on theme: "1 Parse Trees Definitions Relationship to lm and rm Derivations Ambiguity in Grammars."— Presentation transcript:

1 1 Parse Trees Definitions Relationship to lm and rm Derivations Ambiguity in Grammars

2 2 Parse Trees: graphical representation of derivations uParse trees are trees labeled by symbols of a CFG.  Leaves: labeled by a terminal or ε. uInterior nodes: labeled by variables. wChildren are labeled by the right side of a production for the parent. uRoot: labeled by the start symbol. uYield: string produced by derivation by concatenation of leaves left-to-right

3 3 Example: Parse Tree for Derivation of (())() in S -> SS | (S) | () S SS S)( () ()

4 4 Define yield of a Parse Tree uThe concatenation of the labels of the leaves in left-to-right order uExample: yield of is (())() S SS S)( () ()

5 5 Quiz on Wednesday 11-12-2014 Context Free Grammars 1 & 2

6 6 Parse Trees, Left- and Rightmost Derivations uFor every parse tree, there is a unique leftmost, and a unique rightmost derivation. uTo be proved: 1.If there is a parse tree with root labeled A and yield w, then A =>* lm w. 2.If A =>* lm w, then there is a parse tree with root A and yield w.

7 7 Proof: if parse tree then A=> lm *w uInduction on the height of the tree. uBasis: height 1. Tree looks like uA -> a 1 …a n must be a production. wBy children rule uThus, A =>* lm a 1 …a n. wBy definition of yield A a1a1 anan...

8 8 uIH: for trees of height * lm w uConsider a tree of height h: uA => lm X 1 …X n by children rule uBy IH, X i =>* lm w i. uA=>* lm w 1 X 2 …X n =>* lm w 1 w 2 X 3 …X n =>* lm … =>* lm w 1 …w n by repeated use of IH A X1X1 XnXn... w1w1 wnwn Proof: if parse tree then A=> lm *w

9 9 Prove: if A=> lm *w then parse tree with root A and yield w uThe proof is an induction on the length of the derivation. uBasis: If A => lm a 1 …a n by a one-step derivation, then A ->a 1 …a n is production uTree of height=1 exist by children rule A a1a1 anan...

10 10 Proof: if A=> lm *w then parse tree uIH: if A =>* lm w has fewer than k > 1 steps,, then there exist parse tree with root A and yield w uLet A =>* lm w be a k-step derivation. uFirst step is A => lm X 1 …X n. uw can be divided into parts where the first part is derived from X 1, the next is derived from X 2, and so on.

11 11 uAll X i =>* lm w i have fewer than k steps uBy the IH, there is a parse tree with root X i and yield w i. uThus, there is a parse tree A X1X1 XnXn... w1w1 wnwn Proof: if A=> lm *w then parse tree

12 12 Parse Trees and Rightmost Derivations uProof of A=> lm *w iff parse tree root A and yield w did not depend on leftmost uProve of A=> rm *w iff parse tree root A and yield w by same steps uFor any derivation (leftmost, rightmost or neither), there exist a parse tree with yield w.

13 13 Ambiguous CFG’s Equivalent properties of “ambiguous” CFGs Recall that CFL = L(CFG) 1.There is a string in the CFL that has two different leftmost derivations. 2.There is a string in the CFL that has two different rightmost derivations. 3.There is a string in the CFL that is the yield of 2 distinct parse trees

14 14 S -> SS | (S) | () is ambiguous Prove by lm derivation of ()()() S=>SS=>SSS=>()SS=>()()S=>()()() S=>SS=>()S=>()SS=>()()=>()()()

15 15 CptS 317 Fall 2014 Assignment 10; Due 11-14-2014 Exercises 5.4.1 text p 215 Use string aab to show that S->aS|aSbS|  is ambiguous

16 16 Exercise 5.4.3 p216 Remove ambiguity in S->aS|aSbS|  HW10 to show ambiguous using string aab Parse trees, lm & rm derivations can start with either S=>aS or S=>aSbS Whatever changes make to force only one way to start must preserve the content of L(CFG) Difficult to know if changes designed to avoid ambiguity in derivation of aab apply to all strings in L(CFG)

17 17 Ambiguity is a Property of Grammars, not Languages uSame CGL may have ambiguous and unambiguous CFGs uExample: balanced-parentheses CFL uRecall, S -> SS | (S) | () shown to be ambiguous by 2 lm derivations of ()()() uBalanced-parentheses CFL also has an unambiguous CFG

18 18 Unambiguous balanced-parenthesis CFG B -> (RB | ε R -> ) | (RR uTo start new balanced-parentheses string, use B ->(RB uTo expand with “(“, use R->(RR uTo expand with “)”, use R ->)  End with B->  uExample of LL(1): “Leftmost derivation, left-to-right scan, one symbol of look-a- head.”

19 19 Unambiguous balanced-parenthesis CFG B -> (RB | ε R -> ) | (RR uConstruct a unique leftmost derivation for a given balanced string of parentheses by scanning the string from left to right. uExample derivation of (())() wB => lm (RB => lm ((RRB => lm (()RB => lm (())B w(())B => lm (())(RB => lm (())()B => lm (())() wAt each scan step, the production needed is uniquely defined.

20 20 LL(1) Grammars uBy construction obviously unambiguous uYou can always figure out the production to use in a leftmost derivation by scanning the given string left-to-right and looking at the next one symbol only uMost programming languages have LL(1) grammars.

21 21 Another balanced paren. example  Perform leftmost derivation of ()()() by LL(1) B -> (RB | ε; R -> ) | (RR

22 22 Inherent Ambiguity uNot every ambiguous grammar can be “fix” as was the case with the balanced- parentheses grammar. uSome CFL’s are inherently ambiguous; every grammar for the language is ambiguous.

23 23 Example: Inherent Ambiguity uThe language {0 i 1 j 2 k | i = j or j = k} is inherently ambiguous. uAt least some of the strings of the form 0 n 1 n 2 n must be generated by two different parse trees depending on which you check 1 st : equal number of 0’s and 1’s or equal number of 1’s and 2’s.

24 24 One ambiguous grammar for {0 i 1 j 2 k | i = j or j = k} S -> AB | CD A -> 0A1 | 01 B -> 2B | 2 C -> 0C | 0 D -> 1D2 | 12 A generates equal 0’s and 1’s B generates any number of 2’s C generates any number of 0’s D generates equal 1’s and 2’s There are two derivations of every string with equal numbers of 0’s, 1’s, and 2’s, for example, S => AB => 01B =>012 S => CD => 0D => 012

25 25


Download ppt "1 Parse Trees Definitions Relationship to lm and rm Derivations Ambiguity in Grammars."

Similar presentations


Ads by Google