-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement_parser.c
452 lines (388 loc) · 9.7 KB
/
statement_parser.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
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
#include "statement_parser.h"
struct node_t* parse_code(const char* exprs){
struct lex_array_t lexarr = lex_string(&exprs); //Reading expression (chars -> lexems)
if(!lexarr.lexems){
return NULL;
}
printf("\nParsing...\n");
struct node_t* right, *left, *node;
struct lexem_t* current = lexarr.lexems;
left = parse_block(¤t);
if(!left){
printf("Parse failed!\n");
return NULL;
}
node = left;
while(!is_end(*current)){ //doing this till END lexem
node = create_empty_node();
node->right = parse_block(¤t);
if(!node->right){
free_syntax_tree(node);
printf("Parse failed!\n");
return NULL;
}
node->left = left;
left = node;
}
free_lexarray(lexarr);
printf("Parsed!\n");
return node;
}
void skip_to_next_rcurv(struct lexem_t **lex){
int count = 1;
*lex = *lex + 1;
while(count > 0){
if((**lex).kind == BRACE)
switch((**lex).lex.b){
case RCURV: count--; break;
case LCURV: count++; break;
default: break;
}
if((**lex).kind == END)
return;
*lex = *lex + 1;
}
}
void skip_to_next_rbrac(struct lexem_t **lex){
int count = 1;
*lex = *lex + 1;
while(count > 0){
if((**lex).kind == BRACE)
switch((**lex).lex.b){
case RBRAC: count--; break;
case LBRAC: count++; break;
default: break;
}
if((**lex).kind == END)
return;
*lex = *lex + 1;
}
}
//block ::= { statement; statement; ...} | statement
struct node_t* parse_block(struct lexem_t **lex){
struct node_t* ret = create_empty_node();
if(is_lcurv_brace(**lex)){
*lex = *lex + 1;
while(!is_rcurv_brace(**lex)){
if((**lex).kind == END){
free_syntax_tree(ret);
return NULL;
}
struct node_t* right = parse_statement(lex);
if(!right){
free_syntax_tree(ret);
return NULL;
}
struct node_t* new_node = create_empty_node();
new_node->left = ret;
new_node->right = right;
ret = new_node;
}
*lex = *lex + 1;
return ret;
}
else
return parse_statement(lex);
}
// condition ::= (expression)
struct node_t* parse_condition(struct lexem_t **lex){
if(!is_brace(**lex)){
printf("Expected '(', found: '"); print_lexem(**lex); printf("'\n");
return NULL;
}
struct lexem_t* enter_point = *lex;
skip_to_next_rbrac(lex);
*lex = *lex - 1;
if(!is_valid_expression(enter_point, *lex))
return NULL;
if(!is_brace(**lex)){
printf("Missing ')'\n");
return NULL;
}
*lex = *lex - 1;
return build_syntax_tree(lex);
}
// while :: = while condition [*true* block\statement]
struct node_t* parse_while(struct lexem_t **lex){
/*
****WHILE NODE***
->left node contains condition expression
->right node contains block of statements
*/
struct node_t* left = parse_condition(lex);
if(!left)
return NULL;
*lex = *lex + 1;
struct node_t* right = parse_block(lex);
if(!right){
free_syntax_tree(left);
return NULL;
}
struct node_t* ret = create_empty_node();
ret->data.k = NODE_KEYWORD;
ret->data.u.word = WHILE;
ret->left = left;
ret->right = right;
return ret;
}
// if :: = < if condition [*true* block\statement] > + < else [*false* block\statement] > (false block is optional)
struct node_t* parse_if(struct lexem_t **lex){
/*
*******IF NODE*****
->left node contains condition expression
->right one lead to the "true block" with the ->right node and to the "false block" with ->left
*/
struct node_t* left = parse_condition(lex);
if(!left)
return NULL;
*lex = *lex + 1;
struct node_t* right = create_empty_node();
struct node_t* right_right = parse_block(lex);
struct node_t* right_left;
if(is_else(**lex)){
*lex = *lex + 1;
right_left = parse_block(lex);
}
else
right_left = create_empty_node();
right->left = right_left;
right->right = right_right;
if(!right_right || !right_left){
free_syntax_tree(left);
free_syntax_tree(right);
return NULL;
}
struct node_t* ret = create_empty_node();
ret->data.k = NODE_KEYWORD;
ret->data.u.word = IF;
ret->left = left;
ret->right = right;
return ret;
}
// print :: = print var/num/all;
struct node_t* parse_print(struct lexem_t **lex){
/*
*****PRINT NODE*****
->left node contaions info to print
*/
struct node_t* ret = create_empty_node();
ret->data.k = NODE_KEYWORD;
ret->data.u.word = PRINT;
struct node_t* left = create_empty_node();
ret->left = left;
switch((**lex).kind){
case VAR:{
free(left);
left = create_var_node(**lex);
ret->left = left;
break;
}
case NUM:{
left->data.k = NODE_VAL;
left->data.u.d = (**lex).lex.num;
break;
}
case KEYWORD:{
switch((**lex).lex.word){
case ALL:{
left->data.k = NODE_KEYWORD;
left->data.u.word = ALL;
break;
}
default:{
printf("Unexpected keyword '"); print_lexem(**lex); printf("' after 'print'\n");
free_syntax_tree(ret);
return NULL;
}
}
break;
}
default:{
printf("Expected variable or num after 'print'\n");
free_syntax_tree(ret);
return NULL;
}
}
*lex = *lex + 1;
if(!is_semicolon(**lex)){
printf("Missing ';' after 'print' expression\n");
free_syntax_tree(ret);
return NULL;
}
*lex = *lex + 1;
return ret;
}
// out :: = out var; (var used just to contain name of the file)
struct node_t* parse_out(struct lexem_t **lex){
/*
*****OUT NODE*****
->left node contains name of the file
*/
struct node_t* ret = create_empty_node();
ret->data.k = NODE_KEYWORD;
ret->data.u.word = OUT;
struct node_t* left = create_var_node(**lex);
if(!left){
printf("FUCK\n");
free(ret);
return NULL;
}
ret->left = left;
*lex = *lex + 1;
if(!is_semicolon(**lex)){
printf("Missing ';' after 'out' expression\n");
free_syntax_tree(ret);
return NULL;
}
*lex = *lex + 1;
return ret;
}
//assignation :: = VAR = expression;
struct node_t* parse_assignation(struct lexem_t **lex){
/*
****ASSIGNATION NODE*****
->left node contains name of the variable;
->right node contains the expression that will be assigned to this variable
*/
struct node_t* left = create_var_node(**lex);
*lex = *lex + 1;
if(!is_assign(**lex)){
printf("Expected '=' after '%s', found: '", left->data.u.name);
print_lexem(**lex);
printf("'\n");
free_syntax_tree(left);
return NULL;
}
struct lexem_t* enter_point = *lex + 1;
go_to_semicolon(lex);
if(!is_valid_expression(enter_point, *lex)){
free_syntax_tree(left);
return NULL;
}
*lex = *lex - 1;
if(is_end(**lex)){
free_syntax_tree(left);
return NULL;
}
struct node_t* right = build_syntax_tree(lex);
if(!right){
free_syntax_tree(left);
return NULL;
}
struct node_t* ret = create_empty_node();
ret->data.k = NODE_ASSIGN;
ret->left = left;
ret->right = right;
*lex = *lex + 1;
return ret;
}
// statement :: = if | while | assignation | print | block | END | semicolon
struct node_t* parse_statement(struct lexem_t **lex){
switch((**lex).kind){
case VAR:{
return parse_assignation(lex);
}
case KEYWORD:{
switch((**lex).lex.word){
case WHILE:{
*lex = *lex + 1;
return parse_while(lex);
}
case IF:{
*lex = *lex + 1;
return parse_if(lex);
}
case PRINT:{
*lex = *lex + 1;
return parse_print(lex);
}
case OUT:{
*lex = *lex + 1;
return parse_out(lex);
}
default:{
printf("Unexpected keyword: ");
print_lexem(**lex);
printf("\n");
return NULL;
}
}
}
case BRACE:{
switch((**lex).lex.b){
case LCURV:{
return parse_block(lex);
}
default:{
printf("Unexpected brace: ");
print_lexem(**lex);
printf("\n");
return NULL;
}
}
}
case END:{
return create_empty_node();
}
default: {
printf("Unexpected lexem: ");
print_lexem(**lex);
printf("\n");
return NULL;
}
}
}
struct node_t* create_empty_node(){
/*
****EMPTY NODE****
->left node contains statement that should be done first
->right node contains statement taht should be done after ->left
*/
struct node_t* empty = (struct node_t*)calloc(1, sizeof(struct node_t));
empty->data.k = NODE_NONE;
return empty;
}
struct node_t* create_var_node(struct lexem_t lex){
if(lex.kind != VAR){
return NULL;
}
struct node_t* ret = create_empty_node();
ret->data.k = NODE_VAR;
ret->data.u.name = (char*)calloc(1, sizeof(char));
ret->data.u.name = strcpy(ret->data.u.name, lex.lex.name);
return ret;
}
int is_else(struct lexem_t lex){
return (lex.kind == KEYWORD && lex.lex.word == ELSE) ? 1:0;
}
int is_lcurv_brace(struct lexem_t lex){
return (lex.kind == BRACE && lex.lex.b == LCURV) ? 1:0;
}
int is_rcurv_brace(struct lexem_t lex){
return (lex.kind == BRACE && lex.lex.b == RCURV) ? 1:0;
}
int is_keyword(struct lexem_t lex){
return (lex.kind == KEYWORD) ? 1:0;
}
int is_valid_expression(struct lexem_t* start,struct lexem_t* stopsign){ // check if the lexems between 'start' and 'stopsign' satisfy dry expression (without any statement operands and keywords)
if(!stopsign)
return 0;
while(start != stopsign){
if(!is_lcurv_brace(*start) && !is_lcurv_brace(*start) && !is_assign(*start)
&& !is_semicolon(*start) && !is_keyword(*start)){
start = start + 1;
continue;
}
else{
printf("Unexpected '"); print_lexem(*start); printf("' in the middle of expression\n");
return 0;
}
}
return 1;
}
void go_to_semicolon(struct lexem_t** lex){
while(!is_semicolon(**lex) && !is_end(**lex)){
//print_lexem(**lex);
*lex = *lex + 1;
}
}