-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupport.cpp
292 lines (271 loc) · 5.58 KB
/
support.cpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* 辅助函数
*/
#include <iostream>
#include <string>
#include <cstdarg>
#include <set>
#include <vector>
#include "global.h"
#include "lex.h"
#pragma warning(disable:4996)
extern char ch;
extern int lineNo;
extern int symbol;
extern std::string ident;
extern long long value;
extern unsigned int errorCount;
extern std::vector<std::string> errorMsg;
//输出相关信息的函数
void print(std::string type, std::string attr, std::string value)
{
printf("Type:%s\t%s:%s\n", type.c_str(), attr.c_str(), value.c_str());
}
void print(int type)
{
switch (type)
{
case NUM:printf("Type:Number\tValue:%d\n", value); break;
case IDENT:printf("Type:Ident\tName:%s\n", ident.c_str()); break;
case CH:printf("Type:CHAR\tValue:%s\n", ident.c_str()); break;
default:
break;
}
}
//检测字符是否匹配
bool match(char target)
{
if (ch == target)
{
ident.append(sizeof(char), ch);
ch = getchar();
return true;
}
else
{
return false;
}
}
//检查类型是否匹配
bool match(int type)
{
if (symbol == type)
{
print(type);
symbol = lex();
return true;
}
else
{
return false;
}
}
//检测token类型是否匹配,是则生成语法树的节点并将其加入父节点的子节点集合中
bool match(int type, AST_node parent)
{
if (symbol == type)
{
print(type);
AST_node t = makeNode(TERMINAL, (LexType)type, parent);
symbol = lex();
return true;
}
else
{
return false;
}
}
//记录错误信息
void error(std::string msg)
{
errorCount++;
char _message[10];
char* message = itoa(lineNo, _message, 10);
std::string s = message;
msg = ">>>>>> line " + s + " " + msg;
errorMsg.push_back(msg);
}
void error(int lineNo, std::string msg)
{
errorCount++;
char _message[10];
char* message = itoa(lineNo, _message, 10);
std::string s = message;
msg = ">>>>>> line " + s + " " + msg;
errorMsg.push_back(msg);
}
//输出错误信息
void errorRep()
{
for (std::vector<std::string>::iterator i = errorMsg.begin(); i != errorMsg.end(); i++)
{
std::cout << *i << std::endl;
//printf("%s\n", (*i).c_str());
}
}
//清楚错误信息
void errorClean()
{
errorCount = 0;
errorMsg.clear();
}
/*
错误恢复
n:接下来参数的个数
*/
void recovery(int n, ...)
{
va_list ap;
int type = 0;
std::set<int> symSet;
va_start(ap, n);
while (n-- > 0)
{
type = va_arg(ap, int);
symSet.insert(type);
}
va_end(ap);
while (symSet.find(symbol) == symSet.end())
{
if (symbol == EOF)
{
//在错误恢复时遇到文件末尾
error("File is incompleted");
freopen("CON", "w", stdout);
errorRep();
exit(1);
}
//不是错误恢复集合中的token
symbol = lex();
}
return;
}
/*
语法树相关操作
*/
AST_node makeNode(ASTType ast_type, AST_node parent)
{
AST_node t = (AST_node)malloc(sizeof(AST_t));
t->ast_type = ast_type;
t->lex_symbol = (LexType)0;
t->parent = parent;
t->lineNo = lineNo;
t->children = new std::vector<AST_node>;
t->val.ident = new std::string(ident);
t->tableItem = NULL;
t->level = -1;
if (ast_type == FUNDEF || ast_type == PRODEF || ast_type == ROOT)
t->symTable = new Table();
else
{
t->symTable = parent == NULL ? NULL : parent->symTable;
}
if(parent != NULL)
parent->children->push_back(t);
return t;
}
AST_node makeNode(ASTType ast_type, LexType symbol, AST_node parent)
{
AST_node t = (AST_node)malloc(sizeof(AST_t));
t->ast_type = ast_type;
t->parent = parent;
t->lex_symbol = symbol;
t->tableItem = NULL;
t->symTable = parent->symTable;
if (symbol == NUM)
{
t->val.value = value;
}
else
{
t->val.ident = new std::string(ident);
}
t->children = new std::vector<AST_node>;
t->lineNo = lineNo;
if(parent != NULL)
parent->children->push_back(t);
return t;
}
void printAST(AST_node root, int lev)
{
std::string *space = new std::string(lev, '\t');
if (root != NULL)
{
switch (root->ast_type)
{
case TERMINAL:
std::cout << *space << "Terminal\t" << root->lex_symbol << "\t";
if (root->lex_symbol == NUM)
{
std::cout << root->val.value << std::endl;
}
else
{
std::cout << *(root->val.ident) << std::endl;
}
break;
case CONSTDECL:
std::cout << *space << "ConstDecl" << std::endl;
break;
case PROGRAM:
std::cout << *space << "Program" << std::endl;
break;
case CONSTDEF:
std::cout << *space << "ConstDef" << std::endl;
break;
case CONST:
std::cout << *space << "Const" << std::endl;
break;
case VAR:
std::cout << *space << "VAR" << std::endl;
break;
case VARDECL:
std::cout << *space << "VarDecl" << std::endl;
break;
case VARDEF:
std::cout << *space << "VarDef" << std::endl;
break;
case PRODECL:
std::cout << *space << "Procedure Declaration" << std::endl;
break;
case PROHEAD:
std::cout << *space << "Procedure Head Declaration" << std::endl;
break;
case FUNDECL:
std::cout << *space << "Function Declaration" << std::endl;
break;
case FUNHEAD:
std::cout << *space << "Function Head Declaration" << std::endl;
break;
case ARGLIST:
std::cout << *space << "Arglist" << std::endl;
break;
case ARGS:
std::cout << *space << "Args" << std::endl;
break;
case EXPRESSION:
std::cout << *space << "Expression" << std::endl;
break;
case CONDITION:
std::cout << *space << "Condition" << std::endl;
break;
case TERM:
std::cout << *space << "Term" << std::endl;
break;
case FACTOR:
std::cout << *space << "Factor" << std::endl;
break;
default:
break;
}
if (root->children->size() == 0)
{
return;
}
for (std::vector<AST_node>::iterator i = root->children->begin(); i != root->children->end(); i++)
{
printAST(*i, lev + 1);
}
}
return;
}