Professional Documents
Culture Documents
CD Unit - 2
CD Unit - 2
CD Unit - 2
2 Syntax Analysis
Syllabus
Roll· c?( Parser - Grammars - Context:fi'ee gramrnars - Writing a grammar Top Down Parsing -
Gt'neral Srrategies - Recursive Descent Parser Predictive Parser-LL(!) - Parser-Shift Reduce
Parser-LR Parser- LR (0)/tem Construction of SLR Parsing Table - Introduction to LALR Parser -
Error Handling and Recove,:v in Syntax Analyzer-YACC tool - Design of a syntax Analyzer f or a
Sample language.
Contents
2. 1 Introduction to Syntax Analysis
2.2 Role of Parser ..... . . .... . . . . ... Dec.-06, 07, · . · · · · · · · · · · · Marks 6
2.10 Bottom Up Parsing ... . . . . . . .... . .. . . May-04,05, 06,0 7, 10, 11, 16,
. . . .. . .. . ... . . .. . . Dec.-04,06, · · · · · · · · · · · · · Marks 8
2. 13 SLR Parsing .. . . . ............. May-04, 11, 13, 14, 15, 17, 19, Dec.-05,
(2 - 1)
r
Syn tax Ana/ys,s
Com pile r Des ign
2-2 -- - - - - - - ----
2. 14 Canonical LR Parsing -06 07 10, 18,
. . . . . . ... . . . May ' ' . . . . . . Ma rks 13
oec.-10, . . ... .. .
..........··· May-19, Ma rks 16
2. 15 LALR Parsing oec.-06,07, 13, 18,
••· · · · · · · · · · · · · · · ·
... . •·M ark s 2
2. 16 Comparison of LR Parsers . . .. oec.-10, · · · · · · · · ·
.. • · · · · · · · · · . . . . . . . . Ma rks 15
2. 17 Handlin g Ambiguous Gra mm ar
. . . . . . . . . . - · May-12'18' · · ..
2. 18 Err or Handling and Recovery
in _ . _ . . . . . . . Ma rks 8
Syntax Analyz'3r . . ... ... . ... . ... .. May-06,07, 11,
·
2. 19 YA CC
Definition of parser : A parsing or syntax analysis is a process which takes the input
string w and produces either a parse tree (syntactic structure) or generates the syntactic
errors.
®
TECHNICAL PUBL/CA TIONS - an up-thrust for knowledge
~ omp1fe1 Das,gn ? 4
, ti • 1,nrf'ln~ n1>4°
rllhrn hmH•d nn wh k h w 1, )'•·t
• l .a s lly the mo~I crud L,1 i:4~ \lt' 1~
, 11)1,
ll . litf,•r<'nl ,,ppro,h'l1t•~1 t u p ,r,
W will d,~cl1Kt4 l , 1 1, , ► • .
p;)t'SC' tri. .'t' hw thl' ~ivcn input. e . , nr1-1 in~ nlgnri lhirw c orwt •r "
1
1 1 111 1
Tl,p-dl"IWn and hoth1m-up . /\nd we w ill ~ l\l L Y I 11 It>
these a ppronch cs.
Symbol table
Review Question
The parser demands for tokens from lexical analyzer and if t~e tokens do not satisfy
the grammatic al rules of program.m ing language then the syntactica l errors get raised.
3. Semantic e.m>rs
· Semantic errors are those errors which get detected during semantic analysis phase.
Typical errors in this phase are :
• Incompati ble types of operands.
• Undeclare d variables.
• Not matching of actual argument s with formal argument s.
For example
int a(10), b ;
a= b;
It generates a semantic error.
Similarly
Another example
if(f== l)
{
return 1; .
printf("%d" ,f);
}
This will generate an error unreachab le code. This is because after return statement
the control goes back to main and hence after return whatever code we give that
becomes unreachab le. Thus the code printf is beyond the scope of the compiler.
Solution :
State ➔ Type List Terminator
Type ➔ int I float
List ➔ List,id
List ➔ id
Terminator ➔
I
int
~
Li~d
List ' id
I
id
Fig. 2.4.1 Parse tree for derivation of int Id, Id, Id;
--------~~------= --- - - - - - - - -
TECHNICAL PUBLICA TIONs® - an up-thrust for knowledge
~~~ ~1!! ___ __ __ __ __ __2_-_a__ __ __
__ __ __ __s_y_nt_ax Anat .
>_!!)Pife
1 r Design
~
. "d "d "d• can be defin ed by mean s of abov e Cont ext
Hence int 1 , 1 , 1 , Free Gram mar
·
Following rules to be followed whil e writi ng a CFG .
1) A single non-terminal shou ld be at L.H.S.
Z) The rule shou ld be alwa ys in the form of LH.S. ➔ R.H.
S. whe re R.H.S. may be the
combination of non- term inal and term inal symb ols.
3) The NULL deriv ation can be speci fied as NT ➔ E.
4) One of the _non- term inals shou ld be start symb
ol and conv entio nally we should
write the rules for this non- term inal.
xample 2.4.3 Consider the following CFG grammar,
S
aABe ➔
nA➔ Abe I b
B ➔ d
s
a/ /\e
A;1\B\
I b C d
b
Revie w Ques tions
1 - Defin e CFC.
2· Explain contex t free ~ram
- - - .
mers w ith examples.
AU : May- 16, Marks 10
. for "'""'r'" :
F ~ F + E I E ,.. E I id
E
E
/ ~ ff i
E
id
·~
E • E E
~
+
•
E
E
Id
id id id Id
a
/I~ S b S
/I~
a S b S
I~ j I~
a S b S
b S a S
I I £ E
I I f f
Parse tree 2
Parse tree 1
®
TECHNICAL PUBLICA T/ONS - an 11p-thn1st for knowledge
~C~om~p1~ 2 - 10 Synta x Ana/y .
·1e~r~De~si~gn~ - - - -- - - - - - -= -~-- - - - -- - - - - -- - ~
The right most deriv ation for abab is -
s
s
aSb S
/ 1 ~S
a S b
I a
/1
S b
~S - aSb aSb s
I
E
I
E
a Sb a Sb
a Sb ab
aba b
This is a lang uage cont ainin g all the strin gs with equa
l num ber of a's and b's.
Exam ple 2.5.2 Create a parse trees for the following string : string · id
+ id -:- id. Check
whether the string is ambiguous or not.
AU : May- 18 . Marks 7
Solu tion : The gram mar for deriv ing the strin gs will
be
E➔E +E
E ➔ E-E
E ➔ id
The pars e trees will be
id id id id
Fig. 2.5.2
Revi ew Ques tions
th on the
As we know, ere are two parsing techniques, these parsing techniques work
ing principle.
folJoW
th e input string from left to right and identifi.es that the
l. The parser scans
derivation is leftmost or rightmost.
z. The parser makes use of production rules for choosing the approp riate derivation .
g the
The different parsing techniques use different approaches in selectin
appropriate rules for derivation. And finally a parse tree is constructed.
then such
When the parse tree can be constructed from root and expand ed to leaves
tree can
type of parser is called Top-do wn parser. The name itself tells us that the parse
be built from top to bottom .
of parser
When the parse tree can be constructed from leaves to root, then such type
is called as bottom -up parser. Thus the parse tree is built in bottom
up manne r.
Types of parser
SLR LALR LR
parser parser parser
2.
Parse tree can be built from root to leaves.
,.
- - =-=-=-=-=-=-=-~=-=-=-==---:
Wh en the bo tto m up pa
f h il, I$ ki--" dfk icn t po rsi rse r h an
ng
t: h':'
iqu:~ am big uo us gra mm ar con dle s \
\',1rim1-. pn ,bl cm s th " t ~ flic ts occ ur in
do wn tt'Chnlquc J~ om btg ur_ urm g pa rse table.
u,ty left
n._"C'u~ion .
- - - - - -- - - - - -
11 i~ npplicahlc to ~mall cla ss of lan gu ages. It is app lic abl e to a bro ad
cla ss of
lan gu age s.
- - -- - - ----
V,n ious p.,rsi.ng t1..'Ch niq ues Va rio us pa rsi ng tec hn iqu
are es are
\ ) R1._~ urs ive desL-ent par ser 1) Sh ift red uc e 2) Op era tor pre ced ence
2) Pret.iictive par ser . 3) LR par ser .
Review Question
M•M•UMi'MMtitu
I ll Top-down Parsing AU : Ma y-0 4, 07 , 08 , 15
, De c.- 05 , 07 , 16 , Marks
10
• 1n top -d ov.rn pa rsi ng the
pa rse tre e is ge ne rat ed fro
to lea ve s). The de riv ati on m top to bo tto m (i.e. fro
ter mi na m root
tes wh en the req uir ed in pu
t str ing ter mi na tes.
• Th e lef tm ost d eri va tio n ma
tch es thi s req uir em en t.
',' • The ma in tas k in top -d
ow n pa rsi ng is to fin d the
or de r to pr od uc e the co ap pr op ria te pr od uc tio n
rre ct inp ut str ing . W e wi rule in
top-do,..,rn pa rsi ng wi th the ll un de rst an d the pr ocess of
he lp of so me ex am ple .
• Con sid er a gra mm ar,
S ➔ xPz
p ➔ yw ly
• Cons ide r the inp ut str ing
xy z is as sh ow n be low .
\ x \ y \ z j
i
Inp ut bu ffe r
• N~w we wi ll co ns ~ruct
t~e ~a rse tre e for ab ov e
stn ng. And for this de n va gr am ma r de riv ing the giv
ho n we wi ll ma ke us e en input
of top do wn ap pr oa ch . .
Step 1 : (Refe r Fig. 2.7.1 (a))
The firs t leftm ost lea f of the
pa rse tre e ma tches wi th
th~ fi rst inp ut sy mb ol. He
nc e we wi ll ad va nc e the
po int er. The next lea f no de inp ut
is P. W e ha ve to ex pand
no de P. After ex pa ns ion the
we ge t the no de y wh
ma tch es w ith the inp ut sy ich
mb ol y .
F.1g.
2.7.1 (a )
1) Backtracking
S ~ xPz
P ~ ywly
I'
)
◄
c~~~
~ompile ~~?.__ __ _ _ _ _ _ _ _ _ _2_-_1_4_ _ _ _ _ _ _ _ _ __ __s_yn
r Design _ t_ax
_ Ana/ys· ~
Then
Fig. 2.7.2
lf for a non-terminal there are multip le produc tion rules beginn ing with the
same
input symbol then to get the correct derivat ion we need to try all these alterna
tives.
Secondly, in backtracking we need to move some levels upwar d in order to check
the
possibilities. This increases lot of overhe ad in implem entatio n •of parsing . And hence
it
become s necessa ry to e~at e the backtra cking by modify ing the gramm ar.
Exampl e 2.7.1 Construct parse tree for the input string w = cad using top down parser.
S ➔ cAd
A ➔ ab/a
AU : Dec.-16 . Marks 6
Solutio n :
Step 2 :
Fig. 2.7.3
Apply rule A , ab
Step 3 : Apply ru le A , a
Thus the required string LS
parsed .
27
- - - - - - - -- -T~E~C;H~N~IC
~A~L~PU
~B: L~IC
:A: .T
: :l=
O~N~
s ®®- - -~ F~lg!.~ -~~-•~ - - - - - - - - - -
- an up-thrust for knowledge
cm,p1er Design
2- 15
Syntax Analysis
2) Left recursion
Here ~ means derivin g the input in one or more steps. The A can be non-terminal
and a denotes some input string. If left recursio n is present in the gramm
ar then it
creates serious problem s. Because of left recursio n the top-dow n parser can enter
in
infinite loop. This is as shown in the Fig. 2.7.5.
of
Thus expans ion of A causes further expans ion of A only and due to generat ion
ma1or
A, Aa, Aa.a, Aaaa, ... , the input pointer will not be advanc ed. This causes
problem in top-do wn parsing .and therefo re elimina tion of left recursion is a must.
A ➔~ } ... (2.7.1)
... (2.7.2)
A' ➔ E
a a a
Input string
Fig. 2.7.6
Compiler Design 2 - 17 Syntax Analysis
T ➔ FT'
F ➔ (E) I id ⇒ F ➔ (E) I id
Example 2.7.2 Consider the following grammar
A ➔ ABdlAal a
B ➔ Be l b
remove left recursion.
Solution : Consider the rule,
A ➔ ABdlAala
A--A ~la
♦ ♦ ♦ ♦
A A a 13
A ➔ ~A' A ➔ aA'
A' ➔ aA' ⇒ A' ➔ BdA'
A' ➔ E A' ➔ E
For A ➔ Aala
l lll
A A a~
This left recursion ca n be e1iminated as -
A ➔ ~A' A ➔ aA'
A' ➔ aA ' ⇒ A' ➔ aA'
AI ➔ £ A' ➔ E
r Compiler Design
To su m m ar iz e
.:::.yntax Ana1yais
~ aA'
A -4 ABd I Aa Ia :::::> A B dA '\a A '
A' ~
A' ~ £
B -4 S ei b
-4 A a
\A
W e m ap th is gram
mar w1'th th e ru le A 1-1·
B -4 B b e\
t i i i
A A a ~
use the rule A ➔ ~A', a.A' A '~ £ Then· w et
A➔ ' ·
eg
B ➔ b B'
B ➔ eB '
B' ➔ E
To summarize, the gr
ammar withou·t left · will be
recursio n
A ➔ a A'
A' ➔ B d A 'l aA
'
A' ➔ E
B ➔ b B'
B' ➔ e B'
B' ➔ E
3) Left factoring
If .the grammar is
left factored then it
factoring is used w becomes su it ab le
hen it is _not clear for th e us e. Basic
th at w hi ally lett
expand the non-term ch of th e tw o al te rn
inal. By left factorin at iv es is used ~o
which the decision g w e m ay be ab le
can be deferred until to re -w ri te th e prod
en ou gh of th e in pu uction ll\
choice. t is se en to make
the right
In general if
A ➔ a~ 1l a~ 2
is a production th
en it is not possible
rule or second . In su for us to ta ke a d
. .
ch a situation the above
gr am m ar ec1s1on w h e ther to choose fi~1
A ➔ a A' b ft f
can e 1e actored as
S' ➔ eS If
E ➔ b
rs.
be ca us e of fo llo wi ng ru les the left rec ursio n occu
In th e gi ve n gr am m ar
A ➔ Ac an d A ➔ Aa d
lef t re cu rsi on we wi ll sli gh tly ch an ge the ru le :
To eli mi na te th is
A . ➔ f31A' If32A'
A ➔ A a I I A a2 I '31 I f32
⇒ . A' ➔ a1 A' la 2A '
A' ➔
A' ➔ E
-
th e re m ov al of lef t re cu rsi ve gr am ma r we ge t
Af ter
A ➔ bd A 'lc A '
A' ➔ cA ' I ad A '
Rule 1 : E ~ E + TI T
.J,
u.J,
.J,
A A a
Then ·w e get -
E ➔ TE'
Then we get,
T ➔ FT'
T' ➔ *FT'
T' ➔ E
mar ize, we get follo wing set of
Rule 3 : F ➔ (E) I id is not a left recursive . To sum
rules after eliminating left recursion.
E ➔ TE'
E' ➔ +TE 'IE
T ➔ FT'
T' ➔ *FT 'I E
F ➔ (E) I id
Example 2.7.5 Do left factoring in the following grammar -
A➔_ aAB laAl a
B ➔ bBlb
Solution : If the rule is A ➔ a ~1la~2I ... is
a prod ucti on then the gram mar need s to be
left factored ..
Con side r
A ➔ aAB!aAla
LJ LJ
j, j, j, j, ,l, j,
a ~1 a ~2 a ~3
to A A'
The rule is if A ➔ a~1 l a~2I ... then convert it ➔a and A' ➔ t3 1It3 2 I
Hen ce we get
A ➔ a A'
A' ➔ AB/ Aj E
___ _
TE=C
- -- - - - - - -~ :L~P
~H:N:IC~A IC:A~Tl~O=N:s®®_-_n _ _ __ _ _ _ _ __
:B~L~
;U ___
8
up-thrust for knowledge
compiler Design 2 - 21 Syntax Analysis
Similarly
B ~ b Bib
gives B ~ bB'
j, j, j, j,
out B' 4 Blt::
A a 131 a 132
To summarize, the grammar with left factor operation will be -
A ➔ a A'
A' ➔ ABIAI E
B ➔ b B'
B' ➔ B IE
Example 2.7.6 Write down the algorithm to eliminate left-recursion and left-factoring and
apply both to the following grammar.
E ➔ E+T I E-T IT
T ➔ a l b KE) AU : May-15, Marks 8
Solution : Rule to eliminate left recursion is
~'
can be written as -
A ➔ J31A'j ... jJ3mA'
A' ➔ a 1A'j ... janA'IE
Rule to do left factoring is ·
If A ➔ a J3 1 la J3 21 ... is a production then
A ➔ aA'
Then we get
E ➔ TE'
E' ➔ +TE'I-TE' IE
Rule 2 : T ➔ a I b I (E) does not contain left recursion
4) Ambiguity
The ambiguous grammar is not d esirable in top-down parsing. Hence we need to
remove the ambiguity from the g ra mma r if it is present.
®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
2 - 22
~
S11n,_
Compiler Des,gn - : _ _ _ - - - - - - - - - - - . : ' ' '1.6;, A,.~
----------- •141~
For example :
E ➔ E + E r E * E I id
is an ambiguous grammar. We will design the parse tree for id+ id* id as follows.
Removal of ambiguity
For removing the ambiguity we will apply one rule : If the grammar has left
associative operator (such as +, -, *, /) then induce the left recursion and if the
grammar has right associative operator (exponentia l operator) then induce the right
recursion.
The unambiguo us grammar is
E ➔ E+T
E ➔ T
T ➔ T*F
T ➔ F
F ➔ id
Note on e thing that the grammar is unambiguo us but it is le ft recursive al".
elimination o f su ch left recursion is again a must .
Example 2 .7 .7 Explain ambiguous grammar G : E ➔ E + E I E * E I (E) I - E I id fat ,-
sentence id + id* id. IJ•Mrl4Wf#d111
SoJution :
• Ambiguous g rammar is a gramma_r using whkh we can build more thaJl cP
• Cosider string id + id * id
E E
E
/I\
+ E
/1\E
E *
I /I\
Id E • E
/1\ I
E + E Id
I IdI
id
I idI
id
Fig. 2.7.8
• From above ·construct ion, we get two different parse trees clearly. Hence given
grammar is ambiguou s grammer.
There are two types by which the top-down parsing can be performed .
1. Backtracking 2. Predictive parsing
Top-down parsers
Review Question s
E ~ num T
T ~ * num T I E
pro ced ure E
{
ti loo kah ead = num the n
{
ma tch (nu m) ;
T;
/* call to procedure T • /
}
else
error;
ti loo kah ead = $
{
declare success; /* Return on succu1:11:1 ·,. /
}
else
error;
} /"' end of procedure E• /
pro ced ure T
{
if look ahe ad ""' '•'
{
ma tch ('*') ;
if lookah oud • 'num '
{
match(num);
T;
} /* inner if closed*/
else
error;
} /* outer if closed*/
else NULL; /*indicates E Here the other alternate is
combined into same procedure T */
} /* end of procedure T* /
procedure match( token t )
{
if lookahead =t
lookahead = next token- - I /*lookahead pointer is advanced*/.
else
error;
} /* end of procedure match*/
procedure error
{
piint("Error!!!");
} /* end of procedure error*/
~
E➔ num T
will parse this string using above . given
procedures.
The parser will be activated by calling
T➔ * num T
procedure E. Since the first input character 3
is matching with num the procedure match
(num) will be invoked and then the
T➔ * num T
lookahead will point to next token. And a
call to procedure T is given.
= next_token. t
Now '4' is matching with num hence again the procedure fo r match (num) ts
fulfilled. Then procedure for T is invoked. And T is replaced by c.
As lookahead pointer points to $ we quit by reporting success. Thus the input string
can be parsed using recursive descent parser.
®
. TECHNICAL PUBL/CA TIONS - an up-thrust for knowledge
2_-_26_ _ _ _ _ _ _ _ _ _ _SYntax An~
==s,~·g~n_ _ _ _ _ _ _ _ _ _ _
......:p
C_om
_ e~r De
::...:.i:..=l
~
. How ever e prog ramm ing langu
th
Construction of recursive descent parser is easy
recu rsion . The inter nal details are age
that we choose for RD parse r should supp ort 11
<lt
in this pars er we cann ot acce ss the
accessible in this type of parser. For insta nce; current
nt, we cann ot access the stack containin
leftmost sente ntial form. Secondly at any insta
g
recu rsive calls .
(A -Ht~1la~2) is crucial. And to ll\ake
Writing procedur es for left recursive gram mar
factor the gram mar. We cannot wnte .
such procedur e simpler first we have to left ,
· d ext free gram mar.
recursive esce nt parsers for all types cont
if (lookabead = t)
lookahead = next -
token· I
el8e
error;
PIOaclme error { )
~-------
Example 2.8.2 What is recursive . descent parser ? C.onstruct a recursive descent parser for
the following grammar,
E➔ E+TIT
T ➔ TFIF
F ➔ F*lalb.
Consider
1) E ➔ E + T IT E ➔ TE'
J, J, ~ J,
A A a ~
E' ➔ + TE' I£
T ➔ T F IF
2) J, J, J, J,
A A a ~
To summarize,
E ➔ TE'
E' ➔ +TE' le
T ➔ Ff'
-
T' ➔ Ff' I E
F ➔ aF' I bF'
F' ➔ ""F' IE
Now the recursive descent parser is .
Procedure E ( )
{
T( );
Edash ( );
}
Procedure Edash ( )
{ .
if (lookhead = '+') ·
{
match('+');
T( );
· Edash ( );
}
else null;
}
Procedure T( )
{
F( );
Tdash ( );
}
Procedure Tdash ( )
{
if (true) ·
{ F ( );
Tdash ( );
return;
}
else null;
}
Procedure F ()
{
if (lookahead = 'a' 11 lookahead = 'b')
Fdash ( );
}
Procedure Fdash ()
{
if (lookahead = '*')
Fdash ( );
else null;
}
®
TECHNICAL PUBLICA TIONS - an up-thrust for lcnowledge
eompiler Design 2- 29 Syntax Analysis
or = +
and = *
Now the grammar becomes
E ➔ E+T I T
T ➔ T*F I F
A' ➔ aA'IE
l)E ➔ E+TIT E ➔ TE'
j, j,~ j,
A A a ~
. E' ➔ +TE' I€
2) T ➔ T *FI F T ➔ FT'
j, j, ~ j,
T' ➔ *FT' IE
A A a ~
. E' ➔ +TE' IE
T ➔ FT'
T' ➔ *FT'I E
®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
c0mpi/et 00s1g11
r 1!1·1(1() 11n1l' lf,il~c
Th•· l'""'•dun· w recursiv•• Jesccnl parsing are _
J'l1oc-odu11' E ( )
{
T( ):
Edash ( );
Procedure Ectash ( )
{
if (lookhead == '+ ')
{
match('+');
T( );
Edash ( );
else null·'
Procedure T( )
{
F( );
} Tdash ( );
Procedure
{ Tdash ( )
match ('*')·
F ( ); I
} Tdash ( );
} else null·
Proc d '
{ e ure F ( )
if Oookaheact = '!')
{
} match ('!');
else .f F( ) .
{ i Oookah .' = '(' )
eact
match('('
E ( ); );
'f
l Oookaheaa == ')' )
}
match ( ')');
compiler Design 2 - 31 Syntax Analysis
else if (true)
return true
else
return false;
}
Advantages of recursive descent parser
]. Recursive desce nt parse rs are simpl e to build.
tree.
2. Recursive desce nt parse rs can be const ructed with the help of parse
type of parsin g a
This top-d own parsin g algor ithm is of non-recursive type. In this
from left to right. The
table is built. For LL(l) - the first L mean s the input is scann ed
the numb er 1 in the
second L mean s it uses leftm ost deriv ation for input string. And
to predi ct the parsin g
input symb ol mean s it uses only one input symbol (lookahead)
process.
parsi_ng table. The
The data struct ures used by LL(l) are i) input buffe r ii) stack iii)
is used to hold the
LL(l) parse r uses input buffe r to store the input tokens. The stack
the stack in reverse
left sentential form.The symb ols in R.H.S. of rule are pushe d into
ithm non-recursive. The
order i.e. from right to left. Thus use of stack make s this algor
for non-t ermin al and
table is basically a two. dimen siona l array. The table has row
A is a non-t ermin al
column for termi nals. The table can be repre sente d as M[A, a] where
and a is curre nt input symb ol. The parse r works as follows -
The simple block diagr am for LL(l) parse r is as given below
®
TECHNICAL PUBL/CA TIONS - an up-thrust for knowledge
~C-Omp,ler~~Oe.~cSlg/)~
~
- - - - -- -
----
Input
token
a
2-32
+ I J$ I
J
b
J
u.(1) Par98'
:,._-._. Output
Top
-- s
s
-Staci< --
-
Parsing table
/
b $
-
~
a +
~
A -
B -
C ~
- -
A
A
a
-- B
a
a
Error. - ---
Ref.er table M[A, a] if entry at M[A, aJ is e-rror repart Error.
Refer table M{A, aj if entry at MfA, a] is A ➔ PQR then pop
A then push R, then push Q then push P.
TitE parser con5ults the table M[A, a] each time while taking the parsing actions
h(..-nc:e this type of parsing method is called table driven parsing algorithm. The
configuration of LUl ) parser can be defined by top of the stack and a lookahead token.
One by one configuration u; performed and the input is successfully parsed if the parser
r ~ the halting configuration. When the stack is empty and next token is $ then it
"--'"~ ro 5 ~ful parse.
1'ur (:f,n+,trod,im, c,f pred ictive LUJJ parser we ruive to follow the following steps ·
J, 0,rrtpv.tatvJn m fHl'tf amJ FOLLOW function.
zr n~JnJ.t:t tw.: prt:.dktive parsing table w;ing FIRST
11 d FOLLO .
an W functions.
,, Vi,r-~- tlv· mp ut $tring with the help of predictive . b
J parsing ta le.
FIRST function
FTRST(a) is a set of terminal symbols that are first symbols appea ring at R.H.S. in
derivation of a. If a ⇒ E then E is also in FIRST (a ).
Follov,ring are the rules used to compute the FIRST functi ons.
1. If the terminal symbol a then FIRST(a) = ·lal.
2. lf there is a rule X ➔E then FlRST(X)=l£1.
3. For the rule A ➔ X1 X2 X3 ... Xk FIRST(A) = (FIRST(X ) u FIRST(X ) u FIRST(X ) ...
1 2 3
u FIRST(Xk))
,,..,here k X j ~ n such that 1 ~ j ~ k- 1.
FOLLOW function .
FOLLOW (A) is defined as the ·set of terminal symbols that appear immediately to
the right o_f A. In other words
'*
FOLLOW(A) = { a I S ⇒ a Aa J3 where a and J3 are some grammar symbols may be
terminal or non-terminal!.
The rules for computing FOLLOW function are as given below -
1. For the start symbol S place $ in FOLLOW(S).
I .T ➔ FT
1 T' ➔ *FT 1£
F ➔ ([)I id
Find the Frn ST nnd [OLLOW f 11nctions for flu· a/Jove grnm 11111r. AU : Dec.-05. Marks 12
Solution : E ➔ TE' is a rule in which the first sy mbo l at R.l-1 .S. is T. Now T ➔ FT' in
which the first symbol at R.H.S. is P anJ there is a rult> for Fas F ➔ (E) I id.
t~
®
TECHNICAL PUBLICATIONS - an up-thmst for knowledge
Com piler Des ign 2 - 34 Synt ax Ana/YSis
\
/ j
T ~ (E)T ' I idT .
FOLLOW(E') -
i) E ~ TE' the com put atio nal rule
is A~ a Bl3 ..
A = E', a = T, B = E'' 13 =E the n
by com put atio nal rule 3 eve rythin~
FOL LO W(A ) is in FOLLOW(B) i.e. eve iJ1
ryth ing in FOL LO W(E ) is in FOLLOW
(E').
· FOL LQ W(E ') = I ), $ }
ii) E' ~ +TE' the com put atio nal rule is
A ~ a B 13_
A -- E'' a = +T ' B = E' A =c th
' .., en b Y. com put atio nal rule 3 eve ryt h. I.! i1'
FO LLO W( A) is in FOL LOW (B) i.e. eve ino
ryth ing in FOL LO W(E ') is in FOLLOW
(E').
TEC HNIC AL PUB LICA TION s® - an
up.t hrus t for know ledg e
compiler Design 2 - 35
FOLLOW(E') = I ),$ I
We can observe in the given gramm ar that ) is really foll owing E.
FOLLOW(T) •
We have to observe two rules
E ➔ TE'
E' ➔ +TE'
i) Consider
E➔ TE' we will map it with A ➔ a B f3
A = E, a= E, B = T, f3 = E' by comput ational rule 2 FOLLOW(B) = IFlRST (ts) - ti,
That is FOLLOW(T) ={FIRST(E') - E)
= {{+,E}-E}
= l+l
ii) Consid er E' ➔ +TE' we will map it with A ➔a B f3
A = E', a = +, B' = T, f3 = E' by comput ational rule 3
FOLLOW(A) = FOLLOW(B) i.e. FOLLOW(E') = FOLLOW(T)
FOLLOW(T) = {),$)
Finally FOLLOW(T) = l+l u 1),$1
= I+ ,), $I
We can observe in the given gramm ar that + an<;i ) are really followi ng T.
FOLLOW(T') -
T ➔ Ff' -
FOLLOW(F).
T ➔ FT'
A - HX Bil
A~ a BP
A = T, et = r , U = F, II = T'
A = T', a = •, B = F, p = T'
FOLLOW(B} = IFiRST(f:3) - El
FOLLOW(U) = IFll{ST (P) - i:I
FOLLOW(F) = IFIRST(T') - El FOLLOW(F) = IFIRST(T') - El
T' ➔ •FT'
A ➔ a Bp
A = T', a = *, B = F, p = T'
FOLLOW(A) = FOLLOW(B)
FOLLOW(T') = FOLLOW(F)
Hence FOLLOW(F) = l+,),$1
FOLLOW(F) = I+,*,),$)
E l(,idl 1),$1
T l(,idl ) $I
(+,,
P l(,idl (+/,),$1
®
Tf;CIINICAI rtJnt,IGAT!ONS • 011 lll)•th111s1 fot k11ow1octgt1
compiler Design 2 • 37 Syntax Anolysw
Algorithm :
4. All the remaining entries in the table M are marked as SYNTAX ERROR.
Now we will make use of the above algorithm' to create the parsing table for the
grammar-
E ➔ TE'
E' ➔ +TE'lc
T ➔ Ff'
T' ➔ *Ff' 1£
F ➔ (E) I id
id + .. ( ) $
E
I
E' , I
I
T
I I I l
r f
T'
! I
I '
F
I r'
Now we will fill up the entries in the table using the above given algorithm. For that
consider each rule one by one.
E ➔ TE'
A ➔a
A = E, a = TE'
FLRST(TE') if E' = E then FI RST(T) = I(,id I
M[E, (] = E ➔ TE'
MIE, id] = E ➔ TE'
E' ~ +TI·
A ---4 a
A== E', a == +TE·
FlRST( +TE') == \+ }
E' ➔ 1:
A ➔a
A = E', a = 1: then
FOLLOW(E') = {),S}
M[E', )] = E' ➔ 1:
M[E, $] = E' ➔ 1:
T ➔ Fr'
A ➔a
A = E', a = Fr'
A= T, a = *Fr'
FIRST(*FT') = l*l
A ➔a
A = T', a =E
I
FOLLOW(T') = l+,),$1
Hence M[T', +] = T' ➔ E
MfT',)] = T'➔ E
M[T', $] = T'➔ E
F ➔ (E)
A ➔ <X
A= F, a = (E)
FJRST((E)) = l ( I
Hence M[F,(]= F ➔ (E)
F ➔ id
A ➔a
A= F, a = id
FIRST(id) = { id I
Hence M[F, id] = F ➔ id
id ,. ( ) $
+
·,. Error
E E ➔ TE' Error Error E ➔ TE' Error
Now the input .string id + id * id $ can be parsed using above table. At the initial
is
configuration the stack will contain start symbol E, in the input buffer input string
placed.
$E id + id * id $
Now symbol E is at top of the stack and input pointer is at first id, hence M[E, id] is
referred. This entry tells us E ➔ TE', so we will push E' first then T.
Stack Input Action
$E'T' id id+ id * id $ F ➔ id
$E'T id • id $
id "' iJ $ T > Fr'
$E' T' F
$E' T' id id * id $ F > id
$E' T' * iJ $
T > .. FT'
$E' T ' F* * id $
$E'T F id $
_J;E' T' id id $ F -> id
$E' T' $
$E' $ T - > t:
$ $ E' ➔ €
T ➔ T*F
T ➔ F
F ➔ (E) / id
AU : Dec.-16. Marks 10
Solution : The given gram.mer is left recursive W e ca n t
. . • n o p a rse the s trin g if the
g rammar 1s left recursive. Hence we can elimina te left recu rs ion .
For elimination of left recursion - Re fer example 2.7.4.
W e get
E ➔ TE '
E' ➔ +TE' IE
T ➔ FT'
T'➔ * FT' IE
F ➔ (E) I id
For findin g FIRST and FOLLOW of above gr ~
p arsing ta ble refer example 2.9.1. · ammar and fo r bui ldi n g pred i<.i JV
is Lt (1).
fun ction~.
Now w~ will cmn put~ FrnST ond FOLLOW
FlRST(S) = la, bl
Beca use 1) S -+ AaA b
Suppose if A ➔ r:: then
S ➔ aAb lab
Similarly 2) S ➔ BbBa
s S 4 AaAb ·S 4 BbUo
A A 4e A-H
B B ➔ t: B -+ e
-
Now cons ider the strin g "ba" . For pars .i ng
Input Action
Stack
ba$ S - ► BbB.,
$S
B ➔ r
$a8bB
bo$
$a8b bo$
- - - - - --
- - - - - -- - - - - - - -- -s® - an up-thrust for knowledge
TECHNICAL PUBLICA TION
( f .1/l f/)/ft1 / / )1jtj/f} rl
;> 47
a1l
n!\,
s $
-- -- ~<< <'pl
"' ► t1AHll,AI,,
A > nAIJ Ir
H -> '7131 r
Solu tion :
®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
Comp,ler Design 2 - 44 ◄
li~t ~ (l c ,t p -~c g)
number~ id en ti~
- - -- --=-- -~~~~~~~---------
=
FO LL OW (te xt p ) be ca u se
--
-
- = I (, num ber, identifier!· FOLLOW(textp-seg) = {)}
FIRST(textp-seg) --
=.{' , e} FOLLOW(textp-seg') = {)}
FIRST(textp- seg')
Examp le 2.9.6 i) Construct the predictive parser for the following grammar.
S ➔ (U la
L ➔ L, SIS
Construct the behavior of the parser on the sentence (a, a) using the gramm
ii)
ar spe ii'.:
c1, ,ed
above ¥\IN ~
. . . . -·- - - . •»I
-
FM k■J tfl
Soluti on : 1) As the given gram. mar 1s left recur sive becau se - - ---..:. . if!UiJ
of
L ➔ L , S I S.
We will first elimin ate left recurs ion. As
<.f} A ➔ A a I~ can be conve rted as
A ➔ ~A'
A' ➔ aA' IE
We can write L ➔ L, SIS as
.L ➔ SL'
L' ➔ ,SL' IE
Now the gram mar taken for predic tive parsin g is -
S ➔ (L) I a
L ➔ SL'
L' ➔, SL' IE
Now we will comp ute FIRST and FOLL OW of non termi nals
FIRST(S) = I(, a}
FIRST(L) = IC al
FIRST(L') = I', EI
FOLL OW(S ) = I', ), $J
FOLL OW(l ) = FOLL OW(L
') = I )}
The predi ctive parsin g table can be const ructed as
s s ➔a
S ➔ (L)
$
L L ➔ SL' L ➔ SL'
I
L
L' ➔ f
L' ➔ , S L'
ii) As we have co ,
( . nstru cted a predi cti ve . . .
a, a) us ing that tabl ·h pars ing _. _.
table tn (1) we will pdrse the :;tnt1r
e as s o wn below -
- -- - - - - -- ~T;E;C~H~N~
IC:A~L~P
=U
:B~L:IC=-A~T:-IO
:-~N
- ;®
;R___ ___ ______ ____.--/
s - an Up-thru st for kn nLU/orl~ -
►
---
compiler D_e_s..:...ig_n_ __ _ _ __ _ _ _ ____:2: :. . -_~4:..:_7_ _ _
- $S
State
- - - lnput
(a, a) $
-
'
_ _ _ _ _ _ ____:_S~y~nt~
Action s
-
s
n a~ly~s~i
a~x~A~
-
$_) L (
$J !:_ -....
(a, a) $
a, a2.JL
_S ➔ (L)
1..---+ g_' -
--
._lli '_S a, a) $ S➔ a
_ ~ ~ - - ~- -a,-a)
~$
$) L'
---
, a) $ L' ➔ , g_'
$ ) L' S, I a} $
$) L'S a)$ S ➔ a
$) L' a a) $
$) l ' )$ L' ➔ E
$ ) )$
$ .$ Accept
Example 2.9. 7 · Constr uct the predictive parser for the followi ng gramm ar
S ➔ a Ii I (T)
T ➔ T,S IS
Solution : i) We W:ill first elimin ate left recurs ion from given gramm
ar.
S ➔ a I i I (T)
T ➔ ST'
T' ➔ I ST' IE
Now let us compu te FIRST and FOLL OW
FIRST (S) = {a, i, (}
FIRST (T) = {a, i, (}
FIRST (T') = { ' , E}
FOLL OW (S) = { ' , ), $}
FOLL OW (T) = FOLL OW (T') = { ) }
Now the parsin g table can be constr ucted as
:
i ( _) , $
t---
s
a
- -
i
- - S ➔
-
(T)
~
..... T
S➔a
- - - TS ➔- ---
T ➔ ST'
I
·,
- -- ➔ ST'
T ➔ ST'
T' ➔ , ST
- T' T' ➔ r
®
TECHN ICAL PUBLICA TION S - an up -thmst for knowled ge
Cn1111 >lint Oo!fl(Jn _ _ _2_ - 4_B_ _ _ __ __ _ _ _ _s..:__
yntax An
W!' wi ll nnw
~
,.ir~ (• tlw st rings wi th th e h elp of predicti ve parsing table.
1
~ ) T'S a, (a, a) ) $ S ➔ a
~ )T ' n a, (a, a) ) $
$) T' , (a, a))$ T ➔ ,ST'
$) T'S, , (a, a) ) $
:i,) T'S (a, a) ) $ S ➔ (T)
$)T')T( _(~) )J, -
$) T ' ) T a, a) ) $ T ➔ ST'
---
$) T'J T'S ___ a, a)) _$
--·- --- --- -
$) T' ) T'a a, a) ) $ S ➔ a
$) T ' )T'a a) ) $
$) T')T' ))$ . T' ➔ E
$ ) T ') ))$
$ ) T') ))$
-----
$ ) T' )$ T' ➔ C
-· ----·. -·-
$ ) )$
$ $
ii)
$. ) T '.) T' ) )! a ) $
$ ) T' ) T 'S, , a] _$
$) T') T 'S a )$ S ➔ a
$ ) T' ) ) $
$) T' $
®
TECHNICAL PUBL/CA TIONS - an up-thrust for knowledge
2 - 50 Syntax A
~~~~~
~omp,ler ~ - - - - - - - - - - ~::_:= ------~ ~~~~~ ~a/i,,s,.
Design ---.........._ ·
iii)
.
Input Actions
State -
S ➔ (T)
$.5 (a,a)$ -- - - --
$)T( {a,a)$ --
a,a)$ T ➔ ST'
$)T -
~,a)j, S➔ a
$)T'S --
$)Ta a,a)$
-
$)T' ,a)$ T' ➔ ST'
$)T'S, ,a)$
--
a)$ - s ➔ a
$)T'S
- - -
$)T'a a)$
-
S)T' )$ T' ➔ €
- -
$) )$ -
$ $ ACCEPT .
S ~ iEtS/iEtSeS /a
As the production rule for S needs to be left factored, we will rewrite the grammar as·
S ➔ iEtSS' /a
S' --; eS/£
E ~ b
S' ➔ e S' ➔ i
-
s
S' ➔ eS
E E ➔b
Example 2.9.9 Consider the following Pascal-like grammar where terminals are in boldface :
< < var decl > ➔ var < decl list >
< decl list > ➔ < decl > ; < decl list > I < decl >
< decl > ➔ <id list > : id
< id list > ➔ < id list >' id I id
i) Eliminate left-recursion and left-factor the grammar.
ii) Find the FIRST and FOLLOW sets for all the non-terminal symbols of the new
grammar of part (a).
iii) Construct the predictive parsing table
iv) Show the nwves of the parser on
a) var id, id : id b) var id, id AU : May-I 1, Marks 16
®
TECHNICAL PUBUCA TIONS - an up-thrust for knowledge
Com pile r Design 2 • 52 Syntax An:i,
Y~,~
ii) After rem oving left rec
urs ion and le ft fa cto rin g the gra mm ar the ne w ~ ram
' " rl'\;i r
bec om es -
< var dee} >_ , var < dec l list >
< dee l list >_ , < dec l > < dec l list ' >
< dee I list ' > -, ; < dec l list > Ie
< dee l > _, < id list > : id
< id list > _, id< id list ' >
< id list ' > _, ,id < id list ' > \c
FIRST( <var dec1 > ) = {va r}
FIRST( < dec1 list > ): {id}
FIRST{ < dee1 list >)= { ;,E}
FIRST( < dec l > )= {id}
FIRST( <id list >) = {id }
FIRST( <id list >) = {,,E}
FOLLOW( < var dec l > ) = {$}
FO LL OW ( <d ecl list >) = { }
FO LL OW ( <d ecl list'>) = { }
FO LL OW ( <d ecl >) = {;}
FO LL OW (<i d lis t>) ={ :}
FO LL OW {<i d lis t'> )= {:}
iii) The prectictive par sin g tab le
is as giv en bel ow -
id
<dee! list>
<dee) list> ->
<de cl> <de cl
<ded list'> list' >
<de cl list'>->;<d ecl list>
<d ec1 list'> ·>
<decl> £
<de cl>-> <id
<id list> list> :id
<id list> ->id
<id list> <id list' >
<id list' >·>E
<id list'>->
iv) a) M id< id list';
ove s for v ar id id . 'd
, • I
Stack
S <va r decl> Input
Rules applied
va r id, id : id S
~e cl lt.'it>var
<\'a r ded > -+ var <de d lc.t>
- - -- - -
v a r id , id : id S
TECHNICAL PUBU~C;A~T~IO
~~® ; - - - - - - -
- - - - -- - - -
NS • an t1p-thrust for kno
wledge
- compiler Design 2 - 53 Syntax Analysis
r$
$ <dl'CI list>
<d'--cl list' > <d<~l>
id, id : id $
id, id : id $
<dee! li1,t>
<decb ➔
➔ <dcd ><dccl list' >
<id list> : id
$ <d<'CI Hst' >id:<id Hst> id, id : id$ <id list> ➔ id<id lis I'>
$ <dccl Li.st' >.id:<idlis t'>id id, id : id $
$ <decl list' >id:<idlist' > , id : id$ <id list' > ➔, id <id list' >
$ <decl list>var var id, id$ · <decl list> ➔ <decl> <decl list'>
Example 2.9.10 Construct non recursive predictive parsing table for the following grammar.
E ➔ E or E I E and E I not E I (E) I O I l. AU : Dec.-12, Marks 16
Solution : As the given grammar needs to be left factored. Also the ambiguity of this
grammar needs to be removed. The modified grammar will be· as follows.
• Eliminating ambiguity
E ➔ E or T
E ➔ T
T ➔ T and F
T ➔ F
F ➔ not G
F ➔ G
G ➔ (E)IO I 1
®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
~~ ~~~ - - - - - - - -~ 2~-~5:_ Syntax An
4_ _- - - - - - -- -~
~om piler Design
s
• Eliminating left recursion
E ➔ TE'
T ➔ Ff'
G ➔ (E)IO\ 1
The FIRST and FOLLOW for abo ve gra
mm ar is -
FIRST (E) = { not, (,0,1}
FIRST (E') = {or, E}
FIRST (T) = { not, (, 0, 1}
FIRST (T') = {an d, E}
FIRST (F) = {no t,(, 0, 1}
FIRST (G) = {(, 0, 1}
FOLLOW( E) = { ), $}
FOLLOW( E') = { ), $}
FOLLOW( T) = {or,},$}
FOLLOW( T') = {or,),$ }
FOLLOW( F) = { and , or,) ,$}
FOLLOW( G) = { or, and ,),$ }
The pred ictiv e pars ing tabl e will be
or and not ( ) 0 1 $
\ £
· E->TE' E->TE'
£'
- -- - - - - - E->TE' E->TE'
I
E'->orTE'
E'->E E'->E
I
T ----
-----· T-> FT' T-> FT' T-> FT' T-> FT'
T'
T'->andFT'
T'->E T'->E
F ---
F->n ot G F->G
G F->G F->G
G->(E)
Example 2.9.11 Construct a predictive parsing table for the grammar S ➔ (L)/a, L➔ L, S IS
and show whether the following string will be accepted or not. (a, (a, (a,a))).
AU : May-17 , Marks 16
I
))$ L' ➔. e
$) L'
-)$- - - - - - - ·~·-·,., -L' ➔ e _____,,_-..,
,,,,,.,. '
h$
$ ACCEPT
Exa mpl e 2.9.12 Con sider the following CFC grammar over the non
-terminals {X, Y, Z} and
terminal s {a, c, d} with the productions below and
start symbol Z.
X ~ a
x~ Y
Z -, d
Z- , XY Z
Y~ c
y ~£
Compute the Ff RST and FOL LOW sets of every
non-terminal and the set of non-terminals
that are nullable.
AU : May -18, Marks 6
Sol utio n : FIRST set
Sym bol
X
Set
la, c, el
----i
y
le, ti
z la, c, d,cl I
►
~ :..5. .::..ig_n _ _ _ _ _ __ _ _ ____,:2::___-~5~7- - - - - - - - - -~S~y~n~ta~x~A~na~ly~s~is
FOLLOW set
Symbo l Set
X le, di
y
ldl
z I$.
example 2.9.13 Construct LL(l) parsing table for the following grammar, G and check
whether the word "ia<btd" is a string of the grammar .or not
S➔iEtS I iEtSeS I d
£➔id OP id
OP➔<l>l<=l>=l==I! = AU : Dec.-22, Marks 13
S ➔ iEtS I iEtSeS I d
E ➔ id OP id
S' ➔ eSIE
E ➔ id OP id
id e
------------
= < > $
d
s S~ d S' ➔ iEtSS'
s· S' ➔ r
5' 4 eS
I E
OP
E ➔ id
OP id
J
1___ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ o_
_P ➔_<_ o_
=_ _ _
P_➔ >=
_ _ _ _ __ _ .
ln above parsing table we have got m ultiple entries at M [S'~ e] and also at M[OP, <I
and M[OP, >] the above grammar is not LL(l ) grammar.
Example 2.9.14 : Discuss LL(l) parsing method for the following grammar
E➔TE'
E'➔+ TE' I£
T➔IT'
T'➔>t< FT'I £
F➔(E) /id.
A ➔ aB I aC I Ad I Ac
B ➔ b Be IF
C ➔ a.
Review Questions
AU : Dec.-09, Marks 8
3. Write an nlxurith fi
m or non recursive predictive parsing.
AU : May-16, Marks 6
-;::;;;;:-:~ - --;;---------~
TECHNICAL PUBLICA TIONs®
- an up-thrust for knowledge
_~~~~ ~~
-=
_ __ _ __:2::__ 59:____ __ __ __ __ ax Ana lysis
Synt
1er oesig:__n_ _ _ _ _ _ _ _
~
Bottom Up Pa rsi ng Ei•Mufi(IW.v.a,vw1 1 11 •-tN•U&•4MtfN~M1d: ■
1 -- rm PB ll9 ■
---·••=
• .
th ·s sect ion , we .wil l disc uss "Ho w an inp ut stri ng get s parsed effi cien tly ? I.n
- , 1.n 1
compi1ers, the tas k ts don e by a
pro gra m ca lJed parsers .
. . .
par ser is two'Jfold · eith ·t h eck s the mp ut strm g com plet ely fo r its
·h task of . ' · er 1 c
, T e on syn tact ical ly inp ut stri ngs .
syntax or it gen era tes e rror m essa ges
to red uce
tn bot tom -u~ pars ing me tho d, the
inp ut stri ng is take n first and we try
,
th th e hel p of gra mm ar and try to obt ain the star t sym bol.
this stri ng wi
.·
ly as soo n as we reac h to star t sym bol
, TI1e pro cess of par sin g hal ts suc ces sful
tom to up tha t ,is from leav es to roo t.
, The parse tree is con stru cted from bot
ful
plac ed at the leaf nod es afte r suc cess
, In this pro ces s, the inp ut sym bol s are
pars ing.
tog ethe r
star ting from leav es, the leaf nod es
, The bot tom -up par se tree is cre ated d and
thes e inte rna l nod es are furt her red uce
are red uce d furt her to inte rna l nod es,
eve ntu ally a roo t nod e is obt ain ed.
sym bol s.
the list of term inal and non ~ter min al
, The inte rna l nod es are cre ated from
• This inv olv es -
-ter min al U term inal
Non -ter min al for inte rna l nod e = Non and
this pro ces s, bas ical ly par ser trie s to iden tify R.H.S. of pro duc tion rule
• In
s acti vity is call ed red uct ion .
replace it by cor res pon din g L.H.S. Thi
tion s
the cru cial but prim e task in bot tom -up par sing is to find the pro duc
• Thus
that can be use d for red uct ion .
. of
tom -up par se tree con stru ctio n pro ces s ind icat es tha t the trac ing
• The bot
ord er.
der ivat ion s are to be don e in rev erse
For exa mp le
stat eme nt,
Consider the gra mm ar for dec lara tive
S ➔ TL;
T ➔ int I floa t
L ➔ L,id I id
The inp ut stri ng is floa t id,i d,id ;
Parse Tree
Step 1 : We will star t from leaf nod e
G)
Step 2 :
flo at
St ep 4 : Re ad ne xt str in g fro m
in pu t.
0
St ep 5 Re du ci ng id to L. L ➔
id .
St ep 6 Re ad ne xt st rin g fro m
in pu t.
St ep 7 Re ad ne xt st rin g fro m
in pu t.
flo at
6
flo at
St ep 8 id , id ge ts re du ce d.
'--- ---v --"
L
flo at
6
TE CH NI CA L PU BL IC
AT IO NS® - ~n
Comp1/er Design 2 - 61 Syntax Analysis
float
Step 10 : The sentential form produced while constructing this parse tree is ·
float id,id;
T id,id;
T L,id;
TL;
s
Step 11 : Thus looking at .sentential form we can say that the rightmost derivation in
reverse order is performed.
• Thus basic steps in bottom- up parsing are
1) Reduction of input string to start symbol.
2) The sententia l forms that are produce d in the reductio n process should trace out
rightmos t derivatio n in reverse.
Handle pruning
As said earlier, the crucial task in bottom-u p parsing is to find the substring that
could be reduced by appropri ate non-term inal. Such a substring is called handle.
In other words handle is a string of substrin g that matches the right side of
production and we can reduce such string by a non-term inal on left hand side
producti on. Such redu ction represen ts one step along the reverse of rightmos t derivatio n.
Formally we can defi ne handle as follow :
®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
Com pile r Design
For exa mp le
Consider the gra mm ar
E -) E+E
E -) id
. . . + id and the rig htm ost der iva · is
No w cons1·d et• the stn ng id + id tio n
E ⇒ E+E
nn
E ⇒ E+ E+ E
rm
E ⇒ E + E+ id
rm
E ⇒ E +i d+ id
nn
E ⇒ id+ id+ id
nn
The bol d stri ngs are called han dle
s.
---E---
+id + 1.d
--- - -- - - - -id
E + E + id id
r- - - - -- --- - -.. - - -=-- -- - --E..,
-) id
..- -------;:_7
E+ E+ E E+ E E ➔ E+E ·
E+ E ·· E4 E + 'E
E
Review Questions
I w$ J
Fig. 2.11.1 Initial con figu rati on
TEC HNICAL PUBL ®
ICA TIONS - an up- thru st for kno wle dge
p
2 - 63 Syntax Analys is
compiler Design
--- -- ----- ------
• A sruft reduce parse r requi res fo llowing d a ta struc tu res.
1. The input buffe r sto ring the in p ut string .
of ru les.
z.,A stack fo r storin g and accessing the L. 1-1.S. and RJ-1.S.
J11e ini tia l config ura tion of Shift reduce parse r is as show
n in Fig. 2.1J.1.
E➔ E* E
E ➔ id
AU : May-16. Marks 8
Perform Shift-Reduce parsing of the input string "idl + idb id3 ".
Solution :
-- -
Stack Inpu t buffe r Parsing action
$ id 1 , id 2+ic.1 3$ Shill
®
L/CAT IONS - an up-thrust for knowle dge
2 - 64
Compiler Design Syntax A '
Reduce by E ➔ id
~
$E+id2 • id3$
$E+E $ Reduce by E ➔ E + E
$E $ Accept
$ ( L, ( a, a)) $ Shift ·
.$ ( L, (a I a)) $ Reduc e S ➔ a
$ ( L, ( S , a)) $ Reduc e L ➔ S
$ ( L, ( L , a)) $ Shift
Review Questions
dge
TECHN ICAL PUBLICATIONS® • an up-thru st for knowle
2 - 66
i) si m ea ns s hi ft st.,1tc i.
ii) ri mc,rns rcducL' b)' ruk, j.
Types of LR parse r
LR parser
Review Question
-------~-- -- --
AU : Dec.-07. Marks 4
1. What are the reasons for using LR parser techniques ?
■,fa&•f•*-
fjf j SLR Parsing
Context free grammar
• We will start by the simpl est form of LR
parsin g called SLR parse r.
• It is the weake st of the· three m ethod s but
!
Construction of canonical set
of items
it is easies t to imple ment.
• The parsin g can be d one as show n by the
steps in Fig. 2.J 3.1. Construction of SLR parsing
table
• A gramm ar for which SLR parse r ca n be
cons tructe d is called SLR gram mar.
Definition of LR (0) items and related terms - Input
Parsing of input string
string
1) The LR(O) item fo r gramm ar C is produ ction
rule in which t>ymb o l • i~ inse rted <1 1 sonw ♦
Output
positio n in R.11 .S. of the ruk>. For t'\ ,1mpll' Fig . 2.13.1 Steps for working of SLR(1)
S ~ • ABC
This rule has to be applied until no -more new items can be added to cl~sure( I).
_T he m eaning of .rule ~ ➔ a • BP is that during derivat ion of the input string at some
point_ we m ay_re~uire stnngs d erivable from BP as input. A non-ter minal immediately tl1
the n ght of • md1cate s that it has to be expand ed shortly.
Goto operati on -
Closure(I) = x~ • Xb
= x~ . a
The goto function can be computed as X ➔ {x•bgivesX ➔ X•b
goto(I,X) = X ~ X• b
X ➔ {~·-, gives X ➔ a •
Similarly goto (I, a) gives X ~ a•
Example 2.13.2 Consider the grammar
S ➔ AS I b
A ➔ SA I a
Compute closure (I) and goto (I).
Solution : We will first write the grammar using dot operator.
S ➔ •AS
S ~ •b
A~ •SA} Closure (I)
A~ • a
Let us call this as state as IO ·
Now we apply goto on each symbol in Io-
10 : S ➔ •AS
S ➔ •b
A ➔ •SA
A ➔ •a
I1 : goto (1 0 , A)
S ➔ A•S
S ➔ •b
A ➔ •SA
A ➔ •a
12 : goto (1 0 , b)
S ➔ b•
13: goto (1 0 , S)
A ➔ S•A
A ➔ •SA
A ➔ •a
S ➔ •AS
S ➔ •b
14 : goto (1 0 , a)
A➔ a •
®
TECHNICAL PUBLICATIONS - an u;rthrust for knowledge
2 - 70
Com?£p~ile~r!_06~s~,g~n_ _ _ _ _ _ _ _ _ __ __::.__:__:__ _ _ _ _ _ _ _ _ _ _ __Syntax Ana, -----!Sis
Example 2.13.3 Con~ider the followi11g gram111ar :
S ~ Ao I bAc I 8( I /?Ba
A ~d
B~d
Compute closure (]) and goto (}) .
Solution : First we will wri te the grammar using d o t o p e rator.
IO : S ➔ • Aa
S ➔ • bAc
S ➔ • Be
S ➔ • bBa
A ➔ •d
B ➔ •d
Now we will appl y goto on each symbol from state I O. Each goto (1) will generate
new s ubsequ ent states.
I1 : goto (I o , A)
S ➔ A •a
I2 goto (1 o, b)
s➔ b • AC
S ➔ b • Ba
A ➔ •d
B ➔ •d
I] goto (1 0 , B)
S ➔ B •c
1-i goto (l o, d)
A ➔ d•
B ➔ d•
1. For the gra mmar G initially add S' ➔ • S in the set of item C.
. al or
~- For each set of items Ii in C and for ea ch grammar symbol X (ma y be ter~J.!l otc
non-terminal) add closure 0;, X). This process should be repeated by applying~ ol
. . C These
(11 , X) for each X m l i such that goto(½, X) is not empty and not in ·
itl'ms has to constructed until no m ore set of items can be added to C.
®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
-compiler Design 2 - 71 Syntax Analysis
items by applying
Now we will consid er one gram mar and construct the set of
closure and goto functi ons.
Example :
E➔ E+ T
E ➔ T
T ➔ T*F
T ➔ F
F ➔ (E)
F ➔ id
I then we have
In this gram mar we will ·add the augm ented gram mar E' ➔ • E in the
to apply closure(!).
E ➔ •T
T ➔ •T * F
T ➔ •F
F ➔• (E)
F ➔ •id
No w apply goto(l 0, E)
Th u~ 11 b{'co mcs,
goto(l0_ E)
11 : E' ➔ E • ·
E ➔ E • +T
goto(I0, T)
goto(Io, F)
13 : T➔ F •
goto(l0, ( )
l4 : T ➔ (• E)
E ➔ •E+T
E ➔ •T
T ➔ • T*F
T ➔•F
F ➔ • (E)
F ➔ •id
compiler Design 2 - 73 Syrtl.8Y Analy1J1•
goto(T0, id)
15 : P ➔ id •
goto(I1, +)
~: E ➔ E+• .T
T ➔ •T * F
T ➔ •F
FT ➔ • (E)
F ➔ •id
There is no other rule in 11 for applying goto. So we will move to 12 . In 12 there are
two productions E ➔ T • and T ➔ T• *F. We will apply goto on *·
goto(l2, *)
l7: T ➔ T*• F
F ➔ • (E)
F - ➔ • id
The goto cannot · be applied on l3, Apply goto on E in 14_ In 14 there are two
productions having E after dot (F ➔ • E and E ➔ • E + T ). Hence we wiU apply goto on
both of these productions. The Is becomes,
goto (14, E)
Is : F ➔ (E •)
E ➔ E•+T
If we will apply goto on (14, T) but we get E ➔ T• and T ➔ TuF which is 12 onl y.
Hence we will not apply goto on T. Similarly we will not apply goto o n F, ( and id as
we get the states 13, 14, 15 again . Hence these gotos cannot be applied to avoid repe tit ion.
There is no point applying goto on f5_ H ence no w we will move a head by appl ying
goto on J6 for T.
®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
Comp iler Design 2 - 74
Syntax Ana/y .
s,s
goto(l 6, T)
19 : E➔E+T •
T ➔ T•*F
Then ,
goto(I7, F)
110 : T ➔ T* F •
Then ,
goto(l8, ))
111 : F ➔ (E) •
3) T ~ T * F 4) T ➔ F
5) F ~ (£) 6) F ➔ id. AU : Dec.-06, 11 , 16, May-11, Marks 10, May- 19, Marks 13
Solution :
We will first construct a collection of canonical set of items for the above grammar.
Tht:> set of items generated by this method are also called SLR(O) items. As there is
mi lookahead symbol in this set of items, zero is put in the bracket.
Io :
E'➔ •E
goto(r 1 ,+)
E➔ • E +T 16 : E➔ E + • T
E➔ •T T➔ • T*F
T➔ • T*F T➔ •F
T➔ ~F F➔ • (E)
F➔ • (E) F➔ •id
F➔ •id
11 : E'➔ E • 17 : T➔· T* • F
E➔ E • +T F➔ • (E)
F➔ •id
goto (1 0 , T)
goto (1 4 , E)
J2 : E➔ T•
18 :F~ (E•)
T➔ T• * F
E➔ E • +T
goto (1 0 , F)
goto (1 6 , T)
J3 : T➔ F •
lg: E➔ E+ T•
T➔ T • *F
goto (1 0 , ()
14 : T ➔ (• E)
E➔ • E + T goto (17 , F)
E➔ • T 110 : T➔ T*F •
T➔ • T*F
T➔ •F
F➔ • (E) goto (1 8, ) )
F➔ •id
111 : F >(E) •
goto (1 0 , id)
15 : F • id •
Wl' l.lll J1.•:-. 1g11 ,l I JI ·/\ trn ,1li,1v1 • ·, vi ,11 1t, •111 'i 11H follllW H :
Hl nll' 10 is initio l Htalt' . Note• th,H thp
In 1h1· hiwn 1)1 ·/\ vv, ·ry ·it.111 • iH fi11al 1-,ln ft• , Tlw
l)f,=!\ n·r11gni11·!- v1,1hlt• pn•f ixt•~.
To
l
►0
stato
17
F To state
13
To 14
sta te
id To 15
state
T F
To state
14
id To state
15
~--F----0
E
To state
+ 15
T
To state
12
F To state
id 13
Id
I I ; goto (lo, E)
E'-t E•
E➔ E• +T
~ : goto (1 1, +)
E➔ E+•T +
19 : goto (~. T)
E➔ E + T • ·
FOLLOW(E):
E'➔ E that means E' = E ~ start symbol.:. we will add$.
E➔E+T the + is following E. :. we will add +.
F ➔ (E) the ) is following E. :. we will add ).
:. FOLLOW(E) = {+, ), $}
FOLLOW(T):
AB E' ➔ E, E ➔ T the E' = E = T = start symbol. :. we will add $.
E➔ E+ T
E➔ T + T ·: E ➔ T
®
TECHNICAL PUBUCA TIONS - a11 up-thmst fo, knowledge
2 - 78 Synta)( Ana,..,
- ----~1
om
~C~ ~ s,~g~n - - - - - - - - - - -= - - - - -- - - - -
~p~,l~er~De
F ➔ (T) ·: E ➔ T
As ) is follow ing T we will add )
: . FOLL OW(T ) = I+, *, ), $I
FOLL OW(F ):
E ➔ E+ T
E ➔ T +T ·: E ➔ T
E ➔ F + T ·: T ➔ F
F ➔ (T) E ➔ T
F ➔ (F) T ➔ F
Now from canonical collec tion of set of items , consi der 10.
E' ➔ • E
E ➔ •E + T
E ➔ •T
T ➔ •T * F
T ➔ •F
F ➔ • (E)
F ➔ • id
- -~ ~- ~ -- - ---- ---
TECHNICAL PUBLICA TIONs® - an up-thru st for knowle dge
_ _ ~
mpJier oe_sig_n_ _ _ _ _ __ 2 - 79
yn~t~ax~A~n~a~ly~sis
- - - -~S~
----------
~
consider F ~ • (E)
A ~ a • a~
goto(lo, ( ) = l4
:.action[O, ( 1 = shi ft 4
Simila rly for F ~ • id
_ hi
Entry in the. act ion tab le action[O' 1'd] - s ft 5 ·: go to(I0, id) = 15
.
an y acf10n. Hence we will find the actions from I to
Other item m 10 do es no t giv e 0
Il I.
State Action Goto
id * ( ) $ E T · F
+
0 s5 s4
1 s6
2 s7
'3
4 s5 s4
5
s5 s4
6
s5 s4
7
s11
8 s6 ·'
s7
9
10
11
~il l fill it up
rsi ng tab le is fill ed up wi th the shift actions. No w we
Thus SLR pa
.
with reduce an d acc ep t action duction
the rul e 2.c fro m a
p· rsi ng table alg orithm, there is a pro
According to
(l, $].
d the action "accep t" in action
E' ➔ E • in I1. He nc e we will ad
Now in state I2
E➔ T •
A- 1a • rul e 2.b
A= E, a = T
FOLLOW(E) = I+, ), $I
Comp iler Design 2 - 80
Syntax An
alys;s
.Hence add rule E ➔ T in the row of state 2 and
in the colu mn of +, ) and
the give n exam ple of gram mar E ➔ T is rule num
ber 2. :.act ion[2 , +] = r2, $_ II\
actio nl 2, ))= r2, action[2, $]= r2.
Similarly, N ow in state I3
T ➔ F •
rule 2.b
A =T, a= F
FOL LOW (T)= (+, *, ), $1
Hen ce add rule T ➔ F in the row of state 3 and in
the colu mn of +, *, ) and $. ln
the give n exam ple of gram mar T ➔ F is rule num
ber 4. Ther efor e action[3, +] == r
actio n[3, )] = r4, actio n[3, $] = r4. Thu s we can find 4
the matc h for the rule A -t~ ,
from rema ining state s· 1 to 1 and fill up the actio
4 11 n table by resp ectiv e "reduce" rule.
The table with all the actio n[ ] entri es will be
id + ... ( ) $ E T F
'-- -
0 s5 s4 - ..
- - A
1 s6 -Accept
,- -
2 r2 s7 r2 r2
~
3 r4 r4 r4 r4
- -- - - -
4 s5 s4
- - - ----
5 r6 r6 r6 · r6
-- - - ---- - --
6 s5 s4
- - - ---
7 s5 s4
- ---
- - - - - -
8 s6 s11
·- - -- --
9 rl s7
-- -
rl rl
10 r3
- - - - -
-- -- - - - ..
r3 r3 r3
- -- - - -- -
N ow we
---- - 11 rs r5 r5 rs
w,·11 f·1'II up the goto table for a ll the non-
term inals . 1n ' _ ll',
5 tate
goto (I-o, .E) = 11 .' Hen ce goto[O, El =1, s imil arly goto
(lo T) = 1 . Hen ce goto [O, Tl ::: -
Con tin uing in this fash ion we can fill up the goto entri 2
~s of SLR pars ing table.
►
compiler Design 2 - 81 Synta>: Analysis
id +
,. ( $ E T F
0 s5 s4 2 3
1 s6 Accept
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 rl s7 rl rl
10 r3 r3 r3 r3
11 rS rS rS rS
Output : Parse w if w E L(G) using bottom-up. If w e L(G) . then report syntactical e rror.
Algorithm :
1. Initially push O as initial state onto the stack and place the input s tring with $ as
end marker on the input tape.
2. If S is the s tate on the top of the stack and a is the sy mbo l from in put b ufkr
po inted by a lookahead pointe r then .
a) If action [S, a] = shift j the n pus h a, then push j onto the s tack. Ad\'c1nc1.' thl' inpu t
lookahead p o inte r.
b) If action[S, a ] = re du ce A~ f1 the n pop 2* If3I sy mbnl s . If i i..., o n thl' top n f th L' ~L1el-.
th en pus h A, the n pus h goto [i , A] o n th e top o f the ~tnck.
®
TECHNICAL PUBLICATIONS - an up-tluust for knowledge
2 - 82 Syntax Ana,
0'.:es~ig9_!_n: '.__ _ _ _ _ _ _ _ _ _~ :__- --
pi~le:_r9_
~C~o m~ - - - - -- - - -- ~
1) E ➔ E+T
2) E ➔ T
3) T ➔ T * F
4) T ➔ F
5) F ➔ (E)
6) F ➔ id
Input string : id * id + id
We will consider two data structures while · taking the parsing actions and those
are - Stack and input buffer.
fn the above table at first row we get action[0, id] = sS, that means shift id fronl
input buffer onto the stack and then push the. state number 5. On the second row v,'e
get action [S, *] as r6 that means reduce by rule 6, F ~ id, hence in the stack id i-
replaced by F. By referring goto[0, F] we get state number 3 hence 3 is pushed ontli
st
~he a~k. No~e th~t fo r ~very reduce action goto is performed. This process of par~in~
continued 1n this fashion and finally when we get action[1, $] = Accept we halt b_
15
®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
-
compiler Design 2 - 83 Syntax Analysis
Solution : Let us numbe r the produc tion rules in the gramm ar.
1) E ➔ E + T 2) E ➔ T
3) T ➔ TF 4) T ➔ F
5) F ➔ F * 6) F ➔ a
7) F ➔ b
an
Now we will build the canonic al set of SLR (0) items. We will first introdu ce
augme nted gramm ar. We will first introdu ce an augme nted gramm ar E' ➔ •
E and
then the initial set of items I 0 will be genera ted as follows :
Io :
E ➔ •E+T "'-
E ➔ •T }" ' - - As after • the symbol E appear s we will add rules of E.
T ➔ •TF ~ • After • the T appears , so add rules of T
T➔ •F } - - - - As after • the F appear s, so add rules of
F
F➔ • F*
F➔ •a
F➔ •b
be
Now we will use goto function . From state l o goto on E, T, F and a, b w il l
applied step by step. Each goto transiti on wil l genera te new state I i .
11 : goto ( 10 , E)
E' ➔ E •
E ➔ E• + T
Compiler Design ? D4 UynttJ / An1,1
/ I"'•
I 2 : goto (lo, i)
F ➔ T •
) I • I·
I· t • I· •
I· ➔ • a
F -+ • b
h goto (1 0 , F)
T --+ F •
F .-+ F • *
14 goto Oo, a)
F ➔ a•
I i; goto (10 , b)
F -, b •
:\ow we will ~ta rt app lyi ng goto tranbition s on state f 1 . From I 1 state it is possible:'<,
apply goto tranc:; itions on ly on +. Hence
I r-, : goto (I 1 , +) ·
E-,E+ • T
T - , • TF J\s after • th e T comes wi ll ad d T tra nsitions
T-, •F ~amc is tru e fo r F.
I· -, • a
'fh<: g(llO lrd n.,iti on., wil l be: app lic:J on f 2 '> latc: now . We will chom,e I tu .1Ff' ·
f~r)to tra n'> ili"n bNa ui,c: thc:n• ii, no poin t in app lyi ng goto on T.
17 : ~:ot<, (f 2 , I )
·1 7 ·1I· •
·r ., I • •
If W<' wi ll appl y g1 ,lo on n 11r b f rnm t1 lc1 lt· I2 thc:n we wi ll gc:t F , a . or I· -1 b•
which ar<· .,ld lc.• ~ I4 anJ l r; n·~pl•( tivt•ly.
®
TECHNICAL PUBLICA TIONS • on up-thr ust for knowledqe
c ompiler Design 2 - 85 Syntax Analysis
f kncl' we wi ll no t co n ~idcr 14 and Ir; aga in . Now move to s ta te 13; from J3 the goto
tro'lnsit ion:-- on "' . I knCl'
F ➔ F "' •
/\s there is no po int in applying goto on state 14 and 15 . We will choose s tate I 6 for
~oh) trnn:: : ition.
19 : goto (1 6 , T )
E ➔ E + T•
T ➔ T• F
F ➔ •b
Now we will first obtain FOLLOW of E, T and F. As the FOLLOW computatio n 1s
required when the SLR parsing table is building.
FOLLOW(E) = {+, $1
FOLLOW(T) = {+, a, b, $I
POLLOW(F) = {+, *, a, b, $1
As by rule · 2 and 4, E ➔ T and T ➔ F we can state E = T = F. But Eis a start
symbol. Then by rule 2 and 4, T and F can act as start symbol. :. we have added $ in
FOLLOW(E).
FOLLOW(T) and FOLLOW(F).
The SLR pa rsing table can be constructed as follows.
a b $ E T F
+ *
&4 <; r; "'- '\
0
/\ccept
~6
S➔ ', r; r::!
2 r2
1 ..,H r-1 rI r-1
r4
4 r(1 d1 r(, 10 1 /)
rh 11, lh
"
f,
I (1 I (J
-,,1 ,, r, ,1
·'
7 I I 1 I 1 I
r '\ •,H
I r,
8 , r; I 'j I ' I '
L 'i rI ,, I ._i:.,
---
rl
(I<)
TECI IN/CAL PURL/CATION S - An llf> thrust for knowlodqe
2 00
Comp,lor De.c;ign
2) S -i a
?, ) ~ - > r
'\l11w let u-. u in-.tru d c::i no nica l ~l'l nf ilL'rns -
S' >- • S This is augmented gramm ar.
s ➔ • ss
S ;• a
s )•
I, goto (1 0 , S)
S' ;, • S
S 1S •S
S - • SS
s - •a
S4 •
I,, ; goto (¼, a)
s -a •
4: goto (I 1, S)
s ~ ss.
S ➔S •S
Th"' goto grap h w ill be
M[3, aJ = M[3 , $] = r 1
table is -
Hence conf lict occu rs in state 13 . The SLR (l) pars ing
Action Goto
St.ate
a s s
0 S2
S2 Acce pt
2 r2 r2
in Yau ,
li/' illJ srr111111111r is SL R(] ) or not. Expla
t ~,,( \ -J.. rl'!1dlta th ( fi >//t
Example 2. 13.8
111,~; a·r with m1~>• 1~.
L ~ $ 1'.
L ~ rd AU : May--04. Marks 8
(O ) it r--
R ~L
se t of ite m s. H en ce a co lle ct io n of LR #· ...:
go to (Io, S) L ➔ • id
11 :
go to (Io, L) L ➔ *R•
I., :
S ➔ L •=R
R➔ L• 18 : go to 0-v L)
R ➔ L•
13 : go to (Io, R)
S ➔ R •
go to (lo, *) ~ : go to(I6' R)
L➔*•R S ➔ L = R •
R ➔ •L
L➔ •* R
L ➔ • id
·
CA TfO Ns ® - an u{H hru st for knowledge
TECHNICAL PUBU
► 2 - 89 Synwx Anotys,s
' \ l)\\ " ·e " 'ill \-luild th e P~1 ~i n g t 0 bl1.:' fo r the abo\'e set of ite ms .
St.1te
Action goto
• id $ s L R
: s6 / r5
s5
- - - - - -- - -
0 s-l s5
'i
s
9
.-\s " ·e are getting multiple entries in Action (2, =] = s6 and rS. That means shift
and reduce both a shift / reduce conflict occurs on input symbol ·=. Hence given
grammar is not SLR(l) grammar.
Example 2.13.9 Constrnct LR (0) parsing table fo r the given grammar.
lJE ➔ E * B
2J E ➔ E + B
3J E ➔ B
4> B ➔ 0
5) B ➔ 1 AU : Dec.- 10. Marks 10
Sol ution : The collection of canonical set o f item s for the above g rammar \"l ill be -
I{) : E' ➔ • E I6 : goto(11, +)
E ➔ • E* B
E ➔ • E+B
E ➔ • B
B ➔ • O
B ➔ • l
®
TECHNICAL PUBLICA T/ONS - an up-thrust fot knowledge
~ ompiler Design 2 - 90
T2 : goto (10, B)
E ➔ B• Now we will compute FOLLOW.
13 : goto (10, 0) FOLLOW (E') = {$}
B ➔ 0 • FOLLOW (E) = {+, *, $}
14 : goto (10 , 1) FOLLOW (E) = {+, *, $}
B ➔ 1•
15 : goto (1 1, *)
E ➔E *• B
B ➔ •O
B ➔• 1
The parsing table will .be -
State
--
+
------
Action
0 1 $ E
Goto
B
J
0 s3 s4 1 2
1 s6 s5 Accept
2 r3 r3 r3
--- ---
3 r4 r4 r4
--·- ------
-i rS rS
- - - ----~---- ----- -
:;
s3 s4 i
--------
6 s3 8
s4
--·--
7 rl rl
rl
8 r2 r2
r2
-- -
example 2.13.1 O
.~ .... :1.~ 7,
J\·' _.; .-v,1 Al I : Mnv-1:I, Mnrk11 6
1' ~lrut· t •1 l ' 11 1kl·l i( 1 1l 11 1 1·"1H•n k, d •ii'! ,, 1 11 1· 111 •, l(,r IIH' g 1vt•n
Solution : \\\ • wil l 1
l' '
gr,in1111,1r .
14 gotn( l 11 . 11)
;\ ~ ,1 .
S ~ •b I -~ g 111(1 (1,. /\ )
/\ > SI\•
I\ ) ,,-...:,
goto (10 .S) S -t • AS
S' -t S• 5 -t • b
A -t S• A I\ -t • SA
A -t • SA A -t . ll
guto (11 • S)
S -t •AS A -t S•/\
S -t • b A -t • SI\
A -t • a
I, goto (1 0 • A) S -t • AS
S -t A-S S -t • b
S -t •AS I7 goto (12 • S)
S -t • b S -t AS •
A -t • SA A -t S•A
A -t • a A -t • SJ\
A -t • ,l
S -t • AS •
goto (10 . b) S -t • b
I~
S -t b•
We wi ll list o ut the g iven rult'~
1. S -, A S FOLL OW (S) = { a, b,$ }
3. A -, SA
4. A -, a
( Il l
7FCHN ICA I 1-'U(JI IC/\ /IO N .•~ i1111111 11,11 ,sl /, ,. lln.>n 1•, /,ft•
Action got o
0
,)
54
b
S3
I $ s
l
A
1 54 S3 I Acc ept 6 5
2 S4 S3 t 7
t 2
3 r2 r2
j r2 l
4
5
r4
S4 / r3
- -+
r4
S3/r 3
-----
7 ~-
t-
~
S4 S3 6 5
6
S3/ rl rl 6 5
7 54 / rl
SLR.
Tiu s sho ws tha t the giv en gra mm ar is· not
Exa mp le 2.13.11 Consider the foll owi ng grammar
S ➔ AS I b
A ➔ SA I a.
ar. Show the actions of the parser for the
Con stru ct the SLR pf!-rse table for the gramm
AU : May -14, Marks 16
inp ut stri ng "abab ".
tabl e.
Sol uti on : Ref er exa mp le 2.13.9 for par se
_ _ _ _ _ _ _ _ _ __
Par sin g inp ut a__b_a_b_ _ _ _ _ _ _ _ _
Sta ck Inp ut buf fer Act ion
$0 aba b$ shif t 4
$0 a4 bab $ redu ce by A ➔ a
$ 0 A 2 bab $ shif t 3
-
co mp ile r De sig n
Solution : W e wi U w ri te th e gr am m a r us in g d ot op
er at or .
lo : S' ➔ • S
S ➔ • Aa
S ➔ •b A c
S ➔ •Be
S ➔ • bBa
A ➔ •d
B ➔ •d
ne w
l fr om I . Ea ch go to (I) w ill ge ne ra te
sy m bo
N ow w e w ill ap pl y go to on ea ch 0
su bs eq u~ nt st at es .
go to (I 0 , S) S' ➔ S •
go to (I 0 , A)
S ➔ A •a
go to (I 0 , b)
S ➔ b •A c
S ➔ b • Ba
A ➔ •d
B ➔ •d
go to (I o, B)
S ➔ B •c
Is go to (I 0 , d)
A ➔d •
B➔d •
go to (12 , a)
S ➔ Aa •
go to (13 , A)
s ➔ bA. C
lg go to (1 3 , B)
S ➔ bB • a
go to (l4 , c)
S ➔ Be•
-th ru st for knowledge
IC A TI ON s® - an up
TE CH NI CA L PU BL
r Syntax Ana/Ysi!
Compiler D esign 2 - 94
S ► b /\ c •
hulo (1 x . a)
S ~ bl3a •
Now we wil l compute FOLLOW of all no n-terminal symbols.
FOLLOW (S) = I $ I
FOLLOW (A) = I a, c
FOLLOW (B) = I a, c I
Action GoTo
State
a b d $ s A B
I
C
. ss ] 2
lo S3
1,
I I
Accept
12 S6 I
13
r I S5 7 8
14 S9
ls r5/r6 r5/r6
16 rt
17 S10
11< SI I
19 r'.:'\
lio r2
III l'-1
The given g rnn,mor iA not SLR(1) oi-. tlw l.r,. ent,·y Lt", tubll! ~hows rcd m'~/ I'\',Jut"
co nflict.
ll ~(;NNIC/\1. PUIJI. ICA r,oN!i 0 "" Ill) '"' (ISi ro, /(.11ow/odlJu
-c ompiler Design 2- 95 Syntax Analysis
i) Parsing "bdc"
- - - -
bdc $
Action table
IO, bl = S3
Goto tabl e Pars ing action
Shift
Sh ift
I $ Ob3 de$ 13, d] = SS
$ OdS
dd $
d$
[O, d] = 55
No jlCtio n defin ed
No action
i
is inva lid strin g for give n gram mar .
The strin g "dd " can not be pars ed as it
Review Questions
AV : May-04, Marks 2
1. Define LR(O) items.
AU : Dec.-05, Marks 2
2. What are Kernel and non-kernel items ?
parsing table. AU : Dec.-09, Marks 8
3. Give the algo rithm for constructing an SLR
AU : Dec.-17, Marks 5
ple.
4. Explain LR parsing algorithm with an exam
s 13
AU : May-06, 07, 10, 18, Dec.-10, Mark
flII Canonical LR Parsing
bol i:-
ing tech niqu e in wh ich a look a head svm
• The cano nica l set of item s is the pars is
s. He nce the collection 0 f set of itt•m s
gene rate d whi le con stru ctin g set of item
refe rred as LR( l).
tlw
tha t the re is one loo k,1 hc.1d svm hP I in
:-('t
• The va lue ] in the brac ke t indi ca tes
of item s.
thl'
bnb jc; th a t tlw f'clr:-- (· r (\111 d l•~·id (·
• The sign ifica nce of look ahe ad sym
sy mbo k I ll'nce thi:-- p.us in~ pn)C l'dur ('
redu ctio ns base d on th <.· k look a head
beco mes a n effic ient o ne.
®
TECHNICAL PUBL ICA T/ON S - an up-th rust for A.nowledgo
Compiler Design 2 - 96 Syntax An
◄
alys,s
• We follow the same steps as discussed in SLR p arsing technique and those are
1. Construction of can onica l set of items along with the lookahead .
2. Building canonical LR parsing table.
3. Parsing the input string u sing canonical LR parsing table.
Construction of canonical set of items along with the lookahead
· ■.r1111w•~M"'-i'im11~ana"i1
1 · .,+•u-i
1. For the grammar G initially add S' ➔ • S in the set of item C.
2. For each set of items Ii in C and for each grammar symbol X (may be terminal or
non-terminal) add closure (Ii, X). This process should be repeated by applying
goto((Ii, X) for each X in Ii such that goto((Ii, X) is not empty and not in C. The set
o f items has to constructed until no more set of items can be added to C.
3. The closure function can be computed as follows :
For each item A ➔a •X 13, a and rule X➔ y and b E FIRST$ a)
such that X ➔ • y and b is not in I then add X ➔• y, b to I.
4. Similarly the goto function can be computed as : For each item
[A ➔ a• Xj3, a] is in I and rule[A ➔ a X •13, a] is not in goto items then add
[A ➔ aX •13, a] to goto items.
This process is repeated until no more set of items can be added to the collection C.
Example 2.14.1 S' -t S
S ➔ CC
C ➔ aCld
Construct LR(l) set of items for the above grammar.
Solution : We will initially add S'➔ • S, $ as the first rule in 10 . Now match
S'➔ • S, $ with
[A ➔ a •Xl3, a]
Step 1 : S'➔ • S,$
A ➔a •Xl3, a
A = S', a = E, X = S, 13 = E, a =$
Jf the re is a p roduction X ➔ y , b then add X ➔ • y, b
:.s ➔ • CC, b E FIRST (13 a) For $ a) w e consider S' ➔ • S,$
b E FIRST(£ $) as E $ =$
b E FIRST ($)
b = l$1
A =S'. a =£, X = C. J} = C, a = S
If there i~ a production X ➔ y, b then add x ➔ • y ,b
:. C- • aC, Here b E FlRST(J3 a)
c- • d b E FIRSr(C$)
Hence Io:
S' ➔ • S, $
S ➔ •CC,$
C ➔ •aC, a / d
C ➔ • d, a / d
Here a/ d is used to denote a or d. That means for the production C➔ • aC, a and
C➔ •aC, d.
C ➔ •aC, a/ d
C _➔ •d, a/ d
Hence
gotc>Oo, S)
11 : S' ➔ S-, $
t2 : );oto(\ 0, C)
S ➔ C• C, $
C > • tlC, $
C -➔ • d, $
As A = C, a = a, X = C, ~ = £, a = a/ d
H ence X -➔ • y , b
C -➔ • aC b FIRST(~ a)
E
C -➔ • d b E FIRST(i:: a) or FIRST(£ d)
b = a/d
He nce
13 : got o(Io, a)
C-➔ a • C, a / d
C-➔ • aC , a /d
C-➔ • d, a/ d
No w if w e app ly got o on d
of Io in the rule C -➔ • d, a /
a/ d hen ce 1 bec om es d the n we get ( ---) d •,
4
14 : goto(lo, d)
C -➔ d •, a/ d
C➔ a• C, $
A➔ a •X (3, a
A = C, a = a, X = C, f3 = E, a =$
X ➔ •Y
C➔ • aC and C ➔ • d
b E FTRST(f3 a)
b E FIRST(E $)
b=$
16 : goto (l2, a)
C ➔ a• C, $
C ➔ • aC, $
C➔ • d, $
17 : goto (I 2, d)
C ➔ d •, $
1 and 14 respe ctive ly and there
Now if we appl y goto on a and d of I3 we will get 3
y goto on C of 13 .
is no poin t in repe ating the state s. So we will appl
I8 : goto (I 3, C)
C ➔ aC •, a/d
19 : goto( l6' C)
C➔ aC •, $
y goto . Henc e the procc~:- t)f
For the rema ining state s 17, I8 and 19 we cann ot appl
the set of LR(l ) item:- con:- i:-t-- L) f
cons tructi on of set of LR(l ) item s is comp leted . Thus
10 to 19 states.
®
TECH NICAL PUBL /CA TIONS - an up-thr ust for knowledge
Compiler Design 2 - 100 Syntax Ana/ ,
Ys,8
lo: 15 : goto (1 2 , C)
S '➔ •S , $ S➔ CC• , $
S➔ •CC. $
16 : goto (1 2, a)
C ➔ • aC , ai d
C-4 • d. a/d C '➔ a•C , $
C➔ • aC, $
11 : goto (1 0, S) C➔ • d , $
S'➔ S •, $
Ii goto (1 2, d)
12: goto (1 0, C)
C➔ d•, $
S➔ C • C, $
18: goto (1 3, C)
C---1 •aC, $
C---1 aC•, a/d .
C---1 •d, $
14 : goto (1 0, d)
C---1 d •, a/d
Algorithm :
1. Initially construct set of items C · = 110, I1, I2 .. -1nl where C is a collection of set of
LR(1) items for the input grammar G'.
2. The parsing actions are based on each item Ii. The actions are as given below :
a) If [A ➔ a • a ~, b] is in Ii and goto(Ii, a) = Ii then create an entry in the action
table action[Ii, a] = shift j.
b) ff there is a production [A➔ a •, a] in Ii then in the action table
action[li, a] = reduce by A ➔ a. Here A should n ot be S'.
c) If there is a production S' ➔ S •, $ in Ii then action[i, $] = accept.
.3. The goto part of the LR table can be filled as : The goto transitions fo r ·state i i~
consid ered fo r non-terminals only. If goto(Ii, A) = Ii then goto[Ii, A] = j.
-J.. All the entries not defined by rule 2 and 3 are consid ered to be "error".
®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
Syntax Ano/yaw
c omp1/e1 Dos,gn 2 - 10 I
Exnmplo 2.14.2 C<>11~t, 11ct 1111' IR ( I ) 1mrsi11g tnblc for tlw f ollowi 11.1J, 8,rllllllllflr
I) S -> CC
2) C ➔ ,1C
3) C -> d.
)
Solut ion :Firs t we wi ll const ruct the set of LR( I) items.( sec o n next page
2.14.1 .
The OFA for the set nf items ca n be draw n as s hown in Fig.
atJ, b I as
Ncrw consi der 1 in w hich there is a rule match ing with IA -> a •
0
the state J:\· H e nce w e
C-> • nC, n / d a nd if the goto is applie d on a then w e get
wi ll c reate entry ciction[O, a ] = s hift 3.
lo: 15 : goto (1 2 , C)
S'➔ • S, $ S➔ CC•,$
S---+ •CC, $
16: goto (1 2, a)
C➔ • aC, a/d
C ➔ • d, a/d C➔ a• C, $
C➔ • aC, $
C➔ • d, $
t1: goto (t 0 , S)
S ' ➔ S •, $ Ii goto (1 2 , d)
C➔ d•, $
12: goto (t 0 , C)
S➔ C • C , $ 18: goto (1 3, C)
C➔ • aC, $
C➔ aC•, a/d
C➔ • d,$
19: goto (1 6 , C)
t3 : goto (1 0 , a)
C➔ aC•, $
C➔ a• C, a/d
C➔ • aC, a/d
C➔ • d, a/d
14 : goto (1 0 , d)
C ➔ d •, a/d
Simil arly,
In 10
C-> •d a/ d
A->a • a f3, b
A = C, a = £, a = d , f3 = £, b = a/ d
goto (lo, d) = 14
hence action[O, d] = s hift 4
For state 14
C-> d •, a / d
A -, u. •, a
A = C, c.£ = d, a = a/ d
actionl4 , a l = reduce b Y C -, d i.e. rul e 3
S'-, S •, $ in I 1
So \\ e \, ill crea te action[l , SJ = accept.
C C
a C
a C
~ ©
goto (1 0 , S) = 1
1
goto (I O, C) = I
2
goto (I 2 , C) = J
5
Fl)r instctnn.• goto (lo, S) = I 1, I fe nce g o to [O, S] = 1. Continuing in this fa shion "'·e
l ,111 till up tlw LR( I) pnrsing tab le as fo llo ws.
®
TECHNICAL PUBLICATIONS - an up-thrust for know/edge -
Compi ler Design 2 - 103 Syntax Analys is
Actio n Goto
State s
a d $ s C
0 sJ s4 l 2
Accep t
2 s6 s7 5
3 s3 s4 8
4 r3 r3
5 rl
6 s6 s7 9
7 r3
----
8 r2 r2
- ----
9 r2
I
$ acti on(5,$ J=r1 [O,S]= l
$0C2C5
$051 $ accep t
® dge
TECHN ICAL PUBLI CATIO NS - an up-thru st fot knowle
- -
compiler Design
r-xample 2.14.3
2
S ~ AA
A -4 Aa I b iM•Mwtii•M?tii/tj
Sol ution : \\'hill' l'lm~tru cting CL R par<;ing tab le• firc.,t of a ll we ha vt· to built , a
. . . .
Ill
n<,r11 , ;, 1
-.l'I ,11 item~ " ·1th l111<' look.~1he.1d symbo l. The canoni ca l set o f LR(1) 1tc m H ifi a-; fcJII
{JIN<i
S' -t • S, $
S -t • AA , $
\ A ➔ • Aa , b
A ➔ • b, b
I1 goto (l0, S)
S' ➔ S•, $
L : goto(10, A)
S ➔ A • A, $
A ➔ A • a, b
A ➔ • Aa, $
A ➔ • b, $
11 : goto(lo, b)
A ➔ b • , bla
I~ goto(!.,, A)
S ➔ A A • ,$
A ➔ A • a,$
I:; : goto (1 2 , a)
A --, Aa • , b
I,, · goto (I ~, b)
A - ,. b • ,<i>la
l7 · gr ,t() (J4, d)
A ... Aa • <ii
1 Accept
- - - --
2 S5 S6 4
3 r3 r3
4 S7 rl
- - -
5 r2
6 r3 r3
7 r2
Example 2.14.4 Consider the Context-Free Grammar (CFG) depicted below where "begin ",
"end" and "x" are all terminal symbols of the grammar and stat is considered the starting
symbol f or th.is grammar. Productio.n s are numbered in parenthesis and you can abbreviate
"begin" to "b " and "end " to "e" respectively.
Stat ~ Block
Block ~ begin block end
Block ~ Body
Body~ x
i) Compu te the set of LR(l) items for the this grammar and draw the corresponding DFA.
Do not f orget to augment the gra~nmar with the initial production S ~ Start$ as the
production (0).
ii) Construct the corresponding LR parsing table. AU : May-18, Marks 13
Solution :
We will rewrite the grammar using abbreviations as
- - - - - - -------- -- - ®
TECHNICAL PUBLICA TIONS - an up-thrust fo, knowlec lye
11 • , ort<.I ru 110n , f t,,nmu, ,1I v t , f tt,·rr I.P.(t, Id I ~),.
",' ' •"· s
,~ , . ( (
,:!OtcJI , '°:J
I .,_
B --, b • I~, S
B , • bl~:,,:
B , • C., (:
I• gcit.d l ,, , CJ
•
B , ( •, $
1- w,tcfl ,,, / J
( , / •, s
I r, got<fl -~ , BJ
I~ , b • Hu, c•
11 ., • ( , r•
( I • .(, ( •
Is gotc:(1 3 , C)
B ~ C •, e
19 gotc:(13 ,x)
C ~x •, e
110 gotc:(1 6 , e)
B ~ bBe •, £
111 gotc(I 7 , B)
B ~ bB • e, e
I 12 gotc(I 11 , e)
B ~ bBe •, e
The LR pars ing ta ble will be
r1
2
- - -
6 8
3 S7 S9
r3
➔
- --
r4
5 -- - -
- -
S10
6
---- -- - ---
11
7
- - -- - -
8 r3
9 r4
- ----
r2
JO
11 S12
12 r2
®
TECH NICAL PUBL ICA T/ONS - an up-thrust for knowl edge
( 1'111 / u/, / / lt1•, t,/I
I
Syntri;, I\
, .,,
ll;it,,,
R1vl1w Qu11tlon•
Iv, : goto (1 , a)
0
( --, a • C ,a /d /$
C 1 • a( , a/ d / <\,
C - >• d, a/ d/ <,
Ll'I u<-, lnke on(• l' XM ll J) IP l0
pa r<,t•r. uncIc r:--la nd lhc co ns tru
cti on nf LR( I) itc rns
for L,\ L~
- - - - - - -- -- - --
TECH NICAL PUBLICAT/O
NS® - an up-thrust for
-- ----
kno wle dg e
? · 109 '";yr,t1r/ A M/yr.iii
~ -
di·c®~'ill'''
ac
s -> cc
C ~ rl
cmi~tmrt ~ct of LR(l) items for LALR parser.
Solution : rir:-t vv e vv ill constru ct set of LR( I) items.
12 : goto (1 0 , C)
17 : goto (1 2 , d)
S➔ C • C, $ C- > d • , $
C➔ • aC, $
C➔ • a,$
'a :goto (1 3, C)
C➔ aC •, aid
13 : goto (1 0 , a)
C➔ a• C, a/d lg : goto (1 6 , C)
C ➔ • aC, a/d C➔ aC •. $
C➔ • d, aid-
14 : goto (1 0 , d)
C ➔ d •, a/d
15 : goto (1 2 , C)
lo:
S'➔ • S, $ S➔ CC•, $
S➔ •CC, $
18 g: goto (1 3 , C)
C➔ • aC, aid
C➔ aC • aid/$
C➔ • d, aid
11: goto (1 0 , S)
S'➔ S • , $
1 : goto (1 0 , C)
2
S➔ C • C, $
C ➔ • aC , $
C➔ • d , $
We h a H ' m ergL'li two states l'.\ and 16 and made the second compon ent as a or d or
$ . The producti on rule w ill rernai.n as it is. Similarly w e di.d m 1 and 1 . The
4 7 set of
it~m~ consist of sta tes I 10, 11, 12, 136, 147, 1 , 139\
5
Step 2 : Merge the two states L and Ii if the first compon ent (i.e. the producti on rules
w ith d ots) are matchin g and create a new state replacin g one of the older
state such as Li = L u I;
Step 3 The parsing actions are based on each item L. The actions are as given
below:
a) lf [A ~ a • a 13, b I is in Ii and goto (Ii, a) = ~ then create an entry in the
action table action[I, a) = shift j.
b) lf there is a producti on [A ➔ a• , a] in Ii then in the action table
action [I i , a]= reduce by A ➔ a. Here A should not be S'.
c) lf there is a producti on S' ➔ S •, $ in Ii then action[i, $] = accept. ·
Step 4 : The goto part of the LR table can be filled as : The goto transitio ns for state i
is consid ered fo r no n-termin als only. If goto(Ii, A) = ~ then goto[li, A] = j.
Step 5 : If the parsing action conflict then the algorith m fails to produce LALR parser
and gramma r is not LALR(l) . All the entries not defined by rule 3 and 4 are considered
to be "erro r" .
Example 2.15.2 5 ~ cc
C➔ aC
C➔ d
Construe / //,e pnrsing /able for LA LR(l) pnrser.
Solution : First th e set LR(1) items can be constructed as fo llows w ith m erged states.
®
TECHNICA L PUBLICAT IONS - an up-thrust for knowledge
c0 mpile
---_r _De_s_,g_n_ _ _~ - - - -- - - -~2...=-__:_1~1!___
1 _ _ _ _ _ _ _ _ _ _~~~~
~
Synt ax Ana lysis
l o:
s·➔ • S, $ 136: goto (1 , a)
0
S➔ •CC , $ C➔ a• C, a/d/$
C➔ • aC , aid C-) • ac, a/d/$
c➔ • d , aid c-) • d , aid/$
d $ s C
a
-
2
',} 6 !>47
0
-- ---- -- -- -- -- -:=='®
TEC HNI CAL PUB LIC A
T/ON S
-
- an up- thrust for knowledge
-- - -
I fl/I IJli/rJ/ / )tl ,~/{J/1
/ I/ ;>
Synt nx Anfl /
Y'iln
/\,I,.,•I
'
~ i, \ 11 ,. 1,' ,.
)
Kll
r2
Now we will construct canonical set of LR(l) items for the above grammar.
lo : S' ➔ • S, $
S ➔ • Aa, $
S ➔ • bAc, $
S ➔ •de,$
S ➔ • bda, $
A ➔• d, a
In above set of items we will start from S. ➔ • S, $. The second component is $
initially. After • the S comes, hence will add the rules deriving S. Now we have got
the rule.
S ➔ • Aa, $
It is resembling with A ➔ a • x ~, a
Now we can map x to A then the second component is FIRST(~ a)
In our rule
A ➔ • d and second component is FIRST ($a) = a
Hence A ➔ • d , a will be added in Io·· ·
11 : goto (Io, S)
S' ➔ S •, $ We will carry second component as it is.
I 2 : goto (Io, A)
S ➔ A • a,$
I3 goto (J O, b)
S ➔ b • Ac,$
S ➔ b •da, $
A ➔ •d, C
._. S ➔ b • /\c, $ and 1:1RST(lt1) . 1
= FIRST(c$) - l' · llcncl'
r
Sl'Cond cnmp\l1wn t 1)1 1\ > e(i ,~ c
Qi)
TECHNICAL PUBL/CA T/ONS - an 11p-thnist for knowledge
? 114
'/
c n111r,IIr,1 I 111 • 1r111
II goto (l o, d)
s , d • <,'li
A , d •, ,,
11, guln (1 ,, A)
<-; I hi\ . l, 'l,
17 gl>lo (I 1, d)
~ > bd • c1, ~
A I d • ,C
State Action
Go~
a b C d $ S A
fl ', 1 ..,4 2
Acn•pt
2 <, ')
..,7 6
I l .. 1-1
- _..J- - - - - -
r..:
hl
Stack
Input buffe r Action
bdd,
Shift J
d,·$
Shift 7
$0b3d 7 t·$
Red llCL' by /\ )J
$0bJA6 ,·$
Shift 9
SOb3A6c9 $
Rcdw.' L' by S , bAc
$051 $
Accept
B ➔ d
1) S ➔ Aa
2) S ➔ bAc
3) S ➔ Be
➔) S ➔ bBa
5) A ➔ d
6) B ➔ d
®
TECHNICAL PUBLIC A T/ONS - an up-thru st for knowle dge
2 11 6
Compiler Design
compPt1L't1 I in IIH.'Sl' p rl,ullL"l1 t 1n 11, Il .,:-< ,.viii lw ·$ · Thh-· 1 11-1 hl•CO lll-ll' fur IS' , , • S, 1, 1th' . ['
J • •
1 ,
_J
,ltlCI ,l -
_ ,,
J"I
• : ,~ - (IJ )
• • 1 11' "- 1 l ,l
~ Fll<ST (r :f,)
, ,
Fll<ST ($) $. I lt•n c.:,· w,• w ,11 gt•I
\
S' _. . ~- $
~
5 -4 • bAI..', $
S _. • lk. $
~ -4 • bB.1 , $
:'\ow we h,Wt' got one rule IS➔
•Aa, $ ·1 which is m atching with lA --4 a • Xfl, a I
anJ I-' --4 y]. Hen ce w e w ill add the closu re o n A. Hence IA ➔ •d I will be added in
this list. Nov, the second component of A ➔ •d will be d ecided. As IS ➔ • Aa, i l i.,
mdtching " -ith IA --4 A • X B, a] having X as A, ~ as a and a as $.
Then FlRST (Ba) = FIRST (a$) = FlRST (a) = a. He nce rule [A ➔ •d, a] will be add ed .
s· ➔ • S, $
S ➔ • Aa, S
S ➔• bAc, Si
S ➔ •Be.$
~
A ➔ • d, a
Now notice the rule S ➔ • Be, $. It suggest to apply closure on B (As after dot B
comes immediately) This rule is ma~ching with [A ➔ a •X ~, a] and [X ➔ y]. Hence
we will add rule deriving B. Hence B ➔ • d will be added in the above list. Now
S ➔ •Be, $ can be mapped with [A ➔ a •X ~, a].
where a = E
X= B
~ = e
a = $
Hence FJRST (!1 a) = FIRST (e $) = FJRST (c) = c.
Hence ru le [B --4 • d, e J will be added.
Hence finally I O w ill be -
lo· S' --, . c;
S -j • Aa, <J,
S ➔ •Be,$
S ➔ • bBa, $
A ➔ • d, a
B ➔ • d, C
carried as it is.
S' ➔ B•c, $
B ➔ d•,C
16 : goto (1 2 , a) l9 : goto (1 3 , d)
S ➔ Aa•, $ A ➔ d•, C
B ➔ d• ,a
I7 goto (I 3 , A) l1 0 : goto (1 4 , c)
s➔ bA. C, $ S ➔ Be • , $
®
TECHNICAL PUBLICA TIONS - an up-thrust for knowledge
lg goto (1 3, B) l 11 : go to (17 , c)
S ~ bB • «, $ S ~ bA c • , S
I 12 go to (l s, a)
S ~ bBa • , $
'\o "' u,m g th -.,. abo ve set of LR(1) i terns we w i II con str uct
LR( 1) par sin g tab le =·
fl1l
kw,-. :
Act ion Go to
Sta te
a b C d $ s A B
0 s3 s5 2 -l
AC CEPT
2 -,6
~
s9 7 s
-t s lO
:; r, r6
h
rl
-,
s 11
~ c., 12
'-) r6 r5
l l)
r3
11
r2
12
r-!
Sta ck
Input buffer
Ac tio n
:,0
bd a$ Sh irt 3
'>r>h3
d a$ Sh ift q
',{J..., d9
a$
SOh~B~
a$
Sh ift 12
-., 1b1B 8a1 2
$
$OS]
$
Now ,,ve will construct a set of LALR(l) items. In this constructi on we will simply
merge the states deriving sa me production rules w hich di ffe r in their second
components only. ln above set of LR(1) items state 15 and 19 are such states i.e.
goto (1 0 , d)
'~ 19 : goto (1 3 , d)
A ➔ d• , C
B ➔ d • ,a
s➔ b•Ae, $ S ➔ bBa • , $
S ➔ b • Ba, $
A ➔ . d, e
B ➔ • d, a
14 goto (1 0 , B)
s➔ B • e, $
I 59 : goto (1 0 , d)
A ➔ d • ,a/e
B ➔ d•, a/e
®
TECHNICAL PUBLICATIONS an up-t/1111s/ fo, kno1,toch10
Comp1l01 Oes,g n
2 - 120
◄
Syntax Ana/
State
Actio n Coto
-,
a b C d $ s A B
()
ii'.\ s5 1 2 4
ACCEPT
2 sh
:1 s9 7 8
4 s ill
59 r'1 r5
r6 r6
6
rl
7 sl I
8 1-1 12
10 r:1
11
r2
12
~
Thl' pa n1in >,; tnb l,· 1-1huw 1-1 rnu llipk PnlriL·H in t\ctin n
[59, al nnd Actic1n [5(), ~'l. fhi~1'
ca lll'J rt'du <.:(•/rc ducc confl ict. lk•l ',Hll'I<' of thi1-1 r 11nflil..·t
w1.• cn nnnt p.H~1..' input .
T l ,uH il i~1 Hlluw 11 lhu t Fiivt•n >,;rnn imnr i1-1 1.1{(1) bu
t 1Hit- l.1\1.R (I)
E,wm plo 2.16.6 Co11ultf c1· I lit' Ji>llow l 11,'-! ,'-! rt111111111t'
I , .' i ' I, I<
'/.. s ' I<
,l I, > M / ~
1. I, ' /,t
!' , I< ' /,
I
/ )/ti/ /1 /1/1 f/11 • / .,J\, I •l'1 /I// l'II I 11,~ ,,,,. I {111, I Ill' , I, //I ,'l 1 ' ...
l'l/11//11111' , I .i~I ,1111 .
1'111/11 ///( ,,/ l ()//1 1 t h ,,, ... ·11 " ,,
n, /111/ l' l/1 I II /llll'til llM 111/,/,•,
II r , / IN /( ,Al I 'I II II /1 '/I IION~l110 , 111 ,,11 111111 st fo, ~ ) 11 /nd tJ' '
111
c ompiler Design 2 , ,, ,
Solution: To deri v t> lhl' gi ven grn nu nor w dng 1./\ 1,1{ 11 1pt'1 ud Wfi l1,1v1• 111 111,l ld 11 11•
canon ica l co lll'( l ion o f item s w ~in g LI{ ( I ) il PlllH.
The LR(l ) ill-m s arC:'
10 s· -) • S, $ If, golu (1 , = )
2
S 4 • L = R, $ S --> I, - • I<, ~;
S -) • R, $ I{ ➔ • 1,1 $
L -) • * R, = 1$ L ➔ • * R, $
L -) • id, =I $ L -) • id , $
R -) • L, $
II : goto (10 , S) 17 goto (1 4, R)
S' -) S • , $ L -)•l: R• , = I $
'2 : goto (10 , L) 18 : goto (1 4, L)
S -) L • = R, $ R -) L • = 1$
R -) L• ,$
l9 : goto (1 6 , R)
5 -) L = R• ,$
13 : goto (10, R)
S-) R •, $ 1, 0 : goto (16, L)
I4: goto (10 , *) R -) L• ,$
L -) * • R,
= I$ 1, I : goto (1 6 *)
R -) • L, = I $ L -) * • R, $
L-) • * R, = 1$ R -) • L, $
L -) • id, = I $ L -) • * R, $
L -) • id, $
l I2 : goto (l6, id)
L -) id • , $
Is : goto (10, id) 113 : goto (I 11 , R)
L -) id •, = I $ L -) * R• , $
From above set o f item s w e have got
i) 14 and 111 g ive sa me p rodu ctio n but lookheads n n.:' d iffl' rl·n l. I ll·111:1.· 11 11 ·q·,i· ti w in 111
form 14 11
ii) 15 = 112 He nce 1512
iii) 17 = I 1:1 H ence 17 1~
iv) 18 = 110 Hence 18 10
(1,1
TEC HNICAL PUBLICATIONS - .in 11p- tl 11u ~t for kn nwln<f1ir>
Synta/ Ar ,
1 1
2 - 122
Compiler Oe~1gn
S ➔ • L = R, $
S ~ • R, $ I810 ·· R ~ L • ,=1$
L ➔ • * R, = I $
L ➔ • id, =I$
R ➔ • L,$
I1 : S' ➔ S •, $
12 : S ➔ L • = R, $
R ➔ L • ,$
13 : S ➔ R •, $
1411 : L ➔ *• R, = I $
R ➔ L • ,= 1$
L ➔ • * R, = 1$
L ➔ • Id, = I$
1512 L ➔ id •, = I $
16 : S ➔ L = • R, $ L ➔ • * R, $
R ➔ • L, $ L ➔ • id,$
The parsing table can be constructed as follows -
Action Goto
State
id * = $ s L R
0 s5 s4 1 2 3
- ~
--- ----
1 ace
2 s6 r5
3
r2
-- -
4 s5 s4
8 7
5 r4 r4
6 s12 s11
10 9
7 rJ r3
J
8 r5 r5
10
- rl
r5
11 s12 s11 JO 13
12
r4
- - -
13
r3
Action Goto
State
id • = $ s L R
-
0 s512 s411 3
- l
--
2
=
-- 2 -- -
ace
S6 rS
3 r2
411 s512 s411 810 713
512 r4 r4
6 s512 s411 810 9
....
713 r3 r3
.
810 rS r5
9 r1
Example 2.15.6 Design· an LALR parser for the following grammar and parse the input
id= id
S ➔ L=R I R
L ➔ * R l id
R ➔ L AU : Dec.-13, Marks 16
Parsing input id = id
---------- -----------,
Stack Input buffer Action
®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
_ ', ;r,/,1/ / . , ,., . ,
/ 12 4 1
'-, I ii . "
,. I I dI I :,,
., P l )
'"" ,, r• d II I
,, . . , I •
I
"., (JI 2 t, f ( / r, d 1H , . ti :, , , I.
(
r'
Example 2.15. 7 W n tt• llw nlgnn th rn f or ron.,/ructwn of 1./\ IY p1w ,tn7, tab/I, fr/r o 1/_n1-n
s, ran111zar. LJ,.., ,11g //,,· algorith111 f or ron<,lrurlwn of / ./\ I .I<. pa r<. mY, tahlf, rt/n 4-lrur I Jhv I.I-./.?
par~ing table f or the f ollowinx :zrammar
S' ~ S
S ~ aAd I bBd I aBe I b/\f
A ~ C
B~ c AU : Mav•l9, ~ U
Solution : A lgori th m for cons tructin g LA LR ta b le - Rdt=-r ¼:c.ti,,n 2.1 ::;,
Step 1 : Con sider the gi ven g ramma r, in whi ch p rod ucti<>n ruk~ ;;, n; nu ml-w•n.:d
(1) S' ➔ S
(2) S ~ aAd
(3) S ➔ bBd
(4) S ➔ aBe
(5) S ➔ bAe
(6) A ➔ c
(7) B ➔ c
10 S' ➔ • S, $
S - > • a Ad, CS
S ~ • bBd, 'S
S - > • a Be, $
S ➔ • bA e, 'l>
fI got o (I c, , ~)
s· > c., •, i
~
rr (, lff}/( ,/1.l Pl/W J(, /1.. /l()IJ' ; nr, llf, ,,,,,,,., ,,,, ,,,,, ,,11/q1/1v ,
-- compiler Design SyntaY Ana/ySIS
2 - 125
12 goto (1 0 , a)
S ➔ a • Ad ,$
S ➔ a • Be,$
A ➔ • c, d
B ➔ • c, e
1--_, goto (1 0 , b)
S ➔ b • Bd, $
S ➔ b • Ae, $
B ➔ • c, d
B ➔ • c, e
I 4. goto (1 2 , A)
S ➔ aA • d,. $
ls goto (1 2 , B)
S ➔ aB • e, $
16 goto (1 2 , C)
A ➔ C • d
B--, c • e
I7 goto (1 3 , B)
S--, bB • d , $
Is goto (1 3 , A)
S --, bA • e, $
19 goto (13 , c)
A --, c • e
B --, C . d
1 10 goto (I 4 , d )
S --, a Ad • $
(I,)
TECHNICAL PUBLICA 7 IONS · An up f/1r11 st fc11 /..nowlt'ct.1,'
p_,1e
1 2_6_ _ _ _ _ _ _ _ _ _ _sYntax 4
_e_s....:ig:...n_ _ _ _ _ _ _ _ _ _ _ _ _2_ - _
_r_D
◄
c _o_m..:.._
_
~
l12 goto (l 7 , B)
S ➔ bBd •, $
:A ➔ c•,e
16:A 4C • ,d 1
9
B ➔ c•,e B ➔ c•, d
B ➔ c •, d/e
Action Goto
a b C
d. e $ s A B
0 S2 S3
1 Accept
S69 4 ·s
2
8 7
3 S69
4 S10
5 S11
69 r6/r7 r6/r7
- ---
7
12
----
8 s13
10 r2
11 r4
12 r3
13 r5
® knowledge
TECHNICAL PUBL/CA TIONS - an up-thrust for
c ompiler Design 2- 127 Syntax Analysis
Example 2.15.8 Show that the following grammar. S ➔ Aa bAc I de I bda , A ➔ a is LALR
(1) but not SLR (l). AU : Det· .- J8. Marks 13
Solution :
Step 1 : We wiJl first find if the given grammar is SLR (1) or not. Firstly, we will write
the grammar using dot operator.
Step 2 :
Io S' ➔ • S
S ➔ •Aa
S ➔ •bAc
S ➔ •de ·
S ➔ • bda
A ➔ •a
Now we will apply goto on each symbol from 10 , each goto will generate new
subsequent states.
11 : goto(l 0 ,S)
12 goto(l 0 ,A)
S➔A •a
I3 : goto (Io, b)
S ➔ b•Ac
S ➔ b•da
A ➔ •a
I4 goto (I 0 ,d)
I5 goto(I 0 ,a)
A ➔ a•
S ➔ Aa•
17 goto (I 3 , A)
S ➔ bA • C
®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
Cw11p1lfJI U m11u11
i1 , ;,t)
S > h/ \ c •
1 11 • go l o( l ~, ;1)
S > hd ;1 •
7 SHJ
8 sn
9
r3
10
r2
JI
r4
St ep 4 No w w e wi ll co ns tru
ct ca no ni ca l se t of ite ,...,.,..
ca no ni ca l se t of ite m s m s wi th lo ok a he ad
is i.e. LR (l •· ·
lo S' ~ • S, $
S ~ • Aa , $
S ~ • bA c, $
S ~ • d e, $
S ~ • bd a, $
A ~ • a, a
S --) /\ • n , $
1.1 goto(l 0 , b)
S ➔ b • Ac,$
S ➔ b • da, $
A➔ • a, c
14 goto(lo,d)
S ➔ d • c, $
ls goto (1 0 , a)
A ➔ a • ,a
16 goto (1 2 , a)
S➔ Aa •, $
17 goto (1 3, A)
S ➔ bA • c,$
13 goto (13,d)
S➔ bd • a,$
l9 goto (l4, c)
S➔ de• ,$
I 10 goto (1 7 , c)
S➔ bAc • , $
l11 goto (13, a)
s~ bda • , $
The pars ing table is
--
a b
Action
C d $ s
Goto
A
l
0 S5 Sl '-w '
:\ 1 n •l'l
2 ~h
1 '-,5 ",>~
4 '°l'° l
L 5 r5
(!v
TECHNICAL PUBLICATIONS · o n 11p- thw s l fn r l..11owlo<i<10
/ I 'I(}
Comp iler Design
I I
6
~ 1(1
7
-8 S il
9
r:l
10
r2
J1 r4
5. It requir es less time and The time and s pace The lime .:i nd 11pace
space comp lexity . · compl exity is more in LALR comp lexity iH mon.: for
but efficie nt m e thod s exist ca n onica l LR par"lf.: r.
for constr ucting LALR
~-- --- --- --- --- --'' --- --- =-- --- --- --- --- --- --_ ,
parse rs directly .
y is as given be lo w
Grap hjcal repre senta tion for the class of LR famil
LR (1)
LALR (1)
SLR
8
Fig. 2.1 6.1 Class ificati on of gram mars
dge
TECHN ICAL PUBLI CA T/ONs® - an up-thru st for knowle
2
pi/er oesign:___ _ _ _ _ __ _ _ _ _ _ __-_ 13_1_ _ _ _ _ _ _ _ _ _ _ __:::S'.!_y..:_: ta:..=x~A
n.:..:: :.:._a=.:.l~
_:_n is
y s.=_
~ Handling Ambiguous _
G rammar AU : May-12, 18. Marks 15
~ we have _seen v ario u s pars_ing methods in which if at all the_ gran:una r is
. ous then 1t creates the conflicts and we cannot parse the input stn ng w ith s u ch
,a1J1b~guous gram.mar. But for some languages in which arithmetic expressions are given
111
a b'.gu ous gram.mar are most compact and provide more natural specification as
an1b1gu . 1 b" . .
. ared to eqmv a ent unam 1guous grammar. Secondly using ambiguous gr ammar we
C()OlP d . ti" f . 1
can add an y new pro uc ons or spec1a
constructs.
using ambiguous
•v n IJ, e
\An....· . grammar fo r parsing the input string we wi ll use a ll the
disambiguating rules so that each time o nly one p a rse tree w ill be gen erated for t~at
specific ·
. input. . u o u s gra mm ar can b e u sed in contro lled , manner
Th u s am b 1g . for pars ing
. tnput • W e w ill con _s1'd e r som e a mbig uo u s g ra mma r a n d le t u~ ti -v to pa rse son'\e
the
tnput String.
> rd
N,,\,· w,· will 1, 1ri ld tlw ~l'I nl I .R(O) it em H for thi s gra mmar.
id + $ E
l) !-<2 1
sS Accept
., s2 6
-
} r4 r4 r4 r4
-I ... ..,_, s2 7
5 :-3 s2 8
6 s4 sS s9
7 ~-1- or rl sS or rl r1 rl
8 s4 or r2 sS or i-2 r2 r2
9 r3 r3 r3
.__
$0 CJ ➔ 4E7• 5 id 3
I
$ Rcd11cc by E ---1 id
SO £1 +4E7•5E8 $ Rccl ucc by E -> E • 1,:
By this we ca n perform E * E firs t and the n E+E. The refore as you can see in the
above table of parsing conflict can be resolved by i:ISsigning s hift operation. Hence action
(7, *] = sS.
Similarly if we consider the input "id * id + id$" for process ing .
$0 El +id$ Shift
--- -
$0 E l +..i id$ Reduce by E ➔ id
$0E1+-!E7 $ Reduce ~ _E ➔ E + E
$0El $ Acc;e t
®
TECHNICAL PUBLICA T/ONS - an up-thrust for knowledge
~
Com pile r Oo!.rgn
- -- 2 - 134
~
Syn tax An
IYS/3
Stale Ac tio n ----
Go to
id + ... ( ) $ E
(\ !1-_1 ~2 1
i--t st;
Accept
., ~ s2
-' r4 r4 \
- - -s2 - - r4 r4
4
5
:-.:1
~3 -- s2
h s4 s5 s9
7 rl s5 r1 rl
8 r2 r2
-- --
r2 r2
9 r3 r3 r3
Review Questions
!" t h1 "
• input. m ..thod, on discoverin g e rror parser perform local correctio n on remainin g
• It can replace a prefix of remainin g input by some string. This actually heJps the
parb<?r to continu e its job .
• While doing the replacem ent a care should be taken for not going in an infinite
loop.
Review Question
®
TECHNIC AL PUBUCA TIONS - an up-thrust for knowledge
Compile r Design 2 - 136
- -
~
- Syntax Ana,
- Ys,,
fj(: j YACC
• YACC i~ .1ull1m ,1til' tool fo r gL'nc ra ting the parser progra m .
• YACC s ta nd ~ for Yet Anoth e r Co mpile r Co m pile r which is b asicall
y the ur i·
a\ .1 il.1bk f wm UN IX. I 1ty
• LEX and YACC work togeth er to analys e the progr am syntac tically .
• The typ ical YACC transl ato r can be repres ented as shown in Fig. 2.19.1.
• First we w rite a YACC specif ication file; let us name it as x:y. This
file is given to
the YACC compi ler b y UNIX comm and
yacc x.y
• Then it w ill gen e rate a p arser progr am using your YACC specif
ication file. This
parser p rogra m has a standa rd name as y.tab.c . This is basica lly parser
program in
C gen erated a utoma tically . You can also give the comm and with
- d option
yacc -d x.y ·
• By - d option two files will get gener ated one is y.tab. c and other
is y.tab.h. The
heade r fi le y .tab.h will store all the tokens and so you need not
have to create
y .tab .h ex p licitl y.
Specification
file y.tab.c. and.
YACC compile r with y.tab .h
x.y - d option
y.lab.c
a.out
CC - The C compil er
Input string
Execut able progra m Output
a.out
• Then you can test your YACC program wi th the help of some va lid and in va lid
strings.
• Writing YACC specification program is the most logical activity. This specification
file contains. the context free grammar and using the production rules of context
free grammar the parsing of the input string can be done by y.tab.c.
Let us learn how to write YACC program.
%{
/* declaration section */
%}
%%
/* Translation rule section */
%%
/* Required C functions*/
- ---------
1. Declaration part : In this section ordinary C declarations can be put. Not only this
we can also declare grammar tokens in this section. The declaration of tokens
should be w ithin %1and %).
2. The translation rule section : It consists of all the production rules of context free
gra mmar wi th corresponding actions . For instance
rule 1 action 1
rule 2 action 2
rul e n action n
If there are more than one alte rnatives to a sing le rule then those a lte rna ti ves sho uld
be c;eparated by I character. The actions a re typ ica l C statements. ff CFC is
®
TECHNICAL PUBLICATIONS - an up-thrust for knowledge
Compiler
_ ___ Design 138_ _ _ _ _ _ _ _ _ _ _sYntal( A
2 -_
_ _ _ _ _ _ __ _ _ _ _ _ _
n111Ya,,
LHS ~ alternati ve 1 I a lte rnative 2 I . . ... I a lternative n
Then
LHS : alternative 1 I actio n 11
I alte rnative 2 I actio n 21
alternative n {action nl
®
TECHNICAL PUBLICA TIONS - an up-thrust for knowledge
cr.,mp1tor 0 11 '•''1" ;, 1y , ~/ nl a/ Anal/SI';
/"End o f input•/
\S {rm.um O,}
/*Start Btate • /
start: stateme nt '\n'
I start stateme nt '\n'
(i,J
TECHNICAL PUBLICATIONS - an up-thrust fo r knowlodge
:omo fer Design 2 - 140
'1f
mau {)
.1. ~i u ,error),
,tpru.tfLttl c u ,
I
Declaration part -
%token is used to declare tokens in the YACC. In the above program token NUMBER
can be declared as
%token <dva1> NUMBER
The precedence and associativity can be declared in the above program %left, %right
and %nonassoc declares the associativity of the operator being left associative, right
associative or nonassociat ivity respectively.
The precedence can be declared in an increasing order.
The yylval used is the union of type double. YACC can associate data type with the
token by using this yylval.
%token <dval> NUMBER
It means token NUMBER has the data type double. Any number of data types can be
decla red in union.
%left '-' '+' /*Lowest precedence and left associative*/
%left '*' '/ '
%right '/\' /*right associate*/
%left LOC SINE nLOC COS TAN /* Hi ghest precedence* /
!*No associativity~/
%nonassoc UMINUS /* Una ry Min~•s* /
®
TECHNICAL PUBLICATION S - an up-thnist for J.. nowledgo
Com piler Des ign 2 - 142
Rule section
In the ~le sect ion : (colon) is used to
sep arate LHS and RH S of pro duction
tem1ina tion of eac h rule is giv en by ' rul(: _·rr,.
; '. The $$ is use d as attr ibu te valu
gra mm ar. e at LHS c,f tr ,
be generated fo r th e same input. For example - The ~tring id +id *id ca n be represented
ilS •
E E
~ ~
E + E E E
I
id
~ . ~ I
E E E + E id
I I I I
id id id id
Ans : Recursive descent parsing is a kind of parsing in which the reetusive procedures
can be used for parsing the given input string. It is a top down pa rsing, technique .
Q.S What is the function of parser ?
Ans : There are two important functions that can be performed b y the parser and
those are - parsing of input string and generating error messages if there exists errors
in the input.
Q.6 What is handle ?
®
TECHNICAL PUBLICATIONS • on up-thrnst for A.no',\ /~-,
Comp1lo1 Design ? 114
Q.8 What are the two important rules used in shift reduce parsing ?
Ans: Fo llowin g are the two rules that can be used in shift reduce parsing -
• If the incom ing operat or has more priorit y than in stack operato
r trier
perfor m s hift.
• 1f in stack operat or has same or less priorit y than the priorit y of incomin
~
operat or then perfor m reduce .
Q.9 What are the disadvantages of operator precedence parsin g?
AU : May-06, Dec.-06
Ans : Follow ing are the disadv antage s of operat or preced ence parsing -
1. The operat or like minus has two differe nt preced ence
(unary and binary)
Hence it is hard to handle tokens like minus sign.
2. This kind of parsin g is applica ble to only small class of gramm ars.
Q.10 Explain the kernel and non kernel items.
Ans : Kerne l items - It is the collect ion of items S'-> • S and
all the items whose dots
a re at the left end of the R.H.S of the rule.
Non Kerne l items - It is the collect ion of all the items in which
• are the left end ot
the R.H .5. of the rule.
Q.11 What is viable prefix ?
Ans : It is the set of prefixes in the right senten tial form of a produc tion A->a.
Thi,
~ t ca n appea r on the stack during shift/r educe action.
Q.12 What is the significance of lookahead operator ?
Ana : The significance of lookah ead symbo ls is that the
parser can decide th t'
reduct ions based on the k lookah ead symbo ls. Hence this parsin g proced
ure becoolt')
an efficient one.
Q.13 Elimina te left rec ursion from the followi ng gramm ar A-➔AclAadlbd
[Refer exampl e 2 7 3]
lc . Id
f•HN1
handler In a parser do 1
Syntox Analysis
AU : Mt1y•ll
An•. : Thl' errc•r h;i nd k•r in-parser reports lhl' sy ntncticn l l'l'l'nr if ,my.
Ant. : The LR parsers .1rc ca lled bottom up pnrsl'rs becnuSL' in this type of parsing the
input is p.1rscd frorn bnttom tu top .
Compare syntax tree and parse tree. AU : Dec.-12, 17
Q.16
Ans: Parse trees arc muc h more deta iled representatio n of the source la nguage tha n ,
that of syntax trees. Syntnx trees are sma lle r than pa rse trees and these are much space
Jnd time efficient.
AU : Dec.-12
Q.17 Write the rule to eliminate left recursion in a grammar.
Ans: If the left recursive grammar is
A -> PA'
A ->Aa Eliminated as
A' -> a.A'
A ->~ A'-> E
A ➔ Ac I Sd I £
We can re place S by Aa I b.
The grammar the n b ecomes -
A ➔ Ac I Aad I bd I £
. . . . d' t I ft recurs ion from A productions we get -
By elimmatmg 1mme 1a e e
S ➔ Aa I b
A ➔ bdA' I A'
A' ➔ cA' I adA' I £
AU : Dec.-14
eserit palindrome.
Q.19 Write a CF grammar to repr . .
t0 represent the palmdromc ts
Ans : The context Free(CF) g ra mmar
- -- -
TECHNICAL PUBLICA T ®
·----- ------
IONS - an up-thnist for knowledge
-
compiler Design 2 - 14 7
lng 't
What are the various conflic ts that occur during shift reduce par•
Q.23
..
..,.■ ze--1n1ni
,•;_u
Ans. : The re a re two conflic ts tha t occur during s hift reduce parc,ing
c1 nd th< A(• Ml-'
1n~ th 1•
1. Shift/R educe conflic t : The shift reduce co nflict occurs w h en rrn n-·,,d
next input symbol and s tack to p symbol, the pa rs ing table ,:,c tion c.~ ·< ifw~ ft,
shift action as w e ll as the reduce actio n by so m e gra m ma r r ule
f ecid m Y,
2. Reduce /Reduc e conflic t : The s hift reduce conflic t o ccu r~ wht--n on
the n ext input symbo l and stack top symbol, the p a rs ing tabl e action ~pedf~
m o re than one reduce actions . The action s u gges ts reducti on by d i ffL"l'E•nt
produc tion ruJes.
Constr uct a parse tree for (id + id). AU ; ~c.- 16
Q.24
Ans : The parse tree will be -
+
/ \id
id
Fig. 2.20.2
tic error ?
Q.25 What are various stages that a parser can recove r from a syntac
AU r.>ec 12
Ans. : Parser can recove r from syntact ic error with the help o f followi
ng strateg:it:S
(1) Panic mode (2) Phrase level (3) Error produc tions (4) G loba l correct
ion.
AU ~ c l~
Q.26 Define LR(O) item.
Ans. : The LR(O) item is a produc tion G with dot at some positio n on the right side o-f
the produc tion.
Fo r examp le - S->a .BC
The LR(O) item indicat es that what is to the left of dot has jus t been
read and tne
QI)
TECHNI CAL PUBLIC ATIONS - an up-lhn.Jsl for k1,o"'ie.1.x,r-,
2 - 148
Syntax Ana/
YS1s
Q.29 Write LMO for the word (1+1)(1*1) using the grammar,
E->E+E I E/E I E*E I (E.) .
Ans. : n,'-, I MD(L cl t Most Deriv ation ) for the string (i+i)/ (i*i)
F
E r
tE) I'
tE+l') F
l• +E) ' E
(i+i) E
l •+i) / ( E)
(i + iL ( E* E)
(i+i) ( ii<E)
(i+ i) (i~ )
DOD