You are on page 1of 8

SYMBOL TABLE

Ex.no:
Date:

AIM
To create a program for symbol table.

ALGORITHM

Begin
read first input line
if opcode=’START’ then
Begin
save #[OPERAND] and starting address
read next input line
end{if start}
else
initialize LOCCTR to 0
while [OPCODE ≠’E’] do
Begin
if this is not comment line then
Begin
if there is a symbol in the label field then
Begin
search SYMTAB for LABEL
if found then
set error flag (duplicate symbol)
else
insert(LABEL,LOCCTR) into SYMTAB
end{if symbol}
search OPTAB for OPCODE
if found then
add }{instruction length}
else if OPCODE=’WORD’ then
add } to LOCCTR
else if OPCODE=’RESW’ then
add 3 * #[OPERAND] to LOCCTR
else if OPCODE=’BYTE’ then
Begin
find length of constant in bytes
add length to LOCCTR
end{if byte}
else
set error flag(in valid operation code)
end{if not a command}
end {while ≠ END}

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
int computepos(char*str)
{
int i,sum=0;
for(i=0;str[i]!='\0';i++)
sum+=str[i];
return(sum%20);
}
void main()
{
char *symtab[100],tmpstr[20][20],*tmp[20];
int ch,pos,a[20],i,j,k=0,flag=0;
for(i=0;i<20;i++)
for(j=0;j<20;j++)
tmpstr[i][j]=0;
while(1)
{
clrscr();
printf("\n***SIMULATION OF SYMBOLTABLE***");
printf("\n-------------------------------");
printf("\n 1.Insert two Symbol Table");
printf("\n 2.View The Symbol Table");
printf("\n 3.Find The Symbol");
printf("\n 4.Exit");
printf("\n\n\n Enter The Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Enter The Symbol:");
scanf("%s",tmpstr[k]);
pos=computepos(tmpstr[k]);
a[k]=pos;
k++;
break;
case 2:
printf("\t\t\tEnter The Symbol:\n");
printf("\t\t******************\n");
printf("\t\tSymbolname\t\tPostion");
for(i=0;i<k;i++)
printf("\n\t\t%s\t\t%d",tmpstr[i],a[i]);
getch();
break;
case 3:
printf("\n Enter The Symbol Name");
scanf("%s",tmp);
for(i=0;i<k;i++)
{
if(strcmp(tmpstr[i],tmp)==0)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("\nSymbol found at the position:%d",a[i]);
flag=0;
}
else
printf("\nSymbol is not found");
getch();
break;
default:
return;
}
}
}

OUTPUT

***SIMULATION OF SYMBOLTABLE***
-------------------------------
1.Insert two Symbol Table
2.View The Symbol Table
3.Find The Symbol
4.Exit

Enter The Choice:1

Enter The Symbol:#

***SIMULATION OF SYMBOLTABLE***
-------------------------------
1.Insert two Symbol Table
2.View The Symbol Table
3.Find The Symbol
4.Exit

Enter The Choice:1

Enter The Symbol:@

***SIMULATION OF SYMBOLTABLE***
-------------------------------
1.Insert two Symbol Table
2.View The Symbol Table
3.Find The Symbol
4.Exit

Enter The Choice:2


Enter The Symbol:
******************
Symbolname Postion
# 15
@ 4

***SIMULATION OF SYMBOLTABLE***
-------------------------------
1.Insert two Symbol Table
2.View The Symbol Table
3.Find The Symbol
4.Exit

Enter The Choice:3

Enter The Symbol Name@

Symbol found at the position:4

SNAPSHOTS

You might also like