-
Notifications
You must be signed in to change notification settings - Fork 0
/
symTable.h
79 lines (69 loc) · 1.42 KB
/
symTable.h
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<string.h>
#include<stdlib.h>
#define idToken 1
#define numToken 2
#define intType 1
#define realType 2
#define TOKENMAX 100
typedef struct entryRec{
char *name;
int type;
int valueType;
float realVal;
int intVal;}symTableEntry;
symTableEntry symTable[TOKENMAX];
int numOfTokens=0;
int searchSymTable(char *n)
{
int found=0;
int i;
for (i=0;i<numOfTokens && !found;i++)
if (strcmp(n,symTable[i].name)==0)
found=1;
if (found)
return i-1;
else
return -1;
}
void setIntVal(int i,int iv)
{symTable[i].intVal=iv;
return;
}
void setRealVal(int i,float rv)
{symTable[i].realVal=rv;
return;
}
int addToSymTable(char *n,int t)
{
int i=searchSymTable(n);
if(i==-1){
i=numOfTokens;
symTable[i].name=malloc(strlen(n)+1);
strcpy(symTable[i].name,n);
symTable[i].type=t;
numOfTokens++;}
return i;
}
void setValueType(int i,int vt)
{
symTable[i].valueType=vt;
return;
}
void printSymTable()
{
int i;
printf("\n\t____________________Symbol Table____________________\n");
printf("\tToken Name\tToken type\tValue Type\tValue\t\t\n");
for(i=0;i<numOfTokens;i++)
{printf("\t%s\t",symTable[i].name);
if(symTable[i].type ==idToken)
printf("\tidentifier\t");
else {
printf("\tnum\t");
if(symTable[i].valueType==intType)
printf("\tinteger\t\t%d\t",symTable[i].intVal);
else if(symTable[i].valueType==realType)
printf("\treal\t\t%f\t",symTable[i].realVal);}
printf("\n");}
return;
}