0% found this document useful (0 votes)
22 views21 pages

Lecture 1.9 Top Down Parsing and Lecture 1.10 Recursive Descent Parsing

The document outlines the course objectives and outcomes for a Compiler Design course at APEX Institute of Engineering. It covers fundamental concepts of compiler design, practical exposure to theoretical computer science, and optimization techniques for code generation. Additionally, it discusses parsing techniques, including top-down parsing, recursive descent parsing, and predictive parsing, along with error recovery methods.

Uploaded by

Harun Faridi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views21 pages

Lecture 1.9 Top Down Parsing and Lecture 1.10 Recursive Descent Parsing

The document outlines the course objectives and outcomes for a Compiler Design course at APEX Institute of Engineering. It covers fundamental concepts of compiler design, practical exposure to theoretical computer science, and optimization techniques for code generation. Additionally, it discusses parsing techniques, including top-down parsing, recursive descent parsing, and predictive parsing, along with error recovery methods.

Uploaded by

Harun Faridi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

APEX INSTITUTE OF ENGINEERING

DEPARTMENT OF IBM COMPUTER


SCIENCE & ENGINEERING
Bachelor of Engineering (Computer Science & Engineering)
Compiler Design
CST-350

DISCOVER . LEARN .
EMPOWER
Compilers Design
Course Objective: Students will try to learn

1 Provide student with an understanding of the fundamental


concepts of compiler design as well as the skills required to
create compilers for different perspectives that one could
experience in a career in computer science.
2 Provide the practical exposure to aspects of theoretical computer
science including Languages, Grammars, and Machines.

3 To apply the optimization techniques to have a better code for


code generation.

2
Compilers Design
Course Outcomes
CO1 Understand the context and use of a compiler.

CO2 Understanding application of finite state machine and Syntax Analyzing


using different types of parsing techniques.

CO3 Understand the implementation of back end include intermediate code


generation, run time environment, code generation and register allocation
CO4 Understanding and analyzing the program to minimize the code by using
code optimization techniques

CO5 Understand compilation techniques needed to obtain high performance on


modern computer architectures

3
TOP DOWN PARSING
Introduction

• A Top-down parser tries to create a parse tree from the


root towards the leafs scanning input from left to right
• It can be also viewed as finding a leftmost derivation for
an input string
• Example: id+id*id
E -> TE’ E E E E E E
lm lm lm lm lm
E’ -> +TE’ | Ɛ T E’ T E’ T E’ T E’ T E’
T -> FT’
T’ -> *FT’ | Ɛ F T’ F T’ F T’ F T’ + T E’

F -> (E) | id id id Ɛ id Ɛ
Recursive descent parsing

• Consists of a set of procedures, one for each


nonterminal
• Execution begins with the procedure for start symbol
• A typical procedure for a non-terminal
void A() {
choose an A-production, A->X1X2..Xk
for (i=1 to k) {
if (Xi is a nonterminal
call procedure Xi();
else if (Xi equals the current input symbol a)
advance the input to the next symbol;
else /* an error has occurred */
}
}
Recursive
• General recursivedescent parsing
descent may (cont)
require backtracking
• The previous code needs to be modified to allow
backtracking
• In general form it cant choose an A-production
easily.
• So we need to try all alternatives
• If one failed the input pointer needs to be reset and
another alternative should be tried
• Recursive descent parsers cant be used for left-
recursive grammars
Example

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’

E’ E’ -> +TE’ E’ -> Ɛ E’ -> Ɛ

T T -> FT’ T -> FT’

T’ T’ -> Ɛ T’ -> *FT’ T’ -> Ɛ T’ -> Ɛ

F F -> id F -> (E)


Another example

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$

Matched Stack Input Action


E$ id+id*id$
Error recovery in predictive parsing
• Panic mode
– Place all symbols in Follow(A) into synchronization set for nonterminal
A: skip tokens until an element of Follow(A) is seen and pop A from
stack.
– Add to the synchronization set of lower level construct the symbols
that begin higher level constructs
– Add symbols in First(A) to the synchronization set of nonterminal A
– If a nonterminal can generate the empty string then the production
deriving can be used as a default
– If a terminal on top of the stack cannot be matched, pop the terminal,
issue a message saying that the terminal was insterted
Example
Non -
terminal id +
Input Symbol
* ( ) $
EE -> TE’ E -> TE’ synch synch

E’ E’ -> +TE’ E’ -> Ɛ E’ -> Ɛ


synch T -> FT’ synch synch
TT -> FT’
T’ -> Ɛ T’ -> *FT’ T’ -> Ɛ T’ -> Ɛ
T’

FF -> id synch synch F ->synch


(E) synch

Stack Input Action


E$ )id*+id$ Error, Skip )
E$ id*+id$ id is in First(E)
TE’$ id*+id$
FT’E’$ id*+id$
idT’E’$ id*+id$
T’E’$ *+id$
*FT’E’$ *+id$
FT’E’$ +id$ Error, M[F,+]=synch
T’E’$ +id$ F has been poped
THANK YOU

For queries
Email: [email protected]

21

You might also like