1 parse trees definitions relationship to lm and rm derivations ambiguity in grammars

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

Upload: abner-lindsey

Post on 13-Jan-2016

232 views

Category:

Documents


0 download

TRANSCRIPT

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

1

Parse TreesDefinitions

Relationship to lm and rm DerivationsAmbiguity in Grammars

Page 2: 1 Parse Trees Definitions Relationship to lm and rm Derivations Ambiguity 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

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

3

Example: Parse Tree forDerivation of (())() in S -> SS | (S) | ()

S

SS

S )(

( )

( )

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

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 )(

( )

( )

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

5

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

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

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.

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

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

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

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

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

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

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

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.

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

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

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

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.

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

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

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

14

S -> SS | (S) | () is ambiguousProve by lm derivation of ()()()

S=>SS=>SSS=>()SS=>()()S=>()()()

S=>SS=>()S=>()SS=>()()=>()()()

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

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

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

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)

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

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

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

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

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

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.

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

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.

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

21

Another balanced paren. example

Perform leftmost derivation of ()()() by LL(1) B -> (RB | ε; R -> ) | (RR

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

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.

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

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.

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

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

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

25