-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSymtab.cpp
More file actions
166 lines (151 loc) · 3.73 KB
/
Symtab.cpp
File metadata and controls
166 lines (151 loc) · 3.73 KB
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
/*
* @Copyright: minic
* @Author: linmaosen
* @Description: 实现查询、插入符号表以及输出符号表到文件的函数
* @LastEditors: linmaosen
* @LastEditTime: 2020-06-01
* @FilePath: /minic/Symtab.cpp
*/
#include <stdio.h>
#include <cstdlib>
#include "Symtab.h"
#include <string>
using namespace std;
extern int HighScope;
#define SHIFT 4
/**
* @description: 返回哈希值
* @param {string}
* @return: int
*/
static int Hash(string key)
{
int temp = 0;
int i = 0;
while (key[i] != '\0')
{
temp = ((temp << SHIFT) + key[i]) % SIZE;
++i;
}
return temp;
}
/**
* @description: 插入符号表,如果哈希值对应的位置不存在,则在头部插入,如果哈希值对应的位置存在,则在尾部连上
* @param {string, int, int, int, int}
* @return: void
*/
void st_insert(string name, int lineno, int loc, int sp, int para, int arr)
{
int h = Hash(name);
BucketList l = Scope[sp].hashTable[h];
while ((l != NULL) && name != l->name)
l = l->next;
if (l == NULL) /* variable not yet in table */
{
//l = (BucketList)malloc(sizeof(struct BucketListRec));
l = new BucketListRec();
l->name = name;
l->scope = sp; /* scope of variable */
l->isParam = para; /* parameter or not */
l->isArray = arr;
l->lines = (LineList)malloc(sizeof(struct LineListRec));
l->lines->lineno = lineno;
l->memloc = loc;
l->lines->next = NULL;
l->next = Scope[sp].hashTable[h];
Scope[sp].hashTable[h] = l;
}
else
{
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;
}
}
/**
* @description: 查询函数名在符号表中的位置
* @param {string, int}
* @return: BucketList
*/
BucketList fun_lookup(string name, int sp)
{
int h = Hash(name);
BucketList l = Scope[sp].hashTable[h];
while ((l != NULL) && (name != l->name))
l = l->next;
if (l == NULL)
return NULL;
else
return l;
}
/**
* @description: 查询变量在符号表中的位置
* @param {string, int}
* @return: BucketList
*/
BucketList var_lookup(string name, int sp)
{
int h = Hash(name);
BucketList l = Scope[sp].hashTable[h];
while ((l != NULL) && (name != l->name))
l = l->next;
if (l == NULL)
return NULL;
else
return l;
}
/**
* @description: 查询符号在符号表中的位置
* @param {string, int}
* @return: int
*/
int st_lookup(string name, int sp)
{
int h = Hash(name);
BucketList l = Scope[sp].hashTable[h];
while ((l != NULL) && (name != l->name))
l = l->next;
if (l == NULL)
return -1;
else
return l->memloc;
}
/**
* @description: 在指定文件中输出符号表
* @param {FILE}
* @return: void
*/
void printSymTab(FILE *listing)
{
int i, j;
for (j = 0; j <= HighScope; j++)
{
fprintf(listing, "Scope Variable Name Location Line Numbers\n");
fprintf(listing, "----- ------------- -------- ------------\n");
for (i = 0; i < SIZE; ++i)
{
if (Scope[j].hashTable[i] != NULL)
{
BucketList l = Scope[j].hashTable[i];
while (l != NULL)
{
LineList t = l->lines;
fprintf(listing, "%-6d ", l->scope);
fprintf(listing, "%-14s ", l->name.c_str());
fprintf(listing, "%-8d ", l->memloc);
while (t != NULL)
{
fprintf(listing, "%4d ", t->lineno);
t = t->next;
}
fprintf(listing, "\n");
l = l->next;
}
}
}
fprintf(listing, "\n");
}
}