Top-Down and Bottom-Up Parsing
Top-Down and Bottom-Up Parsing
Top-Down and Bottom-Up Parsing
Bottom Up Parsing
Top Down Parsing
Things to know:
Top down parsing is constructing a parse tree for the input starting from the root and
create nodes of the parse tree in preorder(depth first).
A general form of top down parsing is the recursive descent parsing.
A recursive descent parsing is a top down parsing technique that execute a set of
recursive procedures to process the input, that involves backtracking(means
scanning the input repeatedly).
Backtracking is time consuming and therefore, inefficient. Thats why a special case
of top down parsing was developed, called predictive parsing, where no
backtracking is required.
A dilemma can occur if there is a left recursive grammar. Even with backtracking, you
can find the parser to go into an infinite loop.
There are two types of recursion, left recursive and right recursive, based on its
name, a left recursive grammar build trees that grows down to the left, while right
recursive is vice versa.
Top-down Parse tree of Grammar G(Where input=id):
G= E -> T E
E-> +T E | E E E E
T-> F T
T E T E T E
T-> *F T |
F-> (E) | id
F T F T
id
An example of a simple production with left recursive grammar
Consider the grammar: expr -> expr + term
This is an example of a left recursive grammar.
Whenever we call expr , the same procedure is called out, and the parser will loop forever.
By carefully writing a grammar, one can eliminate left recursion from it.
expr -> expr + term, can be written as
Recursive-Descent Parsing
Predictive Parsing
Recursive-Descent
Recursive -Descent Parsing Parsing
A recursive-descent parsing program consists of a set of procedures, one for each
nonterminal. Execution begins with the procedure for the start symbol, which halts
and announces success if its procedure body scans the entire input string.
General recursive-descent may require backtracking; that is, it may require repeated
scans over the input.
Consider the grammar with input string cad:
S -> c A d
A -> a b | a
S S S
c A d c A d c A d
a b a
c a d
Back
Predictive Parsing-a parsing technique that uses a lookahead symbol to
determine if the current input arguments matches the lookahead symbol.
Construction of
First and
Predictive
Follow
Parsing Tables
LL(1)
Error Recovery
Grammars
First and
Follow
ANSWERS(FIRST):
1) FIRST(E) = FIRST(T) = FIRST(F) = { ( , id }
FIRST (E) = { + , }
First and
Follow
Rules in computing FOLLOW ( X) where X is a nonterminal
1) If X is a part of a production and is succeeded by a terminal, for example: A -> Xa; then
Follow(X) = { a }
2) If X is the start symbol for a grammar, for ex:
X -> AB
A -> a
B -> b; then add $ to FOLLOW (X); FOLLOW(X)= { $ }
3) If X is a part of a production and followed by another non terminal, get the FIRST of that
succeeding nonterminal.
ex: A -> XD
D -> aB ; then FOLLOW(X)= FIRST(D) = { a }; and if FIRST(D) contains
(ex: D->aB | ), then everything in FOLLOW(D) is in FOLLOW(X).
4) If X is the last symbol of a production, ex: S -> abX, then
FOLLOW(X)= FOLLOW(S)
First and
Follow
Nontermi
nals
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)
Nontermi
nals
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)
STACK INPUT ACTION
$E id + id * id $
$ET id + id * id $ E->TE
$ETF id + id * id $ T->FT
$ETid id + id * id $ F-> id
$ET + id * id $
$E + id * id $ T->
$ET + + id * id $ E->+TE
$ET id * id $
$ETF id * id $ T->FT
$ETid id * id $ F-> id
$ET * id $
$ETF* * id $ T->*FT
$ETF id $
$ETid id $ F-> id
$ET $
$E $ T->
$ $ Back
E->
LL(1)
Grammars
There remains a question of what should be done when a parsing table has
multiple-defined entries.
One solution is to transform the grammar by eliminating all left recursion and then
left factoring when possible, but not all grammars can yield an LL(1) grammar
at all.
The main difficulty in using a predictive parsing is in writing a grammar for the
source language such that a predictive parser can be constructed from the
grammar.
To alleviate some of the difficulty, one can use a operator precedence, or even
better the LR parser, that provides both the benefits of predictive parsing and
operator precedence automatically.
BACK
Error Recovery
BACK
Bottom Up Parsing
A general style of bottom up parsing will be introduced, it is
the shift-reduce parsing.
Shift reduce parsing works based on its name, Shift and
Reduce, so whenever the stack holds symbols that
cannot be reduced anymore, we shift another input, and
when it matches, we reduce.
Bottom Up Parsing
STACK INPUT ACTION
1) $ id1 + id2 * id3 $ Shift
2) $id1 + id2 * id3 $ Reduce by E
3) $E + id2 * id3 $ ->id
4) $E + id2 * id3 $ Shift
5) $E + id2 Shift
* id3 $ Reduce by E->id
6) $E + E * id3 $
7) $E + E * Shift
8) $E + E * id3 id3 $ Shift
9) $E + E * E $ Reduce by E->id
10)$E + E $ Reduce by E-> E * E
$ Reduce by E-> E+ E
11)$E ACCEPT
$