-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing.h
509 lines (416 loc) · 13.5 KB
/
parsing.h
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
#include "types.h"
#define OUTER_ST 0
int PROGRAM_ST;
hashtable_t* symbol_tables[TABLE_SIZE];
int st_pointer, st_size=0;
int parse_tree(Node* p);
int type_is_valid(char* ret_type){
element_t* t = fetch(symbol_tables[OUTER_ST], ret_type);
if(t == NULL || t->type != TYPE_T)
return 0;
return 1;
}
int fetch_func(char* s){
int i;
if(fetch(symbol_tables[PROGRAM_ST], s) == NULL && fetch(symbol_tables[OUTER_ST], s) == NULL)
return -1;
for(i=st_size-1; i>=OUTER_ST; i--)
if(strcmp(symbol_tables[i]->func, s) == 0)
return i;
return -1;
}
element_t* fetch_id(Node* p){
element_t *t = fetch(symbol_tables[st_pointer], p->value);
if(t != NULL)
return t;
t = fetch(symbol_tables[PROGRAM_ST], p->value);
if(t != NULL)
return t;
t = fetch(symbol_tables[OUTER_ST], p->value);
if(t != NULL)
return t;
return NULL;
}
int is_global(Node* p){
if(st_pointer == PROGRAM_ST || st_pointer == OUTER_ST)
return 1;
element_t *t = fetch(symbol_tables[st_pointer], p->value);
if(t != NULL)
return 0;
return 1;
}
void print_op_error(Node *p){
printf("Line %d, col %d: Operator %s cannot be applied to types %s, %s\n",
p->loc.first_line, p->loc.first_column, p->token, type2string(p->op[0]->op_type), type2string(p->op[1]->op_type) );
}
void print_unary_error(Node *p){
printf("Line %d, col %d: Operator %s cannot be applied to type %s\n",
p->loc.first_line, p->loc.first_column, p->token, type2string(p->op[0]->op_type));
}
void print_variable_expected(Node* p){
printf("Line %d, col %d: Variable identifier expected\n", p->loc.first_line, p->loc.first_column);
}
void print_assign_error(Node *p){
printf("Line %d, col %d: Incompatible type in assigment to %s (got %s, expected %s)\n",
p->op[1]->loc.first_line, p->op[1]->loc.first_column, p->op[0]->value2, type2string(p->op[1]->op_type), type2string(p->op[0]->op_type));
}
void print_valparam_error(Node *p, type_t type1, type_t type2){
printf("Line %d, col %d: Incompatible type in val-paramstr statement (got %s, expected %s)\n",
p->loc.first_line, p->loc.first_column, type2string(type1), type2string(type2));
}
void print_stat_error(Node* p, type_t type1, type_t type2){
if(!strcmp(p->type, "IfElse"))
printf("Line %d, col %d: Incompatible type in if statement", p->op[0]->loc.first_line, p->op[0]->loc.first_column);
else if(!strcmp(p->type, "While"))
printf("Line %d, col %d: Incompatible type in while statement", p->op[0]->loc.first_line, p->op[0]->loc.first_column);
else
printf("Line %d, col %d: Incompatible type in repeat-until statement", p->op[1]->loc.first_line, p->op[1]->loc.first_column);
printf(" (got %s, expected %s)\n", type2string(type1), type2string(type2));
}
void print_already_def_error(Node* p){
printf("Line %d, col %d: Symbol %s already defined\n", p->loc.first_line, p->loc.first_column, p->value2);
}
void print_writeln_error(Node* p){
printf("Line %d, col %d: Cannot write values of type %s\n", p->loc.first_line, p->loc.first_column, type2string(p->op_type) );
}
int parse_funchead(Node* p, int n_args, Node** args, Node* ret){
int i;
char* name = p->op[0]->value;
st_pointer = st_size++;
element_t* f = fetch(symbol_tables[PROGRAM_ST], name);
if(f != NULL){
print_already_def_error(p->op[0]);
return 1;
}
symbol_tables[st_pointer] = new_hashtable(TABLE_SIZE, "Function");
strcpy(symbol_tables[st_pointer]->func, name);
if(parse_id(ret)) return 1;
if(ret->op_type != TYPE_T){
printf("Line %d, col %d: Type identifier expected\n", ret->loc.first_line, ret->loc.first_column);
return 1;
}
element_t* el = store(symbol_tables[st_pointer], name, vartype(ret->value) );
el->flag = RETURN_F;
store(symbol_tables[PROGRAM_ST], name, FUNCTION_T);
parse_id(p->op[0]);
for(i = 0; i < n_args; i++)
if(parse_tree(args[i])) return 1;
return 0;
}
int parse_op(Node* p){ // +,-,*
if(parse_tree(p->op[0])) return 1;
if(parse_tree(p->op[1])) return 1;
if(!is_real_or_int(p->op[0]) || !is_real_or_int(p->op[1])){
print_op_error(p);
return 1;
}else if(is_int(p->op[0]) && is_int(p->op[1]))
p->op_type = INTEGER_T;
else
p->op_type = REAL_T;
return 0;
}
int parse_assign(Node* p){
element_t* r = fetch_id(p->op[0]);
if(r == NULL){
printf("Line %d, col %d: Symbol %s not defined\n", p->loc.first_line, p->loc.first_column, p->op[0]->value2);
return 1;
}
p->op[0]->op_type = r->type;
if((!is_int(p->op[0]) && !is_real(p->op[0]) && !is_boolean(p->op[0])) || r->flag == CONSTANT_F){
print_variable_expected(p);
return 1;
}
if(parse_tree(p->op[1])) return 1;
if(is_int(p->op[0]) && !is_int(p->op[1]) || is_real(p->op[0]) && !is_real_or_int(p->op[1]) || is_boolean(p->op[0]) && !is_boolean(p->op[1]) ){
print_assign_error(p);
return 1;
}
return 0;
}
int parse_boolop(Node* p){ // or,and
if(parse_tree(p->op[0])) return 1;
if(parse_tree(p->op[1])) return 1;
if(!is_boolean(p->op[0]) || !is_boolean(p->op[1])){
print_op_error(p);
return 1;
}else
p->op_type = BOOLEAN_T;
return 0;
}
int parse_eq(Node* p){ // =,<>, <,>,<=,>=
if(parse_tree(p->op[0])) return 1;
if(parse_tree(p->op[1])) return 1;
if(!(is_real_or_int(p->op[0]) && is_real_or_int(p->op[1]) || is_boolean(p->op[0]) && is_boolean(p->op[1]))) {
print_op_error(p);
return 1;
}else
p->op_type = BOOLEAN_T;
return 0;
}
int parse_unary(Node* p){
if(parse_tree(p->op[0])) return 1;
if(!strcmp(p->type, "Not") ){
if(!is_boolean(p->op[0])){
print_unary_error(p);
return 1;
}else
p->op_type = BOOLEAN_T;
}else{
if(!is_real_or_int(p->op[0])){
print_unary_error(p);
return 1;
}else
p->op_type = p->op[0]->op_type;
}
return 0;
}
int parse_id(Node* p){
element_t * t = fetch(symbol_tables[st_pointer], p->value);
if(t != NULL){
p->op_type = t->type;
return 0;
}
t = fetch(symbol_tables[PROGRAM_ST], p->value);
if(t != NULL){
p->op_type = t->type;
return 0;
}
t = fetch(symbol_tables[OUTER_ST], p->value);
if(t != NULL){
p->op_type = t->type;
return 0;
}
printf("Line %d, col %d: Symbol %s not defined\n", p->loc.first_line, p->loc.first_column, p->value2);
return 1;
}
int parse_intop(Node* p){
if(parse_tree(p->op[0])) return 1;
if(parse_tree(p->op[1])) return 1;
if(!is_int(p->op[0]) || !is_int(p->op[1])){
print_op_error(p);
return 1;
}else
p->op_type = INTEGER_T;
return 0;
}
int parse_realop(Node* p){ // USED ONCE
if(parse_tree(p->op[0])) return 1;
if(parse_tree(p->op[1])) return 1;
if(is_boolean(p->op[0]) || is_boolean(p->op[1]) || is_type(p->op[0]) || is_type(p->op[1])){
print_op_error(p);
return 1;
}else
p->op_type = REAL_T;
return 0;
}
int parse_var(Node *p, type_t type, flag_t flag){
element_t *t = fetch(symbol_tables[st_pointer], p->value);
if(t != NULL){
print_already_def_error(p);
return 1;
}
t = store(symbol_tables[st_pointer], p->value, type);
t->flag = flag;
return 0;
}
int parse_if_while(Node* p){
if(parse_tree(p->op[0])) return 1;
if(!is_boolean(p->op[0])){
print_stat_error(p, p->op[0]->op_type, BOOLEAN_T);
return 1;
}
if(parse_tree(p->op[1])) return 1;
if(parse_tree(p->op[2])) return 1;
return 0;
}
int parse_repeat(Node* p){
if(parse_tree(p->op[0])) return 1;
if(parse_tree(p->op[1])) return 1;
if(parse_tree(p->op[2])) return 1;
if(!is_boolean(p->op[1])){
print_stat_error(p, p->op[1]->op_type, BOOLEAN_T);
return 1;
}
return 0;
}
int parse_valparam(Node* p){
if(parse_tree(p->op[0])) return 1;
if(parse_id(p->op[1])) return 1;
if(!is_int(p->op[0]) ){
print_valparam_error(p->op[0], p->op[0]->op_type, INTEGER_T);
return 1;
}
if(!is_int(p->op[1])){
print_valparam_error(p->op[1], p->op[1]->op_type, INTEGER_T);
return 1;
}
return 0;
}
int parse_decl(Node* p, flag_t flag){
if(!type_is_valid(p->op[p->n_op-1]->value)){
if(fetch_id(p->op[p->n_op-1]) != NULL)
printf("Line %d, col %d: Symbol %s not defined\n", p->op[p->n_op-1]->loc.first_line, p->op[p->n_op-1]->loc.first_column, p->op[p->n_op-1]->value2);
else
printf("Line %d, col %d: Type identifier expected\n", p->op[p->n_op-1]->loc.first_line, p->op[p->n_op-1]->loc.first_column);
return 1;
}
type_t type = vartype(p->op[p->n_op-1]->value);
int i;
for(i = 0; i < p->n_op-1; i++)
if(parse_var(p->op[i], type, flag)) return 1;
return 0;
}
int parse_call(Node* p){
int f_st = fetch_func(p->op[0]->value), i;
if(parse_id(p->op[0])) return 1;
if(f_st == -1){
printf("Line %d, col %d: Function identifier expected\n", p->loc.first_line, p->loc.first_column);
return 1;
}
for(i=1; i<p->n_op; i++)
if(parse_tree(p->op[i])) return 1;
symbol_tables[f_st]->next[0];
int n_args_def = 0;
type_t types[TABLE_SIZE];
flag_t flags[TABLE_SIZE];
element_t** el;
for(el = symbol_tables[f_st]->next+1; el != symbol_tables[f_st]->last; ++el){
flag_t flag = (*el)->flag;
if(flag != VARPARAM_F && flag != PARAM_F)
break;
types[n_args_def] = (*el)->type;
flags[n_args_def] = (*el)->flag;
n_args_def++;
}
if(p->n_op-1 != n_args_def){
printf("Line %d, col %d: Wrong number of arguments in call to function %s (got %d, expected %d)\n",
p->loc.first_line, p->loc.first_column, p->op[0]->value2, p->n_op-1, n_args_def);
return 1;
}
for(i=0; i<n_args_def; i++){
type_t t1 = types[i], t2 = p->op[i+1]->op_type;
flag_t f1 = flags[i];
if(f1 == PARAM_F){
if(t1 == INTEGER_T && t2 != INTEGER_T || t1 == BOOLEAN_T && t2 != BOOLEAN_T || t1 == REAL_T && t2 == BOOLEAN_T){
printf("Line %d, col %d: Incompatible type for argument %d in call to function %s (got %s, expected %s)\n",
p->op[i+1]->loc.first_line, p->op[i+1]->loc.first_column, i+1, p->op[0]->value2, type2string(t2), type2string(t1));
return 1;
}
}else{
if(strcmp(p->op[i+1]->type, "Id") || fetch_id(p->op[i+1])->flag == CONSTANT_F || t1 == INTEGER_T && t2 != INTEGER_T || t1 == BOOLEAN_T && t2 != BOOLEAN_T || t1 == REAL_T && t2 != REAL_T){
print_variable_expected(p->op[i+1]);
return 1;
}
}
}
type_t func_type = symbol_tables[f_st]->next[0]->type;
p->op_type = func_type;
return 0;
}
int parse_writeln(Node *p){
int i;
for(i=0; i<p->n_op; i++)
if(parse_tree(p->op[i])) return 1;
for(i=0; i<p->n_op; i++){
if(!is_boolean(p->op[i]) && !is_int(p->op[i]) && !is_real(p->op[i]) && !is_string(p->op[i]) ){
print_writeln_error(p->op[i]);
return 1;
}
}
return 0;
}
int parse_tree(Node* p){
int stp_backup, i;
stp_backup = st_pointer;
if(p == NULL)
return 0;
if(strcmp(p->type, "Program") == 0){
st_pointer = st_size++;
symbol_tables[st_pointer] = new_hashtable(TABLE_SIZE, "Program");
PROGRAM_ST = st_pointer;
for(i = 1; i < p->n_op; i++)
if(parse_tree(p->op[i])) return 1;
}else if(strcmp(p->type, "FuncDef") == 0){
if(parse_funchead(p, p->n_op-3, p->op+1, p->op[p->n_op-3])) return 1;
if(parse_tree(p->op[p->n_op-2])) return 1;
if(parse_tree(p->op[p->n_op-1])) return 1;
}else if(strcmp(p->type, "FuncDef2") == 0){
element_t* el = fetch(symbol_tables[PROGRAM_ST], p->op[0]->value);
if(el == NULL){
printf("Line %d, col %d: Function identifier expected\n", p->op[0]->loc.first_line, p->op[0]->loc.first_column);
return 1;
}
else if(el->flag != FUNCDECL_F){
print_already_def_error(p->op[0]);
return 1;
}
st_pointer = fetch_func(p->op[0]->value);
if(st_pointer == -1){
printf("Line %d, col %d: Function identifier expected\n", p->op[0]->loc.first_line, p->op[0]->loc.first_column);
return 1;
}
el->flag = NONE_F;
parse_id(p->op[0]);
for(i = 1; i < p->n_op; i++)
if(parse_tree(p->op[i])) return 1;
}else if(strcmp(p->type, "FuncDecl") == 0){
if(parse_funchead(p, p->n_op-2, p->op+1, p->op[p->n_op-1])) return 1;
element_t *el = fetch(symbol_tables[PROGRAM_ST], p->op[0]->value);
el->flag = FUNCDECL_F;
}else if(strcmp(p->type, "Params") == 0){
return parse_decl(p, PARAM_F);
}else if(strcmp(p->type, "VarParams") == 0){
return parse_decl(p, VARPARAM_F);
}else if(strcmp(p->type, "VarDecl") == 0){
return parse_decl(p, NONE_F);
}else if(!strcmp(p->type, "Add") || !strcmp(p->type, "Sub") || !strcmp(p->type, "Mul") ){
return parse_op(p);
}else if(!strcmp(p->type, "Or") || !strcmp(p->type, "And") ){
return parse_boolop(p);
}else if(!strcmp(p->type, "Lt") || !strcmp(p->type, "Gt") || !strcmp(p->type, "Leq") || !strcmp(p->type, "Geq") || !strcmp(p->type, "Eq") || !strcmp(p->type, "Neq") ){
return parse_eq(p);
}else if(!strcmp(p->type, "Minus") || !strcmp(p->type, "Plus") || !strcmp(p->type, "Not") ){
return parse_unary(p);
} else if(!strcmp(p->type, "RealDiv")){
return parse_realop(p);
}else if(!strcmp(p->type, "Div") || !strcmp(p->type, "Mod")){
return parse_intop(p);
}else if(!strcmp(p->type, "IntLit") ){
p->op_type = INTEGER_T;
}else if(!strcmp(p->type, "RealLit")){
p->op_type = REAL_T;
}else if(!strcmp(p->type, "Id")){
if(parse_id(p)) return 1;
element_t* id = fetch_id(p);
if(p->op_type == FUNCTION_T || id->flag == RETURN_F){
p->op = (Node **) malloc(sizeof(Node *));
p->op[0] = new_node();
p->n_op = 1;
p->op[0]->value = (char*) strdup(p->value);
p->op[0]->value2 = (char*) strdup(p->value2);
p->op[0]->type = (char*) strdup("Id");
p->type = (char*) strdup("Call");
p->value = NULL;
if(parse_call(p)) return 1;
}
}else if(!strcmp(p->type, "Assign")){
return parse_assign(p);
}else if(!strcmp(p->type, "IfElse") || !strcmp(p->type, "While")){
return parse_if_while(p);
}else if(!strcmp(p->type, "Repeat")){
return parse_repeat(p);
}else if(!strcmp(p->type, "ValParam")){
return parse_valparam(p);
}else if(!strcmp(p->type, "Call")){
return parse_call(p);
}else if(!strcmp(p->type, "WriteLn")){
return parse_writeln(p);
}else if(!strcmp(p->type, "String")){
p->op_type = NONE_T;
}else{
for(i = 0; i < p->n_op; i++)
if(parse_tree(p->op[i])) return 1;
}
st_pointer = stp_backup;
return 0;
}