Repository navigation
Expand file tree
/
Copy pathsemantics.h
More file actions
680 lines (635 loc) · 21.4 KB
/
Copy pathsemantics.h
File metadata and controls
680 lines (635 loc) · 21.4 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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
//This file checks semantic errors.
#ifndef FILE_SEMANTICS_H
#define FILE_SEMANTICS_H
#include <vector>
#include <map>
#include "header.h"
#include "def.h"
#include "ast.h"
using namespace std;
#define MAX_LEVEL 200
#define MAX_DEPTH 200
#define MAX_FUNCS 200
struct ptr_cmp { //map compare char[] with their address; maybe it's better to use map <string, >
bool operator()(const char* s1, const char* s2) const {
return strcmp(s1,s2) < 0;
}
};
struct SymbolTable {
map <char*, char*,ptr_cmp> table;
map <char*, vector<char *>, ptr_cmp > struct_table;
map <char*, vector<char *>, ptr_cmp > struct_id_table;
map < char*, int, ptr_cmp > struct_name_width_table;
map <char*, int, ptr_cmp> width_table; // records the num of "int"s, the actual width should be 4 times of that
map <char*, vector<int>, ptr_cmp> array_size_table;
int parent_index;//which record the index of his parent in the upper level; -1 means it's the gloabl scope
} env[MAX_LEVEL][MAX_DEPTH];
int level = 0,cnt[MAX_LEVEL];// keep track num of symbol tables of this level
map <char*, vector<int>, ptr_cmp > func_table;
map <char*, int, ptr_cmp> func_cnt_table;
map <char*,map<int,char*>, ptr_cmp > func_overload_table;
vector <int> tmp_array_size_vector;
vector <int> func_vector[MAX_FUNCS];
vector <char*> struct_vector[MAX_FUNCS];
set <char*, ptr_cmp> func_set[MAX_FUNCS], struct_set[MAX_FUNCS];
int func_cnt = 0, struct_cnt = 0, tmp_num_of_var = 0, func_vector_cnt = 0, struct_vector_cnt = 0, tmp_num = 1, tmp_num_struct = 0;
bool in_func = false, in_for = false, bool_main = false;//to help check classify whether a 'return' happens in a func
int StringToInt(char *s);
bool isReserved(char* s);
void report_err(char *s1,char *s2, int linenum);
void semantics_check(TreeNode* p);
void semantics_check_program(TreeNode* p);
void semantics_check_extdefs(TreeNode* p);
void semantics_check_init(TreeNode *p);
void semantics_check_exp(TreeNode *p, char *s);
void semantics_check_extdef(TreeNode* p);
void semantics_check_sextvars(TreeNode *p);
void semantics_check_stspec(TreeNode* p);
void semantics_check_paras(TreeNode *p,char *s);
char* semantics_check_type(TreeNode *p);
void semantics_check_func(TreeNode *p);
void semantics_check_extvars(TreeNode *p);
bool semantics_check_id(char* s,int num);
void semantics_check_var(TreeNode *p);
void semantics_check_stmtblock(TreeNode *p);
void semantics_check_decs(TreeNode *p);
void semantics_check_defs(TreeNode* p);
void semantics_check_sdefs(TreeNode *p);
void semantics_check_sdecs(TreeNode *p, int in);
void semantics_check_stmts(TreeNode* p);
void semantics_check_arrs(TreeNode *p);
void semantics_check_args(TreeNode *p, char *s);
void semantics_check_exps(TreeNode *p);
void check_left_value_exps(TreeNode *p);
bool find_id(TreeNode *p);
int find_struct_id(char* s, char*s2);
void semantics_check_stmt(TreeNode *p);
void semantics(TreeNode *p);
//void struct_check_id(char* s1, char* s2);
int get_width_semantics(char* s) {
int tmp_level = level, tmp_depth = cnt[level];
while (1) {
if (env[tmp_level][tmp_depth].table.find(s)!=env[tmp_level][tmp_depth].table.end()) { //find
return env[tmp_level][tmp_depth].width_table[s];
}
else {
int parent = env[tmp_level][tmp_depth].parent_index;
if (parent==-1||tmp_level==0) assert(0);
else {
tmp_level--;
tmp_depth = parent;
}
}
}
}
int StringToInt(char *s) {
assert(s!=NULL);
if (strlen(s)>=2&&s[0]=='0'&&(s[1]=='x'||s[1]=='X')) return strtol(s,NULL,16);
if (strlen(s)>=2&&s[0]=='0'&&s[1]!='x'&&s[1]!='X') return strtol(s,NULL,8);
return strtol(s,NULL,10);
}
bool isReserved(char* s) {
if (strcmp(s,"int")==0||strcmp(s,"return")==0||strcmp(s,"break")==0||strcmp(s,"for")==0||strcmp(s,"if")==0)
return true;
if (strcmp(s,"else")==0||strcmp(s,"continue")==0||strcmp(s,"struct")==0||strcmp(s,"read")==0||strcmp(s,"write")==0) return true;
return false;
}
void report_err(char *s1, char *s2, int linenum) {
fprintf(stderr,"at line %d:\t",linenum);
if (s1!=NULL) fprintf(stderr,"%s ",s1);
if (s2!=NULL) fprintf(stderr,"%s ",s2);
fprintf(stderr,"\n");
exit(1);
}
void semantics_check(TreeNode* p) {
switch (p->type) {
case _PROGRAM: semantics_check_program(p); break;
case _STMTBLOCK: semantics_check_stmtblock(p); break;
case _EXTDEFS: semantics_check_extdefs(p); break;
case _EXTDEF: semantics_check_extdef(p); break;
case _SEXTVARS: semantics_check_sextvars(p); break;
case _EXTVARS: semantics_check_extvars(p); break;
case _STSPEC: semantics_check_stspec(p); break;
case _FUNC: semantics_check_func(p); break;
case _TYPE: semantics_check_type(p); break;
case _STMTS: semantics_check_stmts(p); break;
case _STMT: semantics_check_stmt(p); break;
case _DEFS: semantics_check_defs(p); break;
case _SDEFS: semantics_check_sdefs(p); break;
case _DECS: semantics_check_decs(p); break;
case _VAR: semantics_check_var(p); break;
case _INIT: semantics_check_init(p); break;
case _EXP: semantics_check_exp(p,NULL); break;
case _EXPS: semantics_check_exps(p); break;
case _ARRS: semantics_check_arrs(p); break;
case _OPERATOR: break;
case _INT: break;
case _SDECS: semantics_check_sdecs(p,0); break;
case _NULL: break;//do nothing
default: fprintf(stderr,"%s \n", p->data); assert(0);
}
}
void semantics_check_program(TreeNode* p) {//OK
int i;
for (i = 0; i < p->size; ++i) {
semantics_check(p->children[i]);
}
}
void semantics_check_extdefs(TreeNode* p) {//OK
int i;
for (i = 0; i < p->size; ++i) {
semantics_check(p->children[i]);
}
}
void semantics_check_init(TreeNode *p) {
if (!strcmp(p->data,"init")) {
semantics_check_exps(p->children[0]);
}
else {
semantics_check_args(p,NULL); //array initialization
}
}
void semantics_check_exp(TreeNode *p,char* s) {
int i;
if (s!=NULL) {
if (p->size==0);
else {++func_cnt_table[s];}
}
if (p->size>0) {
semantics_check_exps(p->children[0]);
}
}
void semantics_check_sextvars_1(char *s, TreeNode *p) {
if (p->size==0) return;
int tmp_level = level, tmp_depth = cnt[level];
while (1) {
if (env[tmp_level][tmp_depth].struct_id_table.find(s)!=env[tmp_level][tmp_depth].struct_id_table.end()) {
break;
}
else {
int parent = env[tmp_level][tmp_depth].parent_index;
if (parent==-1||tmp_level==0) {report_err("No such struct named~",s,p->line_num);}
else {
tmp_level--;
tmp_depth = parent;
}
}
}
if (semantics_check_id(p->children[0]->data,p->children[0]->line_num)&&func_table.find(p->children[0]->data)!=func_table.end()) {
report_err("redefinition of",p->children[0]->data,p->children[0]->line_num);
}
else {
env[level][cnt[level]].struct_table[p->children[0]->data] = env[tmp_level][tmp_depth].struct_id_table[s];
env[level][cnt[level]].table[p->children[0]->data] = "struct";
//fprintf(stderr,"flag %s \n", p->children[0]->data);
env[level][cnt[level]].width_table[p->children[0]->data] = env[tmp_level][tmp_depth].struct_name_width_table[s];
}
if (p->size==2) semantics_check_sextvars_1(s,p->children[1]);
}
void semantics_check_extdef(TreeNode* p) {
if (!strcmp("extdef1",p->data)) {//global variable declearations
semantics_check_extvars(p->children[1]);
}
else if (!strcmp("extdef2",p->data)) {//struct declarations
semantics_check_stspec(p->children[0]);
if (p->children[0]->size==3) {
env[level][cnt[level]].struct_id_table[p->children[0]->children[1]->data] = struct_vector[struct_vector_cnt];
env[level][cnt[level]].struct_name_width_table[p->children[0]->children[1]->data] = tmp_num_struct;
}
if (p->children[0]->size==2&&!strcmp(p->children[0]->data,"stspec")) {
semantics_check_sextvars_1(p->children[0]->children[1]->data,p->children[1]);
}
else {
semantics_check_sextvars(p->children[1]);
}
}
else { //function declarations
semantics_check_func(p->children[1]);
in_func = true;
semantics_check_stmtblock(p->children[2]);
in_func = false;
}
}
void semantics_check_sextvars(TreeNode *p) {
if (p->size==0) return;
if (semantics_check_id(p->children[0]->data,p->children[0]->line_num)&&func_table.find(p->children[0]->data)==func_table.end()) {
env[level][cnt[level]].table[p->children[0]->data] = "struct";
env[level][cnt[level]].struct_table[p->children[0]->data] = struct_vector[struct_vector_cnt];
env[level][cnt[level]].width_table[p->children[0]->data] = tmp_num_struct;
if (level>0)
env[level][cnt[level]].parent_index = cnt[level-1]; //which stmtblock in the upper level is my parent
else env[level][cnt[level]].parent_index = -1;
}
else {
report_err("redefinition of",p->children[0]->data,p->children[0]->line_num);
}
if (p->size==2) { semantics_check_sextvars(p->children[1]);}
}
void semantics_check_stspec(TreeNode* p) { //struct
++struct_cnt;
if (!strcmp(p->data,"stspec identifier {}")) {
++struct_vector_cnt;
tmp_num_struct = 0;
semantics_check_sdefs(p->children[2]);
}
else if (!strcmp(p->data,"stspec {}")) {
++struct_vector_cnt;
tmp_num_struct = 0;
semantics_check_sdefs(p->children[1]);
}
}
void semantics_check_paras(TreeNode *p,char *s) {
if (p->size>=1) {
++tmp_num_of_var;
if (env[level+1][cnt[level+1]+1].table.find(p->children[0]->data)==env[level+1][cnt[level+1]+1].table.end()) {
// to see whether multiple declarations of a variable
env[level+1][cnt[level+1]+1].table[p->children[0]->data] = "int";
env[level+1][cnt[level+1]+1].width_table[p->children[0]->data] = 1;
}
else {
report_err("redefinition of", p->children[1]->data,p->children[1]->line_num);
}
}
if (p->size==2) {semantics_check_paras(p->children[1],s);}
}
char* semantics_check_type(TreeNode *p) {
return p->data;
}
bool vector_find(vector<int> p, int num) {
//cout << "vector_find" << endl;
int i, sz = p.size();
//cout << num << endl;
for (i = 0; i < sz; ++i) {
//cout << p[i] << endl;
if (p[i]==num) return true;
}
return false;
}
void rename(TreeNode *p, int num) {
char *s = new char[strlen(p->data)+2];
s[0] = '_';
s[1] = num + '0';
int i;
for (i = 2; i < 2+strlen(p->data); ++i) {
s[i] = p->data[i-2];
}
p->data = s;
}
void semantics_check_func(TreeNode *p) {
++func_cnt;
if (semantics_check_id(p->children[0]->data,p->children[0]->line_num)) { // no conflict with the name of int variables and struct variables
tmp_num_of_var = 0;
semantics_check_paras(p->children[1],p->children[0]->data);
if (func_table.find(p->children[0]->data)==func_table.end()) { // new function name
if (!strcmp(p->children[0]->data,"main")) {bool_main = true;}
func_table[p->children[0]->data] = func_vector[func_vector_cnt];
func_table[p->children[0]->data].push_back(tmp_num_of_var);
++func_vector_cnt;
}
else { // function name exists, check whether it's an overloaded function
/*just rename it!!!!!!!!!!!*/
if (!vector_find(func_table[p->children[0]->data],tmp_num_of_var)) { //overloaded function
func_table[p->children[0]->data].push_back(tmp_num_of_var);
if (func_table[p->children[0]->data].size()>=2) {
func_overload_table[p->children[0]->data][func_table[p->children[0]->data][0]] = p->children[0]->data;
char *ch = p->children[0]->data;
rename(p->children[0],tmp_num_of_var);
func_overload_table[ch][tmp_num_of_var] = p->children[0]->data;
func_table[p->children[0]->data] = func_vector[func_vector_cnt];
func_table[p->children[0]->data].push_back(tmp_num_of_var);
++func_vector_cnt;
}
}
else report_err("multiple definitions of", p->children[0]->data,p->children[0]->line_num);
}
}
else {
report_err("conflicting names of", p->children[0]->data,p->children[0]->line_num);
}
}
void semantics_check_extvars(TreeNode *p) {
if (p->type==_NULL) {return;}
else {
int i;
tmp_num = 1;
tmp_array_size_vector.clear();
semantics_check_var(p->children[0]);
for (i = 1; i < p->size;++i) {
semantics_check(p->children[i]);
}
}
}
bool semantics_check_id(char* s,int num) {
if (isReserved(s)) {
report_err("Reserved words can not be used as identifiers", s,num);
return false;
}
if (env[level][cnt[level]].table.find(s)!=env[level][cnt[level]].table.end()){ //already exist
report_err("redeclearation of",s,num);
return false;
}
else return true; //new variable name
}
void semantics_check_var(TreeNode *p) {//OK
if (p->size==1) {
if (semantics_check_id(p->children[0]->data,p->children[0]->line_num)) { //new variable name
if (level==0&&func_table.find(p->children[0]->data)!=func_table.end())
{report_err("redefinition of", p->children[0]->data,p->children[0]->line_num);}
env[level][cnt[level]].table[p->children[0]->data] = "int";
env[level][cnt[level]].width_table[p->children[0]->data] = tmp_num;
int i,j;
vector <int> final_vector;
for (i = 0; i < tmp_array_size_vector.size(); ++i) {
int t = 4;
for (j = i+1; j < tmp_array_size_vector.size(); ++j) {
t *= tmp_array_size_vector[i];
}
final_vector.push_back(t);
}
env[level][cnt[level]].array_size_table[p->children[0]->data] = final_vector;
}
else {report_err("redefinition of", p->children[0]->data,p->children[0]->line_num);}
}
else {
int intnum = StringToInt(p->children[1]->data);
if (intnum < 0) report_err("index can't be less than zero",NULL,p->line_num);
tmp_array_size_vector.push_back(intnum);
tmp_num *= intnum;
semantics_check_var(p->children[0]);
}
}
void semantics_check_stmtblock(TreeNode *p) {
++level;
++cnt[level];
if (level>0)
env[level][cnt[level]].parent_index = cnt[level-1]; //which stmtblock in the upper level is my parent
else env[level][cnt[level]].parent_index = -1;
semantics_check_defs(p->children[0]);
semantics_check_stmts(p->children[1]);
--level;
}
void semantics_check_defs(TreeNode* p) {
int i;
if (p->size==0) return;
if (!strcmp(p->data,"defs1")) {
semantics_check(p->children[0]);
semantics_check(p->children[1]);
semantics_check(p->children[2]);
return;
}
if (p->children[0]->size==2&&!strcmp(p->children[0]->data,"stspec")) {
semantics_check_stspec(p->children[0]);
semantics_check_sextvars_1(p->children[0]->children[1]->data,p->children[1]);
return;
}
for (i = 0; i < p->size; ++i) {
semantics_check(p->children[i]);
}
}
void semantics_check_decs(TreeNode *p)
{
int i;
tmp_num = 1;
tmp_array_size_vector.clear();
semantics_check_var(p->children[0]);
for (i = 1; i < p->size;++i) {
semantics_check(p->children[i]);
}
}
void semantics_check_sdefs(TreeNode *p) { //inside of a struct
int i;
if (p->size==0) return;
semantics_check_sdecs(p->children[1],1);
semantics_check_sdefs(p->children[2]);
}
void semantics_check_sdecs(TreeNode *p, int in_struct) {
if (in_struct) {
++tmp_num_struct;
if (struct_set[struct_cnt].find(p->children[0]->data)==struct_set[struct_cnt].end()) {//no multiple declarations in a struct
struct_set[struct_cnt].insert(p->children[0]->data);
struct_vector[struct_vector_cnt].push_back(p->children[0]->data);
}
else {
report_err("redefinition of", p->children[0]->data,p->children[0]->line_num);
}
if (p->size==2) {
semantics_check_sdecs(p->children[1], in_struct);
}
}
else {
if (semantics_check_id(p->children[0]->data,p->children[0]->line_num)&&func_table.find(p->children[0]->data)==func_table.end()) {
env[level][cnt[level]].table[p->children[0]->data] = "struct";
env[level][cnt[level]].struct_table[p->children[0]->data] = struct_vector[struct_vector_cnt];
env[level][cnt[level]].width_table[p->children[0]->data] = tmp_num_struct;
if (level>0)
env[level][cnt[level]].parent_index = cnt[level-1]; //which stmtblock in the upper level is my parent
else env[level][cnt[level]].parent_index = -1;
}
else {
report_err("redefinition of",p->children[0]->data,p->children[0]->line_num);
}
}
}
void semantics_check_stmts(TreeNode* p) {
int i;
for (i = 0; i < p->size; ++i) {
semantics_check(p->children[i]);
}
}
void semantics_check_arrs(TreeNode *p) { //arrays
int i;
for (i = 0; i < p->size; ++i) {
semantics_check(p->children[i]);
}
}
void semantics_check_args(TreeNode *p, char *s) { //arguments of function
//cout << "args" << endl;
if (s==NULL) return;
if (p->size==2) {
semantics_check_exp(p->children[0],s);//1 means it's the check process of arguemnts
semantics_check_args(p->children[1],s);
}
else {
semantics_check_exp(p->children[0],s);
}
}
int vector_find_index(vector <int> s, int num) {
int sz = s.size();
int i;
for (i = 0; i < sz; ++i) {
if (s[i]==num) return i;
}
}
void semantics_check_exps(TreeNode *p) {
if (!strcmp(p->data,"exps f()")) {
if (func_table.find(p->children[0]->data)==func_table.end()) { //don't find the function
report_err(p->children[0]->data,"not declared",p->children[0]->line_num);
}
else {
int func_tmp = func_cnt_table[p->children[0]->data];
func_cnt_table[p->children[0]->data] = 0;
semantics_check_args(p->children[1],p->children[0]->data);
if (vector_find(func_table[p->children[0]->data],func_cnt_table[p->children[0]->data])) {
// num of variables match!
int v_num = vector_find_index(func_table[p->children[0]->data],func_cnt_table[p->children[0]->data]);
if (v_num>0) {
int t_num = func_cnt_table[p->children[0]->data];
rename(p->children[0],t_num);
}
}
else {
report_err("number of arguments doesn't match: function", p->children[0]->data,p->children[0]->line_num);
}
func_cnt_table[p->children[0]->data] = func_tmp;
}
}
else if (!strcmp(p->data,"exps ()")) {
semantics_check_exps(p->children[0]);
}
else if (!strcmp(p->data,"exps arr")) { //intergers or arrays!!!!
if (!find_id(p->children[0])) {
report_err("No variable named",p->children[0]->data,p->children[0]->line_num);
}
if (find_struct_id(p->children[0]->data,NULL)) {
report_err(p->children[0]->data,"is a struct",p->children[0]->line_num);
}
if (p->children[1]->size==0) {
if (get_width_semantics(p->children[0]->data)!=1) {
report_err(p->children[0]->data,"is not an integer",p->children[0]->line_num);
}
}
if (p->children[1]->size!=0) { // it should be an array
if (get_width_semantics(p->children[0]->data)==1) {
report_err(p->children[0]->data,"is not array",p->children[0]->line_num);
}
}
}
else if (!strcmp(p->data,"exps unary")) { //unary operations
if (!strcmp(p->children[0]->data,"++")||!strcmp(p->children[0]->data,"--")) {
check_left_value_exps(p->children[1]);
}
semantics_check_exps(p->children[1]);
}
else if (!strcmp(p->data,"exps struct")) { //structs!!!
int sjtu = find_struct_id(p->children[0]->data,p->children[2]->data);
if (sjtu==0) {
report_err("No such struct named", p->children[0]->data,p->children[0]->line_num);
}
else if (sjtu==-1){
report_err("struct has no varibale named", p->children[2]->data,p->children[2]->line_num);
}
}
else if (!strcmp(p->data,"=")) {
check_left_value_exps(p->children[0]);
semantics_check_exps(p->children[0]);
semantics_check_exps(p->children[1]);
}
else {
int i;
for (i = 0; i < p->size; ++i) {
semantics_check_exps(p->children[i]);
}
}
}
int find_struct_id(char *s,char *s2) {
int tmp_level = level, tmp_depth = cnt[level];
int bool_find = 0;
while (1) {
if (env[tmp_level][tmp_depth].table.find(s)!=env[tmp_level][tmp_depth].table.end()&&!strcmp(env[tmp_level][tmp_depth].table[s],"struct")) {
//find
if (s2==NULL) return 1;
int i;
for (i = 0; i < env[tmp_level][tmp_depth].struct_table[s].size(); ++i) {
if (!strcmp(env[tmp_level][tmp_depth].struct_table[s][i],s2)) return 1;
}
return -1;
}
else {
int parent = env[tmp_level][tmp_depth].parent_index;
if (parent==-1||tmp_level==0) break;
else {
tmp_level--;
tmp_depth = parent;
}
}
}
return bool_find;
}
bool find_id(TreeNode *p) {
int tmp_level = level, tmp_depth = cnt[level];
bool bool_find = false;
while (1) {
//cout << "tmp_level" << tmp_level << "tmp_depth" << tmp_depth << endl;
if (env[tmp_level][tmp_depth].table.find(p->data)!=env[tmp_level][tmp_depth].table.end()) { //find
//cout << "find" << endl;
return true;
}
else {
int parent = env[tmp_level][tmp_depth].parent_index;
if (parent==-1||tmp_level==0) break;
else {
tmp_level--;
tmp_depth = parent;
}
}
}
return bool_find;
}
void check_left_value_exps(TreeNode *p) {
//cout << "p->data " << p->data << endl;
if (!strcmp(p->data,"=")) {
check_left_value_exps(p->children[0]);
semantics_check_exps(p->children[1]);
}
else if (!strcmp(p->data,"exps arr")||!strcmp(p->data,"exps struct")||!strcmp(p->data,"exps ()")) {
return;
}
else {report_err("lvalue required as left operand assignment",NULL,p->line_num);}
}
void semantics_check_stmt(TreeNode *p) {
if (!strcmp("stmt",p->data)||!strcmp(p->data,"stmt: exp;")) {
int i;
for (i = 0; i < p->size; ++i) {
semantics_check(p->children[i]);
}
}
else if (!strcmp("return stmt",p->data)) {
if (!in_func) report_err("return not in a function",NULL,p->line_num);
semantics_check_exps(p->children[1]);
}
else if (!strcmp("if stmt",p->data)) {
int i;
for (i = 0; i < p->size; ++i) {
semantics_check(p->children[i]);
}
}
else if (!strcmp("for stmt",p->data)) {
int i;
for (i = 0; i < p->size-1; ++i) {
semantics_check_exp(p->children[i],NULL);
}
in_for = true;
semantics_check_stmt(p->children[p->size-1]);
in_for = false;
}
else if (!strcmp("read stmt",p->data)) {
check_left_value_exps(p->children[0]);
//check if it's a left value
}
else if( !strcmp("write stmt",p->data)) {
semantics_check_exps(p->children[0]);
}
else { // break and continue
if (!in_for) {
report_err(p->children[0]->data,"not in a loop",p->children[0]->line_num);
}
}
}
void semantics(TreeNode *p) {
semantics_check(p);
if (!bool_main) {
fprintf(stderr,"fatal error! undefined reference to main()\n");
exit(1);
}
}
#endif