Lecture 12
Lecture 12
Construction
Sohail Aslam
Lecture 12
Parse Trees
Leftmost G
derivation
E
E op E
x – E op E
2 * y
2
Parse Trees
G
E op E
x – E op E
evaluation order
x–(2*y)
2 * y
3
Parse Trees
G Rightmost
derivation
E
E op E
E op E * y
evaluation order
x – 2 (x – 2 ) * y
4
Precedence
These two derivations point
out a problem with the
grammar
It has no notion of
precedence, or implied
order of evaluation
5
Precedence
These two derivations point
out a problem with the
grammar
It has no notion of
precedence, or implied
order of evaluation
6
Precedence
To add precedence
Create a non-terminal for
each level of precedence
Isolate corresponding part
of grammar
7
Precedence
To add precedence
Create a non-terminal for
each level of precedence
Isolate corresponding part
of grammar
8
Precedence
To add precedence
Create a non-terminal for
each level of precedence
Isolate corresponding part
of grammar
9
Precedence
To add precedence
Force parser to recognize
high precedence
subexpressions first.
10
Precedence
For algebraic expressions
Multiplication and division,
first. (level one)
Subtraction and addition,
next (level two)
11
Precedence
For algebraic expressions
Multiplication and division,
first. (level one)
Subtraction and addition,
next (level two)
12
Precedence
For algebraic expressions
Multiplication and division,
first. (level one)
Subtraction and addition,
next (level two)
13
1 Goal → expr
2 expr → expr + term
3 | expr – term
4 | term
5 term → term factor
6 | term / factor
7 | factor
8 factor → number
9 | Id 14
1 Goal → expr
2 expr → expr + term
level 3 | expr – term
two
4 | term
5 term → term factor
level 6 | term / factor
one 7 | factor
8 factor → number
9 | Id
15
Precedence
This grammar is larger
Takes more rewriting to
reach some of the terminal
symbols
But it encodes expected
precedence
16
Precedence
This grammar is larger
Takes more rewriting to
reach some of the terminal
symbols
But it encodes expected
precedence
17
Precedence
This grammar is larger
Takes more rewriting to
reach some of the terminal
symbols
But it encodes expected
precedence
18
Precedence
Produces same parse tree
under leftmost and rightmost
derivations
Let’s see how it parses
x – 2 *y
19
Precedence
Produces same parse tree
under leftmost and rightmost
derivations
Let’s see how it parses
x – 2 *y
20
Precedence
Rule Sentential Form
- Goal
1 expr
3 expr – term
5 expr – term factor
9 expr – term <id,y>
7 expr – factor
<id,y> 21
Derivations & Precedence
Rule Sentential Form
8 expr – <num,2>
<id,y>
4 term – <num,2>
<id,y>
7 factor – <num,2>
The rightmost
<id,y> derivation
22
Parse Trees
G evaluation order
x–(2*y)
E
E T
–
T T F
*
F T <id,y
>
<id,x <num,2
23
> >
Parse Trees
G evaluation order
x–(2*y)
E
E T
–
T T F
*
F T <id,y
>
<id,x <num,2
24
> >
Precedence
Both leftmost and rightmost
derivations give the same
expression
Because the grammar
directly encodes the desired
precedence.
25
Precedence
Both leftmost and rightmost
derivations give the same
expression
Because the grammar
directly encodes the desired
precedence.
26
Ambiguous Grammars
If a grammar has more
than one leftmost
derivation for a single
sentential form, the
grammar is ambiguous
27
Ambiguous Grammars
If a grammar has more
than one rightmost
derivation for a single
sentential form, the
grammar is ambiguous
28
Ambiguous Grammars
The leftmost and rightmost
derivations for a sentential
form may differ, even in an
unambiguous grammar
Let’s consider the classic
if-then-else example
29
Ambiguous Grammars
if-then-else problem
Stmt → if Expr then Stmt
| if Expr then Stmt else
Stmt
| … other stmts ….
30
Ambiguous Grammars
The following sentential
form has two derivations:
31
Ambiguity if
S1
32
Ambiguity if
Production 2, E1 then
then
Production 1:
if
if E1 then
if E2 then S1
E2 then else
else S2
S1 S2
33
Ambiguity
if if
if S2 if
S1 S1 S2
34
Removing Ambiguity
Must rewrite grammar to
avoid generating the
problem
Match each else to
innermost umatched if
35
Removing Ambiguity
Must rewrite grammar to
avoid generating the
problem
Match each else to
innermost umatched if
36
Removing Ambiguity
1. Stmt → If E then Stmt
2. | If E then WithElse else Stmt
3. | Assignment
4. → If E then WithElse else
WithElse WithElse
5. | Assignment
37
Removing Ambiguity
Let derive the following using
the rewritten grammar:
38
Stmt
E1
if Expr then Withelse else Stmt
E2 A1 A2
39
Context-Free Grammars
We have been using the
term context-free without
explaining why such rules
are in fact “free of context”.
40
Context-Free Grammars
The simple reason is that
nonterminals appear by
themselves to the left of the
arrow in context-free rules:
A →
41
Context-Free Grammars
The rule A → says that
A may be replaced by
anywhere, regardless of
where A occurs.
42
Context-Free Grammars
On the other hand, we
could define a context as
pair of strings , , such
that a rule would apply only
if occurs before and
occurs after the
nonterminal A.
43
Context-Sensitive Grammars
We would write this as
A →
Such a rule in which
≠ is called a context-
sensitive grammar rule
44
Context-Sensitive Grammars
We would write this as
A →
Such a rule in which
≠ is called a context-
sensitive grammar rule
45
Parsing
Techniques
Parsing Techniques
Top-down parsers
Start at the root of the parse
tree and grow towards
leaves.
Pick a production and try to
match the input
47
Parsing Techniques
Top-down parsers
Start at the root of the parse
tree and grow towards
leaves.ck a production and
try to match the input
48
Parsing Techniques
Top-down parsers
Start at the root of the parse
tree and grow towards
leaves.
Pick a production and try to
match the input
49
Parsing Techniques
Top-down parsers
Bad “pick” may need to
backtrack
Some grammars are
backtrack-free.
50
Parsing Techniques
Top-down parsers
Bad “pick” may need to
backtrack
Some grammars are
backtrack-free.
51
Parsing Techniques
Bottom-up parsers
Start at the leaves and grow
toward root
As input is consumed,
encode possibilities in an
internal state.
52
Parsing Techniques
Bottom-up parsers
Start at the leaves and grow
toward root
As input is consumed,
encode possibilities in an
internal state.
53
Parsing Techniques
Bottom-up parsers
Start at the leaves and grow
toward root
As input is consumed,
encode possibilities in an
internal state.
54