Upload
abner-lindsey
View
230
Download
0
Embed Size (px)
Citation preview
1
Parse TreesDefinitions
Relationship to lm and rm DerivationsAmbiguity in Grammars
2
Parse Trees: graphical representation of derivations Parse trees are trees labeled by
symbols of a CFG. Leaves: labeled by a terminal or ε. Interior nodes: labeled by variables.
Children are labeled by the right side of a production for the parent.
Root: labeled by the start symbol. Yield: string produced by derivation
by concatenation of leaves left-to-right
3
Example: Parse Tree forDerivation of (())() in S -> SS | (S) | ()
S
SS
S )(
( )
( )
4
Define yield of a Parse Tree
The concatenation of the labels of the leaves in left-to-right order
Example: yield of is (())() S
SS
S )(
( )
( )
5
Quiz on Wednesday 11-12-2014Context Free Grammars 1 & 2
6
Parse Trees, Left- and Rightmost Derivations
For every parse tree, there is a unique leftmost, and a unique rightmost derivation.
To 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
Proof: if parse tree then A=>lm*w
Induction on the height of the tree. Basis: height 1. Tree looks like A -> a1…an must be a production.
By children rule
Thus, A =>*lm a1…an. By definition of yield
A
a1 an. . .
8
IH: for trees of height < h, root A, yield w, then A=>*lm w
Consider a tree of height h: A =>lm X1…Xn by children rule
By IH, Xi =>*lm wi.
A=>*lm w1X2…Xn =>*lm w1w2X3…Xn =>*lm … =>*lm w1…wn by repeated use of IH
A
X1 Xn. . .
w1 wn
Proof: if parse tree then A=>lm*w
9
Prove: if A=>lm*w then parse tree with root A and yield w The proof is an induction on the length
of the derivation. Basis: If A =>lm a1…an by a one-step
derivation, then A ->a1…an is production
Tree of height=1 exist by children ruleA
a1 an. . .
10
Proof: if A=>lm*w then parse tree
IH: if A =>*lm w has fewer than k > 1 steps,, then there exist parse tree with root A and yield w
Let A =>*lm w be a k-step derivation.
First step is A =>lm X1…Xn. w can be divided into parts where
the first part is derived from X1, the next is derived from X2, and so on.
11
All Xi =>*lm wi have fewer than k steps
By the IH, there is a parse tree with root Xi and yield wi.
Thus, there is a parse tree
A
X1 Xn. . .
w1 wn
Proof: if A=>lm*w then parse tree
12
Parse Trees and Rightmost Derivations
Proof of A=>lm*w iff parse tree root A and yield w did not depend on leftmost
Prove of A=>rm*w iff parse tree root A and yield w by same steps
For any derivation (leftmost, rightmost or neither), there exist a parse tree with yield w.
13
Ambiguous CFG’s
Equivalent properties of “ambiguous” CFGsRecall 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
S -> SS | (S) | () is ambiguousProve by lm derivation of ()()()
S=>SS=>SSS=>()SS=>()()S=>()()()
S=>SS=>()S=>()SS=>()()=>()()()
15
CptS 317 Fall 2014 Assignment 10; Due 11-14-2014Exercises 5.4.1 text p 215
Use string aab to show that S->aS|aSbS| is ambiguous
16
Exercise 5.4.3 p216Remove ambiguity in S->aS|aSbS|
HW10 to show ambiguous using string aabParse 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
Ambiguity is a Property of Grammars, not Languages
Same CGL may have ambiguous and unambiguous CFGs
Example: balanced-parentheses CFL Recall, S -> SS | (S) | () shown to be
ambiguous by 2 lm derivations of ()()() Balanced-parentheses CFL also has an
unambiguous CFG
18
Unambiguous balanced-parenthesis CFG
B -> (RB | ε R -> ) | (RR To start new balanced-parentheses
string, use B ->(RB To expand with “(“, use R->(RR To expand with “)”, use R ->) End with B-> Example of LL(1): “Leftmost
derivation, left-to-right scan, one symbol of look-a-head.”
19
Unambiguous balanced-parenthesis CFG
B -> (RB | ε R -> ) | (RR Construct a unique leftmost derivation for
a given balanced string of parentheses by scanning the string from left to right.
Example derivation of (())() B =>lm (RB =>lm ((RRB =>lm (()RB =>lm (())B
(())B =>lm (())(RB =>lm (())()B =>lm (())() At each scan step, the production needed is
uniquely defined.
20
LL(1) Grammars By construction obviously unambiguous You 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
Most programming languages have LL(1) grammars.
21
Another balanced paren. example
Perform leftmost derivation of ()()() by LL(1) B -> (RB | ε; R -> ) | (RR
22
Inherent Ambiguity
Not every ambiguous grammar can be “fix” as was the case with the balanced-parentheses grammar.
Some CFL’s are inherently ambiguous; every grammar for the language is ambiguous.
23
Example: Inherent Ambiguity
The language {0i1j2k | i = j or j = k} is inherently ambiguous.
At least some of the strings of the form 0n1n2n must be generated by two different parse trees depending on which you check 1st: equal number of 0’s and 1’s or equal number of 1’s and 2’s.
24
One ambiguous grammar for {0i1j2k | i = j or j = k}
S -> AB | CDA -> 0A1 | 01B -> 2B | 2C -> 0C | 0D -> 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 =>012S => CD => 0D => 012
25