Module 2 – Syntax Analysis
Bottom Parsing
Parsing Methods
Parsin
g
Top down Bottom up parsing (Shift
parsing reduce)
Back tracking Operator
precedence
Parsing without
backtracking LR parsing
(predictive Parsing)
SLR
LL(1)
CLR
Recursi
ve LALR
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
Parse the input string int id , id ; using a shift-reduce parser.
Operator precedence parsing
• Operator Grammar: A Grammar in which there is no Є in
RHS of any production or no adjacent non terminals is called
operator grammar.
• Example: E EAE | (E) | id
A + | * | -
• Above grammar is not operator grammar because right side
EAE has consecutive non terminals.
• In operator precedence parsing we define following disjoint
Relation Meaning
relations:
a<.b a “yields precedence to” b
a=b a “has the same precedence as” b
a.>b a “takes precedence over” b
Precedence & associativity of operators
Operator Precedence Associative
↑ 1 right
*, / 2 left
+, - 3 left
Steps of operator precedence parsing
1. Find Leading and trailing of non terminal
2. Establish relation
3. Creation of table
4. Parse the string
Rules to establish a relation
1. For a = b, , where is or a single non terminal [e.g : (E)]
2. a <.b [e.g : +T]
3. a .>b [e.g : E+]
4. $ <. Leading (start symbol)
5. Trailing (start symbol) .> $
Leading & Trailing
Leading:- Leading of a non terminal is the first terminal or
operator in production of that non terminal.
Trailing:- Trailing of a non terminal is the last terminal or
operator in production of that non terminal.
Example: EE+T | T
TT*F | F
Fid
Non terminal Leading Trailing
E {+,*,id} {+,*,id}
T {*,id} {*,id}
F {id} {id}
Example: Operator precedence parsing
Step 1: Find Leading & Trailing of NT
E E +T| T
Nonterminal Leading Trailing
T T *F| F
E {+,*,id} {+,*,id} F id
T {*,id} {*,id}
F {id} {id}
Step 2: Establish Relation Step3: Creation of Table
+ * id $
1. a <.b + .
> <. <. .
>
* .
> .
> <. .
>
id .
> .
> .
>
$ <. <. <.
Example: Operator precedence parsing
Step 1: Find Leading & Trailing of NT
E E+ T| T
Nonterminal Leading Trailing
T T* F| F
E {+,*,id} {+,*,id} F id
T {*,id} {*,id}
F {id} {id}
Step2: Establish Relation Step3: Creation of Table
+ * id $
1. a .>b + .
> <. <. .
>
* .
> .
> <. .
>
id .
> .
> .
>
$ <. <. <.
Example: Operator precedence parsing
Step 1: Find Leading & Trailing of NT
E E+ T| T
Nonterminal Leading Trailing
T T* F| F
E {+,*,id} {+,*,id} F id
T {*,id} {*,id}
F {id} {id}
Step 2: Establish Relation Step 3: Creation of Table
+ * id $
1. $<. Leading (start symbol) + .
> <. <. .
>
2. $ <. * .
> .
> <. .
>
3. Trailing (start symbol) .> $ id .
> .
> .
>
$ <. <. <.
Operator precedence function
E$.
1. Create functions fa and ga for each a that is terminal or E+T | T
T T*F | F
F 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* 4. If the constructed graph
has a cycle then no
precedence functions
g+ f+ exist. When there are no
cycles collect the length of
the longest paths from the
f$ g$
groups of fa and gb
respectively.
Operator precedence function
+ * id $
f 2
gid fid
g 1
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$