· 6 years ago · May 01, 2019, 07:54 AM
1***PASS TWO ASSEMBLER***
2
3PASS 1:
4#include<stdio.h>
5#include<conio.h>
6#include<string.h>
7void main()
8{
9FILE *f1,*f2,*f3,*f4;
10int lc,sa,l,op1,o,len;
11char m1[20],la[20],op[20],otp[20];
12clrscr();
13f1=fopen("input.txt","r");
14f3=fopen("symtab.txt","w");
15fscanf(f1,"%s %s %d",la,m1,&op1);
16if(strcmp(m1,"START")==0)
17{
18 sa=op1;
19 lc=sa;
20 printf("\t%s\t%s\t%d\n",la,m1,op1);
21 }
22 else
23 lc=0;
24fscanf(f1,"%s %s",la,m1);
25while(!feof(f1))
26{
27 fscanf(f1,"%s",op);
28 printf("\n%d\t%s\t%s\t%s\n",lc,la,m1,op);
29 if(strcmp(la,"-")!=0)
30 {
31 fprintf(f3,"\n%d\t%s\n",lc,la);
32 }
33 f2=fopen("optab.txt","r");
34 fscanf(f2,"%s %d",otp,&o);
35 while(!feof(f2))
36 {
37 if(strcmp(m1,otp)==0)
38 {
39 lc=lc+3;
40 break;
41 }
42 fscanf(f2,"%s %d",otp,&o);
43 }
44 fclose(f2);
45 if(strcmp(m1,"WORD")==0)
46
47 {
48 lc=lc+3;
49 }
50 else if(strcmp(m1,"RESW")==0)
51 {
52 op1=atoi(op);
53 lc=lc+(3*op1);
54 }
55 else if(strcmp(m1,"BYTE")==0)
56 {
57 if(op[0]=='X')
58 lc=lc+1;
59 else
60 {
61 len=strlen(op)-2;
62 lc=lc+len;}
63 }
64 else if(strcmp(m1,"RESB")==0)
65 {
66 op1=atoi(op);
67 lc=lc+op1;
68 }
69 fscanf(f1,"%s%s",la,m1);
70 }
71 if(strcmp(m1,"END")==0)
72 {
73 printf("Program length =\n%d",lc-sa);
74 }
75 fclose(f1);
76 fclose(f3);
77 getch();
78 }
79
80Input.txt
81
82COPY START 1000
83- LDA ALPHA
84- ADD ONE
85- SUB TWO
86- STA BETA
87ALPHA BYTE C'KLNCE
88ONE RESB 2
89TWO WORD 5
90BETA RESW 1
91- END -
92
93Optab.txt
94
95LDA 00
96STA 23
97ADD 01
98SUB 05
99
100Output:
101Symtab.txt
102
1031012 ALPHA
1041017 ONE
1051019 TWO
1061022 BETA
107
108COPY START 1000
1091000 - LDA ALPHA
1101003 - ADD ONE
1111006 - SUB TWO
1121009 - STA BETA
1131012 ALPHA BYTE C'KLNCE
1141017 ONE RESB 2
1151019 TWO WORD 5
1161022 BETA RESW 1
1171025 - END -
118Program length = 25
119
120
121PASS 2:
122#include<stdio.h>
123#include<conio.h>
124#include<string.h>
125#include<ctype.h>
126main()
127{
128FILE *fint,*ftab,*flen,*fsym;
129int op1[10],txtlen,txtlen1,i,j=0,len;
130char add[5],symadd[5],op[5],start[10],temp[30],line[20],label[20],mne[10],operand[10],symtab[10],opmne[10];
131clrscr();
132fint=fopen("input.txt","r");
133flen=fopen("length.txt","r");
134ftab=fopen("optab.txt","r");
135fsym=fopen("symbol.txt","r");
136fscanf(fint,"%s%s%s%s",add,label,mne,operand);
137if(strcmp(mne,"START")==0)
138{
139strcpy(start,operand);
140fscanf(flen,"%d",&len);
141}
142printf("H^%s^%s^%d\nT^00%s^",label,start,len,start);
143fscanf(fint,"%s%s%s%s",add,label,mne,operand);
144while(strcmp(mne,"END")!=0)
145{
146fscanf(ftab,"%s%s",opmne,op);
147while(!feof(ftab))
148{
149if(strcmp(mne,opmne)==0)
150{
151fclose(ftab);
152fscanf(fsym,"%s%s",symadd,symtab);
153while(!feof(fsym))
154{
155if(strcmp(operand,symtab)==0)
156{
157printf("%s%s^",op,symadd);
158break;
159}
160else
161fscanf(fsym,"%s%s",symadd,symtab);
162}
163break;
164}
165else
166fscanf(ftab,"%s%s",opmne,op);
167}
168if((strcmp(mne,"BYTE")==0)||(strcmp(mne,"WORD")==0))
169{
170if(strcmp(mne,"WORD")==0)
171printf("0000%s^",operand);
172else
173{
174len=strlen(operand);
175for(i=2;i<len;i++)
176{
177printf("%d",operand[i]);
178}
179printf("^");
180}
181}
182fscanf(fint,"%s%s%s%s",add,label,mne,operand);
183ftab=fopen("optab.txt","r");
184fseek(ftab,SEEK_SET,0);
185}
186printf("\nE^00%s",start);
187fclose(fint);
188fclose(ftab);
189fclose(fsym);
190fclose(flen);
191fclose(fout);
192getch();
193}
194
195 input.txt:
196
197- COPY START 1000
1981000 - LDA ALPHA
1991003 - ADD ONE
2001006 - SUB TWO
2011009 - STA BETA
2021012 ALPHA BYTE C'KLNCE
2031017 ONE RESB 2
2041019 TWO WORD 5
2051022 BETA RESW 1
2061025 - END -
207
208optab.txt:
209
210LDA 00
211STA 23
212ADD 01
213SUB 05
214
215length.txt:25
216
217symbol.txt:
218
2191012 ALPHA
2201017 ONE
2211019 TWO
2221022 BETA
223
224***SINGLE PASS MACRO PROCESSOR***
225
226#include<stdio.h>
227#include<conio.h>
228#include<string.h>
229#include<stdlib.h>
230void main()
231{
232FILE *f1,*f2,*f3,*f4,*f5;
233int len,i,pos=1;
234char arg[20],mne[20],opnd[20],la[20],name[20],mne1[20],opnd1[20],pos1[10],pos2[10];
235clrscr();
236f1=fopen("input.txt","r");
237f2=fopen("namtab.txt","w+");
238f3=fopen("deftab.txt","w+");
239f4=fopen("argtab.txt","w+");
240f5=fopen("op.txt","w+");
241fscanf(f1,"%s%s%s",la,mne,opnd);
242while(strcmp(mne,"END")!=0)
243{
244if(strcmp(mne,"MACRO")==0)
245{
246fprintf(f2,"%s\n",la);
247fseek(f2,SEEK_SET,0);
248fprintf(f3,"%s\t%s\n",la,opnd);
249fscanf(f1,"%s%s%s",la,mne,opnd);
250while(strcmp(mne,"MEND")!=0)
251{
252if(opnd[0]=='&')
253{
254itoa(pos,pos1,5);
255strcpy(pos2,"?");
256strcpy(opnd,strcat(pos2,pos1));
257pos=pos+1;
258}
259fprintf(f3,"%s\t%s\n",mne,opnd);
260fscanf(f1,"%s%s%s",la,mne,opnd);
261}
262fprintf(f3,"%s",mne);
263}
264else
265{
266fscanf(f2,"%s",name);
267if(strcmp(mne,name)==0)
268{
269len=strlen(opnd);
270for(i=0;i<len;i++)
271{
272if(opnd[i]!=',')
273fprintf(f4,"%c",opnd[i]);
274else
275fprintf(f4,"\n");
276}
277fseek(f3,SEEK_SET,0);
278fseek(f4,SEEK_SET,0);
279fscanf(f3,"%s%s",mne1,opnd1);
280fprintf(f5,".\t%s\t%s\n",mne1,opnd);
281fscanf(f3,"%s%s",mne1,opnd1);
282while(strcmp(mne1,"MEND")!=0)
283{
284if((opnd[0]=='?'))
285{
286fscanf(f4,"%s",arg);
287fprintf(f5,"-\t%s\t%s\n",mne1,arg);
288}
289else
290fprintf(f5,"-\t%s\t%s\n",mne1,opnd1);
291fscanf(f3,"%s%s",mne1,opnd1);
292}
293}
294else
295fprintf(f5,"%s\t%s\t%s\n",la,mne,opnd);
296}
297fscanf(f1,"%s%s%s",la,mne,opnd);
298}
299fprintf(f5,"%s\t%s\t%s",la,mne,opnd);
300fclose(f1);
301fclose(f2);
302fclose(f3);
303fclose(f4);
304fclose(f5);
305printf("files to be viewed \n");
306printf("1. argtab.txt\n");
307printf("2. namtab.txt\n");
308printf("3. deftab.txt\n");
309printf("4. op.txt\n");
310getch();
311}
312
313
314 Input.txt
315
316EX1 MACRO &A,&B
317- LDA &A
318- STA &B
319- MEND -
320SAMPLE START 1000
321- EX1 N1,N2
322N1 RESW 1
323N2 RESW 1
324- END -
325
326Argtab.txt
327
328N1
329N2
330
331Op.txt
332
333SAMPLE START 1000
334. EX1 N1,N2
335- LDA ?1
336- STA ?2
337N1 RESW 1
338N2 RESW 1
339- END -
340
341Deftab.txt
342
343EX1 &A,&B
344LDA ?1
345STA ?2
346MEND
347
348Namtab.txt
349EX1
350
351***To implement a LL(1) parser in C language.***
352
353#include<stdio.h>
354 #include<conio.h>
355 #include<string.h>
356 void main()
357{ char fin[10][20],st[10][20],ft[20][20],fol[20][20];
358 int a=0,e,i,t,b,c,n,k,l=0,j,s,m,p;
359clrscr();
360printf("enter the no. of coordinates\n");
361 scanf("%d",&n);
362 printf("enter the productions in a grammar\n");
363 for(i=0;i<n;i++)
364 scanf("%s",st[i]);
365 for(i=0;i<n;i++) fol[i][0]='\0';
366for(s=0;s<n;s++) {
367for(i=0;i<n;i++)
368{ j=3; l=0; a=0;
369 l1:if(!((st[i][j]>64)&&(st[i][j]<91)))
370{ for(m=0;m<l;m++)
371 { if(ft[i][m]==st[i][j]) goto s1;
372 }
373ft[i][l]=st[i][j];
374 l=l+1; s1:j=j+1;
375 }
376
377else { if(s>0)
378 {
379while(while(st[i][j]!=st[a][0])
380 { a++;
381 }
382 b=0;
383 while(ft[a][b]!='\0')
384 {
385for(m=0;m<l;m++)
386 { if(ft[i][m]==ft[a][b]) goto s2;
387 }
388ft[i][l]=ft[a][b];
389l=l+1;
390s2:b=b+1;
391 }
392}
393} while(st[i][j]!='\0')
394{ if(st[i][j]=='|')
395 { j=j+1; goto l1;
396}
397 j=j+1;
398 } ft[i][l]='\0';
399 }
400 }
401printf("first pos\n");
402for(i=0;i<n;i++)
403printf("FIRS[%c]=%s\n",st[i][0],ft[i]);
404 fol[0][0]='$';
405for(i=0;i<n;i++)
406 { k=0; j=3;
407 if(i==0) l=1;
408else l=0;
409k1:while((st[i][0]!=st[k][j])&&(k<n))
410 { if(st[k][j]=='\0')
411{ k++;
412j=2;
413 } j++;
414 } j=j+1;
415if(st[i][0]==st[k][j-1])
416 { if((st[k][j]!='|')&&(st[k][j]!='\0'))
417 { a=0;
418if(!((st[k][j]>64)&&(st[k][j]<91)))
419{ for(m=0;m<l;m++)
420{
421if(fol[i][m]==st[k][j]) goto q3;
422 } fol[i][l]=st[k][j];
423l++;
424q3:
425 } else
426 {
427
428while(st[k][j]!=st[a][0]) { a++;
429 }
430p=0;
431while(ft[a][p]!='\0')
432 { if(ft[a][p]!='@')
433 { for(m=0;m<l;
434m++)
435{ if(fol[i][m]==ft[a][p]) goto q2;
436} fol[i][l]=ft[a][p];
437 l=l+1;
438 } else
439e=1;
440 q2:p++;
441}
442if(e==1)
443 { e=0; goto a1;
444}
445}
446 }
447else
448{ a1:c=0;
449 a=0;
450 while(st[k][0]!=st[a][0])
451{ a++;
452} while((fol[a][c]!='\0')&&(st[a][0]!=st[i][0]))
453
454 { for(m=0;m<l;
455m++)
456 { if(fol[i][m]==fol[a][c]) goto q1;
457} fol[i][l]=fol[a][c];
458 l++; q1:c++;
459} } goto k1;
460 } fol[i][l]='\0';
461 } printf("follow pos\n");
462 for(i=0;i<n;i++)
463printf("FOLLOW[%c]=%s\n",st[i][0],fol[i]); printf("\n");
464 s=0; for(i=0;i<n;i++)
465 { j=3;
466 while(st[i][j]!='\0')
467{ if((st[i][j-1]=='|')||(j==3))
468{ for(p=0;p<=2;p++)
469{ fin[s][p]=st[i][p];
470 } t=j;
471 for(p=3;
472((st[i][j]!='|')&&(st[i][j]!='\0'));
473p++)
474{ fin[s][p]=st[i][j];
475 j++;
476 }
477 fin[s][p]='\0';
478 if(st[i][k]=='@')
479 { b=0;
480 a=0; while(st[a][0]!=st[i][0])
481 { a++;
482 }
483while(fol[a][b]!='\0')
484 { printf("M[%c,%c]=%s\n",st[i][0],fol[a][b],fin[s]);
485b++;
486 }
487} else if(!((st[i][t]>64)&&(st[i][t]<91)))
488printf("M[%c,%c]=%s\n",st[i][0],st[i][t],fin[s]);
489 else { b=0; a=0;
490 while(st[a][0]!=st[i][3]) { a++; }
491while(ft[a][b]!='\0')
492{ printf("M[%c,%c]=%s\n",st[i][0],ft[a][b],fin[s]);
493b++; }
494}
495s++;
496 } if(st[i][j]=='|')
497j++;
498 }
499} getch();
500}
501
502***program for computation of first***
503
504#include<stdio.h>
505#include<ctype.h>
506void FIRST(char[],char );
507void addToResultSet(char[],char);
508int numOfProductions;
509char productionSet[10][10];
510main()
511{
512 int i;
513 char choice;
514 char c;
515 char result[20];
516 printf("How many number of productions ? :");
517 scanf(" %d",&numOfProductions);
518 for(i=0;i<numOfProductions;i++)//read production string eg: E=E+T
519 {
520 printf("Enter productions Number %d : ",i+1);
521 scanf(" %s",productionSet[i]);
522 }
523 do
524 {
525 printf("\n Find the FIRST of :");
526 scanf(" %c",&c);
527 FIRST(result,c); //Compute FIRST; Get Answer in 'result' array
528 printf("\n FIRST(%c)= { ",c);
529 for(i=0;result[i]!='\0';i++)
530 printf(" %c ",result[i]); //Display result
531 printf("}\n");
532 printf("press 'y' to continue : ");
533 scanf(" %c",&choice);
534 }
535 while(choice=='y'||choice =='Y');
536}
537/*
538 *Function FIRST:
539 *Compute the elements in FIRST(c) and write them
540 *in Result Array.
541 */
542void FIRST(char* Result,char c)
543{
544 int i,j,k;
545 char subResult[20];
546 int foundEpsilon;
547 subResult[0]='\0';
548 Result[0]='\0';
549 //If X is terminal, FIRST(X) = {X}.
550 if(!(isupper(c)))
551 {
552 addToResultSet(Result,c);
553 return ;
554 }
555 //If X is non terminal
556 //Read each production
557 for(i=0;i<numOfProductions;i++)
558 {
559//Find production with X as LHS
560 if(productionSet[i][0]==c)
561 {
562//If X → ε is a production, then add ε to FIRST(X).
563 if(productionSet[i][2]=='$') addToResultSet(Result,'$');
564 //If X is a non-terminal, and X → Y1 Y2 … Yk
565 //is a production, then add a to FIRST(X)
566 //if for some i, a is in FIRST(Yi),
567 //and ε is in all of FIRST(Y1), …, FIRST(Yi-1).
568 else
569 {
570 j=2;
571 while(productionSet[i][j]!='\0')
572 {
573 foundEpsilon=0;
574 FIRST(subResult,productionSet[i][j]);
575 for(k=0;subResult[k]!='\0';k++)
576 addToResultSet(Result,subResult[k]);
577 for(k=0;subResult[k]!='\0';k++)
578 if(subResult[k]=='$')
579 {
580 foundEpsilon=1;
581 break;
582 }
583 //No ε found, no need to check next element
584 if(!foundEpsilon)
585 break;
586 j++;
587 }
588 }
589 }
590}
591 return ;
592}
593/* addToResultSet adds the computed
594 *element to result set.
595 *This code avoids multiple inclusion of elements
596 */
597void addToResultSet(char Result[],char val)
598{
599 int k;
600 for(k=0 ;Result[k]!='\0';k++)
601 if(Result[k]==val)
602 return;
603 Result[k]=val;
604 Result[k+1]='\0';
605}
606
607Production
608
609E=TD
610D=+TD
611D=$
612T=FS
613S=*FS
614S=$
615F=(E)
616F=a
617
618***program to generate a three address code for given expression in c***
619
620#include<stdio.h>
621#include<string.h>
622void pm();
623void plus();
624void div();
625int i,ch,j,l,addr=100;
626char ex[10], exp[10] ,exp1[10],exp2[10],id1[5],op[5],id2[5];
627void main()
628{
629clrscr();
630while(1)
631{
632printf("\n1.assignment\n2.arithmetic\n3.relational\n4.Exit\nEnter the choice:");
633scanf("%d",&ch);
634switch(ch)
635{
636case 1:
637printf("\nEnter the expression with assignment operator:");
638scanf("%s",exp);
639l=strlen(exp);
640exp2[0]='\0';
641i=0;
642
643
644while(exp[i]!='=')
645{
646i++;
647}
648strncat(exp2,exp,i);
649strrev(exp);
650exp1[0]='\0';
651strncat(exp1,exp,l-(i+1));
652strrev(exp1);
653printf("Three address code:\ntemp=%s\n%s=temp\n",exp1,exp2);
654break;
655
656case 2:
657printf("\nEnter the expression with arithmetic operator:");
658scanf("%s",ex);
659strcpy(exp,ex);
660l=strlen(exp);
661exp1[0]='\0';
662
663for(i=0;i<l;i++)
664{
665if(exp[i]=='+'||exp[i]=='-')
666{
667if(exp[i+2]=='/'||exp[i+2]=='*')
668{
669pm();
670break;
671}
672else
673{
674plus();
675break;
676}
677}
678else if(exp[i]=='/'||exp[i]=='*')
679{
680div();
681break;
682}
683}
684break;
685
686case 3:
687printf("Enter the expression with relational operator");
688scanf("%s%s%s",&id1,&op,&id2);
689if(((strcmp(op,"<")==0)||(strcmp(op,">")==0)||(strcmp(op,"<=")==0)||(strcmp(op,">=")==0)||(strcmp(op,"==")==0)||(strcmp(op,"!=")==0))==0)
690printf("Expression is error");
691else
692{
693printf("\n%d\tif %s%s%s goto %d",addr,id1,op,id2,addr+3);
694addr++;
695printf("\n%d\t T:=0",addr);
696addr++;
697printf("\n%d\t goto %d",addr,addr+2);
698addr++;
699printf("\n%d\t T:=1",addr);
700}
701break;
702case 4:
703exit(0);
704}
705}
706}
707void pm()
708{
709strrev(exp);
710j=l-i-1;
711strncat(exp1,exp,j);
712strrev(exp1);
713printf("Three address code:\ntemp=%s\ntemp1=%c%ctemp\n",exp1,exp[j+1],exp[j]);
714}
715void div()
716{
717strncat(exp1,exp,i+2);
718printf("Three address code:\ntemp=%s\ntemp1=temp%c%c\n",exp1,exp[i+2],exp[i+3]);
719}
720void plus()
721{
722strncat(exp1,exp,i+2);
723printf("Three address code:\ntemp=%s\ntemp1=temp%c%c\n",exp1,exp[i+2],exp[i+3]);
724}
725
726output:
7271. assignment
7282. arithmetic
7293. relational
7304. Exit
731Enter the choice:1
732Enter the expression with assignment operator:
733a=b
734Three address code:
735temp=b
736a=temp
737
738
739***program to generate assembly code in c***
740#include<stdio.h>
741void main(){
742int a=10,b=20,c;
743asm{
744mov ax,a
745mov bx,b
746add ax,bx
747mov c,ax
748}
749printf("c=%d",c);
750}
751
752***To implement a Symbol table with functions to create, insert, modify, search and display in C language.***
753
754#include<stdio.h>
755#include<conio.h>
756#include<alloc.h>
757#include<string.h>
758#include<stdlib.h>
759#define NULL 0
760int size=0;
761void Insert();
762void Display();
763void Delete();
764int Search(char lab[]);
765void Modify();
766struct SymbTab
767{
768 char label[10],symbol[10];
769 int addr;
770struct SymbTab *next;};
771struct SymbTab *first,*last;
772void main()
773{
774 int op,y;
775 char la[10];
776 clrscr();
777 do
778 {
779 printf("\n\tSYMBOL TABLE IMPLEMENTATION\n");
780 printf("\n\t1.INSERT\n\t2.DISPLAY\n\t3.DELETE\n\t4.SEARCH\n\t5.MODIFY\n\t6.END\n");
781 printf("\n\tEnter your option : ");
782 scanf("%d",&op);
783 switch(op)
784 {
785 case 1:
786 Insert();
787 break;
788 case 2:
789 Display();
790 break;
791 case 3:
792 Delete();
793 break;
794 case 4:
795 printf("\n\tEnter the label to be searched : ");
796 scanf("%s",la);
797 y=Search(la);
798 printf("\n\tSearch Result:");
799 if(y==1)
800 printf("\n\tThe label is present in the symbol table\n");
801 else
802 printf("\n\tThe label is not present in the symbol table\n");
803 break;
804 case 5:
805 Modify();
806 break;
807 case 6:
808 exit(0);
809 }
810 }while(op<6);
811 getch();
812}
813void Insert()
814{
815 int n;
816 char l[10];
817 printf("\n\tEnter the label : ");
818 scanf("%s",l);
819 n=Search(l);
820 if(n==1)
821 printf("\n\tThe label exists already in the symbol table\n\tDuplicate can't be inserted");
822 else
823 {
824 struct SymbTab *p;
825 p=malloc(sizeof(struct SymbTab));
826 strcpy(p->label,l);
827 printf("\n\tEnter the symbol : ");
828 scanf("%s",p->symbol);
829 printf("\n\tEnter the address : ");
830 scanf("%d",&p->addr);
831 p->next=NULL;
832 if(size==0)
833 {
834 first=p;
835 last=p;
836 }
837 else
838 {
839 last->next=p;
840 last=p;
841 }
842 size++;
843 }
844 printf("\n\tLabel inserted\n");
845}
846void Display()
847{
848 int i;
849 struct SymbTab *p;
850 p=first;
851 printf("\n\tLABEL\t\tSYMBOL\t\tADDRESS\n");
852 for(i=0;i<size;i++)
853 {
854 printf("\t%s\t\t%s\t\t%d\n",p->label,p->symbol,p->addr);
855 p=p->next;
856 }
857}
858int Search(char lab[])
859{
860 int i,flag=0;
861 struct SymbTab *p;
862 p=first;
863 for(i=0;i<size;i++)
864 {
865 if(strcmp(p->label,lab)==0)
866 flag=1;
867 p=p->next;
868 }
869 return flag;
870}
871void Modify()
872{
873 char l[10],nl[10];
874 int add,choice,i,s;
875 struct SymbTab *p;
876 p=first;
877 printf("\n\tWhat do you want to modify?\n");
878 printf("\n\t1.Only the label\n\t2.Only the address\n\t3.Both the label and address\n");
879 printf("\tEnter your choice : ");
880 scanf("%d",&choice);
881 switch(choice)
882 {
883 case 1:
884 printf("\n\tEnter the old label : ");
885 scanf("%s",l);
886 s=Search(l);
887 if(s==0)
888 printf("\n\tLabel not found\n");
889 else
890 {
891 printf("\n\tEnter the new label : ");
892 scanf("%s",nl);
893 for(i=0;i<size;i++)
894 {
895 if(strcmp(p->label,l)==0)
896 strcpy(p->label,nl);
897 p=p->next;
898 }
899 printf("\n\tAfter Modification:\n");
900 Display();
901 }
902 break;
903 case 2:
904 printf("\n\tEnter the label where the address is to be modified : ");
905 scanf("%s",l);
906 s=Search(l);
907 if(s==0)
908 printf("\n\tLabel not found\n");
909 else
910 {
911 printf("\n\tEnter the new address : ");
912 scanf("%d",&add);
913 for(i=0;i<size;i++)
914 {
915 if(strcmp(p->label,l)==0)
916 p->addr=add;
917 p=p->next;
918 }
919 printf("\n\tAfter Modification:\n");
920 Display();
921 }
922 break;
923 case 3:
924 printf("\n\tEnter the old label : ");
925 scanf("%s",l);
926 s=Search(l);
927 if(s==0)
928 printf("\n\tLabel not found\n");
929 else
930 {
931 printf("\n\tEnter the new label : ");
932 scanf("%s",nl);
933 printf("\n\tEnter the new address : ");
934 scanf("%d",&add);
935 for(i=0;i<size;i++)
936 {
937 if(strcmp(p->label,l)==0)
938 {
939 strcpy(p->label,nl);
940 p->addr=add;
941 }
942 p=p->next;
943 }
944 printf("\n\tAfter Modification:\n");
945 Display();
946 }
947 break;
948 }
949}
950void Delete()
951{
952 int a;
953 char l[10];
954 struct SymbTab *p,*q;
955 p=first;
956 printf("\n\tEnter the label to be deleted : ");
957 scanf("%s",l);
958 a=Search(l);
959 if(a==0)
960 printf("\n\tLabel not found\n");
961 else
962 {
963 if(strcmp(first->label,l)==0)
964 first=first->next;
965 else if(strcmp(last->label,l)==0)
966 {
967 q=p->next;
968 while(strcmp(q->label,l)!=0)
969 {
970 p=p->next;
971 q=q->next;
972 }
973 p->next=NULL;
974 last=p;
975 }
976 else
977 {
978 q=p->next;
979 while(strcmp(q->label,l)!=0)
980 {
981 p=p->next;
982 q=q->next;
983 }
984 p->next=q->next;
985 }
986 size--;
987 printf("\n\tAfter Deletion:\n");
988 Display();
989 }
990}
991
992output: http://www.sourcecodesolutions.in/2010/09/1implementation-of-symbol-table.html
993
994
995***To implement a TEXT Editor with features like create, append, display and delete.***
996
997#include<stdio.h>
998#include<conio.h>
999#include<process.h>
1000int i,j,ec,fg,ec2;
1001char fn[20],e,c;
1002FILE *fp1,*fp2,*fp;
1003void Create();
1004void Append();
1005void Delete();
1006void Display();
1007void main()
1008{
1009 do {
1010 clrscr();
1011 printf("\n\t\t***** TEXT EDITOR *****");
1012 printf("\n\n\tMENU:\n\t-----\n ");
1013 printf("\n\t1.CREATE\n\t2.DISPLAY\n\t3.APPEND\n\t4.DELETE\n\t5.EXIT\n");
1014 printf("\n\tEnter your choice: ");
1015 scanf("%d",&ec);
1016 switch(ec)
1017 {
1018 case 1:
1019 Create();
1020 break;
1021 case 2:
1022 Display();
1023 break;
1024 case 3:
1025 Append();
1026 break;
1027 case 4:
1028 Delete();
1029 break;
1030 case 5:
1031 exit(0);
1032 }
1033 }while(1);
1034}
1035void Create()
1036{
1037 fp1=fopen("temp.txt","w");
1038 printf("\n\tEnter the text and press '.' to save\n\n\t");
1039 while(1)
1040 {
1041 c=getchar();
1042 fputc(c,fp1);
1043 if(c == '.')
1044 {
1045 fclose(fp1);
1046 printf("\n\tEnter then new filename: ");
1047 scanf("%s",fn);
1048 fp1=fopen("temp.txt","r");
1049 fp2=fopen(fn,"w");
1050 while(!feof(fp1))
1051 {
1052 c=getc(fp1);
1053 putc(c,fp2);
1054 }
1055 fclose(fp2);
1056 break;
1057 }}
1058}
1059void Display()
1060{
1061 printf("\n\tEnter the file name: ");
1062 scanf("%s",fn);
1063 fp1=fopen(fn,"r");
1064 if(fp1==NULL)
1065 {
1066 printf("\n\tFile not found!");
1067 goto end1;
1068 }
1069 while(!feof(fp1))
1070 {
1071 c=getc(fp1);
1072 printf("%c",c);
1073 }
1074end1:
1075 fclose(fp1);
1076 printf("\n\n\tPress any key to continue...");
1077 getch();
1078}
1079void Delete()
1080{
1081 printf("\n\tEnter the file name: ");
1082 scanf("%s",fn);
1083 fp1=fopen(fn,"r");
1084 if(fp1==NULL)
1085 {
1086 printf("\n\tFile not found!");
1087 goto end2;
1088 }
1089 fclose(fp1);
1090 if(remove(fn)==0)
1091 {
1092 printf("\n\n\tFile has been deleted successfully!");
1093 goto end2;
1094 }
1095 else
1096 printf("\n\tError!\n");
1097end2: printf("\n\n\tPress any key to continue...");
1098 getch();
1099}
1100void Append()
1101{
1102 printf("\n\tEnter the file name: ");
1103 scanf("%s",fn);
1104 fp1=fopen(fn,"r");
1105 if(fp1==NULL)
1106 {
1107 printf("\n\tFile not found!");
1108 goto end3;
1109 }
1110 while(!feof(fp1))
1111 {
1112 c=getc(fp1);
1113 printf("%c",c);
1114 }
1115 fclose(fp1);
1116 printf("\n\tType the text and press 'Ctrl+S' to append.\n");
1117 fp1=fopen(fn,"a");
1118 while(1)
1119 {
1120 c=getch();
1121 if(c==19)
1122 goto end3;
1123 if(c==13)
1124 {
1125 c='\n';
1126 printf("\n\t");
1127 fputc(c,fp1);
1128 }
1129 else
1130 {
1131 printf("%c",c);
1132 fputc(c,fp1);
1133 }
1134 }
1135end3: fclose(fp1);
1136 getch();
1137}
1138
1139
1140output: http://www.sourcecodesolutions.in/2010/09/10implementation-of-simple-text-editor.html