Program : Symbol Table
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct table
{
char tab[2];
char label[20];
char ins[20];
char exp[20];
}obj[20];
struct symtab //structure for storing symtab variables
{
char name[9];
int add;
}sym[25];
char array[9],num[9];
int temp,sign=0,ln,tmp,loc;
int z,i,j,t,c,k,no,s,value,pos;
void main() //main function
{
char ch,str[20];
FILE *ptr1;
ptr1=fopen("code.txt","r");
clrscr();
if(!ptr1)
{
printf("FIle not found:");
getch();
exit(0);
}
c=i=0;
while((ch=getc(ptr1))!=EOF)
{
if(ch=='\t')
{
obj[c].tab[0]=NULL;
ch=getc(ptr1);
}
while(ch!='\t')
{
obj[c].tab[i]=ch;
ch=getc(ptr1);
i++;
}
i=0;
while(ch!='\t')
{
obj[c].label[i]=ch;
ch=getc(ptr1);
i++;
}
ch=getc(ptr1);
j=0;
while(ch!='\t')
{
obj[c].ins[j]=ch;
j++;
ch=getc(ptr1);
}
k=0,t=0;
ch=getc(ptr1);
while(ch!='\n')
{
if(isdigit(ch))
{
obj[c].exp[t]=ch;
ch=getc(ptr1);
t++;
}
else
{
obj[c].exp[t]=ch;
ch=getc(ptr1);
t++;
}
}
c++;
}
printf("\n\tSOURCE PROGRAM\n\t*---------------*\n");
for(t=0;t<=c;t++)
{
printf("\n%s\t%s\t%s\n",obj[t].tab,obj[t].ins,obj[t].exp);
}
printf("\n\t SYMBOL TABLE \n\t*-------------------*\n");
value=c;
s=0;
while(s<=value)
{
if((strcmp(obj[s].ins,"START"))==0)
{
sym[s+1].add=atoi(obj[s].exp);
sym[s].add=sym[s+1].add;
loc=sym[s].add;
}
else if((strcmp(obj[s].ins,"STOP"))==0)
{
sym[s+1].add=loc+1;
loc=loc+1;
}
else if((strcmp(obj[s].ins,"DS"))==0)
{
sym[s+1].add=loc+atoi(obj[s].exp);
loc=loc+atoi(obj[s].exp);
}
else if((strcmp(obj[s].ins,"MOVER"))==0)
{
sym[s+1].add=loc+1;
loc=loc+1;
}
else if((strcmp(obj[s].ins,"DC"))==0)
{
sym[s+1].add=loc+1;
loc=loc+1;
}
else if((strcmp(obj[s].ins,"ADD"))==0)
{
sym[s+1].add=loc+1;
loc=loc+1;
}
else if((strcmp(obj[s].ins,"MOVEM"))==0)
{
sym[s+1].add=loc+1;
loc=loc+1;
}
else if((strcmp(obj[s].ins,"PRINT"))==0)
{
sym[s+1].add=loc+1;
loc=loc+1;
}
else if((strcmp(obj[s].ins,"EQU"))==0)
{
for(z=0;z<strlen(obj[s].exp);z++)
{
if(obj[s].exp[z]=='+')
{
pos=z;
sign=1;
ln=s;
goto lab;
}
else if(obj[s].exp[z]=='-')
{
pos=z;
sign=2;
ln=s;
goto lab;
}
} //end of for
lab:
if(sign==1)
{
t=0;
for(i=0;i<pos;i++)
{
array[i]=obj[ln].exp[i];
}
for(j=pos+1;j<strlen(obj[ln].exp);j++)
{
num[t]=obj[ln].exp[j];
t++;
}
tmp=atoi(num);
for(i=0;i<=c;i++)
{
if(strcmp(array,obj[i].tab)==0)
{
sym[s].add=sym[i].add+tmp;
sym[s+1].add=sym[s-1].add+1;
break;
}
} //end of for
} //end of else if sign =1
else if(sign==2)
{
t=0;
for(i=0;i<pos;i++)
{
array[i]=obj[ln].exp[i];
}
for(j=pos+1;j<strlen(obj[ln].exp);j++)
{
num[t]=obj[ln].exp[j];
}
tmp=atoi(num);
for(i=0;i<=c;i++)
{
if(strcmp(array,obj[i].tab)==0)
{
if(obj[s].ins[0]=='O')
{
sym[s+1].add=sym[s].add+1;
}
else
{
sym[s].add=sym[i].add-tmp;
sym[s+1].add=sym[s-1].add+1;
}
break;
}
} //end of for
} //end of else if sign =2
}
else if((strcmp(obj[s].ins,"ORIGIN"))==0)
{
for(z=0;z<strlen(obj[s].exp);z++)
{
if(obj[s].exp[z]=='+')
{
pos=z;
sign=1;
ln=s;
goto lab1;
}
else if(obj[s].exp[z]=='-')
{
pos=z;
sign=2;
ln=s;
goto lab1;
}
} //end of for
lab1:
if(sign==1)
{
t=0;
for(i=0;i<pos;i++)
{
array[i]=obj[ln].exp[i];
}
for(j=pos+1;j<strlen(obj[ln].exp);j++)
{
num[t]=obj[ln].exp[j];
t++;
}
tmp=atoi(num);
for(i=0;i<=c;i++)
{
if(strcmp(array,obj[i].tab)==0)
{
loc=sym[i].add+tmp;
sym[s+1].add=loc;
break;
}
} //end of for
} //end of else if sign =1
else if(sign==2)
{
t=0;
for(i=0;i<pos;i++)
{
array[i]=obj[ln].exp[i];
}
for(j=pos+1;j<strlen(obj[ln].exp);j++)
{
num[t]=obj[ln].exp[j];
}
tmp=atoi(num);
for(i=0;i<=c;i++)
{
if(strcmp(array,obj[i].tab)==0)
{
loc=sym[s].add=sym[i].add-tmp;
sym[s+1].add=loc;
break;
}
} //end of for
} //end of else if sign =2
}
if(strcmp(obj[s].tab,NULL)!=0)
printf("\n\t%s\t%d",obj[s].tab,sym[s].add);
s=s+1;
} //end of for s
fclose(ptr1);
getch();
} //end of main function
0 comments:
Post a Comment