Parsing Methods - Compiler Design

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 46

Module : 3

Parsing Methods
Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking LR parsing
(predictive Parsing)
SLR
LL(1)
CLR
Recursiv
e LALR
descent
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 it to
appropriate left hand side non terminal is known as handle pruning.
EE+E
EE*E String: id1+id2*id3
Eid
Right sentential form Handle Production
Rightmost Derivation
id1+id2*id3 id1 Eid
E
E+id2*id3 id2 Eid
E+E
E+E*id3 id3 Eid
E+E*E
E+E*id3 E+E*E E*E EE*E
E+E E+E EE+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: Stack Input Buffer Action
EE+T | T $ id+id*id$ Shift
TT*F | F $id +id*id$ Reduce Fid
Fid $F +id*id$ Reduce TF
String: id+id*id $T +id*id$ Reduce ET
$E +id*id$ Shift
$E+ id*id$ Shift
$E+id *id$ Reduce Fid
$E+F *id$ Reduce TF
$E+T *id$ Shift
$E+T* id$ Shift
$E+T*id $ Reduce Fid
$E+T*F $ Reduce TT*F
$E+T $ Reduce EE+T
$E $ Accept
Viable Prefix
• The set of prefixes of right sentential forms that can appear on the stack of a
shift-reduce parser are called viable prefixes.
Parsing Methods
Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking LR parsing
(predictive Parsing)
SLR
LL(1)
CLR
Recursiv
e LALR
descent
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 relations:
Relation Meaning
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
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: EE+T | T
TT*F | F
Fid
Non terminal Leading Trailing
E {+,*,id} {+,*,id}
T {*,id} {*,id}
F {id} {id}
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) .> $
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 .
> .
> .
>
$ <. <. <.
Example: Operator precedence parsing
Step 4: Parse the string using precedence table
Assign precedence operator between terminals
String: id+id*id + * id $
+ .
> <. <. .
>
$ id+id*id $ * .
> .
> <. .
>
$ <. id+id*id$ id .
> .
> .
>
$ < id > +id*id$
. . $ < .
< .
<.

$ <. id .> + <. id*id$


$ <. id .> + <. id .> *id$
$ <. id .> + <. id .> *<. id$
$ <. id .> + <. id .> *<. id .> $
Example: Operator precedence parsing
Step 4: Parse the string using precedence table
1. Scan the input string until first .> is encountered.
2. Scan backward until <. is encountered.
3. The handle is string between <. and .>
$ <. Id .> + <. Id .> * <. Id .> $ Handle id is obtained between <. and .>
Reduce this by Fid
$ F + <. Id .> * <. Id .> $ Handle id is obtained between <. and .>
Reduce this by Fid
$ F + F * <. Id .> $ Handle id is obtained between <. and .>
Reduce this by Fid
$F+F*F$ Perform appropriate reductions of all nonterminals.
$E+T*F$ Remove all non terminals.
$ + * $ Place relation between operators
$ <. + <. * >$ The * operator is surrounded by <. and .>. This
indicates * becomes handle so reduce by TT*F.
$ <. + >$ + becomes handle. Hence reduce by EE+T.

$ $ Parsing Done
Operator precedence function
Algorithm for constructing precedence functions
1. Create functions and for each that is terminal or .
2. Partition the symbols in as many as groups possible, in such a way that and are in the same
group if .
3. Create a directed graph whose nodes are in the groups, next for each symbols do:
a) if , place an edge from the group of to the group of
b) if , place an edge from the group of to the group of
4. If the constructed graph has a cycle then no precedence functions exist. When there are no
cycles collect the length of the longest paths from the groups of and respectively.
Operator precedence function
1. Create functions fa and ga for each a that is terminal or $. E 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$
Parsing Methods
Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking LR parsing
(predictive Parsing)
SLR
LL(1)
CLR
Recursiv
e LALR
descent
LR parser
• LR parsing is most efficient method of bottom up parsing which can be used to
parse large class of context free grammar.
• The technique is called LR(k) parsing:
1. The “L” is for left to right scanning of input symbol,
2. The “R” for constructing right most derivation in reverse,
3. The “k” for the number of input symbols of look ahead that are used in making parsing
decision. a + b $ INPUT

X
Y LR parsing
program OUTPUT
Z
$

Parsing Table
Action Goto
Parsing Methods
Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking LR parsing
(predictive parsing)
SLR
LL(1)
CLR
Recursiv
e LALR
descent
Computation of closure & go to function
X Xb
Closure(I):
.
X X b
Goto(I,X)
.
X X b
Steps to construct SLR parser
1. Construct Canonical set of LR(0) items
2. Construct SLR parsing table
3. Parse the input string
Example: SLR(1)- simple LR
S  AA
S’ S AA . 5
A  aA | b S.
𝑰𝟏 A a .
𝑰𝟐 𝐺𝑜𝑡𝑜(𝐼2, 𝐴) A
𝑰𝟎 𝐺𝑜𝑡𝑜(𝐼 0,𝑆) ) S A . ,2 𝑎 )
A. aA 3

, 𝐴 𝐼
𝐼( 0 AA. aA 𝑜(
A. b
S’.S
𝑜 𝑡
S. AA 𝑡𝑜 𝐺
𝑜
A. aA 𝐺
A. b A b.
𝐺𝑜 𝐺𝑜𝑡𝑜(𝐼2,𝑏) ) 4

,3 𝐴
A. b
𝑡𝑜 (𝑜 𝐼 A aA . LR(0) item set
(𝐼 𝑡
3 6
Augmented
grammar 0 , A a . 𝐺𝑜
𝑎) A
𝐺𝑜𝑡𝑜(𝐼 0,𝑏) A. aA 𝐺𝑜𝑡𝑜(𝐼 3 ,𝑎) A a.
A
A b. 4 A. b 𝐺𝑜 A. aA 3

𝑡𝑜 A. b
(𝐼
3,
𝑏)
A b. 4
Rules to construct SLR parsing table
1. Construct , the collection of sets of LR(0) items for
2. Stateis constructed from . The parsing actions for state are determined as
follow :
a) If is in and GOTO , then set to “shift j”. Here a must be terminal.
b) If is in , then set to “reduce A ” for all a in ; here A may not be S’.
c) If is in , then set action to “accept”.
3. The goto transitions for state i are constructed for all non terminals A using the
4. All entries not defined by rules 2 and 3 are made error.
Example: SLR(1)- simple LR
S’ S AA . 5

S.
𝑰𝟏 A a .
𝑰𝟐 𝐺𝑜𝑡𝑜(𝐼2, 𝐴) A
𝑰𝟎 𝐺𝑜𝑡𝑜(𝐼 0,𝑆) ) S A . ,2 𝑎 )
A. aA 3

S’. ,0 𝐴 A ( 𝐼 A. b Action Go to


S (𝑜 𝐼 A. aA 𝑡𝑜
S. AA 𝑡 𝐺𝑜 Item
set
a b $ S A
𝑜
A. aA 𝐺
A. b A b.
𝐺𝑜 𝐺𝑜𝑡𝑜(𝐼2,𝑏) ) 4
0 S3 S4 1 2
,3 𝐴
(𝑜 𝐼
1 Accept
A. b
𝑡𝑜 A aA .
(𝐼 𝑡
3 6
2 S3 S4 5
0 , A a . 𝐺𝑜
𝑎) A 3 S3 S4 6
𝐺𝑜𝑡𝑜(𝐼 0,𝑏) A. aA 𝐺𝑜𝑡𝑜(𝐼 3 ,𝑎) A a. 4 R3 R3 R3
A
A b. 4 A. b 𝐺𝑜 A. aA 3
5 R1
𝑡𝑜 A. b 6 R2 R2 R2
(𝐼
S  AA 3,
𝑏)
A  aA | b A b. 4
Parsing Methods
Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking LR parsing
(predictive Parsing)
SLR
LL(1)
CLR
Recursiv
e LALR
descent
How to calculate look ahead?
How to calculate look ahead?
SCC S’  . S , $
C cC | d A  . X ,

Closure(I)
S’.S,$
$
S.CC, c|d
S  . C C , $
C.cC,c|d A  . X ,
C.d,
Example: CLR(1)- canonical LR
5 S AA. , 6
, 𝐴 ) A aA.,$ 9
S’ S.,
𝑰𝟏 $ A a.A,$
𝑡𝑜 ( 𝐼6 6

𝑎)
𝑰 𝟐 𝐺𝑜𝑡𝑜(𝐼2, 𝐴) A. aA,$ 𝐺𝑜
$
𝐼 6 , 𝑎 ) A a.A,$
𝑡 𝑜(

𝐼2,
𝑰𝟎 𝐺𝑜𝑡𝑜(𝐼 0,𝑆) ) 𝐺𝑜 A. aA,$
𝐴 S A.A,
A. b,
𝐺 𝑜 𝑡𝑜 ( 𝐼 6 , A

𝑡𝑜 (
S’.S, 0𝐼 , $A.aA, $
𝑏) A. b,
$
$ ( b. ,S
𝐺𝑜
𝑡𝑜
7 7
S.AA,$ $A. b, A b. ,
A.aA, a|b 𝐺𝑜 $ 𝐺𝑜𝑡𝑜(𝐼2,𝑏) ) $
,3 𝐴 8
A.b, a|b
(𝑜 𝐼 A aA.,a|b
𝑡
𝐺𝑜

3
LR(1) item set
Augmented
Aa.A, a|b 𝐺
𝑜
𝑡𝑜 (

grammar 3

𝐺𝑜𝑡𝑜(𝐼 0,𝑏) A.aA ,a|b 𝐺𝑜𝑡𝑜(𝐼 3 ,𝑎)A a.A , a|b


𝐼0,

A. b, a|b 𝐺
𝑜 A.aA , a|b
𝑎)

A b., a|b 4
𝑡𝑜 A.b , a|b
(𝐼
S  AA 3,
𝑏)
A  aA | b 4
A b., a|b
Example: CLR(1)- canonical LR
5 S AA. , 6
, 𝐴 ) A aA.,$ 9
S’ S.,
𝑰𝟏 $ A a.A,$
𝑡𝑜 ( 𝐼6 6

𝑎)
𝑰 𝟐 𝐺𝑜𝑡𝑜(𝐼2, 𝐴) A. aA,$ 𝐺𝑜
$
𝐼 6 , 𝑎 ) A a.A,$
𝑡 𝑜(

𝐼2,
𝑰𝟎 𝐺𝑜𝑡𝑜(𝐼 0,𝑆) ) 𝐺 𝑜 A. aA,$
𝐴 S A.A,
A. b,
𝐺 𝑜 𝑡𝑜 ( 𝐼 6 , A

𝑡𝑜 (
S’.S, 0𝐼 , $A.aA, $
𝑏) A. b,
$
$ ( b. ,S
𝐺𝑜
𝑡𝑜
7 7
S.AA,$ $A. b, A
A.aA, a|b 𝐺𝑜 ) Item Action Go to
$ 𝐺𝑜𝑡𝑜(𝐼2,𝑏) b. ,S
,3 𝐴 8
set
a b $ S A
A.b, a|b
(𝑜 𝐼 A aA.,a|b
𝑡
𝐺𝑜

3
0 S3 S4 1 2
Aa.A, a|b 𝐺
𝑜 1 Accept
𝑡𝑜 (

3
2 S6 S7 5
𝐺𝑜𝑡𝑜(𝐼 0,𝑏) A.aA ,a|b 𝐺𝑜𝑡𝑜(𝐼 3 ,𝑎)A a.A , a|b
𝐼0,

3 S3 S4 8
A. b, a|b 𝐺 4 R3 R3
𝑜 A.aA , a|b
𝑎)

A b., a|b 4
𝑡𝑜 A.b , a|b 5 R1
(𝐼 6 S6 S7 9
S  AA 3 ,𝑏 7 R3
A  aA | b 4
) A b., a|b 8 R2 R2
9 R2
Parsing Methods
Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking LR parsing
(predictive Parsing)
SLR
LL(1)
CLR
Recursiv
e LALR
descent
Example: LALR(1)- look ahead LR
, 𝐴)
5 S AA. , 6 A aA.,$ 9
S’ S.,
𝑰𝟏 $ A a.A,$ ( 𝐼 6
𝐺𝑜 𝑡𝑜
6

𝑎)
$
𝑰𝟐 𝐺𝑜𝑡𝑜(𝐼2, 𝐴) ( 𝐼 6 , 𝑎 ) A a.A,$
𝑡 𝑜

𝐼2,
A. aA,$
)
𝑰 𝟎 𝐺𝑜𝑡𝑜(𝐼 0,𝑆) 𝐴 S A.A, 𝐺𝑜 A. aA,$
A. b,
𝐺 𝑜 𝑡𝑜 ( 𝐼 6 , A b. , A. b,

𝑡𝑜 (
S’.S, ,0 $ $
𝑏)
$ ( 𝐼 A.aA, $
$

𝐺𝑜
𝑡𝑜
7 7
S.AA,$ $A. b, A b. ,
A.aA, a|b 𝐺𝑜 $ 𝐺𝑜𝑡𝑜(𝐼2,𝑏) ) $
,3 𝐴 8
36 CLR
A.b, a|b
(𝑜 𝐼 A aA.,a|b Aa.A, a|b|$
𝑡
𝐺𝑜

𝐺𝑜 A.aA , a|b|$
𝑡𝑜 (

Aa.A, a|b 3
A. b, a|b|$
𝐺𝑜𝑡𝑜(𝐼 0,𝑏) A.aA ,a|b 𝐺𝑜𝑡𝑜(𝐼 3 ,𝑎)A a.A , a|b
𝐼0,

A. b, a|b 𝐺
𝑜 A.aA , a|b
𝑎)

47
A b., a|b 4
𝑡𝑜 A.b , a|b A b., a|b|$
(𝐼
S  AA 3,
𝑏) 89

A  aA | b 4
A b., a|b A aA.,a|b|$
Example: LALR(1)- look ahead LR

Item Action Go to
set
a b $ S A
Item Action Go to
0 S3 S4 1 2 set a b $ S A
1 Accept
2 S6 S7 5 0 S36 S47 1 2
3 S3 S4 8 1 Accept
4 R3 R3 2 S36 S47 5
36 S36 S47 89
5 R1 47 R3 R3 R3
6 S6 S7 9 5 R1
7 R3 89 R2 R2 R2
8 R2 R2
9 R2
CLR Parsing Table LALR Parsing Table

You might also like