Creation of SYMBOL TABLE using C


https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJfShSmc4uzJ32B_Cnh6KDE7EeSGsUpqwnmXpsZMSrMSp0wjgo7gGWZu_JIDHC_zUiWwWkPBhm3Pz_vS0k-tjHPGbd4J7sxNkEVv5q-YbMtId0PtXLQqi2n_k4Dee10jKN6F-IOkz4Wqgg/s1600/c+and+c%252B%252B+meansofmine.jpgAIM:

To write a C program to understand the working function of assembler in first pass
creating symbol table where the tables are entered in the first pass along with the
corresponding addresses.

ALGORITHM:

STEP 1: Start the program execution.
STEP 2: Create a structure for opcode table and assign the values.
STEP 3: Create a structure for symbol table and assign the values.
STEP 4: Create a structure for intermediate code table and assign the values.
STEP 5: Write the opcode in separate file and machine code in another separate file.
STEP 6: Open the opcode file and compare it with the given machine code and then
generate opcode for corresponding source code.
STEP 7: Check the forward reference in intermediate code and print the corresponding
jump statement address.
STEP 8: Compare machine code with the opcode.If any jump statement with backward
reference is present, then print backward reference address.
STEP 9: For symbol table, print the symbol and address of the symbol.
STEP 10: Stop the program execution.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct table
{
char var[10];
int value;
};
struct table tbl[20];
int i,j,n;
void create();
void modify();
int search(char variable[],int n);
void insert();
void display();
void main()
{
int ch,result=0;
char v[10];
clrscr();
do
{
printf("Enter ur choice:\n1.Create\n2.Insert\n3.Modify\n4.Search\n5.Display\n6.Exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
modify();
break;
case 4:
printf("Enter the variabe to be searched\n");
scanf("%s",&v);
result=search(v,n);
if(result==0)
printf("The variable does not belong to the table\n");
else
printf("The location of variable is %d. The value of %s is %d",
result,tbl[result].var,tbl[result].value);
break;
case 5:
display();
break;
case 6:
exit(1);
}
}
while(ch!=6);
getch();
}
void create()
{
printf("Enter the number of entries\n");
scanf("%d",&n);
printf("Enter the variable and the value:\n");
for(i=1;i<=n;i++)
{
scanf("%s%d",tbl[i].var,&tbl[i].value);
check:
if(tbl[i].var[0]>='0' && tbl[i].var[0]<='9')
{
printf("The variable should start with an alphabet\nEnter the correct variable name\n");
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check;
}
check1:
for(j=1;j<1;j++)
{
if(strcmp(tbl[i].var,tbl[j].var)==0)
{
printf("The variable already exists.\nEnter another variable\n");
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check1;
}
}
}
printf("The table after creation is\n");
display();
}
void insert()
{
if(i>=20)
printf("Cannotinsert. Table is full");
else
{
n++;
printf("Enter the variable and value\n");
scanf("%s%d",tbl[n].var,&tbl[n].value);
check:
if(tbl[i].var[0]>='0' && tbl[i].var[0]<='9')
{
printf("The variable should start with alphabet\nEnter the correct variable name\n");
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check;
}
check1:
for(j=1;j<n;j++)
{
if(strcmp(tbl[j].var,tbl[i].var)==0)
{
printf("The variable already exist\nEnter another variable\n");
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check1;
}
}
printf("The table after insertion is\n");
display();
}
}
void modify()
{
char variable[10];
int result=0;
printf("Enter the variable to be modified\n");
scanf("%s",&variable);
result=search(variable,n);
if(result==0)
printf("%sdoes not belong to the table",variable);
else
{
printf("The current value of the variable%s is %d, Enter the new variable and its
value",tbl[result].var,tbl[result].value);
scanf("%s%d",tbl[result].var,&tbl[result].value);
check:
if(tbl[i].var[0]>='0' && tbl[i].var[0] <= '9')
{
printf("The variable should start with alphabet\n Enter the correct variable name\n");
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check;
}
}
printf("The table after modification is\n");
display();
} int search(char variable[],int n)
{
int flag;
for(i=1;i<=n;i++)
{
if(strcmp(tbl[i].var,variable)==0)
{
flag=1;
break;
}
}
if(flag==1)
return i;
else
return 0;
}
void display()
{
printf("Variable\t value\n");
for(i=1;i<=n;i++)
printf("%s\t\t%d\n",tbl[i].var,tbl[i].value);
}
 
OUTPUT:

Enter ur choice:
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit
1
Enter the number of entries
2
Enter the variable and the value:
A 26
B 42
The table after creation is
Variable value
A 26
B 42
Enter ur choice:
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit
2
Enter the variable and value
D 10
The table after insertion is
Variable value
A 26
B 42
D 10
Enter ur choice:
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit
3
Enter the variable to be modified
D
The current value of the variableD is 10, Enter the new variable and its value
C
20
The table after modification is
Variable value
A 26
B 42
C 20
Enter ur choice:
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit
4
Enter the variabe to be searched
A The location of variable is 1. The value of A
is 26
Enter ur choice:
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit
5
Variable value
A 26
B 42
C 20
Enter ur choice:
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit

0 comments:

Post a Comment