Monday, June 13, 2011

Symbol Table Creation


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>


struct list
{
char la[50];
char mne[50];
char op[50];
}a[50];





struct symtab
{
char nam[50];
int add;
}s[50];
struct optab
{
char opc[50];
char value[50];
}o[50];
void main()
{
int i,j,t;
long int lc;
clrscr();
printf("INPUT PROGRAM\n");
printf("***** *******\n");
strcpy(a[0].la," ");
strcpy(a[0].mne,"START");
strcpy(a[0].op,"1000");
strcpy(a[1].la," ");
strcpy(a[1].mne,"LDA");
strcpy(a[1].op,"#2");
strcpy(a[2].la," ");
strcpy(a[2].mne,"ADD");
strcpy(a[2].op,"#3");
strcpy(a[3].la," ");
strcpy(a[3].mne,"STA");
strcpy(a[3].op,"VAR1");
strcpy(a[4].la,"VAR1 ");
strcpy(a[4].mne,"RESW");
strcpy(a[4].op,"1");
strcpy(a[5].la," ");
strcpy(a[5].mne,"END");
strcpy(a[5].op," ");
for(i=0;i<10;i++)
printf("%s\t%s\t%s\t\n",a[i].la,a[i].mne,a[i].op);
strcpy(o[0].opc,"LDA");
strcpy(o[0].value,"00");
strcpy(o[1].opc,"ADD");
strcpy(o[1].value,"18");
strcpy(o[2].opc,"STA");
strcpy(o[2].value,"0c");
strcpy(o[3].opc,"SUB");
strcpy(o[3].value,"16");
i=0;
j=0;
if(strcmp(a[i].mne,"START")==0)
{
lc=atoi(a[i].op);
}
else
lc=0;
while(strncmp(a[i].mne,"END",5)!=0)
{
if(strcmp(a[i].la," ")!=0)
{
for(t=0;t<=j;t++)//loop to allot address to symbols using location counter
{
if(strcmp(s[t].nam,a[i].la)==0)
{
printf("Duplicate Declaration");
printf("%s",a[i].mne);
getch();
exit(0);
}
}
strcpy(s[j].nam,a[i].la);
s[j].add=lc;
j++;
}
for(t=0;t<9;t++)
{
if(strcmp(a[i].mne,o[t].opc)==0)
{
lc=lc+3;
break;
}
}
if(strcmp(a[i].mne,"RESW")==0)
lc=lc+(3*atoi(a[i].op));
else if(strcmp(a[i].mne,"WORD")==0);
lc=lc+3;
if(strcmp(a[i].mne,"RESB")==0);
lc=lc+atoi(a[i].op);
i++;
}
printf("SYMBOL TABLE\n");
printf("****** *****\n\n");
printf("SYMBOL\t VALUE\n");
printf("______\t _____\n");
for(t=0;t<9;t++)
printf("\n%s\t%d",s[t].nam,s[t].add);
/*printf("\noptab\n");
printf("______\t _____\n");
for(t=0;t<9;t++)
printf("\n%c\t%c",o[t].opc,o[t].value);*/
getch();
}





No comments:

Post a Comment