To write a C program to implement a symbol table. ALGORITHM:
1) Start the program. 2) Get the input from the user with the terminating symbol ‘$’. ) !llo"ate memory for the #ariable by ynami" memory allo"ation fun"tion. %) &f the ne't "hara"ter of the symbol is an operator then only the memory is allo"ate. () hile reaing* the input symbol is inserte into symbol table along with its memory aress. +) The steps are repeate till ‘$’ is rea"he. ,) To rea"h a #ariable* enter the #ariable to the sear"he an symbol table has been "he"-e for "orresponing #ariable* the #ariable along with its aress is isplaye as result. ) Stop the program. PROGRAM:
3 if644n) 3 p4mallo"")7 a9'4p7 9'4"7 printf;B"tBtientifier;*"*p)7 @ else 3 "h4b96?17 if"h44>?>DD"h44>A>DD"h44>8>DD"h44>4>) 3 p4mallo"")7 a9'4p7 9'4"7 printf;nB"tBtientifiern;*"*p)7 '??7 @@@ 6??7 @ printf;nThe symbol is to be sear"he;)7 sr"h4get"h)7 fori457i04'7i??) 3 ifsr"h449i) 3 printf;nSymbol Eoun;)7 printf;nB"BsBn;*sr"h*; Faress ;*a9i)7 flag417 @ @ ifflag445) printf;nSymbol ot Eoun;)7 get"h)7 @ OUTPUT: <'pression terminate by $ : "4a?b$ Gi#en <'pression : "4a?b Symbol Table Symbol !r Type " 1H55 ientifier a 255% ientifier b 215+ ientifier The symbol is to be sear"he Symbol Eoun b Faress 215+
RESULT: Thus the C program for symbol table is implemente an e'e"ute su""essfully. 2
2. DEVELOP A LEXICAL ANALYZER TO RECOGNIZE A FEW PATTERNS IN C AIM:
To write an e'e"ute a C program to implement the le'i"al analyIer. ALGORITHM:
1. Start the program. 2. Je"lare the file pointer an ne"essary #ariables. . Kpen the input file in the rea moe. %. Lse the analyIe fun"tion to analyIe the input program an store the ientifiers* -eywors an operator on ih* -eyh* oph respe"ti#ely. (. Stores the to-ens in ata stru"ture lin-e lists. +. &n"rement the line number of ea"h to-en an its o""urren"es. ,. Lsing the show fun"tion print the lin-e lists in a tabular format. . Stop the program.
3 str9-4>5>7 -eyworstr)7 -457 @ @ f"losef2)7 f4fopen;spe"ial"har;*;r;)7 printf;nSpe"ial "hara"ters are;)7 while"4get"f))=4 s in the program are1525 The -eywors an ientifiersare: in"lue is an ientifier stio is an ientifier h is an ientifier main is an ientifier int is a -eywor a is an ientifier b is an ientifier " is an ientifier " is an ientifier a is an ientifier b is an ientifier printf is an ientifier is an ientifier " is an ientifier Spe"ial "hara"ters are/0.)34*4*74?7;B;*)7@ Total no. of lines are:
RESULT:
Thus the C program for le'i"al analyIer to re"ogniIe few patterns is implemente an e'e"ute su""essfully.
3. IMPLEMENTATION OF LEXICAL ANALYZER USING LEX TOOL AIM:
To implement the le'i"al analyIer using le' tool for a subset of C language. ALGORITHM:
1.
Start the program.
2.
Je"lare ne"essary #ariables an "reates to-en representation using Regular.
.
rint the pre pro"essor or ire"ti#es* -eywors by analysis of the input program.
%.
&n the program "he"- whether there are arguments.
(.
Je"lare a file an open it as rea moe.
+. Rea the file an if any ta-en in sour"e program mat"hes with R< that all returne as integer #alue. ,.
rint the to-en ientifie using e') fun"tion.
.
Stop the program
PROGRAM:
B3 B@ ientifier9aAI!AQ9aAI!AQ5AH8 BB /.8 3printf;nBs is a prepro"essor ire"ti#e;*yyte't)7@ int D float D "har D ouble D while D o D if D brea- D "ontinue D #oi D swit"h D return D else D goto 3printf;nBs is a -eywor;*yyte't)7@ 3ientifier@ 3printf;n fun"tion Bs;*yyte't)7@ 3 3printf;nblo"- begins;)7@ @ 3printf;nblo"- ens;)7@ 3printf;n;)7
04 D 4 D 0 D D 44 3printf;n Bs is a relational operator;*yyte't)7@ 4 D ? D A D N D V D B 3printf;n Bs is a operator;*yyte't)7@ .D n7 BB int mainint arg"*"har 88arg#) 3 E&M< 8file7 file4fopen;inp.";*;r;)7 if=file) 3 printf;"oul not open the file===;)7 e'it5)7 @ yyin4file7 yyle')7 printf;nn;)7 return5)7 @ int yywrap) 3 return 17 @
"
OUTPUT:
C:Jo"uments an SettingsaminJes-topfle' ale'.l C:Jo"uments an SettingsaminJes-topg"" le'.yy." C:Jo"uments an SettingsaminJes-topa.e'e /in"lue0stio.h is a prepro"essor ire"ti#e #oi is a -eywor fun"tion main blo"- begins int is a -eywor a is an ientifier b is an ientifier " is an ientifier fun"tion printf ;enter the #alue for a*b; is a string fun"tion s"anf ;BB; is a string V is a operator a is an ientifier V is a operator b is an ientifier " is an ientifier 4 is a operator a is an ientifier ? is a operator b is an ientifier fun"tion printf ;the #alue of ":B; is a string V is a operator " is an ientifier blo"- ens
RESULT: Thus the program to implement le'i"al analyIer using le' tool is e'e"ute an implemente
su""essfully. #
4. GENERATE CATEGORIES
YACC
SPECIFICATION
FOR
A
FEW
SYNTACTIC
A$ PROGRAM TO RECOGNIZE A VALID ARITHMETIC EXPRESSION THAT USES OPERATOR % ' ( AND ). ALGORITHM:
1. !""ept the to-en generate in le' part as input 2. Spe"ify orer of pro"eure . Jefine rules with en point %. arse input string from stanar input by "alling yyparse) by main fun"tion (. rint the result of any rules mat"hes +. &f none of results efine mat"hes print Win#ali e'pressionX COMPILATION STEPS:
Bto-en LZ[?> >A> Bleft >8> >N> BB stmt : e'p M 3 printfW\ali <'pressionX)7 e'it5)7@ 7 e'p : e'p >?> e'p D e'p >A> e'p D e'p >8> e'p D e'p >N> e'p D >> e'p >)> D &J D LZ[
main ) 3 printfW
L-,E-.L0
B3 /in"lue Wy.tab.hX B@ BB 95AH? 3 return LZ[
RESULT: Thus the program for #aliating arithmeti" e'pressions using a"" is implemente an e'e"ute su""essfully.
1
B$ PROGRAM TO RECOGNIZE A VALID VARIABLE WHICH STARTS WITH A LETTER FOLLOWED BY ANY NUMBER OF LETTERS OR DIGITS. PROGRAM: Y*++,L.Y0
Bto-en J&G&T M
B3 /in"lue Wy.tab.hX B@ BB 9aAI!AQ 3 return M
RESULT: Thus the program is e'e"ute su""essfully. 11
C$ IMPLEMENTATION OF CALCULATOR USING LEX AND YACC PROGRAM: L-,C*5.L0
Thus the program to implement "al"ulator using M<_ an !CC tool is e'e"ute su""essfully an output is #erifie. 13
. CONVERT THE BNF RULES INTO YACC FORM AND WRITE CODE TO GENERATE ABSTRACT SYNTAX TREE. AIM:
To Con#ert the [E rules into a"" form an write "oe to generate !bstra"t Synta' Tree ALGORITHM:
1. 2. . %. (.
Start the program. &n"lue the heaer file.
&n int "oe.l*e"lare the #ariable lie no as integer an assign it to be e`ual to ‘1’. Start the int "oe.l with e"larati#e se"tion. &n translation rules se"tion efine -eywors *ata types an integer along with their a"tions .
+. Start the main blo"-. &n main blo"- "he"- the statement ,. 1.e"larati#e 2.assignment ."onitional %.if an else (.hile assignment. . erform the a"tions of that parti"ular blo"-. H. &n main program e"lare the parameters arg " as int en 8arg#9 as "har. &n main program open file in rea moe. 15. rint the output in a file. 11.
B3 /in"lue;y.tab.h; /in"lue0stio.h /in"lue0string.h int Mineo417 B@ ientifier 9aAI!AQ9OaAI!AQ5AH8 number 95AH?D95AH8.95AH?) BB main) return Z!&7 if return &E7 else return
04 D 44 3str"pyyyl#al.#ar*yyte't)7 return R
B3 /in"lue0string.h /in"lue0stio.h stru"t `ua 3 "har op9(7 "har arg19157 "har arg29157 "har result9157 @L!J957 stru"t sta"3 int items91557 int top7 @st-7 int &ne'45*t&ne'45*Sto*&n*t&n7 e'tern int Mineo7 B@ Bunion 3 "har #ar9157 @ Bto-en 0#ar LZ \!R RA> >?> Bleft >8> >N> BB RKGR!Z : Z!& [MKC 7 [MKC: >3> CKJ< >@> 7 CKJ<: [MKC D ST!T7> D !SS&GZ<T >7> D CKJST D U&M
7 J*> \!RM&ST D \!R 7 !SS&GZ<T: \!R >4> <_R3 str"pyL!J9&ne'.op*;4;)7 str"pyL!J9&ne'.arg1*$)7 str"pyL!J9&ne'.arg2*;;)7 str"pyL!J9&ne'.result*$1)7 str"py$$*L!J9&ne'??.result)7 @ 7 <_R: <_R >?> <_R 3!uaruple;?;*$1*$*$$)7@ D <_R >A> <_R 3!uaruple;A;*$1*$*$$)7@ D <_R >8> <_R 3!uaruple;8;*$1*$*$$)7@ D <_R >N> <_R 3!uaruple;N;*$1*$*$$)7@ D >A> <_R 3!uaruple;LZ&;*$2*;;*$$)7@ D >> <_R >)> 3str"py$$*$2)7@ D \!R D LZ 7 CKJST: &EST3 &n4pop)7 sprintfL!J9&n.result*;B;*&ne')7 &n4pop)7 sprintfL!J9&n.result*;B;*&ne')7 @ D &EST > CKJ&T&K >)> 3 str"pyL!J9&ne'.op*;44;)7 str"pyL!J9&ne'.arg1*$)7 str"pyL!J9&ne'.arg2*;E!MS<;)7 str"pyL!J9&ne'.result*;A1;)7 push&ne')7 &ne'??7 @ [MKC 3 str"pyL!J9&ne'.op*;GKTK;)7 str"pyL!J9&ne'.arg1*;;)7 str"pyL!J9&ne'.arg2*;;)7 str"pyL!J9&ne'.result*;A1;)7 push&ne')7 &ne'??7 @7
sprintfL!J9&n.result*;B;*&ne')7 @ [MKC3 &n4pop)7 sprintfL!J9&n.result*;B;*&ne')7 @7 CKJ&T&K: \!R R> CKJ&T&K >)> 3 str"pyL!J9&ne'.op*;44;)7 str"pyL!J9&ne'.arg1*$)7 str"pyL!J9&ne'.arg2*;E!MS<;)7 str"pyL!J9&ne'.result*;A1;)7 push&ne')7 &ne'??7 @ [MKC 3 str"pyL!J9&ne'.op*;GKTK;)7 str"pyL!J9&ne'.arg1*;;)7 str"pyL!J9&ne'.arg2*;;)7 str"pyL!J9&ne'.result*;A1;)7 push&ne')7 &ne'??7 @ 7 BB e'tern E&M< 8yyin7 int mainint arg"*"har 8arg#9) 3 E&M< 8fp7 int i7 ifarg"1) 3 fp4fopenarg#91*;r;)7 if=fp) 3 printf;n Eile not foun;)7 e'it5)7 @ yyin4fp7 1"
9linu'pertFfosslab ^$ ya"" A bnf.y 9linu'pertFfosslab ^$ g"" le'.yy." y.tab." All Alm 9linu'pertFfosslab ^$ .Na.out test." AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA O<*;< A<=1 A<=2 R85 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 5 0 a b t5
P;
1
44
2 ?
t5
a
E!MS<
b
4
t1
t1
%
a
GKTK (
(
0
a
+
b
44 , ?
t2
t2 a
b
4
E!MS<
a
GKTK 15
a
1% 1( 1+
b
44
12 A 1
(
04
11
t% a
t% E!MS<
b
4
t(
t(
"
GKTK ? 4
15
t
t
H
(
1, a
b t+
t+ " 1
1(
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA RESULT: Thus the program to "on#ert the [E rules into a"" form is implemente an e'e"ute su""essfully.
#. IMPLEMENT ANY ONE STORAGE ALLOCATION STRATEGIES >HEAP STAC? STATIC$ AIM:
To &mplement any one storage allo"ation strategies Ueap*Sta"-*Stati") PROGRAM:
/in"lue0stio.h /in"lue0"onio.h /in"lue0stlib.h /efine TRL< 1 /efine E!MS< 5 typeef stru"t Ueap 3 int ata7 stru"t Ueap 8ne't7 @noe7 noe 8"reate)7 #oi main) 3 N8lo"al e"larations8N int "hoi"e*#al7 "har ans7 noe 8hea7 #oi isplaynoe 8)7 noe 8sear"hnoe 8*int)7 noe 8insertnoe 8)7 #oi elenoe 88)7 hea4LMM7 o 3 "lrs"r)7 printf;n rogram to perform #arious operations on heapusing ynami" memory management;)7 printf;n1.Create;)7 printf;n2.Jisplay;)7 printf;n.&nsert an element in a list;)7 printf;n%.Jelete an element from list;)7 printf;n(.uit;)7 printf;n
brea-7 "ase :hea4inserthea)7 brea-7 "ase %:eleVhea)7 brea-7 "ase (:e'it5)7 efault:"lrs"r)7 printf;&n#ali Choi"e*Try again;)7 get"h)7 @ @while"hoi"e=4()7 @ noe 8"reate) 3 noe 8temp*8new1*8hea7 int #al*flag7 "har ans4>y>7 noe 8getOnoe)7 temp4LMM7 flag4TRL<7 N8flag to ini"ate whether a new noe is "reate for the first time or not8N o 3 printf;n y>)7 printf;nThe list is "reate;)7 get"h)7 "lrs"r)7 return hea7 @ noe 8getOnoe) 21
3 noe 8temp7 temp4noe8)mallo"siIeofnoe))7 NNusing the mem. !llo"ation fun"tion tempAne't4LMM7 return temp7 @ #oi isplaynoe8hea) 3 noe 8temp7 temp4hea7 iftemp44LMM) 3 printf;n The list is emptyn;)7 get"h)7 "lrs"r)7 return7 @ whiletemp=4 LMM) 3 printf;BA;*tempA ata)7 temp4tempAne't7 @ printf;LMM;)7 get"h)7 "lrs"r)7 @ noe 8sear"hnoe 8hea*int -ey) 3 noe 8temp7 int foun7 temp4hea7 if temp44LMM) 3 printf;The lin-e list is emptyn;)7 get"h)7 "lrs"r)7 return LMM7 @ foun4E!MS<7 whiletemp=4LMM)VVfoun44E!MS<)) 3 iftempAata =4 -ey) temp 4 tempAne't7 else foun 4 TRL<7 @ iffoun 44 TRL<) 3 printf;n The
return temp7 @ else printf;n The
printf ;n
pre# 4 temp7 temp 4 tempAne't7 @ else flag 4 TRL<7 @ ifflag) N8if Elag is true8N return pre#7 else return LMM7 @ #oi elenoe 88hea) 3 int -ey7 noe 8ew*8temp* 8pre#7 temp48hea7 if temp44 LMM) 3 printf ;n The list is emptyn ;)7 get"h)7 "lrs"r)7 return7 @ "lrs"r)7 printf;n<T
2
OUTPUT:
rogram to perform #arious operations on heap using Jynami" memory management. 1.Create 2.Jisplay .&nsert an element in a list %. Jelete an element from list (. uit
rogram to perform #arious operations on Ueap using Jynami" memory management. 1. Create 2. Jisplay . &nsert an element in a list %. Jelete an element from list (. uit
RESULT: Thus the program to implement storage allo"ation for U
2!
1. IMPLEMENT THE BAC? END OF THE COMPILER WHICH TA?ES THE THREE ADDRESS CODE AND PRODUCES THE ##! ASSEMBLY LANGUAGE INSTRUCTIONS THAT CAN BE ASSEMBLED AND RUN USING A ##! ASSEMBLER. THE TARGET ASSEMBLY INSTRUCTIONS CAN BE SIMPLE MOVE ADD SUB @UMP. ALSO SIMPLE ADDRESSING MODES ARE USED. OR IMPLEMENTATION OF CODE GENERATOR AIM:
To write a C program to implement Simple Coe Generator. ALGORITHM:
1. 2. . %. (. +. ,. . H.
Start Get aress "oe se`uen"e. Jetermine "urrent lo"ation of using aress for 1st operan). &f "urrent lo"ation not alreay e'ist generate mo#e [*K). Lpate aress of !for 2n operan). &f "urrent #alue of [ an ) is null*e'ist. &f they generate operator ) !* !JR. Store the mo#e instru"tion in memory Stop