Module 2(b) - Syntax Analysis_Bottom Up Parsing
Module 2(b) - Syntax Analysis_Bottom Up Parsing
Bottom Parsing
Parsing Methods
Parsin
g
descen
t
Handle & Handle pruning
• Handle: A “handle” of a string is a substring of the string that
matches the right side of a production, and whose reduction to
the non terminal of the production is one step along the reverse
of rightmost derivation.
• Handle pruning: The process of discovering a handle and
reducing
EE+E it to appropriate left hand side non terminal is known as
handle
EE*E pruning.
String: id1+id2*id3
Eid
Right sentential form Handle Production
Rightmost Derivation
id1+id2*id3 id1 Eid
E
E+id2*id3 id2 Eid
E+E
E+E*id3 id3 Eid
E+E*E
E+E*id3 E+E*E E*E EE*E
E+E E+E EE+E
E+id2*id3
E
id1+id2*id3
Shift reduce parser
• The shift reduce parser performs following basic operations:
1. Shift: Moving of the symbols from input buffer onto the stack,
this action is called shift.
2. Reduce: If handle appears on the top of the stack then
reduction of it by appropriate rule is done. This action is
called reduce action.
3. Accept: If stack contains start symbol only and input buffer is
empty at the same time then that action is called accept.
4. Error: A situation in which parser cannot either shift or
reduce the symbols, it cannot even perform accept action
then it is called error action.
Example: Shift reduce parser
Grammar:
EE+T | T
TT*F | F
Fid
String: id+id*id
Consider the following grammar-
S→TL;
T → int | float
L → L , id | id
f+ f* fid f$
g+ g* gid g$
Operator precedence function
• Partition
. the symbols in as many as groups possible, in such a
way that fa and gb are in the same group if a = b.
+ * id $
+ .
> <. <. .
>
gid fid
* .
> .
> <. .
>
id .
> .
> .
>
$ <. <. <.
f* g*
g+ f+
f$ g$
Operator precedence function
3. if a <· b, place an edge from the group of g b to the group of fa
if a ·> b, place an edge from the group of f a to the group of gb
g
+ * id $
+ .
> <. <. .
>
gid fid
f * .
> .
> <. .
>
id .
> .
> .
>
f* g* $ <. <. <.
g+ f+
f+ .> g+ f+ g+
f* .> g+ f* g+
fid .> g+ fid g+
f$ g$ f$ <. g+ f$ g+
Operator precedence function
3. if a <· b, place an edge from the group of g b to the group of fa
if a ·> b, place an edge from the group of f a to the group of gb
g
+ * id $
+ .
> <. <. .
>
gid fid
f * .
> .
> <. .
>
id .
> .
> .
>
f* g* $ <. <. <.
g+ f+
f+ <. g* f+ g*
f* .> g* f* g*
fid .> g* fid g*
f$ g$ f$ <. g* f$ g*
Operator precedence function
3. if a <· b, place an edge from the group of g b to the group of fa
if a ·> b, place an edge from the group of f a to the group of gb
g
+ * id $
+ .
> <. <. .
>
gid fid
f * .
> .
> <. .
>
id .
> .
> .
>
f* g* $ <. <. <.
g+ f+
f+ <. gid f+ gid
f* <. gid f* gid
f$ <. gid f$ gid
f$ g$
Operator precedence function
3.if a <· b, place an edge from the group of g b to the group of fa
if a ·> b, place an edge from the group of f a to the group of gb
g
+ * id $
+ .
> <. <. .
>
gid fid
f * .
> .
> <. .
>
id .
> .
> .
>
f* g* $ <. <. <.
g+ f+
f+ <. g$ f+ g$
f* <. g$ f* g$
fid <. g$ fid g$
f$ g$
Operator precedence function
+ * id $
f 2
gid fid g
f* g*
g+ f+
f$ g$
Operator precedence function
+ * id $
f 2 4
gid fid
g 1
f* g*
g+ f+
f$ g$
Operator precedence function
+ * id $
f 2 4
gid fid
g 1 3
f* g*
g+ f+
f$ g$
Operator precedence function
+ * id $
f 2 4 4
gid fid
g 1 3
f* g*
g+ f+
f$ g$
Operator precedence function
+ * id $
f 2 4 4
gid fid
g 1 3 5
f* g*
g+ f+
f$ g$
Operator precedence function
+ * id $
f 2 4 4 0
gid fid
g 1 3 5 0
f* g*
g+ f+
f$ g$