Friday 17 August 2012

System Software program 3:


Symbol table creation:

Program:

#include<stdio.h>
#include<conio.h>
struct sym
{
char lab[10];
int val;
};
void main()
{
FILE *f1;
char la[10],op[10],opr[10],a[1000],c,key[10];
int i,j,k=0,m=0,flag,ch=0;
struct sym s[10];
clrscr();
f1=fopen("a1.txt","r");
c=fgetc(f1);
i=0;
printf("\n SOURCE PROGRAM \n");
while(c!=EOF)
{
a[i]=c;
c=fgetc(f1);
i++;
}
while(ch<4)
{
printf("1.Symbol table creation\n");
printf("2.search\n");
printf("3.display\n");
printf(">3.Exit\n");
printf("Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
i=0;
while(strcmp(op,"end")!=0)
{
if(a[i]=='\t')
{
strcpy(la," ");
i++;
}
else
{
j=0;
while(a[i]!='\t')
{
la[j]=a[i];
i++;
j++;
}
la[j]='\0';
i++;
}
if(a[i]=='\t')
{
strcpy(op," ");
i++;
}
else
{
j=0;
while(a[i]!='\t')
{
op[j]=a[i];
i++;
j++;
}
op[j]='\0';
i++;
}
if(a[i]=='\t')
{
strcpy(opr," ");
i++;
}
else
{
j=0;
while(a[i]!='\n')
{
opr[j]=a[i];
i++;
j++;
}
opr[j]='\0';
i++;
}
j=0;
if(strcmp(la," ")!=0)
{
strcpy(s[m].lab,la);
if(strcmp(op,"start")==o)
{
k=atoi(opr);
s[m].val=k;
m++;
printf("%s\t%s\t%s\n",la,op,opr);
continue;
}
else if(strcmp(op,"equ")==0)
{
s[m].val=atoi(opr);
m++;
}
else if(strcmp(op,"resq")==0)
{
s[m].val=k;
k=k+atoi(opr)*3;
m++;
}
else if(strcmp(op,"resb")==0)
{
s[m].val=k;
k=k+atoi(opr);
m++;
}
else
{
s[m].val=k;
k=k+3;
m++;
}
}
else
k=k+3;
printf("%s\t%s\t%s\n",la,op,opr);
}
break;
case 2:
printf("Enter the label to be searched\n");
scanf("%s",&key);
flag=0;
for(i=0;i<m;i++)
{
if(strcmp(key,s[i].lab)==0)
{
printf("%s\t%d\n",s[i].lab,s[i].val);
flag=1;
break;
}
else
continue;
}
if(flag==0)
printf("Label not found\n");
break;
case 3:
printf("\n Symbol table \n");
for(i=0;i<m;i++)
printf("\n %s\t%d\n",s[i].lab,s[i].val);
break;
}
}
}


Input file:

copy start 1000
add lda one
** sta two
** ldch charz
** stch c1
one word 100
two resw 1
char byte c'z'
c1 resb 1
** end add

Output:

SOURCE PROGRAM
1.symbol table creation
2.search
3.display
>3.exit
Enter your choice 1
copy start 1000
add lda one
** sta two
** ldch charz
** stch c1
one word 100
two resw 1
charz byte c'z'
c1 resb 1
** end add
1.symbol table creation
2.search
3.display
>3.exit
Enter your choice 3
copy 1000
add 1000
** 1003
** 1006
** 1009
one 1012
two 1015
charz 1018
c1 1021
** 1022
1.symbol table creation
2.search
3.display
>3.exit
Enter your choice 2
Enter the label to be searched
two
two 1015
1.symbol table creation
2.search
3.display
>3.exit
Enter your choice 4

No comments:

Post a Comment