-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnalyse.cpp
More file actions
207 lines (187 loc) · 4.68 KB
/
Analyse.cpp
File metadata and controls
207 lines (187 loc) · 4.68 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* @Copyright: minic
* @Author: linmaosen
* @Description: 语义分析:实现构建符号表和类型检查的函数
* @LastEditors: linmaosen
* @LastEditTime: 2020-06-13
* @FilePath: /minic/Analyse.cpp
*/
#include "Analyse.h"
#include "Symtab.h"
// 记录最大的scope
extern int HighScope;
// 记录是哪个scope
static int scope_a = 0;
static int location[MAX_SCOPE] = {0, 0, 0, 0, 0, 0, 0, 0};
static int No_change = 0;
/**
* @description: 前序遍历生成符号表,后序遍历类型检查
* @param {node*, void*, void*}
* @return: void
*/
static void traverse(node *t,
void (*preProc)(node *),
void (*postProc)(node *))
{
if (t != NULL)
{
preProc(t);
{
int i;
for (i = 0; i < MAXCHILDREN; i++)
traverse(t->nodeChild[i], preProc, postProc);
}
postProc(t);
traverse(t->next, preProc, postProc);
}
}
/**
* @description: 空函数,什么也不执行
* @param {node*}
* @return: void
*/
static void nullProc(node *t)
{
if (t == NULL)
return;
else
return;
}
/**
* @description: 将语法树结点中的变量、方法名插入符号表中
* @param {node*}
* @return: void
*/
static void insertNode(node *t)
{
switch (t->nodekind)
{
case StmtK:
switch (t->kind.stmt)
{
case FunK:
location[scope_a] = 0;
t->scope = 0; // scope==0 表示是全局的方法, main() gcd()
if (st_lookup(t->name, 0) == -1)
st_insert(t->name, t->lineno, -1, 0, 0, 0);
else
st_insert(t->name, t->lineno, -1, 0, 0, 0);
break;
case CompK:
{
if (!No_change)
{
scope_a = scope_a + 1;
}
else
{
No_change = 0;
}
HighScope = scope_a;
break;
}
case SelectK:
if ((t->nodeChild[1])->kind.stmt == CompK)
No_change = 1;
break;
case IteraK:
if ((t->nodeChild[1])->kind.stmt == CompK)
No_change = 1;
break;
case VarK:
t->scope = t->isParameter == 1 ? scope_a + 1 : scope_a;
if (st_lookup(t->name, t->isParameter == 1 ? scope_a + 1 : scope_a) == -1)
st_insert(t->name, t->lineno, location[scope_a]++, t->isParameter == 1 ? scope_a + 1 : scope_a, t->isParameter == 1 ? 1 : 0, 0);
else
st_insert(t->name, t->lineno, 0, t->isParameter == 1 ? scope_a + 1 : scope_a, t->isParameter == 1 ? 1 : 0, 0);
break;
case ArrayK:
if (t->isParameter)
{
t->scope = scope_a + 1;
if (st_lookup(t->name, scope_a + 1) == -1)
st_insert(t->name, t->lineno, location[scope_a]++, scope_a + 1, 1, 1);
else
st_insert(t->name, t->lineno, 0, scope_a + 1, 1, 1);
}
else
{
if (st_lookup(t->name, 0) == -1)
{
t->scope = scope_a; // 不是全局的
}
else
{
t->scope = 0;
break;
}
if (st_lookup(t->name, scope_a) == -1)
{
st_insert(t->name, t->lineno, location[scope_a]++, scope_a, t->isParameter == 1 ? 1 : 0, 1);
location[scope_a] += t->val - 1;
}
else
st_insert(t->name, t->lineno, 0, t->isParameter == 1 ? scope_a + 1 : scope_a, t->isParameter == 1 ? 1 : 0, 1);
}
break;
case CallK:
t->scope = 0;
if (st_lookup(t->name, 0) == -1)
st_insert(t->name, t->lineno, -1, 0, 0, 0);
else
st_insert(t->name, t->lineno, -1, 0, 0, 0);
break;
default:
break;
}
break;
case ExpK:
switch (t->kind.exp)
{
case IdK:
if (st_lookup(t->name, 0) == -1)
t->scope = t->isParameter == 1 ? scope_a + 1 : scope_a;
else
t->scope = 0;
// 函数的参数
if (t->isParameter == 1)
{
t->scope = scope_a + 1;
if (st_lookup(t->name,scope_a + 1) == -1)
st_insert(t->name, t->lineno, location[scope_a]++, scope_a + 1,1, 0);
else
st_insert(t->name, t->lineno, 0,scope_a + 1, 1, 0);
}
break;
default:
break;
}
break;
default:
break;
}
}
/**
* @description: 前序遍历构建符号表
* @param {node*}
* @return: void
*/
void buildSymtab(node *syntaxTree)
{
traverse(syntaxTree, insertNode, nullProc);
}
static void typeError(node *t, char *message)
{
}
static void checkNode(node *t)
{
}
/**
* @description: 后序遍历类型检查
* @param {node*}
* @return: void
*/
void typeCheck(node *syntaxTree)
{
traverse(syntaxTree, nullProc, checkNode);
}