Parse Trees and Ambiguity (Dr. Torng) - PowerPoint PPT Presentation

About This Presentation
Title:

Parse Trees and Ambiguity (Dr. Torng)

Description:

Parse Trees and Ambiguity (Dr. Torng) Parse/Derivation Trees Leftmost derivations, rightmost derivations Ambiguous Grammars Examples Arithmetic expressions – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 27
Provided by: EricT162
Learn more at: http://www.cse.msu.edu
Category:
Tags: ambiguity | parse | torng | tree | trees

less

Transcript and Presenter's Notes

Title: Parse Trees and Ambiguity (Dr. Torng)


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 CFLs

1
2
Context-Free Grammars
Parse Trees Leftmost/rightmost derivations Ambiguo
us grammars
2
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

3
4
(No Transcript)
5
(No Transcript)
6
(No Transcript)
7
(No Transcript)
8
Example
  • S gt SS gt SSS gt (S)SS gt ( )SS gt ( )S gt
    ( )
  • The above is a leftmost derivation of the string
    ( ) from the grammar BALG
  • Draw the corresponding parse tree
  • Draw the corresponding rightmost derivation
  • S gt (S) gt (SS) gt (S(S)) gt (S( )) gt ((
    ))
  • The above is a rightmost derivation of the string
    (( )) from the grammar BALG
  • Draw the corresponding parse tree
  • Draw the corresponding leftmost derivation

8
9
Ambiguous Grammars
Examples Arithmetic Expressions If-then-else
statements Inherently ambiguous grammars
9
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 gt SS gt SSS gt SSa gt Saa gt aaa
  • S gt SS gt Sa gt SSa gt Saa gt aaa

10
11
2 Simple Examples
  • Grammar BALG is ambiguous
  • String ( ) in L(BALG) has more than 1 leftmost
    derivation
  • S gt (S) gt ( )
  • S gt (S) gt (SS) gt(S) gt( )
  • Give another leftmost derivation of ( ) from BALG
  • Grammar ABG is NOT ambiguous
  • Consider any string x in aibi i gt 0
  • There is a unique parse tree for x

11
12
Legal Arithmetic Expressions
  • Develop a grammar MATHG (V, S, S, P) for the
    language of legal arithmetic expressions
  • ? 0, 1, , , -, /, (, )
  • Strings in the language include
  • 0
  • 10
  • 1011111100
  • 10(11111100)
  • Strings not in the language include
  • 10
  • 11101
  • )(

12
13
Grammar MATHG1
  • V E, N
  • S 0, 1, , , -, /, (, )
  • S E
  • P
  • N ? N0 N1 0 1
  • E ? N EE EE E/E E-E (E)

13
14
MATHG1 is ambiguous
E ? N EE EE E/E E-E (E)N ? N0 N1
0 1
  • Come up with two distinct leftmost derivations of
    the string 11011
  • E gt EE gt NE gt N1E gt 11E gt 11EE
    gt 11NE gt 110E gt 110N gt 110N1 gt
    11011
  • E gt EE gt EEE gt NEE gt N1EE gt
    11EE gt 11NE gt 110E gt 110N gt
    110N1 gt11011
  • Draw the corresponding parse trees

14
15
(No Transcript)
16
(No Transcript)
17
Implications
  • Two interpretations of string 11011
  • 11(011) 11
  • (110)11 1001
  • What if a line in a program is
  • MSU_Tuition 11011
  • What is MSU_Tuition?
  • Depends on how the expression 11011 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.

17
18
Grammar MATHG2
  • V E, T, F, N
  • S 0, 1, , , -, /, (, )
  • S E
  • P
  • N ? N0 N1 0 1
  • E ? ET E-T T
  • T ? T F T / F F
  • F ? (E) N

18
19
If-Then-Else Statements
  • A grammar ITEG (V, ?, S, P) for the language of
    legal If-Then-Else statements
  • V (S, BOOL)
  • S Dlt85, Dgt50, grade3.5, grade3.0, if, then,
    else
  • S S
  • P
  • BOOL ? Dlt85 Dgt50
  • S ? if BOOL then S else S if BOOL then S
    grade3.5 grade3.0

19
20
ITEG is ambiguous
S ? if BOOL then S grade3.5 grade3.0 if
BOOL then S else S BOOL ? Dlt85 Dgt50
  • Come up with two distinct leftmost derivations of
    the string
  • if Dlt85 then if Dgt50 then grade3.5 else
    grade3.0
  • S gtif BOOL then S else S gt if Dlt85 then S
    else S gt if Dlt85 then if BOOL then S else S gt
    if Dlt85 then if Dgt50 then S else S gt if Dlt85
    then if Dgt50 then grade3.5 else S gt if Dlt85
    then if Dgt50 then grade3.5 else grade3.0
  • S gtif BOOL then S gt if Dlt85 then S gt if
    Dlt85 then if BOOL then S else S gt if Dlt85 then
    if Dgt50 then S else S gt if Dlt85 then if Dgt50
    then grade3.5 else S gt if Dlt85 then if Dgt50
    then grade3.5 else grade3.0
  • Draw the corresponding parse trees

20
21
(No Transcript)
22
Parse Tree Meanings
S
S
if
B
then
S
if
S
B
then
S
else
S
else
if
Dlt85
B
then
S
if
Dlt85
grade3.0
B
then
S
Dgt50
grade3.5
grade3.0
Dgt50
grade3.5
If you receive a 90 on type D points, what is
your grade? By parse tree 1 By parse tree 2
22
23
Implications
  • Two interpretations of string
  • if Dlt85 then if Dgt50 then grade3.5 else
    grade3.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-thens as well as if-then-elses

23
24
Grammar ITEG2
  • V (S, M, U, BOOL)
  • S Dlt85, Dgt50, grade3.5, grade3.0, if, then,
    else
  • S S
  • P
  • BOOL ? Dlt85 Dgt50
  • S ? M U
  • M ? if BOOL then M else M grade3.5 grade3.0
  • U ? if BOOL then S if BOOL then M else U
  • Before ELSE only M appears and M has balance
    IF/ELSE

24
25
Inherently ambiguous CFLs
  • A CFL L is inherently ambiguous iff for all CFGs
    G such that L(G) L, G is ambiguous
  • Examples so far
  • None of the CFLs weve seen so far are
    inherently ambiguous
  • While the CFGs weve seen ambiguous, there do
    exist unambiguous CFGs for those CFLs.
  • There exist inherently ambiguous CFLs
  • Example aibjck ij or jk or ijk
  • Note ijk is unnecessary, but I added it here
    for clarity

25
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 CFLs
  • Thus, the above heuristics do not always work

26
Write a Comment
User Comments (0)
About PowerShow.com