1 parse trees and ambiguity (dr. torng) parse/derivation trees –leftmost derivations, rightmost...

26
1 Parse Trees and Ambiguity (Dr. Torng) • Parse/Derivation Trees – Leftmost derivations, rightmost derivations • Ambiguous Grammars – Examples • Arithmetic expressions • If-then-else Statements – Inherently ambiguous CFL’s

Upload: brittany-clementine-thomas

Post on 13-Jan-2016

232 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

1

Parse Trees and Ambiguity(Dr. Torng)

• Parse/Derivation Trees– Leftmost derivations, rightmost derivations

• Ambiguous Grammars– Examples

• Arithmetic expressions

• If-then-else Statements

– Inherently ambiguous CFL’s

Page 2: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

2

Context-Free Grammars

Parse Trees

Leftmost/rightmost derivations

Ambiguous grammars

Page 3: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

3

Parse Tree

• Parse/derivation trees are structured derivations– The structure graphically illustrates semantic

information about the string

• Formalization of concept we encountered in regular languages unit– Note, what we saw before were not exactly

parse trees as we define them now, but they were close

Page 4: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

4

Parse Tree Example

• Parse tree for string ( )(( )) and grammar BALG– BALG = (V, Σ, S, P)

• V = {S}, Σ = {(, )}, S = S

• P = S → SS | (S) |

– One derivation of ( )(( ))

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

– Parse tree

S

S S

SS( ( ))

S( )

Page 5: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

5

Comments about Example *

• Syntax: – draw a unique arrow from each

variable to each character that is a direct child of that variable

• A line instead of an arrow is ok

– The derived string can be read in a left to right traversal of the leaves

• Semantics– The tree graphically illustrates the

nesting structure of the string of parentheses

S

S S

SS( ( ))

S( )

Page 6: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

6

Leftmost/Rightmost Derivations

• There is more than one derivation of the string ( )(( )).

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

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

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

• Leftmost derivation– Leftmost variable is always expanded– Which one of the above is leftmost?

• Rightmost derivation– Rightmost variable is always expanded– Which one of the above is rightmost?

S

S S

SS( ( ))

S( )

Page 7: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

7

Comments

• Fix a string and a grammar– Any derivation corresponds to a

unique parse tree

– Any parse tree can correspond to many different derivations

• Example– The one parse tree corresponds to all

three derivations

• Unique mappings– For any parse tree, there is a unique

leftmost/rightmost derivation that it corresponds to

S

S S

SS( ( ))

S( )

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

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

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

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

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

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

Page 8: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

8

Example *

• S ==> SS ==> SSS ==> (S)SS ==> ( )SS ==> ( )S ==> ( )– The above is a leftmost derivation of the string ( ) from the

grammar BALG

– Draw the corresponding parse tree

– Draw the corresponding rightmost derivation

• S ==> (S) ==> (SS) ==> (S(S)) ==> (S( )) ==> (( ))– The above is a rightmost derivation of the string (( )) from the

grammar BALG

– Draw the corresponding parse tree

– Draw the corresponding leftmost derivation

Page 9: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

9

Ambiguous Grammars

Examples:

Arithmetic Expressions

If-then-else statements

Inherently ambiguous grammars

Page 10: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

10

Ambiguous Grammars

• A grammar G is ambiguous if there exists a string x in L(G) with two or more distinct parse trees– (2 or more distinct leftmost/rightmost derivations)

• Example– Grammar AG is ambiguous

• String aaa in L(AG) has 2 rightmost derivations– S ==> SS ==> SSS ==> SSa ==> Saa ==> aaa

– S ==> SS ==> Sa ==> SSa ==> Saa ==> aaa

Page 11: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

11

2 Simple Examples

• Grammar BALG is ambiguous– String ( ) in L(BALG) has more than 1 leftmost

derivation• S ==> (S) ==> ( )• S ==> (S) ==> (SS) ==>(S) ==>( )• Give another leftmost derivation of ( ) from BALG

• Grammar ABG is NOT ambiguous– Consider any string x in {aibi | i > 0}

• There is a unique parse tree for x

Page 12: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

12

Legal Arithmetic Expressions

• Develop a grammar MATHG = (V, Σ, S, P) for the language of legal arithmetic expressions– = {0, 1, +, *, -, /, (, )}

– Strings in the language include• 0

• 10

• 10*11111+100

• 10*(11111+100)

– Strings not in the language include• 10+

• 11++101

• )(

Page 13: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

13

Grammar MATHG1

• V = {E, N}• Σ = {0, 1, +, *, -, /, (, )}• S = E• P:

– N → N0 | N1 | 0 | 1

– E → N | E+E | E*E | E/E | E-E | (E)

Page 14: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

14

MATHG1 is ambiguous

• Come up with two distinct leftmost derivations of the string 11+0*11– E ==> E+E ==> N+E ==> N1+E ==> 11+E ==>

11+E*E ==> 11+N*E ==> 11+0*E ==> 11+0*N ==> 11+0*N1 ==> 11+0*11

– E ==> E*E ==> E+E*E ==> N+E*E ==> N1+E*E ==> 11+E*E ==> 11+N*E ==> 11+0*E ==> 11+0*N ==> 11+0*N1 ==>11+0*11

• Draw the corresponding parse trees

E → N | E+E | E*E | E/E | E-E | (E)N → N0 | N1 | 0 | 1

Page 15: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

15

Corresponding Parse Trees

• E ==> E+E ==> N+E ==> N1+E ==> 11+E ==> 11+E*E ==> 11+N*E ==> 11+0*E ==> 11+0*N ==> 11+0*N1 ==> 11+0*11

• E ==> E*E ==> E+E*E ==> N+E*E ==> N1+E*E ==> 11+E*E ==> 11+N*E ==> 11+0*E ==> 11+0*N ==> 11+0*N1 ==>11+0*11

E

E E+

E E*N

N

1

1 N

0

N

N 1

1

E

N

N

1

1

* EE

N

0

+ EE

N

N 1

1

Page 16: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

16

Parse Tree Meanings

E

E E+

E E*N

N

1

1 N

0

N

N 1

1

E

N

N

1

1

* EE

N

0

+ EE

N

N 1

1

Note how the parse trees captures the semantic meaning of string 11+0*11.More specifically, what number does the first parse tree represent?

What number does the second parse tree represent?

Page 17: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

17

Implications

• Two interpretations of string 11+0*11– 11+(0*11) = 11– (11+0)*11 = 1001

• What if a line in a program is– MSU_Tuition = 11+0*11;– What is MSU_Tuition?

• Depends on how the expression 11+0*11 is parsed.• This is not good.• Ambiguity in grammars is undesirable, particularly if the grammar is

used to develop a compiler for a programming language like C++.

Page 18: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

18

Grammar MATHG2 *

• V = {E, T, F, N}• Σ = {0, 1, +, *, -, /, (, )}• S = E• P:

– N → N0 | N1 | 0 | 1

– E → E+T | E-T | T

– T → T * F | T / F | F

– F → (E) | N

Page 19: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

19

If-Then-Else Statements

• A grammar ITEG = (V, , S, P) for the language of legal If-Then-Else statements– V = (S, BOOL)

– Σ = {D<85, D>50, grade=3.5, grade=3.0, if, then, else}

– S = S

– P: • BOOL → D<85 | D>50

• S → if BOOL then S else S | if BOOL then S |grade=3.5 | grade=3.0

Page 20: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

20

ITEG is ambiguous

• Come up with two distinct leftmost derivations of the string – if D<85 then if D>50 then grade=3.5 else grade=3.0– S ==>if BOOL then S else S ==> if D<85 then S else S ==> if D<85 then

if BOOL then S else S ==> if D<85 then if D>50 then S else S ==> if D<85 then if D>50 then grade=3.5 else S ==> if D<85 then if D>50 then grade=3.5 else grade=3.0

– S ==>if BOOL then S ==> if D<85 then S ==> if D<85 then if BOOL then S else S ==> if D<85 then if D>50 then S else S ==> if D<85 then if D>50 then grade=3.5 else S ==> if D<85 then if D>50 then grade=3.5 else grade=3.0

• Draw the corresponding parse trees

S → if BOOL then S |grade=3.5 | grade=3.0 | if BOOL then S else S BOOL → D<85 | D>50

Page 21: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

21

Corresponding Parse Trees• S ==>if BOOL then S else S ==> if D<85

then S else S ==> if D<85 then if BOOL then S else S ==> if D<85 then if D>50 then S else S ==> if D<85 then if D>50 then grade=3.5 else S ==> if D<85 then if D>50 then grade=3.5 else grade=3.0

• S ==>if BOOL then S ==> if D<85 then S ==> if D<85 then if BOOL then S else S ==> if D<85 then if D>50 then S else S ==> if D<85 then if D>50 then grade=3.5 else S ==> if D<85 then if D>50 then grade=3.5 else grade=3.0

S S

if B then S else S

D<85 if B then S grade=3.0

D>50 grade=3.5

if B then S

D<85 else Sif B then S

D>50 grade=3.5 grade=3.0

Page 22: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

22

Parse Tree Meanings

S

if B then S else S

if B then SD<85

D>50 grade=3.5

grade=3.0

S

if B then S

else Sif B then SD<85

D>50 grade=3.5 grade=3.0

If you receive a 90 on type D points, what is your grade?By parse tree 1

By parse tree 2

Page 23: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

23

Implications

• Two interpretations of string – if D<85 then if D>50 then grade=3.5 else grade=3.0

– Issue is which if-then does the last ELSE attach to?

– This phenomenon is known as the “dangling else”

– Answer: Parse tree 2.

– C language: An ELSE belongs to the last preceding IF for which there is no ELSE already.

• In this case, there is an unambiguous grammar for handling if-then’s as well as if-then-else’s

Page 24: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

24

Grammar ITEG2 *

– V = (S, M, U, BOOL)– Σ = {D<85, D>50, grade=3.5, grade=3.0, if, then,

else}– S = S– P:

• BOOL → D<85 | D>50

• S → M | U

• M → if BOOL then M else M | grade=3.5 | grade=3.0

• U → if BOOL then S | if BOOL then M else U

• Before ELSE only M appears and M has balance IF/ELSE

Page 25: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

25

Inherently ambiguous CFL’s

• A CFL L is inherently ambiguous iff for all CFG’s G such that L(G) = L, G is ambiguous

• Examples so far– None of the CFL’s we’ve seen so far are inherently

ambiguous– While the CFG’s we’ve seen ambiguous, there do exist

unambiguous CFG’s for those CFL’s.

• There exist inherently ambiguous CFL’s– Example: {aibjck | i=j or j=k or i=j=k}

• Note i=j=k is unnecessary, but I added it here for clarity

Page 26: 1 Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees –Leftmost derivations, rightmost derivations Ambiguous Grammars –Examples Arithmetic expressions

26

Summary

• Parse trees illustrate “semantic” information about strings

• Ambiguous grammars are undesirable– This means there are multiple parse trees for some string

– These strings can be interpreted in multiple ways

• There are some heuristics people use for taking an ambiguous grammar and making it unambiguous, but this is not the focus of this course

• There are some inherently ambiguous CFL’s– Thus, the above heuristics do not always work