Sunday 25 September 2011


                                            Implement A Symbol Table

AIM:
 To write a C program to implement creation of symbol table.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the choice from the user.
Step 3: If the option is CREATE, get the input and if there is a SYMBOL, place it in the SYMBOL TABLE.
Step 4: If the choice is SEARCH, check the SYMBOL TABLE and display the message appropriately.
Step 5: If the choice is MODIFY, search for the label and change the value of input.
Step 6: If the choice is DISPLAY, set a for loop and display the table with the given input.
Step 7: Stop the program.

SOURCE CODE:
/* CREATION OF SYMBOL TABLE */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void create();
void search();
void modify();
void display();
struct symbol
{
char label[10], adddress[10], value[5];
}s[10];
struct instructions
{
char address[10],label[10],opcode[10],operand[10];
}t;
int ch,n=0,i,flag=0;
char a[10];
void main()
{
char a[10];
int opt;
clrscr();
ch='y';
do
{
printf("\n Enter the choice\n");
printf("1.Create\n2.Search\n3.Modify\n4.Display\n5.Exit\n");
scanf("%d",&opt);
switch(opt)
{
case 1:
create();
break;
case 2:
search();
break;
case 3:
modify();
break;
case 4:
display()
break;
case 5:
exit(0);
break;
}
}
while(opt<5);
getch();
}
void create()
{
printf("\nEnter the Address, Label, Opcode, Operand\n");
scanf("%s%s%s%s",&t.address,&t.label,&t.opcode,&t.operand);
if(strcmp(t.label,"_")!=0)
{
flag=0;
for(i=0;i<=n:i++)
{
if(strcmp(s[i].label,t.label)==0)
{
flag=1;
break;
}
}
if(flag==0)
{
strcpy(s[n].address,t.address);
strcpy(s[n].label,t.label);
strcpy(s[n].value,t.operand);
n++;
}
else
{
printf("\nThe Label is already there");
}
}
}
void search()
{
printf("\nEnter the Label");
scnaf("%s",&a);
for(i=0;i<=n;i++)
{
if(strcmp(a,s[i].label)==0)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("\t\tLABEL\tADDRESS\tVALUE\t\n");
printf("\t\t%s\t%s\t%s\n",s[i].label,s[i].address,s[i].value);
}
else
{
printf("Not Found");
}
}
void modify()
{
char a[10];
flag=0;
printf("\nEnter the label\n");
scanf("%s",&a);
for(i=0;i<=n;i++)
{
if(strcmp(a,s[i].label)==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("\nError");
}
else
{
printf("\nEnter the Address, Value\n");
scanf("%s%s",&s[i].address,&s[i].value);
}
}
void display()
{
printf("\t\tLABEL\tADDRESS\tVALUE\t\t\n");
for(i=0;i<=n;i++)
{
printf("\t\t%s\t%s\t%s\t\t\n",s[i].label,s[i].address,s[i].value);
}
}



OUTPUT:


 Enter the choice
1.Create
2.Search
3.Modify
4.Display
5.Exit
1

Enter the Address, Label, Opcode, Operand
1000 COPY START 4890

 Enter the choice
1.Create
2.Search
3.Modify
4.Display
5.Exit
1

Enter the Address, Label, Opcode, Operand
2000 COPY START 2345

The Label is already there
 Enter the choice
1.Create
2.Search
3.Modify
4.Display
5.Exit
1

Enter the Address, Label, Opcode, Operand
2000 CLOOP LDA 9000

 Enter the choice
1.Create
2.Search
3.Modify
4.Display
5.Exit
2

Enter the Label COPY
                LABEL   ADDRESS VALUE
                COPY    1000    4890

 Enter the choice
1.Create
2.Search
3.Modify
4.Display
5.Exit
3

Enter the label
CLOOP

Enter the Address, Value
2003 5000

 Enter the choice
1.Create
2.Search
3.Modify
4.Display
5.Exit
4
LABEL   ADDRESS VALUE
   COPY    1000    4890
  CLOOP   2003    5000


 Enter the choice
1.Create
2.Search
3.Modify
4.Display
5.Exit
5
Result
Thus above program executed and output verified.

0 comments:

Post a Comment