Parse Trees: Definitions Relationship To Left-And Rightmost Derivations Ambiguity in Grammars
Parse Trees: Definitions Relationship To Left-And Rightmost Derivations Ambiguity in Grammars
Parse Trees
Parse trees are trees labeled by symbols of a particular CFG. Leaves: labeled by a terminal or .
Interior nodes: labeled by a variable.
Children are labeled by the right side of a production for the parent.
S
( ( S ) ) (
S
)
( )
Proof Part 1
Induction on the height (length of the longest path from the root) of the tree. A Basis: height 1. Tree looks like a1 . . . A -> a1an must be a production. Thus, A =>*lm a1an.
an
Part 1 Induction
Assume (1) for trees of height < h, and A let this tree have height h: By IH, Xi =>*lm wi. X1 . . . Xn
Note: if Xi is a terminal, then Xi = wi.
Thus, A =>lm X1Xn =>*lm w1X2Xn =>*lm w1w2X3Xn =>*lm =>*lm w1wn.
w1
wn
Proof: Part 2
Given a leftmost derivation of a terminal string, we need to prove the existence of a parse tree. The proof is an induction on the length of the derivation.
Part 2 Basis
If A =>*lm a1an by a one-step derivation, then there must be a parse tree A
a1 . . . an
Part 2 Induction
Assume (2) for derivations of fewer than k > 1 steps, and let A =>*lm w be a k-step derivation. First step is A =>lm X1Xn. Key point: w can be divided so the first portion is derived from X1, the next is derived from X2, and so on.
If Xi is a terminal, then wi = Xi.
10
Induction (2)
That is, Xi =>*lm wi for all i such that Xi is a variable.
And the derivation takes fewer than k steps.
By the IH, if Xi is a variable, then there is a parse tree with root Xi and yield wi. A Thus, there is a parse tree
X1 . . . Xn
w1 wn
11
12
Ambiguous Grammars
A CFG is ambiguous if there is a string in the language that is the yield of two or more parse trees. Example: S -> SS | (S) | () Two parse trees for ()()() on next slide.
14
Example Continued
S
S S ( ) ( S ) ( S ) ( S ) ( S ) (
S
S S )
15
17
18
B -> (RB |
R -> ) | (RR
20
B -> (RB |
R -> ) | (RR
21
B -> (RB |
R -> ) | (RR
22
B -> (RB |
B -> (RB |
24
B -> (RB |
Steps of leftmost derivation: B (())(RB (RB ((RRB (()RB (())B R -> ) | (RR
25
Next symbol
B -> (RB |
26
Next symbol
B -> (RB |
27
LL(1) Grammars
As an aside, a grammar such B -> (RB | R -> ) | (RR, where you can always figure out the production to use in a leftmost derivation by scanning the given string left-to-right and looking only at the next one symbol is called LL(1).
Leftmost derivation, left-to-right scan, one symbol of lookahead.
28
29
Inherent Ambiguity
It would be nice if for every ambiguous grammar, there were some way to fix the ambiguity, as we did for the balanced-parentheses grammar. Unfortunately, certain CFLs are inherently ambiguous, meaning that every grammar for the language is ambiguous.
30
And there are two derivations of every string with equal numbers of 0s, 1s, and 2s. E.g.: S => AB => 01B =>012 S => CD => 0D => 012
32