Top Banner
7/23/2019 CS0312 Compiler LAB MANUALedited http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 1/70 `  SRM UNIVERSITY Ramapuram, Chennai- 600089. FACULTY F EN!INEERIN! AN" TEC#NL!Y "epar$men$ %& C%mpu$er S'ien'e ( En)ineerin) LA* MANUAL CM+ILER "ESI!N LA* CS0/ CLASS B.Tech. [U.G] YEAR 1 SEM. III Year / 5th Semester +RE+ARE" *Y S.N%. S$a&& Name "e2i)na$i%n Si)na$ure . Mr.S.Arun3umar A+!/ 4 M2.M.S.An$%n5 i)i7 A+!/ Appr%e *5 #"1CSE/ 1
70

CS0312 Compiler LAB MANUALedited

Feb 18, 2018

Download

Documents

miss147
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 1/70

`

  SRM UNIVERSITY 

Ramapuram, Chennai- 600089.

FACULTY F EN!INEERIN! AN" TEC#NL!Y

"epar$men$ %& C%mpu$er S'ien'e ( En)ineerin)

LA* MANUAL

CM+ILER "ESI!N LA*

CS0/

CLASS B.Tech. [U.G]

YEAR 1 SEM. III Year / 5th Semester 

+RE+ARE" *Y

S.N%. S$a&& Name "e2i)na$i%n Si)na$ure

. Mr.S.Arun3umar A+!/

4 M2.M.S.An$%n5 i)i7 A+!/

Appr%e *5

#"1CSE/

1

Page 2: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 2/70

`

SYLLA*US

L T + C

+CS CM+ILER "ESI!N LA* 0 0 4

Prerequisite

  PCS204

+UR+SE

T Practice a!" im#$eme!t the s%stem s&t'are t$s a!" cm#i$er "esi(! tech!iques

INSTRUCTINAL *ECTIVES

1. T im#$eme!t )a"er* )i!+er* ,ssem-$er acr #rcessr 2. T im#$eme!t the ,*,* irst $$' #rce"ures3. T im#$eme!t T# "'! a!" Bttm u# #arsi!( tech!iques

LIST F E:+ERIMENTS ;<

1. Im#$eme!tati! & a )i!+er 

2. Im#$eme!tati! & a )a"er 

3. Im#$eme!tati! & a! ,ssem-$er 

4. Im#$eme!tati! & acr #rcessr 

5. Im#$eme!tati! & a )eica$ ,!a$%ser . C!6erti!( a re(u$ar e#ressi! t ,

7. C!6erti!( , t ,

8. Cm#utati! & I9ST a!" :)):; sets

<. C!structi! & Pre"icti6e Parsi!( Ta-$e

10. Im#$eme!tati! & Shi&t 9e"uce Parsi!(

11. Cm#utati! & $$' a!" Trai$i!( Sets

12. Cm#utati! & )9=0> items

13. C!structi! & ,G

14. I!terme"iate C"e Ge!erati!

15. esi(! & Sim#$e Cm#i$er usi!( Tami$ 'r"s

1. Trace the eecuti! & a!ther #r(ram ? "e-u((er 

  TTAL ;<

2

Page 3: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 3/70

`

TA*LE F CNTENTS

E:+.N TITLE +A!E N

Im#$eme!tati! & a )i!+er  ;

4 Im#$eme!tati! & a )a"er  =

Im#$eme!tati! & a! ,ssem-$er  0

; Im#$eme!tati! & acr #rcessr  ;

< Im#$eme!tati! & a )eica$ ,!a$%@er  66 C!6erti!( a re(u$ar e#ressi! t , 8

= C!6erti!( , t , 40

8 Cm#utati! & I9ST a!" :)):; sets 4<

9 C!structi! & Pre"icti6e Parsi!( Ta-$e 49

0 Im#$eme!tati! & Shi&t 9e"uce Parsi!( ;

Cm#utati! & $$' a!" Trai$i!( Sets =

4 Cm#utati! & )9=0> items ;4

C!structi! & ,G ;=

; I!terme"iate C"e Ge!erati! <=

< :#eratr Prece"e!ce 64

6Stu"% e#erime!tsA Phases & Cm#i$eresi(!

66

3

Page 4: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 4/70

`

E:.N

Im#$eme!tati! & a )i!+er 

 AIM T 'rite a C #r(ram t im#$eme!t the $i!+er c!ce#t

AL!RIT#M

1. Create &i$e #i!ters2. ,cce#t the &i$e !ame as the i!#ut3. :#e! the &i$e a!" chec+ &r i!c$u"e4. I& #rese!t #e! that a(ai! a!" c#% c!te!ts t $i!+e".5. is#$a% !ames & &i$es that are $i!+e" a!" tta$ !um-er & &i$es $i!+e"

6. Termi!ate the #r(ram

.

SURCE C"E> 

1?C +r%)ram $% I77u2$ra$e $he imp7emen$a$i%n %& Lin3er?1

i!c$u"est"i.hDi!c$u"ec!i.hD

i!c$u"eI)E.FDi!c$u"emath.hD6i" mai!=>

i!t !*#*aHc$rscr=>H'hi$e=1>

 #ri!t&=J!J!1.actria$J!2.i!acci seriesJ!3.:/EKEJ!4.eit>H #ri!t&=J! E!ter %ur chiceA>Hsca!&=L"*a>Hs'itch=a>

case 1A#ri!t&=J!E!ter the 6a$ue & !A>H  sca!&=L"*!>H  &act=!>H  -rea+Hcase 2A#ri!t&=J!E!ter 6a$ue & !A>H  sca!&=L"*!>H  &i-=!>H  -rea+H

case 3A#ri!t&=J!E!ter the 6a$ue & !A>H  sca!&=L"*!>H

4

Page 5: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 5/70

`

  &u!=!>H  -rea+Hcase 4A#ri!t&=J!Pr(ram termi!ate">H  eit=0>H  -rea+H"e&au$tA#ri!t&=J!I!6a$i" chice>H

MM

M

UT+UT-

1?C +r%)ram $% I77u2$ra$e $he imp7emen$a$i%n %& Lin3er?1

1.actria$

2.i!acci series3.:/EKE4.eit E!ter %ur chiceA1

E!ter the 6a$ue & !A3actria$ & 3 N

1.actria$2.i!acci series3.:/EKE

4.eit E!ter %ur chiceA2

E!ter 6a$ue & !A3

 i-!!aci seriesA0112

1.actria$2.i!acci series3.:/EKE4.eit E!ter %ur chiceA3

E!ter the 6a$ue & !A3

Gi6e! !um is :""

1.actria$2.i!acci series

5

Page 6: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 6/70

`

3.:/EKE4.eit E!ter %ur chiceA4

Pr(ram termi!ate"

RESULT  Thus the )i!+er Pr(ram 'as eecute" a!" 6eri&ie" Success&u$$%.

E:.N 4

  Im#$eme!tati! & a )a"er 

Page 7: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 7/70

`

AIM T 'rite a C #r(ram t im#$eme!t the $a"er

AL!RIT#M

. Create &i$e #i!ters

4. ,cce#t the &i$e !ame as the i!#ut. :#e! the i!#ut &i$e a!" (et $e!(th* starti!( a""ress a!" $cati!;. )a" the i!structi! &rm that $cati!<. is#$a% the $a"e" c"e a!" its memr% $cati!6. Termi!ate the #r(ram.

SURCE C"E> 

1?C +r%)ram $% I77u2$ra$e $he imp7emen$a$i%n %& L%aer?1

i!c$u"est"i.hDi!c$u"ec!i.hD6i" mai!=>

i!t i*O*!Hi!t ir$[30]*r$[20]*$i!e*sta*ar"*rr$*$e!Hchar !ame[10]*he[30][5]*r#[2]Hc$rscr=>H

 #ri!t&=J!E!ter !ame & the #r(ramA>Hsca!&=Ls*!ame>H

 #ri!t&=J!E!ter the $e!(th>Hsca!&=L"*$e!>H

 #ri!t&=J!E!ter the i!#ut #r(ramJ!J!9e$ati6e a""rJtFec"eJ!>HiN0H"

sca!&=L"*ir$[i]>H(ets=he[i]>H

iHM'hi$e=ir$[i?1]$e!>H$i!eNiH

 #ri!t&=J!E!ter the starti!( a""essA>Hsca!&=L"*sta>H

 #ri!t&=J!E!ter the "etai$s & 9)A>H #ri!t&=J!E!ter the #eratrA>Hsca!&=Ls*r#>H

 #ri!t&=J!E!ter the re$cati6e $cati!A>Hsca!&=L"*rr$>H

&r=iN0Hi$i!eHi>

7

Page 8: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 8/70

`

i&=ir$[i]NNrr$>

i&=strcm#=r#*>NN0>

!Nstaat$=he[i]>Hs#ri!t&=he[i]*L"*!>H

Mi&=strcm#=r#*?>NN0>

!Nsta?at$=he[i]>Hs#ri!t&=he[i]*L"*!>H

M -rea+H

MM

 #ri!t&=J!The #r(ram Ls is $a"e" asAJ!*!ame>H #ri!t&=J!emr% $cati! hec"eJ!>H

&r=iN0Hi$i!eHi>

r$[i]Nir$[i]staHM&r=iN0Hi$i!eHi>

 #ri!t&=J!L"JtLs*r$[i]*he[i]>HM(etch=>H

M

UT+UT-

1?C +r%)ram $% I77u2$ra$e $he imp7emen$a$i%n %& L%aer?1

E!ter !ame & the Pr(ram "c

E!ter the $e!(th 3E!ter the i!#ut #r(ram

9e$ati6e ,""ress FeaC"e

0 101 202 40

E!ter the starti!( a""ress N3000E!ter the etai$s & 9)

E!ter the :#eratr E!ter the re$cati6e $cati! 1

8

Page 9: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 9/70

Page 10: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 10/70

`

AL!RIT#M

1. ,cce#t the starti!( a""ress &rm the user 2. ' acce#t the #r(ram i! assem-$% $a!(ua(e &rm the user 3. ,ssi(! the crrect #c"es &r each & the cmma!"s4. Ge!erate a""ress &r each $i!e a!" #ri!t the crres#!"i!( #c"e5. The &i!a$ ut#ut is (e!erate" !$% 'he! the cmma!" is e!tere" 'hich i!"icates

e!"i!( & #r(ram. St# the #r(ram

SURCE C"E> 

1?+r%)ram $% I77u2$ra$e $he imp7emen$a$i%n %& A22em@7er?1

i!c$u"est"i.hDi!c$u"ec!i.hD i!c$u"e#rcess.hD i!c$u"est"$i-.hD i!c$u"estri!(.hDI!t start i"*i"1HKi" mai!=>I)E Q&1*Q&2HI!tI*!HChar str[100]HC$rscr=>HPri!t&=RJ! e!ter the starti!( a""ressA>HSca!&=RL"*start>H1N&#e!=R@AJJsam#$e.tt*'>HPri!t&=RJ! e!ter the assem-$% $a!(ua(e #r(ramA>H;hi$e=1>

Sca!&=RLs*str>H'rite=str*si@e&=strN>*1*&1>HI&=strcm#=str*h$t>NN0>'rite=str*si@e&=str>*1*&1>HBrea+HMMc$se=&1>H1N&#e!=R@AJJsam#$e.tt*r>HPrit!&=RJ!>H;hi$e=1>

)1A

10

Page 11: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 11/70

`

rea"=str*si@e&=str>*1*&1>HI&=strcm#=str*m6>NN0>rea"=str*si@e&=str>*1*&1>HI&=strcm#=str*a*->NN0>

Pri!t&=RL" 3eJ!*start>HGt $1HMI&=strcm#=str*-*c>NN0>Pri!t&=RL" 25 J!*start>HGt $1HMMI&=strcm#=str*su->NN0>Pri!t&=RL" 3aJ!*start>H

Gt $1HMI&=strcm#=str*sta>NN0>Pri!t&=RL" 5eJ!*start>Hrea"=str*si@e&=str>*1*&1>H

 Nati=str>HI"N!HI"N!L100H

 N!/100HI"1N!HI&=i"NN0>Pri!t&=RL"L"L"J!*start*i"*i">HGta-cHMPri!t&=RL"L" J!*start*i">H,-cAPri!t&=RL"L" J!*start*i"1>HMI&=strcm#=str*$i>NN0>

Pri!t&=RL" 7"J!*start>Hrea"=str*si@e&=str>*1*&1>H

 Nati=str>HI"N!HI"N!L100H

 N!/100HI"1N!HI&=i"NN0>Pri!t&=RL"L"L"J!*start*i"*i">H

Gta-HM

11

Page 12: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 12/70

`

Pri!t&=RL"L" J!*start*i"1>H,-APri!t&=RL"L" J!*start*i"1>HMI&=strcm#=str*h$t>NN0>

Pri!t&=RL" 7-J!*start>HBrea+HMMc$se=&1>H(etch=>HM

UT+UT

1?+r%)ram $% I77u2$ra$e $he imp7emen$a$i%n %& A22em@7er?1

E!ter the start a""ressA 1000E!ter the assem-$% $a!(ua(e #r(ramA6a*-Sta 42006-*cStr 4300

 et

1000 3e1001 5e1002 001003 01004 251005 5e100 001007 01008 7

):C,TI: E:ICS :BECT C:E )EGTF

2003 U) 3 80 4

2001 , 1 40 2

2004 IK 4 100 5

2000 )V 0 20 1

12

Page 13: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 13/70

`

 2005 TIV 5 120

2002 SUB 2 0 3

RESULTThus the assem-$er #r(ram 'as success&u$$% eecute"

E:.N ;

  Imp7emen$a$i%n %& Ma'r% +r%'e22%r

13

Page 14: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 14/70

`

AIM T 'rite a C #r(ram t im#$eme!t the acr Prcessr c!ce#t

AL!RIT#M

1. Start the #r(ram2. Create 2 &i$e #i!ters3. Get the #r(ram &rm user a!" cm#are 'hether itWs a macr4. I& %es re#$ace the macr 'ith its "e&i!iti!5. ;rite the !e' #r(ram i! a !e' &i$e. is#$a% the c!te!ts & the !e' &i$e.7. St# the #r(ram

SURCE C"E> 

1?C +r%)ram $% I77u2$ra$e $he imp7emen$a$i%n %& Ma'r% +r%'e22%r?1

+r%)ram

i!c$u"est"i.hDi!c$u"ec!i.hDi!c$u"estri!(.hD6i" mai!=>

char a*!ame[20]*arra%[100]*ch[50]HI)E Q&1*Q&2H&1N&#e!=i!2.tt*r>H&2N&#e!=ut2.tt*'>H&sca!&=&1*Ls*ch>Hc$rscr=>H

 #ri!t&=Ls*ch>Hstrc#%=arra%* >H'hi$e=strcm#=ch*me!">XN0>

i&=strcm#=ch*marc>NN0>&sca!&=&1*Ls*!ame>H

&sca!&=&1*Ls*ch>Hi&=strcm#=ch*me!">XN0>

strcat=arra%*ch>HaN&(etc=&1>Hi&=aNNJ!>strcat=arra%*J!>H

M #ri!t&=Ls*arra%>H

&sca!&=&1*Ls*ch>H'hi$e=strcm#=ch*e!">XN0>

14

Page 15: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 15/70

`

i&=strcm#=ch*!ame>NN0>

&#ri!t&=&2*Ls*arra%>He$se

&#ri!t&=&2*Ls*arra%>HaN&(etc=&1>H

i&=aNNJ!>&#ri!t&=&2*J!>H

&sca!&=&1*Ls*ch>HM&#ri!t&=&2*Ls*ch>H

M

.

???IN4.T:T???

macr&ruitsma!((ra#esra!(e

 #i!ea##$e

me!"start

stra'-eer%&riutse!"

1?C +r%)ram $% I77u2$ra$e $he imp7emen$a$i%n %& Ma'r% +r%'e22%r?1

???UT+UT???

macr &ruitsma!((ra#esra!(e

 #i!ea##$e

RESULT

Thus the assem-$er #r(ram 'as success&u$$% eecute"

E:.N <

  Imp7emen$a$i%n %& a Lei'a7 Ana75Ber

15

Page 16: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 16/70

`

AIM T 'rite a C #r(ram t Im#$eme!t the )eica$ ,!a$%er

AL!RIT#M

1. Start the #r(ram2. ,cce#t the i!#ut &rm the user 3. Chec+ usi!( the &u!cti! it is a$#ha-et r !t4. I& it the! chec+ &r the i"e!ti&ier 5. St# the #r(ram

SURCE C"E> 

1?C +r%)ram $% I77u2$ra$e $he imp7emen$a$i%n %& Lei'a7 Ana75Ber?1

i!c$u"est"i.hDi!c$u"ec!i.hDi!c$u"estri!(.hDi!c$u"ect%#e.hD6i" mai!=>

i!t iN1*$H

char a[15]Hc$rscr=>H #ri!t&=RE!ter the stri!(A>sca!&=RLs*a>H$Nstr$e!=a>H&r=iN0Hi$Hi>

i&=isa$#ha=a[i]>> #ri!t&=RJ!Lc is a character*a[i]>H

e$se i&=is"i(it=a[i]>> #ri!t=RJ!Lc is a "i(it*a[i]>H

e$se i&==a[i]DNWaWa[i]NW@W>ZZ =a[i]DNW,Wa[i]NWW>> #ri!t&=RJ!Lc is character*a[i]>H

e$se i&=a[i]NNWQWZZa[i]NWWZZa[i]NW?\ZZa[i]NW/W> #ri!t&=RJ!Lc is a! #eratr*a[i]>H

e$se i&=a[i]NNWWZZa[i]NNWWZZa[i]NNWLW> #ri!t&=RJ!Lc is a S#ecia$ character*a[i]>H

e$se #ri!t&=RJ!Lc is !um-er*a[i]>H

M(etch=>H

M

1

Page 17: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 17/70

`

UT+UT-

1?C +r%)ram $% I77u2$ra$e $he imp7emen$a$i%n %& Lei'a7 Ana75Ber?1

E!ter the stri!(A he$$'e$h is a character  e is a character  $ is a character  $ is a character   is a character 

is a #eratr 

' is a character 

e is a character  $ is a character 

RESULT  Thus the Lei'a7 Ana75Ber 'as eecute" a!" 6eri&ie" success&u$$%.

E:.N 6

i!"i!( e#si$! c$sure &rm re(u$ar e#ressi!

AIM T 'rite a #r(ram t &i!" the e?c$sure & a$$ states &rm 9E

17

Page 18: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 18/70

`

AL!RIT#M

1. Start the program2. Declare and initialise variables and arrays3. Check whether it is a kleen closre or positive closre

4. !" kleen closre per"orm the necessary steps also per"orm positiveclosre

#. Show the varios states o" e$closre6. Stop the program

SURCE C"E> 

i!c$u"est"i.hD

i!c$u"ec!i.hDKi" mai!=>Char i#[10]*#*s[10][10]HI!t iN0*ON0*!N0HcN0HC$rscr=>HPri!t&=RJ! e!ter the i!#utA>Hr=iN0Hi3Hi>Sca!&=RLc*i#[i]>HCN!H

MS[0][1]NWeWHPNi#[1]HPri!t&=RJ! ! & states areA L"*!1>HI&=cNN2>&r=iNHi!Hi>&r=ONHO!HO>s[i][O]NW0WHs'itch=#>CaseWQWA

S[0][1]NWeWHS[1][2]Ni#[0]HS[0][3]NWeWHS[2][1]NWeWHBrea+HCase \WAS[0][1]NWeWHS[1][2]Ni#[0]HS[0][3]NWeWHS[2][1]NWeWHBrea+H

Mr=iN0Hi4Hi>

18

Page 19: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 19/70

`

#ri!t&=RJ! e?c$sure &=L">*i*i>Hr=ON0HO4HO>I&=s[i][O]NNWeW>Pri!t&=R"*O>MMGetch=>HM

UT+UT

E!ter the i!#utAaQ--

! & statesA 4

e?c$sure & =0>N0*0*1*2*3

e?c$sure & =1>N1*0*1*2*3

e?c$sure & =2>N2*0*1*2*3

e?c$sure & =3>N3*0*1*2*3

RESULT Thus the E#si$! c$sure #r(ram is success&u$$% eecute"

E:.N =

  C!6erti!( , t ,

AIM T 'rite a #r(ram t c!6ert , t ,

1<

Page 20: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 20/70

`

AL!RIT#M

1. Start the program2. %ssign an inpt string terminated by end o" &le' D(% with start

3. )he &nal state is assigned to (4. %ssign the state to S#. %ssign the inpt string to variable C6. *hile C+,e o" do

  S,move-s'c  C,ne/t char

. !" it is in ten retrn yes else no. Stop the program

SURCE C"E> 

i!c$u"ec!i.hDi!c$u"estri!(.hDi!c$u"e#rcess.hDi!c$u"emath.hDI!t ![11]*I*O*c*+*h*$*h1*&*h2*tem#1[12]*tem#2[12]*cu!tN0*#trN0HChar a[20][20]*s[5][8]HI!ttr[5][2]*ec$[5][8]*str[5]*&$a(H

I!ttr[5][2]*ec$[5][8]*st[5]*&$a(HKi" ec$s=i!t -[10]*i!t >IN0H^N?1H&$a(N0H;hi$e=$>

 [+]N-[$]HIN-[$]HhN+1HaA

&r=ONIHON11HO>I&=a[i][O]NNWeW>![+]NOHMI&=ONN11hN+>IN![h]HFHGt aHMM)HM&r=iN0Hi+Hi>

20

Page 21: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 21/70

`

&r=ONi1HO+HO>i&=![i]D![O]>CN![i]H

 [i]N![O]H [O]NcH

M&r=iN0Hi#trHi>&r=ON0HO+HO>I&=ec$[i][O]XN![O]>I&=icu!t>iHN0HME$seGt -H

ME$se i&==ec$[i][O]NN![O]>=ONN+>>tr[#tr][&]Nst[i]H$a(N1HBrea+HMMBAI&=&$a(NN0>&r=iN0HiN+Hi>Ec$[cu!t][i]N![i]Hst[cu!t]Ncu!t5Htr[#tr][&]Nst[cu!t]Hcu!tHMMKi" m6a=i!t (>h1N0H&r=iN0Hi7Hi>i&=ec$[(][i]NN3>Tem#1[h1]N4Hi&=ec$[(][i]NN8>Tem#1[h1]N<H

MPri!t&=RJ! m6e=Lc*a>A*st[(]>Hr=iN0Hih1Hi>Pri!t&=RL"*tem#1[i]>HN0HEc$s=tem#1*h1>HMKi" m6-=i!t (>F2N0Hr=iN0Hi7Hi>

I&=ec$[(][i]NN5>

21

Page 22: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 22/70

`

Tem#2[h2]NHI&=ec$[(][i]NN<>Tem#2[h2]N10HI&=ec$[(][i]NN10>Tem#2[h2]N11HMPri!t&=Rm6e=Lc*->Ast[(]>H

r=iN0Hih2Hi>Pri!t&=RL"*tem#2[i]>HN1HEc$s=tem#2*h2>HMKi" mai!=>C$rscr=>HPri!t&=RJ! the !. & states i! , =a/->Qa-- areA11>Hr=iN0HiN11Hi>r=ON0HON11HO>

,[i][O]NWJ0WH,[1][2]NWeWH,[1][8]NWeWH,[2][3]NWeWH,[2][5]NWeWH,[3][4]NWaWH,[5][]NW-WH,[4][7]NWeWH,[][7]NWeWH,[7][8]NWeWH,[7][2]NWeWH,[8][<]NWaWH,[<][10]NW-WH,[10][11]NW-WHPri!t&=RJ! the tra!smissi! ta-$e is as $$'s>HPri!t&=RJ! states 1 2 3 4 5 7 8 < 10 11>HGetch=>Hr=iN1HiN11Hi>Pri!t&=RJ! L" Jt*i>Hr=ON1HON11HO>

Pri!t&=RLc*a[i][O]>HMGetch=>HPri!t&=RJ! J! #ress a!% +e% t c!ti!ue>HC$rscr=>HIN1H+N1HhN1H

 [0]NIHPri!t&=RJ!>H,Ar=ON1HON11HO>i&=a[i][O]NNWeW>

 [+]NOH

22

Page 23: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 23/70

`

MI&=ONN11h+>IN![h]HFHGt aH

MMr=iN1HO+Hi>r=ONi1HO+HO>I&=![i]D![O]>cN![i]H

 [i]N![O]H [O]NcHMCu!tHSt[0]N5Hr=iN0Hi+Hi>

Ec$[0][i]N![i]HPri!t&=Rthe m6es are & the $$'i!( ma!!er>H6a=#tr>H6-=#tr>HPtrHMPri!t&=RJ! the !e' states & , are as $$'s>Hr=iN0Hi5Hi>#ri!t&=RJ! Lc*st[i]>Hr=ON0HO7HO>Pri!t&=RL"*ec$[i][O]>HMPri!t&=R the tra!siti! ta-$e are as $$'s>HPri!t&=RJ! a J! - J!>Hr=iN0Hi5Hi>Pri!t&=RLc*st[i]>Hr=ON0HO2HO>Pri!t&=RLc Jt*tr[i][O]>HMGetch=>HM

UT+UT

The !. & states i! ,=a/->Qa-- areA11

The tra!siti! ta-$e is as $$'s1 2 3 4 5 7 8 < 10 11

1 e e2 e e

3 a4 e

23

Page 24: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 24/70

`

5 - e e7 e e8< a10 -

11 -

RESULT

Thus the a-6e c!6ersi! #r(ram is success&u$$% eecute"

E:.N 8

Cm#utati! & I9ST a!" :)):; sets

AIM T ca$cu$ate the &irst a!" $$' & the (i6e! e#ressi!

24

Page 25: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 25/70

`

AL!RIT#M

1. Start the #r(ram

2. I! the #r"ucti! the &irst termi!a$ ! 9.F.S -ecmes the &irst & it3. I& the &irst character is !!?termi!a$ the! its &irst is ta+e! e$se $$' & $e&t is ta+e!4. T &i!" $$' &i!" 'here a$$ the !! termi!a$s a##ear. the &irst & its $$'s is its

$$'5. I& the $$' is t the! the $$' & $e&t is ta+e!. i!a$$% #ri!t &irst a!" its $$'7. St# the #r(ram

SURCE C"E> 

i!c$u"est"i.hDi!c$u"ec!i.hDi!c$u"e#rcess.hDKi" mai!=>I!t!#*N0*%N0*$N0*+N0*cN0*sN0*@N0HChar #[10][10]**&i[10][10]*&[10][10]HC$rscr=>Hr=N0H10H>r=%N0H%10H%>P[][%]NW0WHi[][%]NW0WH[][%]NW0WHMMPri!t&=Re!ter the ! & #r"ucti!sA>HSca!&=RL"*!#>HPri!t&=RJ! e!ter the !um-er & #r"ucti!>Hr=N0H!#H>

Sca!&=RLs*#[]>HI&=#[][0]NN#[][2]>Pri!t&=RJa #r"ucti! is !t &ree &rm $e&t recursi!>HPri!t&=RJa #r(ram has t -e termi!ate">HPri!t&=Ja #ress a!% +e%>HGetch=>HEit=0>HMMPri!t&=RJ!J!>Pri!t&=R&irst J!>H

r=%N0H%!#H%>

25

Page 26: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 26/70

`

Pri!t&=R&irst=Lc>N*#[%][0]>HI&=#[%][2]DNW,W #[%][2]NW@W>:N#[%][2]Hr=N%1H!#H>

I&=#[][0]NN>I&=#[][2]DNW,W#[][2]NW@W>:N#[][2]HE$se i&=#[][2]W,WZZ#[][2]DW@W>Pri!t&=RLc*#[][2]>Hi[%][+]N#[][2]>Hr=$N0H$str$e!=#[]>H$>I&=#[][$]NNW/W>

Pri!t&=RLc*#[][$1]>Hi[%][+]N#[][$1]HBrea+HMMMMMME$se i&=#[%][2]W,W ZZ #[%][2]DWW>Pri!t&=RLc*#[%][2]>Hi[%][+]N#[%][2]HM)Nstr$e!=#[%]>Hr=cN0Hc$Hc>I&=#[%][c]NNW/W>Pri!t&=RLc*#[%][c1]>Hi[%][+]N#[%][c1]HMMPri!t&=RJ!>H^N0HPri!t&=R$$' J!>H

r=N0H!#H>r=%N0H%!#H%>r=$N2H$str$e!=#[]>H$>I&=#[%][$]NN#[][0]>I&=#[%][$1]W,WZZ#[%][$1]DWWZZ#[%][$1]XNW/WZZ#[%][$1]XNW0W>[][+]N#[%][$1]HI&=NN0>

[][+]NWWHMM

2

Page 27: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 27/70

`

CN+HI&=#[%][$1]NNW0WZZ#[%][$1]NNW/W>r=sN0Hsc10Hs>[][+]N&[?1][s]H

MMCN+HI&=#[%][$1]DNW,W#[%][$1]NW@W>r=sN0HsNcHs>[][+]N&[?1][s]H[][+]N&[?2][s]H[][+]N&i[?1][s]HMMCN+H

MPri!t&=R$$'=Lc>N*#[][0]>Hr=@N0H@N+10H@>I&=&[][@]NNWQWZZ&[][@]NNWWZZ&[][@]NNWWZZ&[][@]NNW>WZZ&[][@]NNW=\ZZ&[][@]NNW?\ZZ&[][@]NNWLW>Pri!t&=RLc*&[][@]>H^N0HPri!t&=RJ!>HMGetch=>HM

UT+UT

E!ter the !. & #r"ucti!A5E?DTEWEW?DTEW/eT?DTWTW?DQTW/e

?D=E> /i"

27

Page 28: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 28/70

`

irstirst=E>N=*i"Mirst=EW>N*eMirst=T>N=*i"Mirst=TW>NQ*eMirst=>N=*i"M

$$'$$'=E>N>*M$$'=EW>N>**M$$'=T>N>**M$$'=TW>NQ**>*M$$'=>NQ**>*M

RESULT

Thus the a-6e cm#utati! & I9ST :)):; #r(ram is success&u$$% eecute".

E:.N 9

C!structi! & Pre"icti6e Parsi!( Ta-$e

AIM T 'rite a C #r(ram &r the im#$eme!tati! & #re"icti6e #arsi!( ta-$e

28

Page 29: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 29/70

`

AL!RIT#M

1. start the program2. ,ssi(! a! i!#ut stri!( a!" #arsi!( ta-$e i! &r the! G.

3. Set i# t #i!t t the &irst s%m-$ & t 4. 9e#eat i& V is a termi!a$ & the! i& !Na*the! ## V &rm the stac+ 5. Push Y i!t the stac+ 'ith Y*! t#. :ut#ut the #r"ucti! ?D*%7. E!" e$se errr u!ti$ N8. Termi!ate the #r(ram

SURCE C"E> 

i!c$u"est"i.hDi!c$u"ec!i.hDi!c$u"estri!(.hDchar str[10]*ut*i!*ut#ut[10]*i!#ut[10]*tem#Hchar t$[10]N\W*WW*WQW*W=\*W>W*WW*WWMHchar !t$[10]N\eW*WeW*WtW*WtW*W&WMHi!t errN0*&$a(N0*i*O*+*$*mHchar c[10][10][7]NRteM*RerrrXM*RerrrXM*RteM*RerrrXM*RerrrXM*M*RerrrX*te*errrX*errr**M*R&t*errrX*errr*&terrrX*errrM*

Rerrr**Q&t*errrX**M*R*errrX*errrX*=e>*errrX*errrXMMHstruct stac+ char sic[10]Hi!t t#HMH6i" #ush=struct stac+ Qs*char #>s?Dsic[s?Dt#]N#Hs?Dsic[s?Dt#1]NWJ0WHM

char ##=struct stac+ Qs>char aHaNs?Dsic[s?Dt#]Hs?Dsic[s?Dt#??]NWJ0WHretur!=a>HMchar st#=struct stac+ Qs>retur!=s?Dsic[s?Dt#]>HM

6i" #-=struct stac+ Qs>

2<

Page 30: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 30/70

`

mNH'hi$e=str[m]XNWJ0W>mHm??H'hi$e=mXN?1>

i&=str[m]XNWW> #ush=s*str[m]>Hm??HMM6i" search=i!t $>&r=+N0H+7H+>i&=i!NNt$[+]>

 -rea+Hi&=$NN0>strc#%=str*c[$][+]>H

e$se i&=$NN1>strc#%=str*c[$][+]>He$se i&=$NN2>strc#%=str*c[$][+]>He$se i&=$NN3>strc#%=str*c[$][+]>He$se strc#%=str*c[$][+]>HM6i" mai!=>struct stac+ s1Hstruct stac+ QsHsNs1Hs?Dt#N?1Hc$rscr=>H

 #ri!t&=RJtJt #arsi!( ta-$e JtJt>H&r=iN0Hi5Hi>

 #ri!t&=RLcJt*!t$[i]H&r=ON0HOHO>i&=strcm#=c[i][O]*errrX>NN0>

 #ri!t&=RerrrXJt>H

e$se #ri!t&=RLc?DLs Jt*!t$[i]*c[i][O]>HM

 #ush=s*WW>H #ush=s*WeW>H #ri!t&=Re!ter the i!#ut stri!(>Hsca!&=RLs*i!#ut>H

 #ri!t&=RJ!J! the -eha6iur & the #arser &r (i6e! i!#ut stri!( isA J!J R>H #ri!t&=RJ! stac+J! i!#utJ! ut#ut>HiN0H

i!Ni!#ut[i]H #ri!t&=RLsJt*s?Dsic>H

30

Page 31: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 31/70

`

&r=+NiH+str$e!=i!#ut>H+> #ri!t&=RLc*i!#ut[+]>Hi&=strcm#=str*W \>XN0>

 #ri!t&=RJtLc?DLs!t$[O]*str>H'hi$e==s?Dsic[s?Dt#]XNWW>errXN1strcm#=str*errrX>XN0>

strc#%=str* R>H&$a(N0H&r=ON0HO7HO>i&=i!NNt$[O]>&$a(N1H

 -rea+HMi&=&$a(NN0>i!NWWH&$a(N0H

utNst#=s>H&r=ON0HO7HO>i&=utNNt$[O]>&$a(N1H

 -rea+HMi&=&$a(NN1>i&=utNNi!>tem#N##=s>Hi!Ni!#ut[i]Hi&=strNNWW>tem#N##=s>HMe$sestrc#%=sstr*errrX>HerrN1HMM

e$se&$a(N0H&r=ON0HO5HO>i&=utNN!t$[O]>&$a(N1H

 -rea+HMi&=&$a(NN1>

search=O>Htem#N##=s>H

31

Page 32: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 32/70

`

 #-=s>HMe$sestrc#%=str*errrX>HerrN1H

MMi&=strcm#=str*errrX>XN0>

 #ri!t&=RLsJt*s?Dsic>H&r=+NiH+str$e!=i!#ut>H+>

 #ri!t&=RLc*i!#ut[+]>Hi&==strcm#=str* R>XN0>=strcm#=str*errrX>XN0>>

 #ri!t&=RJt Lc?DLs*!t$[O]*str>HMMi&=strcm#=str*errrX>NN0>

 #ri!t&=RJ! the stri!( is !t acce#te"XX>H

e$se #ri!t&=RJt Jt acce#t J!J!J! the stri!( is acce#te">H(etch=>HM

UT+UT

Parsi!( ta-$e

V Q = > E.E?DTe E99:9X E99:9X E?DteE99:9X E99:9XE E99:9X E?DteE99:9X E99:9X E?D e?DT T?Dt E99:9X E99:9X T?D&tE99:9X E99:9XT E99:9X T?D t?DQ&t E99:9 t?D t?D.?D E99:9X E99:9X ?D =E> E99:9X E99:9X

E!ter the i!#ut stri!(A VThe -eha6iur & the #arser &r (i6e! i!#ut stri!( is

Stac+ i!#ut ut#utSE VVSeT VV E?DTeSet VV T?DtSet V VV ?DVSet VSe V t?DSeT V e?DTeseT VSet V T?Dt

32

Page 33: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 33/70

`

RESULT

Thus the Pre"icti6e Parser #r(ram is eecute" success&u$$%.E:.N 0

Imp7emen$a$i%n %& Shi&$ Reu'e +ar2in)

AIM T 'rite a C #r(ram &r shi&t re"uce #arsi!(

33

Page 34: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 34/70

`

AL!RIT#M

1. start the #r(ram

2. rea" the e#ressi! a!" "ec$are the 6aria-$es

3. set s%m-$ t i!"icate the start & stac+ 

4. 9e#eat &r iN0 t ! 'here ! is the !. & #r"ucti!s

  ,. rea" #r"=i>  B.set #r-$em[i]Nstr$e!=#r"[i]>

  5. chec+ &r the !!?termi!$a 'hich crres#!"s t the termi!a$

  . I& it equa$s the! re#$ace the termi!a$ 'ith !!?termi!a$

  7. 9e#eat this u!ti$ termi!a$s are re#$ace" -% !!?termi!a$s

SURCE C"E> 

i!c$u"eistream.hDi!c$u"ec!i.hDi!c$u"estri!(.hDi!c$u"est"$i-.hD6i" #ush=char>H

char ##=>H6i" #ri!tstac+=>Hstruct (rammar char $#r*r#r[10]HMHchar stac+[20]Hi!t t#N?1H6i" mai!=>(rammar (r[10]Hchar -u&&er[10]H

char ch*ch1*tem#[10]*startHi!t i*O*+*s*t*$e!Hc$rscr=>Hcute!ter the ! & #r"ucti!sAHci!DD!H&r=iN0Hi!Hi>cutJ! e!ter the $e&t si"e & #r"ucti!si1AHci!DD(r[i].$#rHcutJ! e!ter the ri(ht si"e & #r"ucti!sAHci!DD(r[i].r#rHMcutJ! e!ter the i!#ut stri!(AH

34

Page 35: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 35/70

`

ci!DD-u&&erHcutJ! the (rammarisAJ!H&r=iN0Hi!i>cut(r[i].$#r??D(r[i].r#re!"$HcutJ! i!#ut stri!( is A-u&&erH

 #ush=>H

startN(r[0].$#rH$e!Nstr$e!=-u&&er>H

 -u&&er[$e!]NH -u&&er[$e!1]NJ0HcutJ! J! stac+ JtJt -u&&erJtJtJt acti!J!Hcutstac+JtJt-u&&ere!"$H(etch=>H'hi$e=1>chN-u&&er[i]H$a-AtN0H

&r=+Nt#H+D0H+??>tem#[t]Nstac+[t]NJ0Hstrre6=tem#>H&r=ON0HO!HO>i&=strcm#=tem#*(r[O].r#r>NN0>&r=sN0HstHs>ch1N##=>H

 #ush=(r[O].$#r>H #ri!tstac+=>HcutJtJtJt-u&&er[i]JtJt9e"ucee!"$H(etch=>H(t $a-HMMstrre6=tem#>HMch1N##=>Hi&=chXN>#ush=ch1>H

 #ush=ch>H #ri!tstac+=>HcutJtJtJt-u&&er[i1]JtJtshi&te!"$H(etch=>HiHMe$se i&=chNN ch1NNstart t#NN0>cutJ! stri!( is acce#te"H(etch=>Heit=0>H

Me$se

35

Page 36: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 36/70

`

cutJ! stri!( is !t acce#te"H(etch=>Heit=0>HMM

M6i" #ush=char a>stac+[t#]NaHMchar ##=>retur! stac+[t#??]HM6i" #ri!tstac+=>&r=i!t iN0Hit#Hi>cutstac+[i]HM

UT+UT

E!ter the !. & #r"ucti!A 3E!ter the #r"ucti!A S?Da,BC,?D,-c/-B?D"

The #r"ucti! areSDDa,BC,DD,-c/-BDD"I)9 SDDa,BCuDD-c/-uBDD"uDthe &irst s%m-$s are V 

RESULT

Thus the shi&t re"uce #arser #r(ram is success&u$$% eecute"

E:.N

Cm#utati! & $$' a!" Trai$i!( Sets

AIM T 'rite a C #r(ram t Cm#ute & $$' a!" Trai$i!( Sets

AL!RIT#M

3

Page 37: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 37/70

`

tep tep1: Start.Ste#1 A Start the Pr(ramA Start.

Ste#2A Get the ! & #r"ucti! a!" ca$cu$ate the $e!(th & each #r"ucti!.

Ste#3A ;ith a 6aria-$e 6a$ &r chec+i!( the 6a$i" !! termi!a$s i& the% are "u#$icate" (et a$$

 ! termi!a$s i! a! arra%.

Ste#4A I! each #r"ucti! chec+ the &irst accurate & termi!a$s a!" ta+e that termi!a$ a!" a"" it

t $$' & !! termi!a$ i! arra% a!" eitthe $#.

Ste#5A Sca! the #r"ucti! a!" &i!" the $ast termi!a$ a!" a"" it t res#ecti6e trai$i!( arra% &

assciate" !! termi!a$ eit the $#.

Ste#A C!si"er #r"ucti! 'ith a !! termi!a$ ! ri(ht si"e a!" chec+ the $$' & that !!

termi!a$ assciate" #r"ucti! a!" a$s trai$i!( a!" a"" it t the $$' a!" trai$i!( & $e&t si"e !!

termi!a$.

Ste#7A ;rite the Follow and trailing terminals for each non terminal.

SURCE C"E> 

i!c$u"eistream.hDi!c$u"est"i.hD

i!c$u"ec!i.hDi!c$u"estri!(.hDi!c$u"ect%#e.hD

char !t[5]*#[5]*q[5]*a[5][5][5]*-[5][5][5]*&i[5][5]*$e&t[5]*ri(ht[5]*$ea"[5][10]*trai$[5][10]Hi!t !1*![5]*c[5][5]*m*$[5]*&[5]*+*a1H

6i" $ea"i!(=char*i!t>H6i" trai$i!(=char*i!t>H

6i" mai!=>  c$rscr=>H  cutE!ter the !um-er & !!?termi!a$s H  ci!DD!1H  cutE!ter the set & !!?termi!a$s H  &r=i!t iN0Hi!1Hi>

ci!DD!t[i]H  &r=iN0Hi!1Hi>    cute!"$H

  cutE!ter the !um-er & #r"ucti!s &r !t[i] H  ci!DD![i]H

37

Page 38: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 38/70

`

  &r=i!t ON0HO![i]HO> 

  cutJ!E!ter the #r"ucti!s H  (ets=#>H  //qNstrre6=#>H  c[i][O]Nstr$e!=#>H

  &r=i!t +N0H+c[i][O]H+>    a[i][O][+]N#[+]H  -[i][O][+]N#[c[i][O]?+?1]H

  M M  M  &r=iN0Hi!1Hi> 

cute!"$Hcut!t[i]???DH&r=i!t ON0HO![i]HO>

  &r=i!t +N0H+c[i][O]H+>  cuta[i][O][+]H  cut/H

  M M  cutJ!J!H  char H  &r=iN0Hi!1Hi>    $[i]N0H  &[i]N0H  N!t[i]H  +NiH  $ea"i!(=*+>H  trai$i!(=*+>H  M  /Q&r=i!t m!N0Hm!N!1Hm!>    cutri(ht[m!] H  M Q/  i!t cu!tN0H

  char @H  &r=i!t m!N0Hm!!1Hm!>    &r=iN0Hi!1Hi> 

  @Nri(ht[i]H  &r=i!t +N0H+!1H+> 

i&=@NN!t[+]>  &r=i!t "N0H"$[+]H">

  &r=i!t (N0H($[i]H(>

38

Page 39: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 39/70

`

  i&=$ea"[i][(]NN$ea"[+]["]>  cu!tN1HMi&=cu!tNN0>

  $ea"[i][$[i]?1]N$ea"[+]["]H  $[i]HMcu!tN0H

  M  &r="N0H"&[+]H"> 

&r=i!t (N0H(&[i]H(>  i&=trai$[i][(]NNtrai$[+]["]>  cu!tN1H

Mi&=cu!tNN0>  trai$[i][&[i]?1]Ntrai$[+]["]H  &[i]HMcu!tN0H

  M

M  M  cu!tN0H

  M  M

  &r=iN0Hi!1Hi>    cut)ea"i!(=!t[i]>N H  &r=i!t ON0HO$[i]HO>    cut$ea"[i][O] H

  M

  cutMJtJtJtJtH

  cutTrai$i!(=!t[i]>N H  &r=ON0HO&[i]HO>    cuttrai$[i][O] H  M

  cutMJ!H

  M  (etch=>H

3<

Page 40: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 40/70

`

M

6i" $ea"i!(=char *i!t i>  char @H  &r=i!t ON0HO![i]HO>

    i&=isu##er=a[i][O][0]>>    $e&t[i]N!t[i]H  ri(ht[i]Na[i][O][0]H  i&=Xisu##er=a[i][O][1]>>    //cuta[i][O][1] H  $ea"[i][$[i]]Na[i][O][1]H  $[i]H  M

  M  e$se    //cuta[i][O][0] H  $ea"[i][$[i]]Na[i][O][0]H  $[i]H  M  MM

6i" trai$i!(=char *i!t i>  char @H  &r=i!t ON0HO![i]HO>    i&=isu##er=-[i][O][0]>>    i&=Xisu##er=-[i][O][1]>>    //cuta[i][O][1] H

  trai$[i][&[i]]N-[i][O][1]H  &[i]H  M  M  e$se    //cuta[i][O][0] H  trai$[i][&[i]]N-[i][O][0]H  &[i]H  M  M

M

40

Page 41: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 41/70

`

UT+UT-

E!ter the !. & #r"ucti!A5E?DTEWEW?DTEW/eT?DTWTW?DQTW/e?D=E> /i"

)ea"i!()ea"i!( =E>N=*i"M)ea"i!( =EW>N*eM)ea"i!( =T>N=*i"M)ea"i!( =TW>NQ*eM)ea"i!( =>N=*i"M

Trai$i!(Trai$i!( =E>N>*MTrai$i!( =EW>N>**MTrai$i!( =T>N>**MTrai$i!( =TW>NQ**>*M Trai$i!( =>NQ**>*M

RESULT  Thus the )ea"i!( a!" Trai$i!( 'as eecute" a!" 6eri&ie" Success&u$$%

E:.N 4

  Cm#utati! & )9 =0> items

AIM T% Dri$e a '%e &%r LR0/ +ar2er &%r F%77%Din) +r%u'$i%n

  E-ET

  T-T?F1F

  F-E/1'har

41

Page 42: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 42/70

`

AL!RIT#M

.Ini$ia7iBe $he 2$a'3 Di$h $he 2$ar$ 2$a$e.

4. Rea an inpu$ 25m@%7

. Dhi7e $rue %

. U2in) $he $%p %& $he 2$a'3 an $he inpu$ 25m@%7 e$ermine $he ne$ 2$a$e.

.4 I& $he ne$ 2$a$e i2 a 2$a'3 2$a$e$hen.4. 2$a'3 $he 2$a$e

.4.4 )e$ $he ne$ inpu$ 25m@%7

. e72e i& $he ne$ 2$a$e i2 a reu'e 2$a$e

$hen

.. %u$pu$ reu'$i%n num@er, 3 

..4 p%p R#S3 - 2$a$e2 &r%m $he 2$a'3 Dhere R#S3 i2 $he ri)h$ han 2ie %&

pr%u'$i%n 3.

.. 2e$ $he ne$ inpu$ 25m@%7 $% $he L#S3 

.; e72e i& $he ne$ 2$a$e i2 an a''ep$ 2$a$e

$hen

.;. %u$pu$ a7i 2en$en'e

.;.4 re$urn

e72e

.;. %u$pu$ ina7i 2en$en'e

.;.; re$urn

SURCE C"E> 

in'7ueG2$rin).hin'7ueG'%ni%.h

in'7ueG2$i%.h

in$ anHH6H4JK

  KK00,<,K-,-,K-,-,K00,;,K-,-,K-,-,

  KK-,-,K00,6,K-,-,K-,-,K-,-,K04,04,

  KK-,-,K0,4,K00,=,K-,-,K0,4,K0,4,

  KK-,-,K0,;,K0,;,K-,-,K0,;,K0,;,

  KK00,<,K-,-,K-,-,K00,;,K-,-,K-,-,

  KK00,<,K0,6,K0,6,K-,-,K0,6,K0,6,

  KK00,<,K-,-,K-,-,K-,-,K-,-,K-,-,  KK00,<,K-,-,K-,-,K00,;,K-,-,K-,-,

  KK-,-,K00,6,K-,-,K-,-,K00,,K-,-,

  KK-,-,K0,,K00,=,K-,-,K0,,K0,,

  KK-,-,K0,,K0,,K-,-,K0,,K0,,

  KK-,-,K0,<,K0,<,K-,-,K0,<,K0,<

in$ )%$%$H4HJK,4,,-,-,-,-,-,-,-,-,-,8,4,,-,-,-,-,

  9,,-,-,0,-,-,-,-,-,-,-,-,-,-,-,-

in$ aH0'har @H0

42

Page 43: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 43/70

`

in$ $%pJ-,@$%pJ-,i

%i pu2hin$ 3/

K

  i&$%pG9/

  aH$%pJ3

%i pu2h@'har 3/K

  i&@$%pG9/

  @H@$%pJ3

'har TS/

K

  re$urn aH$%p

%i p%p/

K

  i&$%pJ0/  $%p--

%i p%p@/

K

  i&@$%pJ0/

  @H@$%p--JO0

%i i2p7a5/

K  &%riJ0iGJ$%pi/

  prin$&PQQ'P,aHi,@Hi/

%i i2p7a5'har pH,in$ m/

K

  in$ 7

  prin$&PO$O$P/

  &%r7JmpH7JO07/

  prin$&PQ'P,pH7/

  prin$&POnP/

%i err%r/

K

  prin$&POnOnS5n$a Err%rP/

%i reu'ein$ p/

K

  in$ 7en,3,a

  'har 2r',?e2$

  2Di$'hp/

  K

43

Page 44: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 44/70

`

  'a2e e2$JPETP

  2r'JE

  @rea3

  'a2e 4e2$JPTP

  2r'JE

  @rea3

  'a2e e2$JPT?FP  2r'JT

  @rea3

  'a2e ;e2$JPFP

  2r'JT

  @rea3

  'a2e <e2$JPE/P

  2r'JF

  @rea3

  'a2e 6e2$JPiP

  2r'JF

  @rea3  e&au7$e2$JPO0P

  2r'JO0

  @rea3

 

  &%r3J03G2$r7ene2$/3/

  K

  p%p/

  p%p@/

 

  pu2h@2r'/

  2Di$'h2r'/

  K

  'a2e E aJ0

  @rea3

  'a2e T aJ

  @rea3

  'a2e F aJ4

  @rea3

  e&au7$ aJ-

  @rea3

    pu2h)%$%$HTS/Ha/

in$ main/

K

  in$ ,2$,i'

  'har ipH40JPO0P,an

  '7r2'r/

  prin$&PEn$er an5 S$rin) - P/

  )e$2ip/

  pu2h0/

  i2p7a5/  prin$&PO$Q2OnP,ip/

44

Page 45: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 45/70

`

  &%rJ0ipHJO0/

  K

  2$JTS/

  anJipH

  i&anJa(anGJB/

  i'J0

  e72e i&anJJ/  i'J

  e72e i&anJJ?/

  i'J4

  e72e i&anJJ/

  i'J

  e72e i&anJJ//

  i'J;

  e72e i&anJJ/

  i'J<

  e72e

  K  err%r/

  @rea3

 

  i&anH2$Hi'H0JJ00/

  K

  pu2h@an/

  pu2hanH2$Hi'H/

  i2p7a5/

 

  i2p7a5ip,/

 

  i&anH2$Hi'H0JJ0/

  K

  reu'eanH2$Hi'H/

  i2p7a5/

  i2p7a5ip,/

 

  i&anH2$Hi'HJJ04/

  K

  prin$&P!ien S$rin) i2 A''ep$eP/

  @rea3 

 

  )e$'h/

  re$urn 0

UT+UT

En$er an5 S$rin) - a@?'

0 a@?'0a< @?'

45

Page 46: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 46/70

`

0F @?'

0T4 @?'

0E @?'

0E6 @?'

0E6@< ?'

0E6F ?'

0E6T9 ?'0E6T9?= '

0E6T9?='<

RESULT  Thus the )9=0> Pr(ram 'as eecute" a!" 6eri&ie" Success&u$$%.

E:.N

  C!structi! & ,G

AIM T 'rite a C #r(ram t #er&rm the

AL!RIT#M

4

Page 47: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 47/70

`

/*************************************************************************

 * Compilation: javac DirectedCycle.java

 * Execution: java DirectedCycle < input.txt

 * Dependencies: Digraph.java Stack.java Stdut.java !n.java

 * Data "iles: http://algs4.cs.princeton.edu/42directed/tinyDG.txt

 * http://algs4.cs.princeton.edu/42directed/tinyDAG.txt

 *

 * #inds a directed cycle in a digraph.

 * $uns in %E & '( time.

 *

 * ) java DirectedCycle tinyD.txt

* Cycle: + , - +

*

 * ) java DirectedCycle tinyD.txt

* o cycle

 *

  *************************************************************************/ 

SURCE C"E> 

public class DirectedCycle {

  private boolean[ !ar"ed#  // marked0v1 2 has vertex v 3een

marked4

  private int[ edge$o#  // edge5o0v1 2 previous vertex on path

to v 

  private boolean[ on%tac"#  // onStack0v1 2 is vertex on the

stack4

  private %tac"&'nteger( cycle#  // directed cycle %or null i" no such

cycle(

  public DirectedCycle)Digraph G* {

  !ar"ed + ne, boolean[G. V )*#

  on%tac" + ne, boolean[G. V )*#

  edge$o + ne, int[G. V )*#

  -or )int v + # v & G. V )*# v*

  i- )0!ar"ed[v* dfs)G1 v*#

  // check that digraph has a cycle

  assert check)G*#

 

  // check that algorithm computes either the topological order or "inds

a directed cycle

  private void dfs)Digraph G1 int v* {

  on%tac"[v + true#

  !ar"ed[v + true#

  -or )int , : G.adj)v** {

  // short circuit i" directed cycle "ound 

  i- )cycle 0+ null* return#

  //"ound ne6 vertex7 so recur 

  else i- )0!ar"ed[,* {

  edge$o[, + v#  dfs)G1 ,*#

47

Page 48: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 48/70

`

 

  // trace 3ack directed cycle

  else i- )on%tac"[,* {

  cycle + ne, %tac"&'nteger()*#

  -or )int x + v# x 0+ ,# x + edge$o[x* {

  cycle. push)x*#

 

  cycle. push),*#

  cycle. push)v*#

 

 

  on%tac"[v + -alse#

 

  public boolean hasCycle)*  { return cycle 0+ null# 

  public 'terable&'nteger( cycle)* { return cycle# 

  // certi"y that digraph is either acyclic or has a directed cycle

  private boolean check)Digraph G* {

  i- )hasCycle)** {

  // veri"y cycle

  int -irst + 31 last + 3#

  -or )int v : cycle)** {

  i- )-irst ++ 3* -irst + v#

  last + v#

 

  i- )-irst 0+ last* {

  %yste!.err. printf)5cycle begins ,ith 6d and ends ,ith

6d7n51 -irst1 last*#

  return -alse#

   

  return true#

 

  public static void  main)%tring[ args* {

  'n in + ne, In)args[*#

  Digraph G + ne, Digraph)in*#

  DirectedCycle -inder + ne, DirectedCycle)G*#

  i- )-inder.hasCycle)** {

  %td8ut. print)5Cycle: 5*#  -or )int v : -inder.cycle)** {

  %td8ut. print)v  5 5*#

 

  %td8ut. println)*#

 

  else {

  %td8ut. println)59o cycle5*#

 

 

48

Page 49: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 49/70

`

Re&

Digraphs.

 A directed graph (or digraph) is a set of vertices and a collectionof directed edges that each connects an ordered pair of vertices. We saythat a directed edge points from the first vertex in the pair and pointsto the second vertex in the pair. We use the names 0 through V-1 for thevertices in a V-vertex graph.

Glossary.

 Here are some definitions that e use.• A self-loop is an edge that connects a vertex to itself.

• !o edges are parallel  if they connect the same ordered pair of

vertices.• !he outdegree of a vertex is the num"er of edges pointing from it.

!he indegree of a vertex is the num"er of edges pointing to it.• A subgraph is a su"set of a digraph#s edges (and associated

vertices) that constitutes a digraph.• A directed path in a digraph is a se$uence a se$uence of vertices in

hich there is a (directed) edge pointing from each vertex in the

se$uence to its successor in the se$uence. A simple path is one ith

no repeated vertices.• A directed cycle is a directed path (ith at least one edge) hose

first and last vertices are the same. A simple cycle is a cycle ith no

repeated edges or vertices (except the re$uisite repetition of the

first and last vertices).• !he length of a path or a cycle is its num"er of edges.

• We say that a vertex , is reachable from a vertex v if there exists a

directed path from v to ,.

• We say that to vertices v and , are strongly connected  if they are

mutually reacha"le% there is a directed path from v to , and a

directed path from , to v.

• A digraph is strongly connected  if there is a directed path from

every vertex to every other vertex.

4<

Page 50: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 50/70

`

• A digraph that is not strongly connected consists of a set

of strongly-connected components& hich are maximal strongly-

connected su"graphs.• A directed acyclic graph (or 'A) is a digraph ith no directed

cycles.

 

Digraph graph data type.

 We implement the folloing digraph A*.

!he +ey method ad)* allos client code to iterate through the vertices

ad,acent from a given vertex.

We prepare the test data tiny'.txt and tiny'A.txt using the folloing

input file format.

50

Page 51: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 51/70

`

Graph representation.

 We use the adjacency-lists representation& here e maintain a vertex-indexed array of lists of the vertices connected "y an edge to each vertex.

51

Page 52: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 52/70

`

'igraph.,ava implements the digraph A* using the ad,acency-lists

representation. Ad,atrix'igraph.,ava implements the same A* using the

ad,acency-matrix representation.

Reachability in digraphs.

 'epth-first search and "readth-first search are fundamentally digraph-processing algorithms.

• Single-source reachability: iven a digraph and source s& is there a

directed path from s to v *f so& find such a

path. 'irected'/.,ava uses depth-first search to solve this

pro"lem.• Multiple-source reachability: iven a digraph and a set  of source

vertices& is there a directed path from any  vertex in the set to v

'irected'/.,ava uses depth-first search to solve this pro"lem.• Single-source directed paths: given a digraph and source s& is there

a directed path from s to v *f so& find such a

path.'epth/irst'irectedaths.,ava uses depth-first search to solvethis pro"lem.

52

Page 53: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 53/70

`

• Single-source shortest directed paths% given a digraph and

source s& is there a directed path from s to v *f so& find a shortest

such path.readth/rist'irectedaths.,ava  uses "readth-first search

to solve this pro"lem.

Cycles and DAGs.

 'irected cycles are of particular importance in applications that involveprocessing digraphs.

• Directed cycle detection% does a given digraph have a directed

cycle *f so& find such a cycle. 'irected2ycle.,ava solves this

pro"lem using depth-first search.• Depth-first orders% 'epth-first search search visits each vertex

exactly once. !hree vertex orderings are of interest in typical

applications%o Preorder % ut the vertex on a $ueue "efore the recursive calls.

o Postorder % ut the vertex on a $ueue after the recursive calls.

o Reverse postorder % ut the vertex on a stac+ after therecursive calls.

'epth/irst3rder.,ava computes these orders.

53

Page 54: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 54/70

`

• Topological sort % given a digraph& put the vertices in order such that

all its directed edges point from a vertex earlier in the order to avertex later in the order (or report that doing so is not

possi"le). !opological.,ava solves this pro"lem using depth-first

search. 4emar+a"ly& a reverse postorder in a 'A provides a

topological order.

Proposition.

 A digraph has a topological order if and only if it is a 'A.

Proposition.

 4everse postorder in a 'A is a topological sort.

54

Page 55: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 55/70

`

Proposition.

 With depth-first search& e can topologically sort a 'A in timeproportional to V 5 6.

Strong connectivity.

 trong connectivity is an e$uivalence relation on the set of vertices%• Reflexive% 6very vertex v is strongly connected to itself.

• Symmetric % *f v is strongly connected to & then is strongly

connected to v.• Transitive% *f v is strongly connected to and is strongly

connected to x& then v is also strongly connected to x.

trong connectivity partitions the vertices into e$uivalence classes& hiche refer to as strong components for short. We see+ to implement the

folloing A*%

4emar+a"ly& 7osara,uharir22.,ava implements the A* ith ,ust a fe

lines of code added to 22.,ava& as follos%• iven a digraph & use 'epth/irst3rder.,ava to compute the reverse

postorder of its reverse& 4.• 4un standard '/ on & "ut consider the unmar+ed vertices in the

order ,ust computed instead of the standard numerical order.• All vertices reached on a call to the recursive d-s)* from the

constructor are in a strong component (8)& so identify them as in

22.

Proposition.

 !he 7osara,u-harir algorithm uses preprocessing time and spaceproportional to V 5 6 to support constant-time strong connectivity $ueriesin a digraph.

Transitive closure.

 !he transitive closure of a digraph is another digraph ith the same setof vertices& "ut ith an edge from v to if and only if is reacha"le fromv in .

55

Page 56: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 56/70

`

!ransitive2losure.,ava computes the transitive closure of a digraph "y

running depth-first search from each vertex and storing the results. !his

solution is ideal for small or dense digraphs& "ut it is not a solution for the

large digraphs e might encounter in practice "ecause the constructor

uses space proportional to V9: and time proportional to V (V 5 6).

RESULT  Thus the ,G 'as eecute" a!" 6eri&ie" Success&u$$%.

E:.N ;

5

Page 57: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 57/70

`

  I!terme"iate C"e Ge!erati!

AIM T 'rite a C #r(ram t im#$eme!tati! & c"e (e!erati!

AL!RIT#M

ste# 1A Start.Ste# 2A E!ter the three a""ress c"es.Ste# 3A I& the c"e c!stitutes !$% memr% #era!"s the% are m6e" tthe re(ister a!" accr"i!( t the #erati! the crres#!"i!(assem-$% c"e is (e!erate".Ste# 4A I& the c"e c!stitutes imme"iate #era!"s the! the c"e 'i$$ ha6ea s%m-$ #rcee"i!( the !um-er i! c"e.

Ste# 5A I& the #era!" r three a""ress c"e i!6$6e #i!ters the! the c"e(e!erate" 'i$$ c!stitute #i!ter re(ister. This c!te!t ma% -estre" t ther $cati! r 6ice 6ersa.Ste# A ,##r#riate &u!cti!s a!" ther re$e6a!t "is#$a% stateme!ts areeecute".Ste# 7A St#.

SURCE C"E> i!c$u"est"i.hD

i!c$u"estri!(.hD6i" #m=>H6i" #$us=>H6i" "i6=>Hi!t i*ch*O*$*a""rN100Hchar e[10]*e#[10]*e#1[10]*e#2[10]*i"1[5]*#[5]*i"2[5]H6i" mai!=>c$rscr=>H'hi$e=1>

 #ri!t&=J!1.assi(!me!tJ!2.arithmeticJ!3.re$ati!a$J!4.EitJ!E!ter the chiceA>Hsca!&=L"*ch>Hs'itch=ch>case 1A

 #ri!t&=J!E!ter the e#ressi! 'ith assi(!me!t #eratrA>Hsca!&=Ls*e#>H$Nstr$e!=e#>He#2[0]NJ0HiN0H

'hi$e=e#[i]XNN>

57

Page 58: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 58/70

`

iHMstr!cat=e#2*e#*i>Hstrre6=e#>He#1[0]NJ0Hstr!cat=e#1*e#*$?=i1>>H

strre6=e#1>H #ri!t&=Three a""ress c"eAJ!tem#NLsJ!LsNtem#J!*e#1*e#2>H -rea+H

case 2A #ri!t&=J!E!ter the e#ressi! 'ith arithmetic #eratrA>Hsca!&=Ls*e>Hstrc#%=e#*e>H$Nstr$e!=e#>He#1[0]NJ0H

&r=iN0Hi$Hi>i&=e#[i]NNZZe#[i]NN?>i&=e#[i2]NN/ZZe#[i2]NNQ>

 #m=>H -rea+HMe$se

 #$us=>H -rea+HMMe$se i&=e#[i]NN/ZZe#[i]NNQ>"i6=>H

 -rea+HMM

 -rea+H

case 3A #ri!t&=E!ter the e#ressi! 'ith re$ati!a$ #eratr>Hsca!&=LsLsLs*i"1*#*i"2>Hi&===strcm#=#*>NN0>ZZ=strcm#=#*D>NN0>ZZ=strcm#=#*N>NN0>ZZ=strcm#=#*DN>NN0>ZZ=strcm#=#*NN>NN0>ZZ=strcm#=#*XN>NN0>>NN0>

 #ri!t&=E#ressi! is errr>He$se

 #ri!t&=J!L"Jti& LsLsLs (t L"*a""r*i"1*#*i"2*a""r3>H

a""rH #ri!t&=J!L"Jt TAN0*a""r>H

58

Page 59: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 59/70

`

a""rH #ri!t&=J!L"Jt (t L"*a""r*a""r2>Ha""rH

 #ri!t&=J!L"Jt TAN1*a""r>HM

 -rea+H

case 4Aeit=0>HMMM6i" #m=>strre6=e#>H

 ON$?i?1Hstr!cat=e#1*e#*O>Hstrre6=e#1>H

 #ri!t&=Three a""ress c"eAJ!tem#NLsJ!tem#1NLcLctem#J!*e#1*e#[O1]*e#[O]>HM6i" "i6=>str!cat=e#1*e#*i2>H

 #ri!t&=Three a""ress c"eAJ!tem#NLsJ!tem#1Ntem#LcLcJ!*e#1*e#[i2]*e#[i3]>HM6i" #$us=>str!cat=e#1*e#*i2>H

 #ri!t&=Three a""ress c"eAJ!tem#NLsJ!tem#1Ntem#LcLcJ!*e#1*e#[i2]*e#[i3]>HM

:UTPUT A

Eam#$e Ge!erati! & Three ,""ress PrOect :ut#ut 9esu$t

1. assi(!me!t

2. arithmetic3. re$ati!a$4. EitE!ter the chiceA1E!ter the e#ressi! 'ith assi(!me!t #eratrAaN-Three a""ress c"eAtem#N-aNtem#

1.assi(!me!t

2.arithmetic3.re$ati!a$

5<

Page 60: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 60/70

`

4.EitE!ter the chiceA2E!ter the e#ressi! 'ith arithmetic #eratrAa-?cThree a""ress c"eAtem#Na-

tem#1Ntem#?c

1.assi(!me!t2.arithmetic3.re$ati!a$4.EitE!ter the chiceA2E!ter the e#ressi! 'ith arithmetic #eratrAa?-/cThree a""ress c"eAtem#N-/c

tem#1Na?tem#

1.assi(!me!t2.arithmetic3.re$ati!a$4.EitE!ter the chiceA2E!ter the e#ressi! 'ith arithmetic #eratrAaQ-?cThree a""ress c"eAtem#NaQ-tem#1Ntem#?c

1.assi(!me!t2.arithmetic3.re$ati!a$4.EitE!ter the chiceA2E!ter the e#ressi! 'ith arithmetic #eratrAa/-QcThree a""ress c"eAtem#Na/-

tem#1Ntem#Qc1.assi(!me!t2.arithmetic3.re$ati!a$4.EitE!ter the chiceA3E!ter the e#ressi! 'ith re$ati!a$ #eratr aN

 -

100 i& aN- (t 103101 TAN0

0

Page 61: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 61/70

`

102 (t 104103 TAN1

1.assi(!me!t2.arithmetic3.re$ati!a$

4.EitE!ter the chiceA4

RESULT  Thus the Ge!erati! & Three ,""ress 'as eecute" a!" 6eri&ie" Success&u$$%.

E:.N. 15

1

Page 62: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 62/70

`

+ERATR +RECE"ENCE

AIM

T 'rite a C #r(ram t im#$eme!t the c!ce#t & #eratr #rece"e!ce.

AL!RIT#MA

1. Start the #r(ram.2. I!c$u"e the require" hea"er &i$es a!" start the "ec$arati! & mai! meth".3. ec$are the require" 6aria-$e a!" "e&i!e the &u!cti! &r #ushi!( a!" ##i!( the

characters.4. The #eratrs are "is#$a%e" i! c$ium! a!" r' 'ise a!" stre" it i! a queue.5. Usi!( a s'itch case &i!" the 6a$ues & the #eratrs.. is#$a% the #rece"e!ce & the #eratr a!" (e!erate the c"e &r #rece"e!ce &

#eratr &r the (i6e! e#ressi!.7. Cm#i$e a!" eecute the #r(ram &r the ut#ut.8. St# the #r(ram

+R!RAM

i!c$u"est"i.hDi!c$u"ec!i.hDi!c$u"estri!(.hDi!c$u"ect%#e.hDi!c$u"est"$i-.hDchar str[20]*st+[20]*#st+[20]Hi!t tsN?1*&$a(N0*#trN0*rmN?1*i*OHchar q[<][<]ND*D*****D**DM*D*D*****D**DM*D*D*D*D***D**DM*D*D*D*D***D**DM*D*D*****D**DM*******N**EM*D*D*D*D*D*E*D*E*DM*D*D*D*D*D*E*D*E*DM*******E**,M*MHchar c[<]N*?*Q*/*_*a*=*>*MH6i" #ushi!=char a>tsHst+[ts]NaHM

char ##ut=>char aHaNst+[ts]Hts??Hretur!=a>HMi!t &i!"=char a>s'itch=a>

caseAretur! 0Hcase?Aretur! 1H

2

Page 63: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 63/70

`

caseQAretur! 2Hcase/Aretur! 3Hcase_Aretur! 4Hcase=Aretur! 5Hcase>Aretur! HcaseaAretur! 7H

caseAretur! 8HMretur!?1HM6i" "is#$a%=char a>

 #ri!t&=J! SFIT Lc*a>HM6i" "is#$a%1=char a>i&=aXN=>

i&=isa$#ha=a>>

 #ri!t&=J! 9EUCE E??D Lc*a>He$se i&=aNN>>

 #ri!t&=J! 9EUCE E??D=E>>He$se

 #ri!t&=J! 9EUCE E??DE Lc E*a>HMMi!t re$=char a*char -*char ">i&=isa$#ha =a>>aNaHi&=isa$#ha=->>

 -NaHi&=q[&i!"=a>][&i!"=->]NN">retur! 1He$seretur! 0HM6i" mai!=>

c$rscr=>H #ri!t&=J!J!Jt The #r"ucti!s use" areAJ!Jt>H #ri!t&=E??DEQE/EE/E_E/EQE/E?EJ!JtE??DE/E J!JtE??Da/-/c/"/e.../@>H #ri!t&=J!JtE!tera!e#ressi!thattermi!a$s'ithA>H&&$ush=st"i!>HiN?1H'hi$e=str[i]XN>iHsca!&=Lc*str[i]>HM

&r=ON0HOiHO>

3

Page 64: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 64/70

`

i&==str[O]NN=>ZZ=str[O]NN>>ZZ=str[O1]NN=>ZZ=str[O1]NN>>>Me$se i&===isa$#ha=str[O]>NN0>=isa$#ha=str[O1]>NN0>>ZZ==isa$#ha=str[O]>XN0>=isa$#ha=str[O1]>XN0>>>

 #ri!t&=E99:9>H

(etch=>Heit=0>HMMi&====isa$#ha=str[0]>>XN0>ZZ=str[0]NN=>>===isa$#ha=str[i?1]>>XN0>ZZ=str[i?1]NN>>>>

 #ushi!=>H #ri!t&=J!J!J!JtJt?JtQJt/Jt_JtaJt=Jt>JtJ!J!>H&r=iN0Hi<Hi>

 #ri!t&=Lc*c[i]>H&r=ON0HO<HO> #ri!t&=JtLc*q[i][O]>H#ri!t&=J!>HM(etch=>H'hi$e=1>i&=str[#tr]NN st+[ts]NN>#ri!t&=J!J!Jt,CCEPTX>H-rea+HMe$se i&=re$=st+[ts]*str[#tr]*>ZZre$=st+[ts]*str[#tr]*NN>>

"is#$a%=str[#tr]>H#ushi!=str[#tr]>H#trHMe$se i&=re$=st+[ts]*str[#tr]*D>>"rmH#st+[rm]N##ut=>H"is#$a%1=#st+[rm]>HM'hi$e=Xre$=st+[ts]*#st+[rm]*>>HMe$se#ri!t&=J!J!Jt :T ,CCEPTEXXXXXXX>H(etch=>Heit=1>HMM(etch=>HMe$se

 #ri!t&=E99:9>H(etch=>HMM

UT+UT

 The #r"ucti!s use" areA  E??DEQE/EE/E_E/EQE/E?E  E??DE/E  E??Da/-/c/"/e.../@  E!ter a! e#ressi! that termi!a$s 'ith A a-Qc

  ? Q / _ a = >

D D D D? D D D DQ D D D D D D/ D D D D D D_ D D D Da N E= D D D D D E D E D> D D D D D E D E D

E ,

4

Page 65: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 65/70

`

 SFIT a 9EUCE E??D a SFIT  SFIT - 9EUCE E??D - SFIT Q

 SFIT c 9EUCE E??D c 9EUCE E??DE Q E 9EUCE E??DE E

  ,CCEPTX

RESULT

  Thus the C #r(ram im#$eme!tati! &r #eratr #rece"e!ce is eecute" a!" 6eri&ie".

E. N 6 S$u5 Eperimen$2

  +rin'ip7e2 %& C%mpi7er "e2i)n

5

Page 66: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 66/70

`

 ha$ i2 a '%mpi7er

Sim#$% state"* a cm#i$er is a #r(ram that rea"s a #r(ram 'ritte! i! !e $a!(ua(e?the surce $a!(ua(e?a!" tra!s$ates it i!t a! equi6a$e!t #r(ram i! a!ther $a!(ua(e?thetar(et $a!(ua(e =see &i(.1> ,s a! im#rta!t #art & this tra!s$ati! #rcess* the cm#i$er re#rts t its user the #rese!ce & errrs i! the surce #r(ram.

Cm#i$ers are smetimes c$assi&ie" as si!($e?#ass* mu$ti?#ass* $a"?a!"?(*"e-u((i!(* r #timi@i!(* "e#e!"i!( ! h' the% ha6e -ee! c!structe" r ! 'hat &u!cti!the% are su##se" t #er&rm. es#ite this a##are!t cm#$eit%* the -asic tas+s that a!%cm#i$er must #er&rm are esse!tia$$% the same.

 

/ ha$ are $he pha2e2 %& a '%mpi7er

+ha2e2 %& a C%mpi7er

1. )eica$ a!a$%sis =Rsca!!i!(>o 9ea"s i! #r(ram* (ru#s characters i!t Rt+e!s

2. S%!ta a!a$%sis =R#arsi!(>o Structures t+e! seque!ce accr"i!( t (rammar ru$es & the $a!(ua(e.

3. Sema!tic a!a$%siso Chec+s sema!tic c!strai!ts & the $a!(ua(e.

4. I!terme"iate c"e (e!erati!

o Tra!s$ates t R$'er $e6e$ re#rese!tati!.5. Pr(ram a!a$%sis a!" c"e #timi@ati!

o Im#r6es c"e qua$it%.

. i!a$ c"e (e!erati!.

4/ Ep7ain in e$ai7 i&&eren$ pha2e2 %& a '%mpi7er.

T#E "IFFERENT +#ASES F A CM+ILER  

C!ce#tua$$%* a cm#i$er #erates i!  phases* each & 'hich tra!s&rms the surce

 #r(ram &rm !e re#rese!tati! t a!ther.

Page 67: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 67/70

`

 

The &irst three #hases* &rms the -u$+ & the a!a$%sis #rti! & a cm#i$er. S%m-$ta-$e ma!a(eme!t a!" errr ha!"$i!(* are sh'! i!teracti!( 'ith the si #hases.

 

S5m@%7 $a@7e mana)emen$

 

,! esse!tia$ &u!cti! & a cm#i$er is t recr" the i"e!ti&iers use" i! the surce #r(ram a!" c$$ect i!&rmati! a-ut 6arius attri-utes & each i"e!ti&ier. , symbol table isa "ata structure c!tai!i!( a recr" &r each i"e!ti&ier* 'ith &ie$"s &r the attri-utes & thei"e!ti&ier. The "ata structure a$$'s us t &i!" the recr" &r each i"e!ti&ier quic+$% a!" tstre r retrie6e "ata &rm that recr" quic+$%. ;he! a! i"e!ti&ier i! the surce #r(ram is"etecte" -% the $e a!a$%@er* the i"e!ti&ier is e!tere" i!t the s%m-$ ta-$e.

 

Err%r "e$e'$i%n an Rep%r$in)

 

7

Page 68: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 68/70

`

Each #hase ca! e!cu!ter errrs. , cm#i$er that st#s 'he! it &i!"s the &irst errr is!t as he$#&u$ as it cu$" -e.

The s%!ta a!" sema!tic a!a$%sis #hases usua$$% ha!"$e a $ar(e &racti! & the errrs"etecta-$e -% the cm#i$er. The $eica$ #hase ca! "etect errrs 'here the charactersremai!i!( i! the i!#ut " !t &rm a!% t+e! & the $a!(ua(e. Errrs 'he! the t+e! stream

6i$ates the s%!ta & the $a!(ua(e are "etermi!e" -% the s%!ta a!a$%sis #hase. uri!(sema!tic a!a$%sis the cm#i$er tries t "etect c!structs that ha6e the ri(ht s%!tactic structure

 -ut ! mea!i!( t the #erati! i!6$6e".

 

The Ana752i2 pha2e2

 

,s tra!s$ati! #r(resses* the cm#i$erWs i!ter!a$ re#rese!tati! & the surce #r(ram

cha!(es. C!si"er the stateme!t*

 #siti! AN i!itia$ rate Q 10

 

The $eica$ a!a$%sis #hase rea"s the characters i! the surce #(m a!" (ru#s themi!t a stream & t+e!s i! 'hich each t+e! re#rese!ts a $(ica$$% chesi6e seque!ce & characters* such as a! i"e!ti&ier* a +e%'r" etc. The character seque!ce &rmi!( a t+e! isca$$e" the lexeme &r the t+e!. Certai! t+e!s 'i$$ -e au(me!te" -% a \$eica$ 6a$ueW. r eam#$e* &r a!% i"e!ti&ier the $e a!a$%@er (e!erates !t !$% the t+e! i" -ut a$s e!ter s the$eeme i!t the s%m-$ ta-$e* i& it is !t a$rea"% #rese!t there. The $eica$ 6a$ue assciate"this ccurre!ce & i" #i!ts t the s%m-$ ta-$e e!tr% &r this $eeme. The re#rese!tati! & the stateme!t (i6e! a-6e a&ter the $eica$ a!a$%sis 'u$" -eA

 

i"1A N i"2 i"3 Q 10

 

S%!ta a!a$%sis im#ses a hierarchica$ structure ! the t+e! stream* 'hich is sh'! -%s%!ta trees =&i( 3>.

 

In$ermeia$e C%e !enera$i%n

 

,&ter s%!ta a!" sema!tic a!a$%sis* sme cm#i$ers (e!erate a! e#$icit i!terme"iatere#rese!tati! & the surce #r(ram. This i!terme"iate re#rese!tati! ca! ha6e a 6ariet% & 

&rms.

8

Page 69: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 69/70

`

I! three?a""ress c"e* the surce #(m mi(ht $+ $i+e this*

 

tem#1A N i!ttrea$ =10>

tem#2A N i"3 Q tem#1

tem#3A N i"2 tem#2

i"1A N tem#3

 

C%e p$imi2a$i%n

 

The c"e #timi@ati! #hase attem#ts t im#r6e the i!terme"iate c"e* s that &aster ru!!i!( machi!e c"es 'i$$ resu$t. Sme #timi@ati!s are tri6ia$. There is a (reat 6ariati!i! the amu!t & c"e #timi@ati! "i&&ere!t cm#i$ers #er&rm. I! thse that " the mst*ca$$e" \#timisi!( cm#i$ersW* a si(!i&ica!t &racti! & the time & the cm#i$er is s#e!t !this #hase.

 

C%e !enera$i%n

 

The &i!a$ #hase & the cm#i$er is the (e!erati! & tar(et c"e* c!sisti!( !rma$$% & re$cata-$e machi!e c"e r assem-$% c"e. emr% $cati!s are se$ecte" &r each & the6aria-$es use" -% the #r(ram. The!* i!terme"iate i!structi!s are each tra!s$ate" i!t aseque!ce & machi!e i!structi!s that #er&rm the same tas+. , crucia$ as#ect is theassi(!me!t & 6aria-$es t re(isters.

 

/ ha$ i2 )r%upin) %& pha2e2!r%upin) %& +ha2e2

o  Front end  A machi!e i!"e#e!"e!t #hases

o )eica$ a!a$%sis

o S%!ta a!a$%sis

o Sema!tic a!a$%sis

o I!terme"iate c"e (e!erati!

o Sme c"e #timi@ati!

o  Back end  A machi!e "e#e!"e!t #hases

o i!a$ c"e (e!erati!

o achi!e?"e#e!"e!t #timi@ati!s

<

Page 70: CS0312 Compiler LAB MANUALedited

7/23/2019 CS0312 Compiler LAB MANUALedited

http://slidepdf.com/reader/full/cs0312-compiler-lab-manualedited 70/70

`

;/ Ep7ain Di$h ia)ram h%D a 2$a$emen$ i2 '%mpi7e .

RESULT Thus the a-6e Stu"% E#erime!ts is stu"ie" .

position := initial + rate * 60

   T   h  e

   P   h  a  s  e  s

  o   f  a

   C  o  m  p   i   l

  e  r

lexical analyzer 

id1 := id2 + id3 * 60

syntax analyzer 

  :=

id1

+

  id2

  *

id3

60

semantic analyzer 

  :=

id1

+

  id2

  *

id3  inttoreal

60

intermediate code generator 

temp1 := inttoreal (60)temp2 := id

3 * temp1

temp3 := id2 + temp2

id1 := temp3

code optimizer 

temp1 := id3 * 60.0

id1 := id2+ temp1

code generator 

M!"  id3# $2

M%&"  '60.0# $2

M!" id2# $1

 " $2#  $1

M!" $1# id1