#include<stdio.h>
#include<conio.h>
int isnum(int n)
{
if(n>0&&n<100)
return 1;
else
return 0;
}
void main()
{
FILE *fp;
char words[50][25],str[25],ch;
char name[50][25];
int size[50],add[50];
int temp,ad=1000;
int i,j,w=0,d=0;
fp=fopen("tsym.c","r");
clrscr();
printf("\n File: tsym.c\n\n");
if(fp!=NULL)
{
do
{
j=0;
ch='d';
strcpy(str,"");
while(ch!=' ')
{
ch=getc(fp);
printf("%c",ch);
if(ch==EOF) break;
str[j++]=ch;
if(ch==' '||ch=='\n'||ch=='='||ch=='[')
{
j--;
break;
}
if(ch==';'||ch==','||ch==']')
{
j--;
break;
}
}
str[j]='\0';
if(j>0)
strcpy(words[w++],str);
if(ch==';')
strcpy(words[w++],";");
j=0;
}while(ch!=EOF);
}
for(i=0;i<w;i++)
{
if(!strcmp(words[i],"int"))
{
i++;
for(;strcmp(words[i],";");i++)
{
temp=atoi(words[i]);
if(isnum(temp))
{
d--;
ad=ad-size[d];
size[d]=temp*2;
ad=ad+size[d];
d++;
}
else
{
strcpy(name[d],words[i]);
size[d]=2;
add[d]=ad;
ad=ad+size[d];
d++;
}
}
}
else if(!strcmp(words[i],"char"))
{
i++;
for(;strcmp(words[i],";");i++)
{
temp=atoi(words[i]);
if(isnum(temp))
{
d--;
ad=ad-size[d];
size[d]=temp*1;
ad=ad+size[d];
d++;
}
else
{
strcpy(name[d],words[i]);
size[d]=1;
add[d]=ad;
ad=ad+size[d];
d++;
}
}
}
else if(!strcmp(words[i],"float"))
{
i++;
for(;strcmp(words[i],";");i++)
{
temp=atoi(words[i]);
if(isnum(temp))
{
d--;
ad=ad-size[d];
size[d]=temp*4;
ad=ad+size[d];
d++;
}
else
{
strcpy(name[d],words[i]);
size[d]=4;
add[d]=ad;
ad=ad+size[d];
d++;
}
}
}
else if(!strcmp(words[i],"double"))
{
i++;
for(;strcmp(words[i],";");i++)
{
temp=atoi(words[i]);
if(isnum(temp))
{
d--;
ad=ad-size[d];
size[d]=temp*8;
ad=ad+size[d];
d++;
}
else
{
strcpy(name[d],words[i]);
size[d]=8;
add[d]=ad;
ad=ad+size[d];
d++;
}
}
}
}
printf("\n\n\tSYMBOL TABLE:\n");
printf("SYMBOL SIZE ADDRESS");
for(i=0;i<d;i++)
{
printf("\n%-10s",name[i]);
printf("%-10d",size[i]);
printf("%-10d",add[i]);
}
getch();
}
0 Comments