2. Derivation
• A derivation is the sequence of productions
S … … … …
• A derivation can be drawn as a tree• A derivation can be drawn as a tree
– Start symbol is the tree’s root
– For a production X Y1 … … Yn add children
Y1 … … Yn to node X
X
Y1 … … Yn
28-Jan-15 2CS 346 Lecture 5
3. Example: Left-most derivation
• Grammar: E E + E | E * E | ( E ) | id
• String : id*id+id E
E
E + E E + EE + E E + E
E * E + E
id * E + E E * E id
id * id + E
id * id + id id id Parse Tree
28-Jan-15 3CS 346 Lecture 5
4. Parse Tree
• A parse tree has
– Terminals at the leaves
– Non-terminal at the interior nodes
• An in-order traversal of the leaves is the
original input
• The parse tree shows the association of
operations, the input string does not
28-Jan-15 4CS 346 Lecture 5
5. Right-most derivation
• The example is a left-
most derivation
– At each step, replace the
left most non terminals
E
E + E
E * E + E
• There is a equivalent
notion of right-most
derivation
E * E + E
id * E + E
id * id + E
id * id + id
28-Jan-15 5CS 346 Lecture 5
6. Parse Tree Derivations
• Note that the left-most and right-most
derivation have the same parse tree
• Every parse tree has the left-most and right-• Every parse tree has the left-most and right-
most derivations
28-Jan-15 6CS 346 Lecture 5
7. Ambiguity
• Grammar: E E + E | E * E | ( E ) | id
• String : id*id+id
• This string has two parse trees
E E
E + E E * E
E * E id id E + E
id id id id
28-Jan-15 7CS 346 Lecture 5
8. Ambiguous Grammar
• A grammar is ambiguous if it has more than
one parse tree for some string
– Equivalently, there is more than one right-most or
left-most derivation for some stringleft-most derivation for some string
• Ambiguity means that some programs are ill-
defined
28-Jan-15 8CS 346 Lecture 5
9. How to handle Ambiguous Grammar
• Several ways to handle
• Rewrite the grammar unambiguously
E E’+ E| E’
E’ id * E’| id| ( E ) * E | ( E )
• Enforce precedence of * over +
28-Jan-15 9CS 346 Lecture 5
10. • Grammar: E E’+ E | E’
E’ id * E’ | id | ( E ) * E’ | ( E )
• String : id*id+id
E
Enforce precedence of * over + by
divide the productions in two classes;
one handle + and one handles *; so one
non terminals for each operators
How to handle Ambiguous Grammar
E
E’ + E
id * E’ E’
id id
non terminals for each operators
E E’+ E E’ + E’ + E
E’+E’+E’+E E’+… +E’
Handles +
E’ id * E’ id*id*E’
id*id*id*E’ id*…*id
Handles *28-Jan-15 10CS 346 Lecture 5
11. How it works
• The grammar has two separate groups of
productions
• All the pluses (+) must be generated before any of
the times (*); so time(s) (*) are nested more
deeply inside the parse tree; pluses (+) aredeeply inside the parse tree; pluses (+) are
generated in the outermost levels and times (*)
are generated inside the pluses (+).
• So the grammar enforces that the time (*) has
higher precedence than the plus (+)
28-Jan-15 11CS 346 Lecture 5
12. Ambiguity
• Grammar: E E + E | E * E | ( E ) | id
• String : id*id+id
• This string has two parse trees
E E
E + E E * E
E * E id id E + E
id id id id
28-Jan-15 12CS 346 Lecture 5
13. Ambiguity
• Grammar: E E + E | E * E | ( E ) | id
• String : id*id+id
• Now, this string has only one parse trees
E
E
E + E
E * E id
id id
E’ + E
id * E’ E’
id id
28-Jan-15 13CS 346 Lecture 5
14. Example
• Another Expression
E if E then E
| if E then E else E
| OTHER
• The expression: if E1 then if E2 then E3 else E4 has two separate
parse treesparse trees
if if
E1 if E4 E1 if
E2 E3 E2 E3 E4
28-Jan-15 14CS 346 Lecture 5
15. How to handle the Ambiguity
• The property that we want is: else matches the
closest unmatched then
• Can be resolved as:
E MIF| UIFE MIF| UIF
MIF if E then MIF else MIF
| OTHER
UIF if E then E
| if E MIF else UIF
28-Jan-15 15CS 346 Lecture 5
16. Associativity of Operators
• Let a Grammar : S S + S | S – S | 0 – 9
• Input: 9 – 5 + 2
S S
S + S S - SS + S S - S
S - S 2 9 S + S
9 5 5 2
(9-5)+2 = 6 9-(5+2) = 2
28-Jan-15 16CS 346 Lecture 5
17. How to handle
• When an operand like 5 has operators to its left and
right, conventions are needed for deciding which
operator applies to that operand.
• Rule: operator + is associates to the left. Arithmetic• Rule: operator + is associates to the left. Arithmetic
operators (+, -, *, /) are left associative.
• Assignment operator (“=“) in C is right associative.
28-Jan-15 17CS 346 Lecture 5
18. How to handle
• Left Associative
S S + d | S – d | d
d [0-9]
• Input: 9 – 5 + 2
S
• Right Associative
r l = r | l
l [a-z]
• Input: a = b = c
r
S + d
S – d 2
d 5
9
Left-most derivation
l = r
a l = r
b l
c
Right-most derivation
28-Jan-15 18CS 346 Lecture 5
19. Push Down Automata
• The language for balanced parenthesis, i.e.
{( i ) i | i ≥ 0}, CFG productions are
S (S)|S
S ɛ
• The situation can be handled if the DFA is• The situation can be handled if the DFA is
augmented with memory
– Memory implemented as a stack
– Such an automata is called Push Down Automata
(PDA)
28-Jan-15 19CS 346 Lecture 5