Lecture 1.9 Top Down Parsing and Lecture 1.10 Recursive Descent Parsing
Lecture 1.9 Top Down Parsing and Lecture 1.10 Recursive Descent Parsing
DISCOVER . LEARN .
EMPOWER
Compilers Design
Course Objective: Students will try to learn
2
Compilers Design
Course Outcomes
CO1 Understand the context and use of a compiler.
3
TOP DOWN PARSING
Introduction
F -> (E) | id id id Ɛ id Ɛ
Recursive descent parsing
S->cAd
A->ab | a Input: cad
S S S
c A d c A d c A d
a b a
First and Follow
• First() is set of terminals that begins strings derived from
• If α=>ɛ then is also in First(ɛ)
• In predictive parsing when we have A-> α|β, if First(α) and
First(β)
* are disjoint sets then we can select appropriate A-
production by looking at the next input
• Follow(A), for any nonterminal A, is set of terminals a that can
appear immediately after A in some sentential form
– If we have S => αAaβ for some αand βthen a is in Follow(A)
• If A can be the rightmost symbol in some sentential form,
then $ is in Follow(A)
*
Computing First
• To compute First(X) for all grammar symbols X,
apply following rules until no more terminals or
ɛ can
* be added to any First set:
1. If X is a terminal then First(X) = {X}.
2. If X is a nonterminal and X->Y1Y2…Yk is a
production for some k>=1, then place a in First(X) if
for some i a is in First(Yi) and ɛ is in all of First(Y1),
…,First(Yi-1)
*
that is Y1…Yi-1 => ɛ. if ɛ is in First(Yj)
for j=1,…,k then add ɛ to First(X).
3. If X-> ɛ is a production then add ɛ to First(X)
• Example!
Computing follow
• To compute First(A) for all nonterminals A,
apply following rules until nothing can be
added to any follow set:
1. Place $ in Follow(S) where S is the start symbol
2. If there is a production A-> αBβ then everything
in First(β) except ɛ is in Follow(B).
3. If there is a production A->B or a production
A->αBβ where First(β) contains ɛ, then
everything in Follow(A) is in Follow(B)
• Example!
LL(1) Grammars
• Predictive parsers are those recursive descent parsers
needing no backtracking
• Grammars for which we can create predictive parsers are
called LL(1)
– The first L means scanning input from left to right
– The second L means leftmost derivation
– And 1 stands for using one input symbol for lookahead
• A grammar G is LL(1) if and only if whenever A-> α|βare two
distinct productions of G, the following conditions hold:
– For no terminal a do αandβ both derive strings beginning with a
– At most one of α or βcan derive empty string
*
– If α=> ɛ then βdoes not derive any string beginning with a
terminal in Follow(A).
Construction of predictive parsing table
• For each production A->α in grammar do the
following:
1. For each terminal a in First(α) add A-> in M[A,a]
2. If ɛ is in First(α), then for each terminal b in
Follow(A) add A-> ɛ to M[A,b]. If ɛ is in First(α)
and $ is in Follow(A), add A-> ɛ to M[A,$] as well
• If after performing the above, there is no
production in M[A,a] then set M[A,a] to error
First Follow
Example
E -> TE’ F {(,id} {+, *, ), $}
T {(,id} {+, ), $}
E’ -> +TE’ | Ɛ {(,id} {), $}
T -> FT’ E
E’ {+,ɛ} {), $}
T’ -> *FT’ | Ɛ {+, ), $}
F -> (E) | id T’ {*,ɛ}
Input Symbol
Non -
terminal id + * ( ) $
E E -> TE’ E -> TE’
S -> iEtSS’ | a
S’ -> eS | Ɛ
E -> b
Input Symbol
Non -
terminal a b e i t $
S S -> a S -> iEtSS’
S’ S’ -> Ɛ S’ -> Ɛ
S’ -> eS
E E -> b
Non-recursive predicting parsing
a + b $
Predictive
parsing output
stack X
Y program
Z
$
Parsing
Table
M
Predictive parsing algorithm
Set ip point to the first symbol of w;
Set X to the top stack symbol;
While (X<>$) { /* stack is not empty */
if (X is a) pop the stack and advance ip;
else if (X is a terminal) error();
else if (M[X,a] is an error entry) error();
else if (M[X,a] = X->Y1Y2..Yk) {
output the production X->Y1Y2..Yk;
pop the stack;
push Yk,…,Y2,Y1 on to the stack with Y1 on top;
}
set X to the top stack symbol;
}
Example
• id+id*id$
For queries
Email: [email protected]
21