-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.c
341 lines (336 loc) · 8.43 KB
/
tree.c
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "tree.h"
#include <stdlib.h>
#include <stdio.h>
struct TreeNode *root;
//将n个子女节点插入到parent中,修改parent的line,numChildrean和Children
struct TreeNode *insert(int type, int n, ...)
{
// printf("insert\n");
struct TreeNode *parent = (struct TreeNode *)malloc(sizeof(struct TreeNode));
parent->numChildren = n;
parent->type = type;
if (n > 0)
{
// printf("insert:root=%d, parent=%d, n=%d, parent.type=%d, ",(int)root,(int)parent,n,type);
parent->children = (struct TreeNode **)malloc(sizeof(struct TreeNode *) * n);
struct TreeNode *tmp = NULL;
va_list vl;
va_start(vl, n);
// printf("chaild: ");
for (int i = 0; i < n; ++i)
{
tmp = va_arg(vl, struct TreeNode *);
// printf("add = %d, type=%d ",(int)tmp,tmp->type);
if (i == 0)
{
parent->line = tmp->line;
}
parent->children[i] = tmp;
}
va_end(vl);
// printf("\n");
parent->lastLine = parent->children[n-1]->lastLine;
}
return parent;
}
int isLexicalUnit(struct TreeNode *r)
{
int type = r->type;
//判断type是不是词法单元,全靠之前词法单元和语法单元define的顺序
return (type >= Node_INT) && (type <= Node_MINUS);
}
void preTraverse(struct TreeNode *r, int spaceNum)
{
//先序遍历,spaceNum为空格数
if (r != NULL)
{
if (isLexicalUnit(r))
{
printSpace(spaceNum);
switch (r->type)
{
case Node_ID:
{
printf("ID: %s", r->idName);
}
break;
case Node_TYPE:
{
printf("TYPE: ");
if (r->val.typeVal == TYPE_INT)
{
printf("int");
}
else
{
printf("float");
}
}
break;
case Node_INT:
{
printf("INT: %d", r->val.intVal);
}
break;
case Node_FLOAT:
{
printf("FLOAT: %f", r->val.floatVal);
}
break;
case Node_SEMI:
{
printf("SEMI");
}
break;
case Node_COMMA:
{
printf("COMMA");
}
break;
case Node_ASSIGNOP:
{
printf("ASSIGNOP");
}
break;
case Node_RELOP:
{
printf("RELOP");
}
break;
case Node_PLUS:
{
printf("PLUS");
}
break;
case Node_STAR:
{
printf("STAR");
}
break;
case Node_DIV:
{
printf("DIV");
}
break;
case Node_AND:
{
printf("AND");
}
break;
case Node_OR:
{
printf("OR");
}
break;
case Node_DOT:
{
printf("DOT");
}
break;
case Node_NOT:
{
printf("NOT");
}
break;
case Node_LP:
{
printf("LP");
}
break;
case Node_RP:
{
printf("RP");
}
break;
case Node_LB:
{
printf("LB");
}
break;
case Node_RB:
{
printf("RB");
}
break;
case Node_LC:
{
printf("LC");
}
break;
case Node_RC:
{
printf("RC");
}
break;
case Node_STRUCT:
{
printf("STRUCT");
}
break;
case Node_RETURN:
{
printf("RETURN");
}
break;
case Node_IF:
{
printf("IF");
}
break;
case Node_ELSE:
{
printf("ELSE");
}
break;
case Node_WHILE:
{
printf("WHILE");
}
break;
case Node_MINUS:
{
printf("MINUS");
}
break;
case Node_NEGETIVE:
{
printf("MINUS");
}
break;
}
printf("\n");
}
else
{
//是语法单元
if (r->numChildren > 0)
{
//非空
//先根
// printf("switch: r->type=%d\n\n\n", r->type);
printSpace(spaceNum);
switch (r->type)
{
case Node_Program:
{
printf("Program (%d)", r->line);
}
break;
case Node_ExtDefList:
{
printf("ExtDefList (%d)", r->line);
}
break;
case Node_ExtDef:
{
printf("ExtDef (%d)", r->line);
}
break;
case Node_ExtDecList:
{
printf("ExtDecList (%d)", r->line);
}
break;
case Node_Sepcifier:
{
printf("Specifier (%d)", r->line);
}
break;
case Node_StructSpecifier:
{
printf("StructSpecifier (%d)", r->line);
}
break;
case Node_OptTag:
{
printf("OptTag (%d)", r->line);
}
break;
case Node_Tag:
{
printf("Tag (%d)", r->line);
}
break;
case Node_VarDec:
{
printf("VarDec (%d)", r->line);
}
break;
case Node_FuncDec:
{
printf("FunDec (%d)", r->line);
}
break;
case Node_VarList:
{
printf("VarList (%d)", r->line);
}
break;
case Node_ParamDec:
{
printf("ParamDec (%d)", r->line);
}
break;
case Node_Compst:
{
printf("CompSt (%d)", r->line);
}
break;
case Node_StmtList:
{
printf("StmtList (%d)", r->line);
}
break;
case Node_Stmt:
{
printf("Stmt (%d)", r->line);
}
break;
case Node_DefList:
{
printf("DefList (%d)", r->line);
}
break;
case Node_Def:
{
printf("Def (%d)", r->line);
}
break;
case Node_DecList:
{
printf("DecList (%d)", r->line);
}
break;
case Node_Dec:
{
printf("Dec (%d)", r->line);
}
break;
case Node_Exp:
{
printf("Exp (%d)", r->line);
}
break;
case Node_Args:
{
printf("Args (%d)", r->line);
}
break;
}
printf("\n");
for (int i = 0; i < r->numChildren; ++i)
{
preTraverse(r->children[i], spaceNum + 2);
}
}
}
// printf("\n");
}
}
void printSpace(int num)
{
for (int i = 0; i < num; ++i)
{
printf(" ");
}
}