-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsymtab.cc
170 lines (157 loc) · 4.74 KB
/
symtab.cc
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/****************************************************/
/* File: symtab.c */
/* Symbol table implementation for the TINY compiler*/
/* (allows only one symbol table) */
/* Symbol table is implemented as a chained */
/* hash table */
/* Compiler Construction: Principles and Practice */
/* Kenneth C. Louden */
/* Adapted by Gustavo O. Souza */
/****************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "symtab.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
/* SIZE is the size of the hash table */
#define SIZE 211
/* SHIFT is the power of two used as multiplier
in hash function */
#define SHIFT 4
/* the hash function */
static int hash(std::string name)
{
char *key = (char *)name.c_str();
int temp = 0;
int i = 0;
while (key[i] != '\0')
{
temp = ((temp << SHIFT) + key[i]) % SIZE;
++i;
}
return temp;
}
/* the hash table */
BucketList hashTable[SIZE];
/* Procedure st_insert inserts line numbers and
* memory locations into the symbol table
* loc = memory location is inserted only the
* first time, otherwise ignored
*/
BucketList st_declare(std::string name, int lineno, int loc, IDType type, std::string escopo)
{
// printf( "%s name %d lineno %d loc %d type %s scope\n",name, lineno, loc, type, scope);
int h = hash(name);
BucketList l = new BucketListRec();
l->name = name;
l->lines = std::vector<int>();
l->lines.push_back(lineno);
l->memloc = loc;
l->scope = escopo;
l->next = hashTable[h];
l->vtype = type;
l->dtype = Integer;
hashTable[h] = l;
return l;
}
BucketList st_declare_function(std::string name, int lineno, int loc, IDType type, ExpType eType, std::string escopo)
{
// printf( "%s name %d lineno %d loc %d type %s scope\n",name, lineno, loc, type, scope);
int h = hash(name);
BucketList l = new BucketListRec();
l->name = name;
l->lines = std::vector<int>();
l->lines.push_back(lineno);
l->memloc = loc;
l->scope = escopo;
l->next = hashTable[h];
l->vtype = type;
l->dtype = eType;
hashTable[h] = l;
return l;
}
BucketList advanceNode(BucketList node)
{
return (node != NULL) ? node->next : node;
}
BucketList st_reference(BucketList l, int lineno)
{
l->lines.push_back(lineno);
// LineList t = l->lines;
// while (t->next != NULL)
// t = t->next;
// t->next = (LineList)malloc(sizeof(struct LineListRec));
// t->next->lineno = lineno;
// t->next->next = NULL;
return l;
} /* st_insert */
int cantMatchNameAndScopeInRange(BucketList node, std::string name, std::string escopo)
{
return (node != NULL) && ( //in range
(escopo != node->scope) || //Scopes don't match
(name != node->name)); //names don't match
}
BucketList st_find_at_scope(std::string name, std::string escopo)
{
BucketList EntryNode = hashTable[hash(name)];
while (cantMatchNameAndScopeInRange(EntryNode, name, escopo))
{
EntryNode = advanceNode(EntryNode);
}
return EntryNode;
}
/* Function st_find searches a variable in local scope and then in
* global scope.
* Not finding anything makes it return NULL
*/
BucketList st_find(std::string name, std::string escopo)
{
BucketList result = st_find_at_scope(name, escopo);
if (result == NULL)
result = st_find_at_scope(name, "global");
return result;
}
/* Procedure printSymTab prints a formatted
* listing of the symbol table contents
* to the listing file
*/
void printSymTab(FILE *listing)
{
int i;
fprintf(listing, "Variable Name Data type Location Scope Line Numbers\n");
fprintf(listing, "------------- --------- -------- ----- ------------\n");
for (i = 0; i < SIZE; ++i)
{
if (hashTable[i] != NULL)
{
BucketList l = hashTable[i];
while (l != NULL)
{
fprintf(listing, "%-14s ", l->name.c_str());
if (l->dtype == Integer)
{
fprintf(listing, "%-10s ", "Integer");
}
else if (l->dtype == boolean)
{
fprintf(listing, "%-10s ", "boolean");
}
else
{
fprintf(listing, "%-10s ", "Void");
}
fprintf(listing, "%-8d ", l->memloc);
fprintf(listing, "%-10s ", l->scope.c_str());
for (int line : l->lines)
{
fprintf(listing, "%4d ", line);
}
fprintf(listing, "\n");
l = l->next;
}
}
}
} /* printSymTab */