This action might not be possible to undo. Are you sure you want to continue?
Top Top Down Parser Down Parser Top Top Down Parser Down Parser
A topdown parser starts
with the root of the parse
tree.
The root node is labeled
with the goal symbol of the
grammar
2
Top Top Down Parser Down Parser Top Top Down Parser Down Parser
A topdown parser starts
with the root of the parse
tree.
The root node is labeled
with the goal symbol of the
grammar
3
Top Top Down Parsing Algorithm Down Parsing Algorithm Top Top Down Parsing Algorithm Down Parsing Algorithm
Construct the root node of
the parse tree
Repeat until the fringe of the
parse tree matches input
string
4
Top Top Down Parsing Algorithm Down Parsing Algorithm Top Top Down Parsing Algorithm Down Parsing Algorithm
Construct the root node of
the parse tree
Repeat until the fringe of the
parse tree matches input
string
5
Top Top Down Parsing Down Parsing Top Top Down Parsing Down Parsing
At a node labeled A, select
a production with A on its
lhs
for each symbol on its rhs,
construct the appropriate
child
6
Top Top Down Parsing Down Parsing Top Top Down Parsing Down Parsing
At a node labeled A, select
a production with A on its
lhs
for each symbol on its rhs,
construct the appropriate
child
7
Top Top Down Parsing Down Parsing Top Top Down Parsing Down Parsing
When a terminal symbol is
added to the fringe and it
does not match the fringe,
backtrack
Find the next node to be
expanded
8
Top Top Down Parsing Down Parsing Top Top Down Parsing Down Parsing
When a terminal symbol is
added to the fringe and it
does not match the fringe,
backtrack
Find the next node to be
expanded
9
Top Top Down Parsing Down Parsing Top Top Down Parsing Down Parsing
The key is picking right
production in step 1.
That choice should be
guided by the input string
10
Top Top Down Parsing Down Parsing Top Top Down Parsing Down Parsing
The key is picking right
production in step 1.
That choice should be
guided by the input string
11
Expression Grammar Expression Grammar Expression Grammar Expression Grammar
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
10  ( expr )
12
Top Top Down Parsing Down Parsing Top Top Down Parsing Down Parsing
Let¶s try parsing
x ² 2
*
y
13
P Sentential Form input
 Goal Tx ² 2
*
y
1 expr Tx ² 2
*
y
2 expr + term Tx ² 2
*
y
4 term + term Tx ² 2
*
y
7 factor + term Tx ² 2
*
y
9 <id,x> + term Tx ² 2
*
y
9 <id,x> + term x T² 2
*
y
14
This worked well except that ³²´ does not
match ³+´
P Sentential Form input
 Goal Tx ² 2
*
y
1 expr Tx ² 2
*
y
2 expr + term Tx ² 2
*
y
4 term + term Tx ² 2
*
y
7 factor + term Tx ² 2
*
y
9 <id,x> + term Tx ² 2
*
y
9 <id,x> + term x T² 2
*
y
15
The parser must backtrack to here
P Sentential Form input
 Goal Tx ² 2
*
y
1 expr Tx ² 2
*
y
2 expr + term Tx ² 2
*
y
4 term + term Tx ² 2
*
y
7 factor + term Tx ² 2
*
y
9 <id,x> + term Tx ² 2
*
y
9 <id,x> + term x T² 2
*
y
16
This time the ³²´ and ³²´ matched
P Sentential Form input
 Goal Tx ² 2
*
y
1 expr Tx ² 2
*
y
2 expr ² term Tx ² 2
*
y
4 term ² term Tx ² 2
*
y
7 factor ² term Tx ² 2
*
y
9 <id,x> ² term Tx ² 2
*
y
9 <id,x> ² term x T² 2
*
y
17
We can advance past ³²´ to look at ³2´
P Sentential Form input
 Goal Tx ² 2
*
y
1 expr Tx ² 2
*
y
2 expr ² term Tx ² 2
*
y
4 term ² term Tx ² 2
*
y
7 factor ² term Tx ² 2
*
y
9 <id,x> ² term Tx ² 2
*
y
9 <id,x> ² term x T² 2
*
y
 <id,x> ² term x ² T2
*
y
18
Now, we need to expand ³term´
P Sentential Form input
 Goal Tx ² 2
*
y
1 expr Tx ² 2
*
y
2 expr ² term Tx ² 2
*
y
4 term ² term Tx ² 2
*
y
7 factor ² term Tx ² 2
*
y
9 <id,x> ² term Tx ² 2
*
y
9 <id,x> ² term x T² 2
*
y
 <id,x> ² term x ² T2
*
y
19
P Sentential Form input
 <id,x> ² term x ² T2
*
y
7 <id,x> ² factor x ² T2
*
y
9 <id,x> ² <num,2> x ² T2
*
y
 <id,x> ² <num,2> x ² 2 T
*
y
³2´ matches ³2´
We have more input but no nonterminals
left to expand
20
The expansion terminated
too soon
÷ Need to backtrack
P Sentential Form input
 <id,x> ² term x ² T2
*
y
7 <id,x> ² factor x ² T2
*
y
9 <id,x> ² <num,2> x ² T2
*
y
 <id,x> ² <num,2> x ² 2 T
*
y
21
P Sentential Form input
 <id,x> ² term x ² T2
*
y
5 <id,x> ² term
*
factor x ² T2
*
y
7 <id,x> ² factor
*
factor x ² T2
*
y
8 <id,x> ² <num,2>
*
factor x ² T2
*
y
 <id,x> ² <num,2>
*
factor x ² 2 T
*
y
 <id,x> ² <num,2>
*
factor x ² 2
*
T y
9 <id,x> ² <num,2>
*
<id,y> x ² 2
*
T y
 <id,x> ² <num,2>
*
<id,y> x ² 2
*
y T
Success! We matched and consumed all the input
22
Another Possible Parse Another Possible Parse Another Possible Parse Another Possible Parse
P Sentential Form input
 Goal Tx ² 2
*
y
1 expr Tx ² 2
*
y
2 expr +term Tx ² 2
*
y
2 expr +term +term Tx ² 2
*
y
2 expr +term +term +term Tx ² 2
*
y
2 expr +term +term +term +.... Tx ² 2
*
y
consuming no input!!
Wrong choice of expansion leads to nontermination
Parser must make the right choice
23
Left Recursion Left Recursion Left Recursion Left Recursion
Topdown parsers cannot
handle leftrecursive
grammars
24
Left Recursion Left Recursion Left Recursion Left Recursion
Formally,
A grammar is left recursive
if n A NT such that n a
derivation A ÷* A o, for
some string o (NT T)*
25
Left Recursion Left Recursion Left Recursion Left Recursion
Our expression grammar is
left recursive.
This can lead to non
termination in a topdown
parser
26
Left Recursion Left Recursion Left Recursion Left Recursion
Our expression grammar is
left recursive.
This can lead to non
termination in a topdown
parser
27
Left Recursion Left Recursion Left Recursion Left Recursion
Nontermination is bad in
any part of a compiler!
28
Left Recursion Left Recursion Left Recursion Left Recursion
For a topdown parser, any
recursion must be a right
recursion
We would like to convert
left recursion to right
recursion
29
Left Recursion Left Recursion Left Recursion Left Recursion
For a topdown parser, any
recursion must be a right
recursion
We would like to convert
left recursion to right
recursion
30
Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion
To remove left recursion, we
transform the grammar
31
Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion
Consider a grammar fragment:
A ĺ A o
 ß
where neither o nor ß starts
with A.
32
Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion
We can rewrite this as:
A ĺ ß A'
A' ĺ o A'
 I
where A' is a new nonterminal
33
Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion
We can rewrite this as:
A ĺ ß A'
A' ĺ o A'
 I
where A' is a new nonterminal
34
Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion
A ĺ ß A '
A' ĺ o A'
 I
This accepts the same
language but uses only right
recursion
35
Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion
The expression grammar we
have been using contains two
cases of left recursion
36
Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion
expr ĺ expr + term
 expr ² term
 term
term ĺ term
*
factor
 term ¼ factor
 factor
37
Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion
Applying the transformation yields
expr ĺ term expr'
expr' ĺ + term expr'
 ² term expr'
 I
38
Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion
Applying the transformation yields
term ĺ factor term'
term' ĺ
*
factor term'
 ¼ factor term'
 I
39
Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion
These fragments use only
right recursion
They retain the original left
associativity
A topdown parser will
terminate using them.
40
Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion
These fragments use only
right recursion
They retain the original left
associativity
A topdown parser will
terminate using them.
41
Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion Eliminating Left Recursion
These fragments use only
right recursion
They retain the original left
associativity
A topdown parser will
terminate using them.
42
1 Goal ĺ expr
2 expr ĺ term expr'
3 expr' ĺ + term expr'
4  ² term expr'
5  I
6 term ĺ factor term'
7 term' ĺ
*
factor term'
8  ¼ factor term'
9  I
10 factor ĺ number
11  id
12  ( expr )
43
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
If a top down parser picks
the wrong production, it
may need to backtrack
Alternative is to look ahead
in input and use context to
pick correctly
44
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
If a top down parser picks
the wrong production, it
may leed to backtrack
Alternative is to look ahead
in input and use context to
pick correctly
45
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
How much lookahead is
needed?
In general, an arbitrarily
large amount
46
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
How much lookahead is
needed?
In general, an arbitrarily
large amount
47
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
Fortunately, large classes
of CFGs can be parsed
with limited lookahead
Most programming
languages constructs fall in
those subclasses
48
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
Fortunately, large classes
of CFGs can be parsed
with limited lookahead
Most programming
languages constructs fall in
those subclasses
49
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
Basic Idea:
Given A ĺ o  ß,
the parser should be
able to choose
between o and ß.
50
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
FIRST Sets:
For some rhs o G, define
FIRST(o) as the set of
tokens that appear as the
first symbol in some string
that derives from o.
51
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
That is,
x FIRST(o)
iff o ÷

x K for some K.
52
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
The LL(1) Property
If A ĺ o and A ĺ ß both
appear in the grammar, we
would like
FIRST(o) · FIRST(ß) = ©
53
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
Predictive parsers accept LL(k)
grammars
³lefttoright´ scan of input
leftmost derivation
LL(k)
³k´ tokens of lookahead
54
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
The LL(1) Property
FIRST(o) · FIRST(ß) = ©
allows the parser to make a
correct choice with a
lookahead of exactly one
symbol!
55
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
What about Iproductions?
÷They complicate the
definition of LL(1)
56
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
What about Iproductions?
÷They complicate the
definition of LL(1)
57
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
If A ĺ o and A ĺ ß and
I FIRST(o) , then we need
to ensure that FIRST(ß) is
disjoint from FOLLOW(o),
too
58
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
FOLLOW(o)
is the set of all words in
the grammar that can
legally appear after an o.
59
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
For a nonterminal X,
FOLLOW(X )
is the set of symbols
that might follow the
derivation of X.
60
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
FIRST and FOLLOW
X
FIRST FOLLOW
61
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
Define FIRST
+
(o) as
FIRST(o) FOLLOW(o), if
I FIRST(o)
FIRST(o), otherwise
62
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
Then a grammar is LL(1)
iff A ĺ o and A ĺ ß
implies
FIRST
+
(o) · FIRST
+
(ß) = ©
63
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
Given a grammar that has
the is LL(1) property
we can write a simple
routine to recognize each
lhs
code is simple and fast
64
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
Given a grammar that has
the is LL(1) property
we can write a simple
routine to recognize each
lhs
code is simple and fast
65
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
Given a grammar that has
the is LL(1) property
we can write a simple
routine to recognize each
lhs
code is simple and fast
66
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
Consider
A ĺ ß
1
ß
2
ß
3
which satisfies the LL(1)
property
FIRST
+
(o)·FIRST
+
(ß) = ©
67
/* find an A */
if(token FIRST(ß
1
))
find a ß
1
and return true
else if(token FIRST(ß
2
))
find a ß
2
and return true
if(token FIRST(ß
3
))
find a ß
3
and return true
else error and return false
68
/* find an A */
if(token FIRST(ß
1
))
find a ß
1
and return true
else if(token FIRST(ß
2
))
find a ß
2
and return true
if(token FIRST(ß
3
))
find a ß
3
and return true
else error and return false
69
/* find an A */
if(token FIRST(ß
1
))
find a ß
1
and return true
else if(token FIRST(ß
2
))
find a ß
2
and return true
if(token FIRST(ß
3
))
find a ß
3
and return true
else error and return false
70
/* find an A */
if(token FIRST(ß
1
))
find a ß
1
and return true
else if(token FIRST(ß
2
))
find a ß
2
and return true
if(token FIRST(ß
3
))
find a ß
3
and return true
else error and return false
71
/* find an A */
if(token FIRST(ß
1
))
find a ß
1
and return true
else if(token FIRST(ß
2
))
find a ß
2
and return true
if(token FIRST(ß
3
))
find a ß
3
and return true
else error and return false
72
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
Grammar with the LL(1)
property are called predictive
grammars because the parser
can ³predict´ the correct
expansion at each point in the
parse.
73
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
Parsers that capitalize on
the LL(1) property are
called predictive parsers
One kind of predictive
parser is the recursive
descent parser
74
Predictive Parsing Predictive Parsing Predictive Parsing Predictive Parsing
Parsers that capitalize on
the LL(1) property are
called predictive parsers
One kind of predictive
parser is the recursive
descent parser
75
Recursive Descent Parsing Recursive Descent Parsing Recursive Descent Parsing Recursive Descent Parsing
1 Goal ĺ expr
2 expr ĺ term expr'
3 expr' ĺ + term expr'
4   term expr'
5  I
6 term ĺ factor term'
7 term' ĺ
*
factor term'
8  ¼ factor term'
9  I
10 factor ĺ number
11  id
12  ( expr )
76
Recursive Descent Parsing Recursive Descent Parsing Recursive Descent Parsing Recursive Descent Parsing
This leads to a parser with
six mutually recursive
routines
Goal Term
Expr TPrime
EPrime Factor
77
Recursive Descent Parsing Recursive Descent Parsing Recursive Descent Parsing Recursive Descent Parsing
Each recognizes one non
terminal (NT) or terminal (T)
Goal Term
Expr TPrime
EPrime Factor
78
Recursive Descent Parsing Recursive Descent Parsing Recursive Descent Parsing Recursive Descent Parsing
The term descent refers to
the direction in which the
parse tree is built.
Here are some of these
routines written as functions
79
Recursive Descent Parsing Recursive Descent Parsing Recursive Descent Parsing Recursive Descent Parsing
The term descent refers to
the direction in which the
parse tree is built.
Here are some of these
routines written as functions
This action might not be possible to undo. Are you sure you want to continue?
We've moved you to where you read on your other device.
Get the full title to continue reading from where you left off, or restart the preview.