answersLogoWhite

0

C program to create symbol table?

Updated: 8/9/2023
User Avatar

Wiki User

13y ago

Best Answer

Aim:
To write a C program to implement Symbol Table system software lab CS1207
Algorithm:
Start the program for performing insert, display, delete, search and modify option in symbol table
Define the structure of the Symbol Table
Enter the choice for performing the operations in the symbol Table
If the entered choice is 1, search the symbol table for the symbol to be inserted. If the symbol is already present, it displays "Duplicate Symbol". Else, insert the symbol and the corresponding address in the symbol table.
If the entered choice is 2, the symbols present in the symbol table are displayed.
If the entered choice is 3, the symbol to be deleted is searched in the symbol table. If it is not found in the symbol table it displays "Label Not found". Else, the symbol is deleted.
If the entered choice is 5, the symbol to be modified is searched in the symbol table. The label or address or both can be modified.

Source Code program in c implement symbol table
# include
# include
# include
# include
# define null 0
int size=0;
void insert();
void del();
int search(char lab[]);
void modify();
void display();
struct symbtab
{
char label[10];
int addr;
struct symtab *next;
};
struct symbtab *first,*last;
void main()
{
int op;
int y;
char la[10];
clrscr();
do
{
printf("\nSYMBOL TABLE IMPLEMENTATION\n");
printf("1. INSERT\n");
printf("2. DISPLAY\n");
printf("3. DELETE\n");
printf("4. SEARCH\n");
printf("5. MODIFY\n");
printf("6. END\n");
printf("Enter your option : ");
scanf("%d",&op);
switch(op)
{
case 1:
insert();
display();
break;
case 2:
display();
break;
case 3:
del();
display();
break;
case 4:
printf("Enter the label to be searched : ");
scanf("%s",la);
y=search(la);
if(y==1)
{
printf("The label is already in the symbol Table");
}
else
{
printf("The label is not found in the symbol table");
}
break;
case 5:
modify();
display();
break;
case 6:
break;
}
}
while(op<6);
getch();
}
void insert()
{
int n;
char l[10];
printf("Enter the label : ");
scanf("%s",l);
n=search(l);
if(n==1)
{
printf("The label already exists. Duplicate cant be inserted\n");
}
else
{
struct symbtab *p;
p=malloc(sizeof(struct symbtab));
strcpy(p->label,l);
printf("Enter the address : ");
scanf("%d",&p->addr);
p->next=null;
if(size==0)
{
first=p;
last=p;
}
else
{
last->next=p;
last=p;
}
size++;
}
}
void display()
{
int i;
struct symbtab *p;
p=first;
printf("LABEL\tADDRESS\n");
for(i=0;i{
printf("%s\t%d\n",p->label,p->addr);
p=p->next;
}
}
int search(char lab[])
{
int i,flag=0;
struct symbtab *p;
p=first;
for(i=0;i{
if(strcmp(p->label,lab)==0)
{
flag=1;
}
p=p->next;
}
return flag;
}
void modify()
{
char l[10],nl[10];
int add, choice, i, s;
struct symbtab *p;
p=first;
printf("What do you want to modify?\n");
printf("1. Only the label\n");
printf("2. Only the address of a particular label\n");
printf("3. Both the label and address\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the old label\n");
scanf("%s",l);
printf("Enter the new label\n");
scanf("%s",nl);
s=search(l);
if(s==0)
{
printf("NO such label");
}
else
{
for(i=0;i{
if(strcmp(p->label,l)==0)
{
strcpy(p->label,nl);
}
p=p->next;
}
}
break;
case 2:
printf("Enter the label whose address is to modified\n");
scanf("%s",l);
printf("Enter the new address\n");
scanf("%d",&add);
s=search(l);
if(s==0)
{
printf("NO such label");
}
else
{
for(i=0;i{
if(strcmp(p->label,l)==0)
{
p->addr=add;
}
p=p->next;
}
}
break;
case 3:
printf("Enter the old label : ");
scanf("%s",l);
printf("Enter the new label : ");
scanf("%s",nl);
printf("Enter the new address : ");
scanf("%d",&add);
s=search(l);
if(s==0)
{
printf("NO such label");
}
else
{
for(i=0;i{
if(strcmp(p->label,l)==0)
{
strcpy(p->label,nl);
p->addr=add;
}
p=p->next;
}
}
break;
}
}
void del()
{
int a;
char l[10];
struct symbtab *p,*q;
p=first;
printf("Enter the label to be deleted\n");
scanf("%s",l);
a=search(l);
if(a==0)
{
printf("Label not found\n");
}
else
{
if(strcmp(first->label,l)==0)
{
first=first->next;
}
else if(strcmp(last->label,l)==0)
{
q=p->next;
while(strcmp(q->label,l)!=0)
{
p=p->next;
q=q->next;
}
p->next=null;
last=p;
}
else
{
q=p->next;
while(strcmp(q->label,l)!=0)
{
p=p->next;
q=q->next;
}
p->next=q->next;
}
size--;
}
}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

#include

#include

#include

#include

void main()

{

int i,j,k,n,add=0,size;

char c,in[20],temp[20],val[20],var[20];

FILE*fp;

clrscr();

fp=fopen("input.c","r");

c=getc(fp);

printf('\n THE INPUT FILE IS:")

while(!feof(fp))

{

{

printf("%c",c);

c=getc(fp);

}

fclose(fp);

printf("\n**************symbol table**************");

printf("\n DAta type\tName\tNo.of Elements\tSize\tAddress");

printf("************************************");

while(!feof(fp))

{

if(isalpha(c))

{

i=0;

do

{

temp[i==]=c

c=getc(fp);

}while(isalpha(c));

temp[i]='\0';

if(strcmp("int",temp)==0)

{

strcpy(val," ");

strcpy(in," ");

strcpy(var," ");

while(c!=';')

{

strcpy(val," ");

c=getc(fp);

i=0;

do

{

var[i++]=c;

c=getc(fp);

}while(isalpha(c));

var[i]='\0';

if(c=='[')

{

i=0;

c=getc(fp);

strcpy(in," ")

do

{

in[i++]=c;

c=getc(fp);

k=atoi(in);

size=size*k;

}

else

{

strcpy(in,'1');

size=2;

}

if(c=='=')

c=getc(fp);

i=0;

do

{

val[i++]=c;

c=getc(fp);

}while(c!=','&&c!=';')

val[i]='\0';

}

printf("\n%s\t\t%s\t%s\t\t%s\t%dBytes\t%d",temp,var,val,in,size,add);

add=add+size;

}

}

}

else

c=getc(fp);

}

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

# include <stdio.h>

# include <conio.h>

# include <alloc.h>

# include <string.h>

# define null 0

int size=0;

void insert();

void del();

int search(char lab[]);

void modify();

void display();

struct symbtab

{

char label[10];

int addr;

struct symtab *next;

};

struct symbtab *first,*last;

void main()

{

int op;

int y;

char la[10];

clrscr();

do

{

printf("\nSYMBOL TABLE IMPLEMENTATION\n");

printf("1. INSERT\n");

printf("2. DISPLAY\n");

printf("3. DELETE\n");

printf("4. SEARCH\n");

printf("5. MODIFY\n");

printf("6. END\n");

printf("Enter your option : ");

scanf("%d",&op);

switch(op)

{

case 1:

insert();

display();

break;

case 2:

display();

break;

case 3:

del();

display();

break;

case 4:

printf("Enter the label to be searched : ");

scanf("%s",la);

y=search(la);

if(y==1)

{

printf("The label is already in the symbol Table");

}

else

{

printf("The label is not found in the symbol table");

}

break;

case 5:

modify();

display();

break;

case 6:

break;

}

}

while(op<6);

getch();

}

void insert()

{

int n;

char l[10];

printf("Enter the label : ");

scanf("%s",l);

n=search(l);

if(n==1)

{

printf("The label already exists. Duplicate cant be inserted\n");

}

else

{

struct symbtab *p;

p=malloc(sizeof(struct symbtab));

strcpy(p->label,l);

printf("Enter the address : ");

scanf("%d",&p->addr);

p->next=null;

if(size==0)

{

first=p;

last=p;

}

else

{

last->next=p;

last=p;

}

size++;

}

}

void display()

{

int i;

struct symbtab *p;

p=first;

printf("LABEL\tADDRESS\n");

for(i=0;i<size;i++)

{

printf("%s\t%d\n",p->label,p->addr);

p=p->next;

}

}

int search(char lab[])

{

int i,flag=0;

struct symbtab *p;

p=first;

for(i=0;i<size;i++)

{

if(strcmp(p->label,lab)==0)

{

flag=1;

}

p=p->next;

}

return flag;

}

void modify()

{

char l[10],nl[10];

int add, choice, i, s;

struct symbtab *p;

p=first;

printf("What do you want to modify?\n");

printf("1. Only the label\n");

printf("2. Only the address of a particular label\n");

printf("3. Both the label and address\n");

printf("Enter your choice : ");

scanf("%d",&choice);

switch(choice)

{

case 1:

printf("Enter the old label\n");

scanf("%s",l);

printf("Enter the new label\n");

scanf("%s",nl);

s=search(l);

if(s==0)

{

printf("NO such label");

}

else

{

for(i=0;i<size;i++)

{

if(strcmp(p->label,l)==0)

{

strcpy(p->label,nl);

}

p=p->next;

}

}

break;

case 2:

printf("Enter the label whose address is to modified\n");

scanf("%s",l);

printf("Enter the new address\n");

scanf("%d",&add);

s=search(l);

if(s==0)

{

printf("NO such label");

}

else

{

for(i=0;i<size;i++)

{

if(strcmp(p->label,l)==0)

{

p->addr=add;

}

p=p->next;

}

}

break;

case 3:

printf("Enter the old label : ");

scanf("%s",l);

printf("Enter the new label : ");

scanf("%s",nl);

printf("Enter the new address : ");

scanf("%d",&add);

s=search(l);

if(s==0)

{

printf("NO such label");

}

else

{

for(i=0;i<size;i++)

{

if(strcmp(p->label,l)==0)

{

strcpy(p->label,nl);

p->addr=add;

}

p=p->next;

}

}

break;

}

}

void del()

{

int a;

char l[10];

struct symbtab *p,*q;

p=first;

printf("Enter the label to be deleted\n");

scanf("%s",l);

a=search(l);

if(a==0)

{

printf("Label not found\n");

}

else

{

if(strcmp(first->label,l)==0)

{

first=first->next;

}

else if(strcmp(last->label,l)==0)

{

q=p->next;

while(strcmp(q->label,l)!=0)

{

p=p->next;

q=q->next;

}

p->next=null;

last=p;

}

else

{

q=p->next;

while(strcmp(q->label,l)!=0)

{

p=p->next;

q=q->next;

}

p->next=q->next;

}

size--;

}

}

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

Symbol tables are implemented as a hierarchical tree of scopes, with the global scope serving as the root. All names declared at global scope are listed in the root. Global function names are implemented as child nodes. Each function node serves as the root of its own local names (its formal arguments and local variables) with child nodes catering for each nested scope of the function. Data structures are also implemented as nodes, each of which lists the member names (with additional children to cater for members which are themselves data structures).

When searching for a name, we look within the node representing the nearest enclosing scope of that name. If the name doesn't exist there, we look to the parent scope, working our way up the tree until the name is found. If we reach the global scope and the name still isn't found, the name was not declared so the compilation has to fail at that point.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

no

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C program to create symbol table?
Write your answer...
Submit
Still have questions?
magnify glass
imp