You are on page 1of 47

Ex.No.: Date: Implementation of Lexical Analyser Aim: To execute a C program to implement the Lexical analyser.

Algorithm: Step1: Start the program. Step2: Declare the required keywords ,varia les ,identi!iers and sym ols in the required varia les. Step3: "ead the input program word y word.

Step4: Compare the each word in the input program with varia les declared in the main program. Step5: The similar varia les are stored in the varia le array. Step6: #rint the varia le array !or the list o! operators, special sym ols, data values, constant , keywords in the output !ile. Step7: Stop the program. Co ing: $include%stdio.h& char str'())*,token'+)*'+)*,sym '())*,temp'())*,op'())*,ss'())*,kw'+)*'+)*,dt'+)*'+)*,var'+)* '+)*,con'+)*'+)*, char kywrd'(-*'()*./0include0,0stdio.h0,0conio.h0,0main0,0getch0,0clrscr0,0void0,0i!0,0print!01, char dttyp'((*'()*./0int0,0char0,0!loat0,0dou le0,0long0,0short0,0struct0,0union0,0void01, char oper'2)*./343,353,363,373,383,393,3:3,3;3,3<3,3%3,3=3,3&3,3.31,

/ int i.),>.), while?str'i*5.3@)3A / i!?5isalpha?str'i*A77str'i*5.3 377str'i*5.3@n377str'i*5.3 3775isdigit?str'i*AA sym 'x<<*.str'i<<*, else i!?str'i*..3 3==str'i*..3@n3A i<<, 1

else / while?isalpha?str'i*A==str'i*..3.3==isdigit?str'i*AA temp'><<*.str'i<<*, temp'>*.3@)3, strcpy?token'k<<*,tempA, >.), 1 1 len.k;(, 1 void symsrch?A / int i,>.),k.),x.),!, !or?i.),i%strlen?sym A,i<<A / !.), !or?>.),>%strlen?operA,><<A / i!?sym 'i*..oper'>*A / op'k<<*.sym 'i*, !.(, reak, 1 1 i!?5!A ss'x<<*.sym 'i*, 1 2

1 void tknsrch?A / int i,>,!k.),!d.), !or?i.),i%len,i<<A / !or?>.),>%(-,><<A / i!?strcmp?token'i*,kywrd'>*A..)A !k.(, i!?strcmp?token'i*,dttyp'>*A..)A !d.(, 1 i!?!kA / !k.), strcpy?kw'ck<<*,token'i*A, 1 else i!?!dA / !d.), strcpy?dt'cd<<*,token'i*A, 1 else i!?5!d775!kA strcpy?var'cv<<*,token'i*A, 1 1 void elimdup?A / 3

char t, int i,>, !or?i.),i%strlen?opA,i<<A !or?>.i<(,>%strlen?opA,><<A i!?op'i*..op'>*A op'>*.3 3, !or?i.),i%strlen?ssA,i<<A !or?>.i<(,>%strlen?ssA,><<A i!?ss'i*..ss'>*A ss'>*.3 3, !or?i.),i%ck,i<<A !or?>.i<(,>%ck,><<A i!?strcmp?kw'i*,kw'>*A..)A strcpy?kw'>*,0 0A, !or?i.),i%cd,i<<A !or?>.i<(,>%cd,><<A i!?strcmp?dt'i*,dt'>*A..)A strcpy?dt'>*,0 0A, !or?i.),i%cv,i<<A !or?>.i<(,>%cv,><<A i!?strcmp?var'i*,var'>*A..)A strcpy?var'>*,0 0A, 1 void consep?A / int i, !or?i.),i%cv,i<<A i!?isdigit?var'i*')*AA / 4

strcpy?con'cc<<*,var'i*A, strcpy?var'i*,0 0A, 1 1 void main?A / int i, BCLD 8ip,8o, ip.!open?0kko.c0,0r<0A, clrscr?A, while?!gets?str,()),ipAA sepr?A, symsrch?A, tknsrch?A, elimsrch?A, consep?A, o.!open?0kkoout.txt0,0w<0A, !or?i.),i%strlen?opA,i<<A / i!?op'i*5.3 3A !print!?o,0:c0,op'i*A, 1 !print!?o,0@n@nS#DCCEL SFGHILS@n0A, !or?i.),i%strlen?ssA,i<<A / i!?ss'i*5.3 377ss'i*5.3@t3A !print!?o,0:c0,ss'i*A, 1 !print!?o,0@n@nDETE JELKDS@n0A, 5

!or?i.),i%cd,i<<A / i!?strcmp?dt'i*,0 0A5.)A !print!?o,0:s0,dt'i*A, 1 98!print!?o,0@n@nCDDLTCBCD"S@n0A, !or?i.),i%cv,i<<A / i!?strcmp?var'i*,0 0A5.)A !print!?o,0:s0,dt'i*A, 189 !print!?o,0@n@nCILSTELTS@n0A, !or?i.),i%cc,i<<A / i!?strcmp?con'i*,0 0A5.)A !print!?o,0:s0,con'i*A, 1 !print!?o,0@n@nMDFNI"DS@n0A, !or?i.),i%ck,i<<A / i!?strcmp?kw'i*,0 0A5.)A !print!?o,0:s0,kw'i*A, 1 1

6

!"tp"t:
I#D"ETI"S / . < 1

S#DCCEL SFGHILS

DETE JELKDS int CILSTELTS ()) MDFNI"DS void main clrscr print! scan! OO

#es"lt:
Thus the program to implement the lexical analyser was executed.

7

$x%&o% : 'ate:

Implementation of Sym(ol )a(le
Aim: To write a PCQ program !or the implementation o! Sym ol Ta le.

Algorithm: Step 1: Start the program. Step 2: Ipen the input !ile and read the input data. Step 3: The data in the !ile is read character y character and identi!y the required tokens.

Step 4: The tokens are identi!ied y comparing the grouped tokens with the declared values. Step 5: Ksing the required loops and !unctions the a ove operations are per!ormed. Step 6: Ipen the output !ile and write the required output into that !ile. Step 7: Stop the program.

Co ing: $include%stdio.h& $include%conio.h& $include%string.h& void main?A / char dtype'R*'+*./0int0,0dou le0,0!loat01, char str'R)*,ch,val'()*,temp'()*, int !.),>.),i,k,m, BCLD 8!p,8!p(, clrscr?A, !p.!open?0in.dat0,0r0A, !p(.!open?0out.dat0,0w0A, 8

!or?i.),i%R),i<<A str'i*.LKLL, !print!?!p(,0@n@tdtype@t@tidenti!er@tvalue0A, !scan!?!p,0:s0,strA, while?5!eo!?!pAA / !or?m.),m%R,m<<A / i!?5strcmpi?str,dtype'm*AA / !<<, reak, 1 1 i!?!&.(A / !scan!?!p,0:c:c0,7ch,7chA, while?ch5.3i3A / !.>.k.), !or?i.),i%(),i<<A / temp'i*.LKLL, val'i*.LKLL, 1 i!?isalpha?chAA / while?isalpha?chA==isdigit?chAA / 9

temp'>*.ch, !scan!?!p,0:c0,7chA, ><<, 1 !print!?!p(,0@n@t:s@t@t:s0,dtype'm*,tempA, i!?ch..3,3A / !scan!?!p,0:c0,7chA, while?isdigit?chAA / val'k*.ch, !scan!?!p,0:c0,7chA, 1 !print!?!p(,0@t@t:s0,valA, 1 else / !print!?!p(,0@t@tLKLL0A, 1 1 i!?ch..3,3A reak, !scan!?!p,0:c0,7chA, 1 1 !or?i.),i%R),i<<A str'i*.LKLL, !scan!?!p,0:s0,strA, 1!close?!pA, 10

!close?!p(A, getch?A, 1

!"tp"t:
Inp"t file: in.dat void main?A / print ( int a.2, .R,c, c.a< , print!?S"esult .:dT,cA, 1 !"tp"t file: out.dat Dtype int Cdenti!ier a Jalues Lull

#es"lt:
Thus the PCQ program to implement the Sym ol Ta le was executed.

11

$x%&o% : 'ate:

Implementation of Syntax Analy*er
Aim: To execute a PCQ program !or the implementation o! Syntax EnalyUer.

Algorithm: Step 1: Start the program. Step 2: Declare the required the varia les. Step 3: Ipen the required input !ile and read the data in that !ile. Step 4: The data that has een read is grouped into tokens to !ind the errors in the input program. Step 5: Ipen the required output !ile and write the required output into that !ile. Step 6: Stop the program.

Co ing: 99SFLTEV ELELWD" $include%stdio.h& $include%conio.h& $include%math.h& $include%string.h& $include%stdli .h& char input'-)*'())*,str'())*, char test'())*, char var'-)*'-)*, int n,vptr,a, ,c,d, get!ile?A / int i.), 12

BCLD 8!(, !(.!open?0hello.txt0,0r0A, while?5!eo!?!(AA !gets?input'i<<*,()),!(A, n.i, return, 1 int nochar?char chA / int i,>,noc.), !or?i.),i%n,i<<A !or?>.),input'i*'>*5.3@)3,><<A i!?input'i*'>*..chA noc<<, return noc, 1 void main?A / int i,>,temp,o!!set, char 8cp,m, vptr.), clrscr?A, get!ile?A, !or?i.),i%n,i<<A i!?input'i*')*..3$3A i!?5strstr?input'i*,0include0A==5strp rk?input'i*,0%0A==strp rk?input'i*,0&0AA print!?0@n line.:d error include line0,i<(A, !or?i.),i%n,i<<A i!?strstr?input'i*,0main0AA 13

/ i!?5strp rk?input'i*,0?0AA print!?0@n line:d:error in main statement missing?0,i<(A, else i!?5strp rk?input'i*,0A0AA print!?0@n line:d:error in main statement missingA0,i<(A, reak, 1 a.a s?nochar?3/3A;nochar?313AA, .a s?nochar?313A;nochar?3/3AA, c.a s?nochar?3?3A;nochar?3A3AA, d.a s?nochar?3A3A;nochar?3?3AA, i!?nochar?3/3A%nochar?313AA print!?0@n error unmatched:d1paranthesis0,aA, else i!?nochar?313A&nochar?3/3AA print!?0@n error unmatched:d/paranthesis0, A, else i!?nochar?3?3A%nochar?3A3AA print!?0@n error unmatched:dAparanthesis0,cA, else i!?nochar?3A3A&nochar?3?3AA print!?0@n error unmatched:d?paranthesis0,dA, print!?0@n0A, getch?A, 1

14

!"tp"t:
Inp"t file: hello.txt void main?A int a, ,c, a. <c, print!?S:dT,aA, 1 !"tp"t: Line (: error in main statement missing ? error unmatched (1 paranthesis

#es"lt:
Thus the PCQ program to implement the Syntax EnalyUer was executed.

15

Dx Lo : Date:

C!&S)#+C)!# !, &,A

Aim: To construct LBE !or the given regular expression.

Algorithm: Step 1:Xet the regular expression. Step 2:mark the initial state with a line and !inal state with dou le circle. Step 3:Gark the intermediate states with a single circle and a line connecting previous state and current state with an arrow towards the current state. Step 4:Display the input a ove the arrow !or every transition. Step 5:Cnitial state must have no incoming states and !inal state should not have any outgoing states. Step 6:The input is, E "DXKLE" DV#"DSSCIL PrQ IJD" EL EL#YEHDT PDQ. Step 7:The output is , EL LBE PLQ ECCD#TCLX L?rA. Step -:#arse PrQ into the su expressions. Step .:Ksing the rules construct LBE !or each o! the asic sym ols in PrQ. Co ing:

$include%stdio.h& $include%conio.h& $include%dos.h& $include%graphics.h& $include%math.h& void a?int,intA, void p?A, void main?A /

int gd.DDTDCT, gm, initgraph?7gd,7gm, Se:@@tc@@ gi@@A, 16

int ch, while?ch5.+A / print!?SGenu @n (. Dmpty Cnput @n -. a @n R. a= @n Z. a @n 2. ?a= A68 @n Dnter the exp SA, scan!?S:dT, 7chA, switch?chA /

case (: a?(2),(2)A, circle?-(),(2),(2A, outtextxy?([),(R2,TeT, outtetxy?(-2,(Z\,T&TA, line?((2,(2),(Z),(2)A, getch?A, cleardevice?A, reak, case -: a?(2),(2)A, circle?-(),(2),(2A, line?((2,(2),(Z),(2)A, outtextxy?(-2,(Z\,T&TA, outtextxy?([),(R2,TaTA, getch?A, cleardevice?A, reak, case R: circle?-Z2,-)),(2A, p?A, 17

getch?A, cleardevice?A, reak, case Z: a?(2),(2)A, a?-\2,(2)A, line?--),(2),-+2,(2)A, circle?RR2,(2),(2A, outtextxy?(\2,(Z),TaTA, outtextxy?R)),(Z),T TA, outextxy?-R2,(Z),TeTA, outtextxy?(-2,(Z\,T&TA, line?((),(2),(Z),(2)A, line?R(2,(2),R(),(Z2A, line?-Z2,(2),-Z),(Z2A, line?-Z2,(2),-Z),(22A, getch?A, cleardevice?A, reak, case 2: p?A, circle?R)),-)),()A, circle?R)),-)),(2A, circle?+2,-)),()A, line?-2,-)),22,-))A, line?-22,-)),-[2,-))A, outtexty?+2,-)),TCTA, outtextxy?R2,(O\,T&TA, outetxtxy?R)),-)),TBTA, 18

arc?([2,-(2,([),),(-)A, outtextxy?([2,RR-,T&TA, outextxy?-+2,([2,TeTA, outtextxy?-+2,(O\,T&TA, outtextxy?[2,([2,TeTA, outtextxy?([2,((),TeTA, outtextxy?([2,R-),TeTA, getch?A, cleardeive?A, 1 1 getch?A, closegraph?A, 1

void a?int x, int yA / int ch, circle?x,y,()A, circle?x<+),y,()A, line?x<(),y,x<2),yA, outtextxy?x,y,T(TA, outtextxy?x<+),y,T-TA, line?([2,y,([),y;2A, line?([2,y,([),y<2A, 1 void p?A / a?(2),(2)A, 19

a?(2),-2)A, circle?(-2,-)),()A, outtextxy?(-),(\),TeTA, outtextxy?[2,(O\,T&TA, outextxy?(-),--),TeTA, outtextxy?-Z),(\),TeTA, outtextxy?-Z),--),TeTA, outextxy?([),(R2,TaTA, outtextxy?([),-+),T TA, line?\2,-)),((2,-))A, line?(-2,(O),(Z),(22A, line?(-2,-(),(Z2,-Z)A, circle?-Z2,-)),()A, line?--),(22,-Z),([2A, line?-Z),-(2,--),-2)A, outtextxy?(-2,-)),T2TA, outextxy?-Z2,-)),T+TA, 1

20

!"tp"t

Genu (. -. R. Z. 2. D a < a. a ?a9 A8

Dnter the option -

a 0 1

+ 2

b 4

#es"lt

Thus the program !or Constructing LBE !or the given regular expression is executed success!ully

21

Dx Lo: Date: Aim:

C!/0+)A)I!& !, $1CL!S+#$

To execute a C program to compute the D;closure

Algorithm: Step 1:Xet the regular expression. Step 2:mark the initial state with a line and !inal state with dou le circle. Step 3:Gark the intermediate states with a single circle and a line connecting previous state and current state with an arrow towards the current state. Step 4:Display the input a ove the arrow !or every transition. Step 5:Cnitial state must have no incoming states and !inal state should not have any outgoing states. Step 6:The input is, E "DXKLE" DV#"DSSCIL PrQ IJD" EL EL#YEHDT PDQ. Step 7:The output is , EL LBE PLQ ECCD#TCLX L?rA. Step -:#arse PrQ into the su expressions. Step .:Ksing the rules construct LBE !or each o! the asic sym ols in PrQ.

Co ing: $include%stdio.h& $include%string.h& main?A / int i,>,k,n,tem, int t'()*'()*, int g'()*,stak'()*,ch, !or?i.),i%[,i<<A / !or?>.),>%[,><<A / t'i*'>*.), 1 1 t')*'(*.(, t')*'\*.\, t'(*'-*.-, t'(*'Z*.Z, t'R*'+*.+, t'2*'+*.+, t'+*'(*.(, t'+*'\*.\, print!?0@n@tstates@tD;closures@n0A, 22

!or?i.),i%[,i<<A / print!?0:d0,iA, print!?0@t0A, !or?>.),>%[,><<A / i!?t'i*'>*5.)A / print!?0@t:d0,t'i*'>*A, 1 1 print!?0@n0A, 1 do / k.), n.), print!?0@nenter the state o! e;closure0A, scan!?0:d0,7stak'k*A, do / tem.stak'k*, !or?>.),>%[,><<A / i!?t'tem*'>*5.)A / stak'k*.t'tem*'>*, k<<, 1 1 k;;, g'n*.tem, n<<, 1 while?k5.;(A, print!?0@n the e;closure o! the state :d0,g')*A, !or?i.),i%n,i<<A / print!?0@t :d0,g'i*A, 1 print!?0@n continue] (9)0A, scan!?0:d0,7chA, 1while?ch..(A,1

23

!"tp"t

States ) ( R Z 2 + \ Dnter the state o! D;closure : R

D;closure ( \ Z

+

+ (

\

The D;closure o! the state R are: R + \ ( Z -

#es"lt
Thus the program !or computing D;closure executed success!ully

24

Dx Lo: Date:

I/0L$/$&)A)I!& !, ,#!&) $&' !, A C!/0IL$# 2Arithmetic 3 Assignment !perator4

Aim

To implement !ront end o! a compiler Algorithm

Step (:Start the process Step -: Xet the regular expression as the input Step R: Scan the input and generate the R address code 8, 9 having higher precedence and <, ; while lower precedence

Co ing $include%stdio.h& $include%conio.h& void !unc?intA, struct / char op,arg('R*,arg-'R*,result'R*, 1ta le'2)*, char str'(2*'R*, int cnt.),tcnt.), void main?A / char c, int i.), 25

clrscr?A, print!?0@n@nDnter The Dxpression:0A, while??c.getch?AA5.(RA / putch?cA, i!?c..3;3==c..3<3==c..393==c..383==c..3.3A / str'cnt<<*'i*.3@)3, str'cnt*')*.c, str'cnt<<*'(*.3@)3, i.), 1 else str'cnt*'i<<*.c, 1 cnt<<, while?cnt&RA / !or?i.),i%cnt,i<<A i!?str'i*')*..383==str'i*')*..393A !unc?iA, !or?i.),i%cnt,i<<A i!?str'i*')*..3<3==str'i*')*..3;3A !unc?iA, 1 strcpy?ta le'tcnt*.result,str')*A, ta le'tcnt*.op.str'(*')*, strcpy?ta le'tcnt*.arg(,ta le'tcnt;(*.resultA, strcpy?ta le'tcnt<<*.arg-,00A, print!?0@n@n#os@tIp@tErg(@tErg-@t"es@n@n0A, !or?i.),i%tcnt,i<<A print!?0:d@t:c@t:s@t:s@t:s@n0,i,ta le'i*.op,ta le'i*.arg(,ta le'i*.arg-,ta le'i*.resultA, 26

print!?0@n@n@n0A, !or?i.),i%tcnt;(,i<<A print!?0:s.:s:c:s@n0,ta le'i*.result,ta le'i*.arg(,ta le'i*.op,ta le'i*.arg-A, print!?0:s:c:s@n0,ta le'i*.result,ta le'i*.op,ta le'i*.arg(A, getch?A, 1 void !unc?int iA / int >,k, ta le'tcnt*.op.str'i*')*, strcpy?ta le'tcnt*.arg(,str'i;(*A, strcpy?ta le'tcnt*.arg-,str'i<(*A, sprint!?ta le'tcnt*.result,0t:d0,tcntA,tcnt<<, strcpy?str'i;(*,ta le'tcnt;(*.resultA, !or?>.),>%-,><<A / !or?k.i,k%cnt,k<<A strcpy?str'k*,str'k<(*A, cnt;;, 1 1

27

!"tp"t:

Dnter the expression:

a. <c8d

#IS To T( T-

I# 8 < <

E"X( c

E"Xd )

"DSKLT t) t( a

t(

T).c8d T(< <) E.t(

#es"lt: Thus the !ront end o! a compiler is implemented y generating R address codes as input !or the regular expression

28

Dx Lo:

I/0L$/$&)A)I!& !, ,#!&) $&' !, A C!/0IL$# 2#elational !perators 4

Date:

Aim

To implement the !ront end o! a compiler.

Algorithm:

(. Xet the i! structure as input . -. Scan the input and generate the R address code ased on the true and !alse condition o! the i! then statement. R. The output is generated in the !ollowing !ormat, IKED"K#LD BI"GET Iperator arg( argresult

Co ing: $include%stdio.h& $include%string.h& void !unc?intA, struct / char op'R*,arg('R*,arg-'+*,result'R*, 1ta le'2)*, char str'(2*'R*, int cnt,tcnt.),tc.(, main?A / char c,s'-*'R)*, 29

int i,>,k, print!?0@n@nDnter The Dxpression@n0A, scan!?0:s :s :s :s :s0,s')*,ta le'tcnt*.arg(,ta le'tcnt*.op,ta le'tcnt*.arg-,s')*A, scan!?0:s :s :s0,s')*,s'-*,s'(*A, strcpy?ta le'tcnt<<*.result,0t)0A, strcpy?ta le'tcnt*.op,0..0A, strcpy?ta le'tcnt*.arg(,0t)0A, strcpy?ta le'tcnt<<*.arg-,0BELSD0A, !or?k.),k%-,k<<A / cnt.),i.), i!?k..(A sprint!?ta le'(*.result,0:d0,tcntA, !or?>.),>%strlen?s'k*A,><<A / i!?s'k*'>*..3;3==s'k*'>*..3<3==s'k*'>*..393==s'k*'>*..383==s'k*'>*..3.3A / str'cnt<<*'i*.3@)3, str'cnt*')*.s'k*'>*, str'cnt<<*'(*.3@)3, i.), 1 else str'cnt*'i<<*.s'k*'>*, 1 cnt<<, while?cnt&RA / !or?i.),i%cnt,i<<A i!?str'i*')*..383==str'i*')*..393A !unc?iA, 30

!or?i.),i%cnt,i<<A i!?str'i*')*..3<3==str'i*')*..3;3A !unc?iA, 1 strcpy?ta le'tcnt*.result,str')*A, sprint!?ta le'tcnt*.op,0:c0,str'(*')*A, strcpy?ta le'tcnt*.arg(,ta le'tcnt;(*.resultA, strcpy?ta le'tcnt<<*.arg-,00A, 1 print!?0@n@n#os@tIp@tErg(@tErg-@t"es@n@n0A, !or?i.),i%tcnt,i<<A print!?0:d@t:s@t:s@t:s@t:s@n0,i,ta le'i*.op, ta le'i*.arg(,ta le'i*.arg-,ta le'i*.resultA, print!?0@n@n@n0A,

1 void !unc?int iA / int >,k, sprint!?ta le'tcnt*.op,0:c0,str'i*')*A, strcpy?ta le'tcnt*.arg(,str'i;(*A, strcpy?ta le'tcnt*.arg-,str'i<(*A, sprint!?ta le'tcnt*.result,0t:d0,tc<<A,tcnt<<, strcpy?str'i;(*,ta le'tcnt;(*.resultA, !or?>.),>%-,><<A / !or?k.i,k%cnt,k<<A strcpy?str'k*,str'k<(*A, cnt;;, 1 31

1 !"tp"t:

DLTD" TYD DV#"DSSCIL C!?a% A E.a< Dlse E.a;

#IS ) ( R Z 2

I# % .. < . ; .

arg( a t) a t( a t-

arg-

"DSKLT t)

BELSD Z t( a ta

#es"lt: The !ront end o! the compiler is implemented and the [)[+ instructions are generated !or the given R address codes.

32

$x%&o%: 'ate:

Co e !ptimi*ation
Aim:
To execute a C program to implement the code optimiUation.

Algorithm:
Step 1: Start the program. Step 2: Ipen the input !ile in.txt and read the data !rom it. Step 3: Cdenti!y the varia les and operators separately and assign them to speci!ic varia les. Step 4: Convert the varia les to speci!ic identi!iers and their respective operators. Step 5: Ksing the required loops print the converted identi!iers in the !orm o! assem ly instructions. Step 6: #rint the assem ly instructions into the output !ile out.txt Step 7: Stop the program.

Co ing:
$include%stdio.h& $include%conio.h& $include%string.h& void main?A / char 8a,8 ,8c,8temp, int i.),!lag.),l(.), char 8!(,8!-, clrscr?A, print!?0@n enter the expression0A, scan!?0:s:s0,a, A, !lag.), !or?i.),i%.strlen?aA,i<<A 33

/ i!?a'i*..3.3A / !lag.(, i<<, 1 i!?!lagA / 8?temp<l(A.8?a<iA, l(<<, 1 1 8?temp<l(A.3@)3, !(.strstr? ,tempA, !-.strst r?c,tempA, i!?!(..LKLL77!-..LKLLA / print!?0@n expression couldn3t e optimiUed0A, exit?)A, 1 print!?0@n optimiUed code !or the expression0A, print!?0@n@n0A, print!?0t(.:s@n0,aA, i!?strlen?!(A&)A / !or?i.),i%-,i<<A print!?0@nt(0A, !or?i.),i%-,i<<A print!?0:c0,8? <strlen? A<i;-AA, 34

1 else / print!?0:s0, A, 1 print!?0@n0A, i!?strlen?!-A&)A / 98!or?i.),i%-,i<<A print!?0:c0,8?c<iAA, print!?0t(0A, 89 !or?i.),i%-,i<<A print!?0:c0,8?c<strlen?cA<i<-AA, 1 else / 99print!?0:s0,cA, 1 getch?A, 1

35

!"tp"t:
Dnter the expression a. <c d. <c;!

IptimiUed code !or the expression t(.a. <c t( t(;!

#es"lt: Thus the program to implement the code generation was executed.

36

$x%&o%: 'ate:

Co e 5eneration
Aim:
To execute a C program to implement the code generation.

Algorithm:
Step 1: Start the program. Step 2: Ipen the input !ile in.txt and read the data !rom it. Step 3: Cdenti!y the varia les and operators separately and assign them to speci!ic varia les. Step 4: Convert the varia les to speci!ic identi!iers and their respective operators. Step 5: Ksing the required loops print the converted identi!iers in the !orm o! assem ly instructions. Step 6: #rint the assem ly instructions into the output !ile out.txt Step 7: Stop the program.

Co ing:
$include%stdio.h& $include%conio.h& $include%ctype.h& void main?A / char code'\*'()*./0sta0,0mul0,0div0,0add0,0su 0,0mov01, char oper'()*./3.3,383,3<3,3;3,3931, char 8a,8a(,8 , int i,v,c,>,len,reg,reg(,r, int var.),k.(, BCLD 8!p,8!p(, clrscr?A, !p.!open?0in.txt0,0r<0A, 37

!p(.!open?0outp.txt0,0w<0A, print!?0@n code generation0A, rewind?!pA, while?5!eo!?!pAA / char !ound.),!.),s.), var<<, !scan!?!p,0:s0,a(A, strcpy?a,a(A, len.strlen?aA, !print!?!p(,0@n@n expr :s@n0,aA, !or?i.),i%len,i<<A / i!?isalpha?a'i*A==isdigit?a'i*AA / i!?a'i<(*5.3<3775!oundA / i!?5!A / reg.k, !print!?!p,0@n mov id:d,r:d0,var,kA, !.(, 1 i!?5!ound77!A / reg.k, !ound.(, 1 k<<, 38

1 1 i!?5isalpha?a'i*AA / i!?!ound77!A / !ound.), !or?>.),>%(),><<A / i!?a'i*..oper'>*A / i!?sA / !print!?!p(,0@n:s,id:d,r:d0,code'>*,var<<,<<regA, s.), i<<, var<<, reak, 1 i!?5sA / !print!?!p(,0@n mov id:d,r:d0,var<(,<<regA, v.var;(, !print!?!p(,0@n:s r:d,r:d0,code'>*,v,regA, reg(.reg, var<<, reak, 1 1 39

1 !ound.(, reg(.reg, 1 1 1 !print!?!p(,0@nmov r:d,id:d0,reg(,var<(A, var<<, 1 getch?A, 1

40

!"tp"t:
in%txt a. <c^d

o"t%txt CIDD XDLD"ETCIL expr a . < c ^ d mov id (d,rstart mov id Rd,rZ div rR mov id 2d,rZ addr 2 mov rR,id [d

#es"lt: Thus the program to implement the code generation was executed.

41

$x%&o%: 'ate:

St" y !f 0arser 5enerator
Aim:
To study a out parser generator?FECCA.

'escription:
Facc stands !or Syet another compiler computerT re!lecting the popularity o! parser generators in the early (O\)Qs when the !irst version o! yacc was created on the KLCV system and has een used to keys implementation o! hundreds o! compilers. E translator can e constructed using FECC.Birst a !ile, say translate y, containing a FECC speci!ication o! the translator is required. The KLCV system command, Facc translate.y Trans!orm the !ile translate.y into c program y.ta .c using the LEL" method. The program y.ta .c is a representation o! LEL" method.The y.ta .c is written in c along with another c routines that the user may prepared.Hy compiling y.ta .c along with the li rary that contains the L" parsing program using the command, Cc y.ta .c ^ly Ne o tain the desired o >ects program a.out that per!orm the translation y the original yacc program. C! other procedure are needed they can e compiled or loaded with y.ta .c, they stands with any c program. E yacc source program has three parts . Declaration. :: _ Translation !iles :: Supporting C routines. Facc speci!ication o! a simple desk calculator.consider the grammer !or arithmetic expressions.

42

D ;&D<T9T T ;&T8B9B B ;&?DA9digit The token digit is a single digit etween ) and \.

Co ing:
:s $include%ctype.h& :s1 :token DCXCT :: line:expr@nQ/print!?S:d@nT,`(A,1 , expr:exprQ<Qterm/``.`(<`R,1 = term , term:termQ8Q!actor/``.`(8`R,1 = !actor , Bactor:Q/PexprQ1Q/``.`-,1 = DCXCT , yylex?A/ int c, C.getchar?A, C!?is digit?AA/

43

yy val.c;P)Q, "eturn DCXCT, 1 "eturn C, 1

)he 'eclaration 0art: The declaration part o! yacc program is delimited y :s and :y.There are two optional selections in the declaration.Cn the !irst section, we put ordinary c declaration.Yere we place declaration o! any temporaries used y the translation rules or procedures o! the second and third sections. The include statement $include %ctype.h&causes the c processor to include the std.Yeader !ile %ctype.h&that contains the predicates its DCXCT. Elso in the declaration part ,are declared o! grammer tokens.The statement : token DCXCT to e token.Tokens declared in this section can e used in second and third parts o! the yacc speci!ication.

)he )ranslation #"les 0art: Cn the part o! the yacc speci!ication a!ter the !irst :: pair,we put the translation a!ter rule.E set o! production have een writing %le!tside& a %alt(9%9%alt-&b9%altn&. Nould e written in yacc as %le!tside&=%alt(&/semantic action(1 =%alt-&/semantic action-1 =b =%altn&/semantic action1

Cn tacc production,a quoted single character PCQ is taken to e the terminal sym ol PcQ and the unquoted string o! letter and DCXCT not declared to the token and nonterminals

44

E yacc semantic action is the sequence o! c statements.Cn a semantic action the sym ol `` re!ers to the attri ute value associated with the nonterminal on the le!t ,while `i re!ers to the value associated on the right. Cn a yacc speci!ication we have two PDQ productions, DaD<T9T End the associative semantic actions are, Dpr:exprQ<Qterm/``.`(<`R,1 =term ,

$rror 6an ling: The token error is reserved !or error handling. The parser will detect the sym ol error when it is in a state when the action associated with the look ahead sym ol is error. E semantic action can cause a parser to initiate error handling. Nhen FFD""I" is executed semantic action will cause the control to the parser. FFD""I" canQt e used outside the semantic action. Interface )o )he Lexical Analy*er: The yylex?A !unction is an integer valued !unction that return a token num er representing the kind o! token read. C! there is a value associated with token return yylex?A, it will e assigned to the external varia les yyval?A. C! the parser and yylex?A do not agree on this token num ers relia le communication etween them cannot occur. Bor ?one characterA literals, the token is simply a numerical values o! the character in the current character set. C! the token num ers are chosen y yacc the token other than end marker which has a token num er that is Uero. Comp"ting )he 0rogram: Cn addition to the yyparse?A and yylex?A, the !unction FFD""I" are required to make a complete program. Facc li rary: Cnt main?voidA This !unction can run yyparse?A and exit with an unspeci!ied value. Cnt yyerror?const char8sA 45

This !unction will return null terminal argument to standard errors !ollowed y a new line character.

'e("gging )he 0arser: The parser generated y the yacc will have a diagnostic !acility init that can e optionally ena led at either compile or run time. Compilation o! run time de ugging code is under the control o! yyde ug, a preprocessor sym ol, i! FFDDHKX as an non Uero value, the de ugging code is coded. Nhen ^t is speci!ied, the code !ile will e uilt such that i! DDHKX is not already de!ined at compilation time ^t. FFDDHKX is not readily de!ined, it will e set explicitly Uero. Limit: The yacc utility may have several internal ta les. The exact value o! these values are implementation dependent. The implementation will de!ine the relationship etween the values in etween them. Eny error manages that the implementation may com ine groups o! this resources into as long as the total availa le to the user does not !act elow the sum o! the siUes. LCGCT /LTD"GS1 /LLITD"GS1 /L#"ID1 /LSTETDS1 /GDGSCWD1 /ECTSCWD1 GCLCGKG ELLICETCIL (-+ -)) R)) +)) 2-)) Z))) DDSC"C#TCIL Lo o! tokens Lo o! non terminals Lo o! rules Lo o! states Length o! rules Lo o! action

DVCT STETKS: The !ollowing DVCT values are extended.

) ;&success!ul completion. ) ;&error occurs.

46

#es"lt:
Thus the parser generator?FECCA has een studied.

47