0% found this document useful (0 votes)
38 views32 pages

Parse Trees: Definitions Relationship To Left-And Rightmost Derivations Ambiguity in Grammars

This document discusses parse trees and their relationship to leftmost and rightmost derivations in context-free grammars. It defines parse trees and shows how their yields correspond uniquely to leftmost and rightmost derivations. It also discusses ambiguity in grammars, showing that ambiguous grammars have strings with multiple parse trees, while unambiguous grammars like LL(1) grammars have a single parse tree per string. Some context-free languages are inherently ambiguous with no unambiguous grammar possible.

Uploaded by

Anant Gor
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views32 pages

Parse Trees: Definitions Relationship To Left-And Rightmost Derivations Ambiguity in Grammars

This document discusses parse trees and their relationship to leftmost and rightmost derivations in context-free grammars. It defines parse trees and shows how their yields correspond uniquely to leftmost and rightmost derivations. It also discusses ambiguity in grammars, showing that ambiguous grammars have strings with multiple parse trees, while unambiguous grammars like LL(1) grammars have a single parse tree per string. Some context-free languages are inherently ambiguous with no unambiguous grammar possible.

Uploaded by

Anant Gor
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

Parse Trees

Definitions Relationship to Left- and Rightmost Derivations Ambiguity in Grammars


1

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.

Root: must be labeled by the start symbol.


2

Example: Parse Tree


S -> SS | (S) | ()
S

S
( ( S ) ) (

S
)

Yield of a Parse Tree


The concatenation of the labels of the leaves in left-to-right order
That is, in the order of a preorder traversal.

is called the yield of the parse tree. Example: yield of S is (())()


S S ( S ) ( )
4

( )

Parse Trees, Left- and Rightmost Derivations


For every parse tree, there is a unique leftmost, and a unique rightmost derivation. Well prove:
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.
5

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

Parse Trees and Rightmost Derivations


The ideas are essentially the mirror image of the proof for leftmost derivations. Left to the imagination.

12

Parse Trees and Any Derivation


The proof that you can obtain a parse tree from a leftmost derivation doesnt really depend on leftmost. First step still has to be A => X1Xn. And w still can be divided so the first portion is derived from X1, the next is derived from X2, and so on.
13

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

Ambiguity, Left- and Rightmost Derivations


If there are two different parse trees, they must produce two different leftmost derivations by the construction given in the proof. Conversely, two different leftmost derivations produce different parse trees by the other part of the proof. Likewise for rightmost derivations.
16

Ambiguity, etc. (2)


Thus, equivalent definitions of ambiguous grammar are:
1. There is a string in the language that has two different leftmost derivations. 2. There is a string in the language that has two different rightmost derivations.

17

Ambiguity is a Property of Grammars, not Languages


For the balanced-parentheses language, here is another CFG, which is unambiguous. B, the start symbol, derives balanced strings. B -> (RB | R -> ) | (RR
R generates strings that have one more right paren than left.

18

Example: Unambiguous Grammar


B -> (RB | R -> ) | (RR
Construct a unique leftmost derivation for a given balanced string of parentheses by scanning the string from left to right.
If we need to expand B, then use B -> (RB if the next symbol is ( and if at the end.

If we need to expand R, use R -> ) if the next symbol is ) and (RR if it is (.


19

The Parsing Process


Remaining Input: (())()
Next symbol

Steps of leftmost derivation: B

B -> (RB |

R -> ) | (RR

20

The Parsing Process


Remaining Input: ())()
Next symbol

Steps of leftmost derivation: B (RB

B -> (RB |

R -> ) | (RR

21

The Parsing Process


Remaining Input: ))()
Next symbol

Steps of leftmost derivation: B (RB ((RRB

B -> (RB |

R -> ) | (RR

22

The Parsing Process


Remaining Input: )()
Next symbol

Steps of leftmost derivation: B (RB ((RRB (()RB


R -> ) | (RR
23

B -> (RB |

The Parsing Process


Remaining Input: ()
Next symbol

B -> (RB |

Steps of leftmost derivation: B (RB ((RRB (()RB (())B R -> ) | (RR

24

The Parsing Process


Remaining Input: )
Next symbol

B -> (RB |

Steps of leftmost derivation: B (())(RB (RB ((RRB (()RB (())B R -> ) | (RR

25

The Parsing Process


Remaining Input: Steps of leftmost derivation: B (())(RB (RB (())()B ((RRB (()RB (())B R -> ) | (RR

Next symbol

B -> (RB |

26

The Parsing Process


Remaining Input: Steps of leftmost derivation: B (())(RB (RB (())()B ((RRB (())() (()RB (())B R -> ) | (RR

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

LL(1) Grammars (2)


Most programming languages have LL(1) grammars. LL(1) grammars are never ambiguous.

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

Example: Inherent Ambiguity


The language {0i1j2k | i = j or j = k} is inherently ambiguous. Intuitively, at least some of the strings of the form 0n1n2n must be generated by two different parse trees, one based on checking the 0s and 1s, the other based on checking the 1s and 2s.
31

One Possible Ambiguous Grammar


S -> AB | CD A -> 0A1 | 01 B -> 2B | 2 C -> 0C | 0 D -> 1D2 | 12
A generates equal 0s and 1s B generates any number of 2s C generates any number of 0s D generates equal 1s and 2s

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

You might also like