Title: Module 29
1Module 29
- Parse/Derivation Trees
- Leftmost derivations, rightmost derivations
- Ambiguous Grammars
- Examples
- Arithmetic expressions
- If-then-else Statements
- Inherently ambiguous CFLs
2Context-Free Grammars
- Parse Trees
- Leftmost/rightmost derivations
- Ambiguous grammars
3Parse 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
4Parse Tree Example
- Parse tree for string ( )(( )) and grammar BALG
- BALG (V, S, S, P)
- V S, S (, ), S S
- P S --gt SS (S) l
- One derivation of ( )(( ))
- S gt SS gt (S)S gt ( )S gt ( )(S) gt (
)((S)) gt ( )(( )) - Parse tree
5Comments 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
6Leftmost/Rightmost Derivations
- There is more than one derivation of the string (
)(( )). - S gt SS gt (S)S gt( )S gt ( )(S)
- gt ( )((S)) gt ( )(( ))
- S gt SS gt (S)S gt (S)(S) gt ( )(S)
- gt ( )((S)) gt ( )(( ))
- S gt SS gt S(S) gt S((S)) gt S(( ))
- gt (S)(( )) gt( )(( ))
- 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?
7Comments
- 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 gt SS gt (S)S gt( )S gt ( )(S)
- gt ( )((S)) gt ( )(( ))
- S gt SS gt (S)S gt (S)(S) gt ( )(S)
- gt ( )((S)) gt ( )(( ))
- S gt SS gt S(S) gt S((S)) gt S(( ))
- gt (S)(( )) gt( )(( ))
8Example
- 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
9Ambiguous Grammars
- Examples
- Arithmetic Expressions
- If-then-else statements
- Inherently ambiguous grammars
10Ambiguous 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
112 Simple Examples
- Grammar BALG is ambiguous
- String ( ) in L(BALG) has gt1 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
12Legal Arithmetic Expressions
- Develop a grammar MATHG (V, S, S, P) for the
language of legal arithmetic expressions - S 0, 1, , , -, /, (, )
- Strings in the language include
- 0
- 10
- 1011111100
- 10(11111100)
- Strings not in the language include
- 10
- 11101
- )(
13Grammar MATHG1
- V E, N
- S 0, 1, , , -, /, (, )
- S E
- P
- E --gt N EE EE E/E E-E (E)
- N --gt N0 N1 0 1
14MATHG1 is ambiguous
E --gt N EE EE E/E E-E (E)N --gt 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
15Corresponding Parse Trees
- 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
E
E
16Parse Tree Meanings
Note how the parse trees captures the semantic
meaning of string 11011. More specifically,
what number does the first parse tree
represent? What number does the second parse
tree represent?
17Implications
- 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. - In this case, there is an unambiguous grammar for
the language of arithmetic expressions
18If-Then-Else Statements
- A grammar ITEG (V, S, 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
- S --gt if BOOL then S else S if BOOL then S
grade3.5 grade3.0 - BOOL --gt Dlt85 Dgt50
19ITEG is ambiguous
S --gt if BOOL then S grade3.5 grade3.0 if
BOOL then S else S BOOL --gt 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
20Corresponding Parse Trees
- 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
S
S
21Parse 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
22Implications
- 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 Typically, else binds to NEAREST if-then
- In this case, there is an unambiguous grammar for
handling if-thens as well as if-then-elses
23Inherently 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. - Later result
- There exist inherently ambiguous CFLs
- Example aibjck ij or jk or ijk
- Note ijk is unnecessary, but I added it here
for clarity
24Summary
- 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