You are on page 1of 19

DESIGNED BY

PARTHA SARATHI PAUL (ROLL NO 30)


SAURAV CHAKRABORTY (ROLL NO
35)
MANOSHREE BISWAS (ROLL NO 46 )
PARSING

Basically parsing can be defined to be the process of


determining how a string of terminals can be generated by
means of a given grammar. Most parsing methods fall into
one of two methods,
(1)“Top Down”
(2)“Bottom Up”
These terms refer to the order in which nodes in the parse
tree are constructed.
TOP DOWN PARSERS

In top down parsers, construction starts at


the root and proceeds towards the leaves.
In this method the derivation of the parse
tree starts from the start symbol of the
grammar. These are two main approaches
of top down parsing.
(a) Recursive Descent Parsing
(b) Predictive Parsing
Recursive Descent parsing

A recursive descent parsing program


consists of a set of procedures one for each
terminal. Execution begins with the set of
procedures for the start symbol which halts
and announces its success if its procedure
body scans the entire string.
General recursive descent parsing
requires backtracking, i.e. it may
require repeated scans over the input.
Construction of a top-down parse tree

The top-down construction of a parse-tree is done


by starting with the root, labeled with the starting
non-terminal symbol and repeatedly performing
the following two steps:->
° At node (say) ‘N’, labeled with non-terminal ‘N’
(say) ‘A’, select one of the productions for A and
construct children at ‘N ‘,for the symbols in the
production body.

° Find the next node at which a subtree is to be


constructed, typically the leftmost unexpanded
EXAMPLE OF A TOP-DOWN PARSE TREE

Let us consider a grammar for


constructing a top-down parse tree->
G: S-> abA
A-> cd /c /ε
S S S S

a b A a b A a b A a b A

c d c ε
(a) (b) (c) (d)

PARSE TREE
Predictive Parsing
Predictive parsing are special case of recursive descent
parsing where no backtracking is required. Predictive
parsing chooses the correct A production by looking ahead
at the input a fixed number of symbols, usually the next input
symbol.
The class of grammars for which predictive parsers
can be constructed by looking k symbols ahead in the input is
called the LL (k) class. A non-recursive predictive parsers
can be built by maintaining a stack explicitly. The table
driven parser has an input buffer, a stack containing a
sequence of grammar symbols and a parsing table.
Example of Predictive Parsing

Let us consider a grammar


G: E->TE'
E′->+TE′/ε
T->FT′
T′->*FT′/ε
F->(E)/id
Parsing Table
Input Symbol
Non
Terminal id + * ( ) $

E E->TE' E->TE'

E′-> ε
E' E′->+TE′ E′-> ε

T->FT T->FT
T ′ ′
T′->ε T'->*FT
T' ′ T′->ε T′->ε

F F->id F->(E)
Moves made by a predictive parser on input id+id*id

STACK INPUT ACTION


E$ id+id*id$
TE'$ id+id*id$ Output E->TE'
FT'E'$ id+id*id$ Output T->FT'
id+id*id$ Output F->id
idT'E'$

+id*id$
T'E'$ Match id

Output T'->ε
E'$ +id*id$

+TE'$ +id*id$ Output E'->+TE'


TE'$ id*id$ Match +
id*id$ Output T->FT'
FT'E'$

idT'E'$ id*id$ Output F->id


MOVES CONTD….

T'E'$ Match id
*id$

*id$
*FT'E'$ Output T‘->*FT'

id$ Match *
FT'E'$

Output F->id
idT'E'$ id$

Match id
T'E'$ $

E'$
$ Output T'->ε

$ $ Output E'->ε
Bottom Up Parsers

Bottom-up parsing is based on the reverse process to top-


down. A bottom up parse corresponds to the construction of a
parse tree for input string beginning at leaves (the bottom)
and working up towards the top (the root). Instead of
expanding successive non-terminals according to production
rules, a current string or right-sentential form is collapsed
each time until the start non-terminals is reached to predict
the legal next symbol. This can be regarded as a series of
reductions. The approach is also known as shift-reduce
parsing.
EXAMPLE OF A BOTTOM UP
PARSING

Let us consider a grammar for constructing


a bottom up parse tree->
G: E->E+T/T
T->T*F/F
F->(E)/id
PARSE TREE FOR BOTTOM UP APPROACH

id*id F*id T*id

id F

id

T*F T E

F id T * F T

id F id T * F

F
id
id
Shift Reduce Parsing

Shift reduce parsing is a form of bottom


up parsing in which a stack holds the
grammar symbols and an input buffer
holds the rest of the strings to be parsed.
End marker $ is used to mark the
bottom of the stack and also the right end
of the input. The top of the stack is shown
on the right.
EXAMPLE OF A SHIFT REDUCE PARSING

Let us consider a grammar for


constructing a bottom up parse
tree->
G: E->E+T/T
T->T*F/F
F->(E)/id
MOVES MADE BY SHIFT REDUCE PARSER

STACK INPUT ACTION


$ id1*id2$ Shift
$id1 *id2$ Reduce by F->id

$F *id2$ Reduce by T->F

$T *id2 Shift
$T* id2$ Shift
Reduce by F->id
$T*id2 $
Reduce by T->T*F
$T*F $
Reduce by E->T
$T $
$E $ Accept
Actions of a Shift-Reduce Parser

A shift reduce parser can make four possible


actions
(1)Shift
(2) Reduce
(3)Accept
(4) Error
1. Shift This shifts the next input symbol
onto the top of the stack.
2. Reduce The right end of the string to be
reduced must be at the top of the stack
locate the left end of the string within
the stack and decide with what non-
terminal to replace the string.
3.Accept Announce the successful
completion of parsing
4. Error Discover syntax error

You might also like