-
Notifications
You must be signed in to change notification settings - Fork 2
/
symboltable.c
62 lines (56 loc) · 1.33 KB
/
symboltable.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
BATCH NO. 27
Mayank Agarwal (2014A7PS111P)
Karan Deep Batra(2014A7PS160P)
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "symboltable.h"
#include "hashtable.h"
#include "token.h"
symboltable* makesymboltable()
{
symboltable* temp = (symboltable*)malloc(sizeof(symboltable));
for(int i=0; i<symboltablesize; i++)
temp->buckets[i] = NULL;
return temp;
}
tokeninfo* presentsymboltable(symboltable* table, char* lexeme) //checks if lexeme is already present in symbol table and returns pointer to it
{
int h = hash(lexeme);
tokeninfo* pt = table->buckets[h];
while(pt != NULL)
{
if(strcmp(lexeme,pt->lexeme) == 0)
return pt;
pt = pt->next;
}
return NULL;
}
void insertsymboltable(symboltable* table, char* tokenname, char* lexeme, int linenumber) //insert token to symbol table
{
// printf("%s\n",str);
// static int ntval=1, tval=1;
int h = hash(lexeme);
tokeninfo* pt = table->buckets[h];
tokeninfo* tok = makeToken(tokenname, lexeme, linenumber);
tok->next = pt;
table->buckets[h] = tok;
return;
}
void printsymboltable(symboltable* table)
{
for(int i=0; i<symboltablesize; i++)
{
tokeninfo* pt = table->buckets[i];
printf("%d --> ", i);
while(pt != NULL)
{
printf("lexeme=%s token=%s line=%d | ", pt->lexeme, pt->tokenname, pt->linenumber);
pt = pt->next;
}
printf("\n");
}
return;
}