Symbol Table Implementation Ex. No.: 1 Date: 19.12.2018 Aim
Symbol Table Implementation Ex. No.: 1 Date: 19.12.2018 Aim
Aim
To write a C program to implement a symbol table.
Description
A Symbol table is a data structure used by a language translator such as a compiler or
interpreter, where each identifier in a program’s source code is associated with
information relating to its declaration or appearance in the source. Possible entries in a
symbol table:
Name : a string
Attribute:
1. Reserved word
2. Variable name
3. Type Name
4. Procedure name
5. Constant name
Data type
Scope information: where it can be used.
Storage allocation
Algorithm
1. Start the Program.
2. Get the input from the user with the terminating symbol ‘$’.
3. Allocate memory for the variable by dynamic memory allocation function.
4. If the next character of the symbol is an operator then only the memory is allocated.
5. While reading, the input symbol is inserted into symbol table along with its memory
address.
6. The steps are repeated till”$”is reached.
7. To reach a variable, enter the variable to the searched and symbol table has been
checked for corresponding variable, the variable along its address is displayed as result.
8. Stop the program.
Program
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<alloc.h>
#include<string.h>
#include<math.h>
void main()
{
int i=0,j=0,x=0,n,flag=0;
void *p,*add[5];
charch, srch, b[15],d[15],c;
clrscr();
printf("Expression terminated by ; : ");
while((c=getchar())!=';')
{
b[i]=c;
i++;
}
n=i-1;
printf("Given Expression :”);
i=0;
while(i<=n)
{
printf("%c",b[i]);
i++;
}
printf("\nSymbolTable\n");
printf("Symbol\taddr\ttype");
while(j<=n)
{
c=b[j];
if(isalpha(toascii(c)))
{
if(j==n)
{
p=malloc(c);
add[x]=p;
d[x]=c;
printf("%c\t%d\tidentifier",c,p);
}
else
{
ch=b[j+1];
if(ch=='+'||ch=='-'||ch=='*'||ch=='=')
{
p=malloc(c);
add[x]=p;
d[x]=c;
printf("\n%c\t%d\tidentifier\n",c,p);x++;
}
}
}
j++;
}
printf("\nThe symbol is to be searched");
srch=getche();
for(i=0;i<=x;i++)
{
if(srch==d[i])
{
printf("\nSymbol Found");
printf("\nThe symbol %c%s%d\n",srch," is at address : ",add[i]);
flag=1;
}
}
if(flag==0)
printf("\nSymbol Not Found");
getch();
}
}
OUTPUT
Result
Thus the above the program is executed and the required output is obtained.