-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
976 lines (772 loc) · 24.9 KB
/
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
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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
//
// main.c
//
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
/*
http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm
Informal grammar:
+++++++++++++++++
S ::= STMT_LIST$
STMT_LIST ::= STMT STMT_LIST | EPSILON
STMT ::= id = E | id = STRING;
STMT ::= def();
STMT ::= >> EX; | >> STRING; | >> def()
STMT ::= is EX ( stmt_list )
STMT ::= times EXPR ( STMT_LIST )
STMT ::= def ID1 ID2 IDn ( STMT_LIST )
STRING ::= ""
EX ::= E cmp E
Expressions:
+++++++++++++
E --> P A
A -> BPA | Epsilon
P --> V | "(" E ")" | U P
B --> "+" | "-" | "*" | "/" | "^"
U --> "-"
V = id | number
FIRST(E) = id number ( -
FIRST(A) = Epsilon + - * / ^
FOLLOW(A) = TOK_SEMICOLON, TOK_PAREN_OPEN, TOK_CMP
FIRST(P) = id number ( -
FIRST(B) = + - * / ^
FIRST(U) = -
FIRST(V) = id number
*/
#include "scanner/scanner.h"
#include <stdlib.h>
#include <stdio.h>
#include "queue.h"
#include "stack.h"
#include <string.h>
#include <assert.h>
#include "hash_table.h"
// constants associativity
#define LEFT 0
#define RIGHT 1
enum types {
AST_STMT_LIST, AST_ASSIGNMENT, AST_PRINT, AST_IS, AST_CMP,AST_TIMES, AST_FUNC_DEF, AST_FUNC_CALL
};
struct Binary {
short sentinel;
Token *token;
};
// the current lookahead-terminal
static Token *lookahead;
// nodes needed for evaluation of expressions
struct NExpr {
struct Binary *operator;
struct NExpr *left;
struct NExpr *right;
Token *value;
char *symkey;
};
struct NAssignment {
Token *lvalue;
int rvalue;
};
// AST nodes
typedef struct {
int type;
void *ref;
} Node;
typedef struct {
Token *lvalue;
struct NExpr *expr;
char *string;
} NodeAssign;
typedef struct {
struct NExpr *expr;
char *string;
} NodePrint;
typedef struct {
Token *cmp;
struct NExpr *expr1;
struct NExpr *expr2;
Node *then;
} NodeCmp;
typedef struct {
QHandle *stmts;
} NodeStmtList;
typedef struct {
struct NExpr *times;
Node *block;
} NodeTimes;
typedef struct {
Token *id;
HState *local_sym;
Node *body;
} NodeFuncDef;
typedef struct {
Token *id;
} NodeFuncCall;
void syntax_error(int tok);
Token *consume_token(void);
void match(int token);
void test_tokens(void);
void parse_start(void);
void parse_stmt_list(NodeStmtList *stmt_list);
void parse_assignment(NodeStmtList *stmt_list);
void parse_print(NodeStmtList *stmt_list);
void parse_is(NodeStmtList *stmt_list);
void parse_expr(void);
void parse_times(NodeStmtList *stmt_list);
void parse_func_def(NodeStmtList *stmt_list);
void parse_func_call(NodeStmtList *stmt_list);
void parse_a(void);
void parse_p(void);
void parse_b(void);
void parse_u(void);
void parse_v(void);
struct NExpr *shunting_yard(void);
short prec_for_binop(struct Binary *binary);
short assoc_for_binop(struct Binary *binary);
void stack_test_callback(void *it);
struct NExpr *make_leaf(Token *tok, char *symkey);
struct NExpr *make_node(struct Binary *binary, struct NExpr *e0, struct NExpr *e1);
void push_operator(struct Binary *binary, SHandle *operators, SHandle *operands);
void pop_operator(SHandle *operators, SHandle *operands);
struct Binary *make_binary(short is_sentinel, Token *token);
int interpret_expr(struct NExpr *expr);
void debug_expr_ast(struct NExpr *expr);
void interpret_node(Node *node);
char *string_replace(char *search, char *replace, char *string);
// toplevel statements
Node *toplevel;
// state of the queue which is used as input for shunting yard
QHandle *expr_queue_state;
// TODO: collect symbols in an "environment-thing"
// symbol table for variables
HState *symtab;
// symbol tables for functions
HState *symtab_funcs;
int main(int argc, char **args) {
char *source = "/Users/tom/tom/NetBeansProjects/xcode/C-Progr/BinTree/BinTree/example.strike";
init_scanner(source);
// test_tokens();
// exit(0);
NodeStmtList *stmt_list = (NodeStmtList *) malloc(sizeof(NodeStmtList));
stmt_list->stmts = queue_new();
toplevel = malloc(sizeof(Node));
toplevel->type = AST_STMT_LIST;
toplevel->ref = stmt_list;
symtab = hash_new();
symtab_funcs = hash_new();
consume_token();
parse_start();
printf("\nsyntax ok\n");
return EXIT_SUCCESS;
}
short prec_for_binop(struct Binary *binary) {
short result;
if(binary->sentinel == 1)
return 0;
switch (binary->token->lexem_one[0]) {
case '-':
case '+':
result = 10;
break;
case '*':
case '/':
result = 20;
break;
default:
printf("fehler, prec_for_binop: %c\n", binary->token->lexem_one[0]);
}
return result;
}
short assoc_for_binop(struct Binary *binary) {
short result;
if(binary->sentinel == 1)
return LEFT;
switch (binary->token->lexem_one[0]) {
case '-':
case '+':
case '/':
case '*':
result = LEFT;
break;
case '^':
result = RIGHT;
break;
default:
printf("fehler, assoc_for_binop\n");
}
return result;
}
struct NExpr *shunting_yard() {
/*
// TEST-SUITE, Queue
queue_enqueue(output, "tom");
queue_enqueue(output, "franz");
queue_enqueue(output, "hoefer");
assert(strcmp(queue_dequeue(output), "tom") == 0);
assert(strcmp(queue_dequeue(output), "franz") == 0);
assert(strcmp(queue_dequeue(output), "hoefer") == 0);
assert(queue_dequeue(output) == NULL);
assert(queue_dequeue(output) == NULL);
assert(queue_dequeue(output) == NULL);
queue_enqueue(output, "munich");
queue_enqueue(output, "new york");
assert(queue_dequeue(output) == NULL);
assert(queue_dequeue(output) == NULL);
printf("queue ok \n");
return;
*/
/*
// TEST-SUITE, STACK
stack_push(ops, "test");
stack_push(ops, "tom");
stack_push(ops, "hoefer");
assert(stack_is_empty(ops) == 0);
assert(strcmp(stack_top(ops), "hoefer") == 0);
assert(strcmp(stack_pop(ops), "hoefer") == 0);
assert(strcmp(stack_pop(ops), "tom") == 0);
assert(strcmp(stack_top(ops), "test") == 0);
stack_push(ops, "a");
stack_push(ops, "b");
assert(strcmp(stack_top(ops), "b") == 0);
assert(strcmp(stack_pop(ops), "b") == 0);
stack_pop(ops);
assert(stack_is_empty(ops) == 0);
assert(strcmp(stack_pop(ops), "test") == 0);
stack_pop(ops);
stack_pop(ops);
assert(stack_is_empty(ops) == 1);
printf("stack ok\n");
return;
*/
Token *input;
SHandle *operators = stack_new();
SHandle *operands = stack_new();
stack_push(operators, make_binary(1, NULL)); // == sentinel
input = queue_dequeue(expr_queue_state);
if(input->tok_type == TOK_NUMBER)
stack_push(operands, make_leaf(input, NULL));
else if(input->tok_type == TOK_ID)
stack_push(operands, make_leaf(NULL, input->lexem_one));
while((input = queue_dequeue(expr_queue_state)) != NULL) {
if(input->tok_type == TOK_BINOP) {
push_operator(make_binary(0, input), operators, operands);
input = queue_dequeue(expr_queue_state);
if(input->tok_type == TOK_NUMBER)
stack_push(operands, make_leaf(input, NULL));
else if(input->tok_type == TOK_ID)
stack_push(operands, make_leaf(NULL, input->lexem_one));
}
}
struct Binary *top;
while((top = stack_top(operators)) != NULL && top->sentinel != 1) {
pop_operator(operators, operands);
}
struct NExpr *tos = stack_top(operands);
return (tos);
// printf("result: %i", interpret(tos));
// debug_expr_ast(tos);
}
struct Binary *make_binary(short is_sentinel, Token *token) {
struct Binary *sentinel = (struct Binary *) malloc(sizeof(struct Binary));
if(sentinel == NULL) {
printf("no memory available");
}
sentinel->sentinel = is_sentinel;
sentinel->token = token;
return sentinel;
}
int interpret_expr(struct NExpr *expr) {
if(expr->value != NULL) {
return atoi(expr->value->lexem_one);
} else if (expr->symkey != NULL) {
int value = hash_lookup(symtab, expr->symkey);
return value;
} else {
switch (expr->operator->token->lexem_one[0]) {
case '+':
return interpret_expr(expr->left) + interpret_expr(expr->right);
break;
case '-':
return interpret_expr(expr->left) - interpret_expr(expr->right);
break;
case '*':
return interpret_expr(expr->left) * interpret_expr(expr->right);
break;
case '/':
return interpret_expr(expr->left) / interpret_expr(expr->right);
break;
}
}
}
void debug_expr_ast(struct NExpr *expr) {
if(expr->value != NULL) {
printf(expr->value->lexem_one);
} else {
printf("(");
debug_expr_ast(expr->left);
printf(" %s ", expr->operator->token->lexem_one);
debug_expr_ast(expr->right);
printf(")");
}
}
struct NExpr *make_leaf(Token *tok, char *symkey) {
struct NExpr *leaf = (struct NExpr *) malloc(sizeof(struct NExpr));
if(NULL != tok) {
leaf->value = tok;
} else if(NULL != symkey) {
leaf->symkey = symkey;
}
return leaf;
}
struct NExpr *make_node(struct Binary *binary, struct NExpr *e0, struct NExpr *e1) {
struct NExpr *node = (struct NExpr *) malloc(sizeof(struct NExpr));
if(NULL == node) {
printf("mem error make_node");
}
node->left = e0;
node->right = e1;
node->operator = binary;
return node;
}
void push_operator(struct Binary *binary, SHandle *operators, SHandle *operands) {
struct Binary *top;
while (prec_for_binop((top = stack_top(operators))) > prec_for_binop(binary) || (assoc_for_binop(top) == LEFT && prec_for_binop(top) == prec_for_binop(binary))) {
pop_operator(operators, operands);
}
stack_push(operators, binary);
}
void pop_operator(SHandle *operators, SHandle *operands) {
struct NExpr *expr1 = stack_pop(operands);
struct NExpr *expr2 = stack_pop(operands);
stack_push(operands, make_node(stack_pop(operators), expr2, expr1));
}
void stack_test_callback(void *it) {
Token *token = it;
printf("token: %s, lexem: %s\n", tok_type_tostring(token->tok_type), token->lexem_one);
}
void interpret_node(Node *node) {
NodeStmtList *stmt_list;
NodePrint *print;
NodeAssign *assign;
NodeCmp *cmp;
NodeTimes *times;
NodeFuncDef *func_def;
NodeFuncCall *func_call;
Node *current;
switch (node->type) {
case AST_STMT_LIST:
stmt_list = node->ref;
while ((current = queue_dequeue(stmt_list->stmts)) != NULL) {
interpret_node(current);
}
break;
case AST_PRINT:
print = (NodePrint *) node->ref;
if(print->expr != NULL) {
printf("%i\n", interpret_expr(print->expr));
} else {
char *mystring = print->string;
char *result = malloc(sizeof(char) * strlen(mystring));
char *final = result;
while (*mystring != '\0') {
if(*mystring == '#' && *++mystring == '{') {
while (*++mystring != '}') {
*result++ = *mystring;
}
*result = '\0';
break;
}
mystring++;
}
char *string = hash_lookup(symtab, final);
char *out = calloc(sizeof(char), strlen(final) + 3);
strcat(out, "#{");
strcat(out, final);
strcat(out, "}");
printf("%s\n", string_replace(out, string, print->string));
}
break;
case AST_ASSIGNMENT:
assign = node->ref;
if(assign->expr != NULL) {
hash_add(symtab, assign->lvalue->lexem_one, interpret_expr(assign->expr));
int result = hash_lookup(symtab, assign->lvalue->lexem_one);
//printf("ast_assignment (expr): %i \n", result);
} else {
hash_add(symtab, assign->lvalue->lexem_one, assign->string);
//printf("ast_assignment (string): %s \n", assign->string);
}
break;
case AST_CMP:
cmp = node->ref;
if(strcmp("==", cmp->cmp->lexem_one) == 0) {
if (interpret_expr(cmp->expr1) == interpret_expr(cmp->expr2)) {
interpret_node(cmp->then);
}
}
break;
case AST_TIMES:
times = node->ref;
int i;
int iter = interpret_expr(times->times);
NodeStmtList *stmts = times->block->ref;
for (i = 1; i <= iter; i++) {
interpret_node(times->block);
queue_reset(stmts->stmts);
}
break;
case AST_FUNC_DEF:
func_def = node->ref;
hash_add(symtab_funcs, func_def->id->lexem_one, func_def);
break;
case AST_FUNC_CALL:
func_call = node->ref;
NodeFuncDef *func_def = hash_lookup(symtab_funcs, func_call->id->lexem_one);
if(func_def == NULL) {
printf("sematic error - trying to call non-existant function");
exit(EXIT_FAILURE);
}
interpret_node(func_def->body);
break;
default:
break;
}
}
Node *generate_node(int type, void *ref) {
Node *node = (Node *) malloc(sizeof(Node));
if(NULL == node)
printf("mem error at generate_node");
node->type = type;
node->ref = ref;
return node;
}
void parse_start() {
parse_stmt_list(toplevel->ref);
match(TOK_TERMINATE);
// AST creation finished!
interpret_node(toplevel);
}
void parse_stmt_list(NodeStmtList *stmt_list) {
switch (lookahead->tok_type) {
case TOK_ID:
parse_assignment(stmt_list);
parse_stmt_list(stmt_list);
break;
case TOK_P:
parse_print(stmt_list);
parse_stmt_list(stmt_list);
break;
case TOK_IS:
parse_is(stmt_list);
parse_stmt_list(stmt_list);
break;
case TOK_TIMES:
parse_times(stmt_list);
parse_stmt_list(stmt_list);
break;
case TOK_FUNC_DEF:
parse_func_def(stmt_list);
parse_stmt_list(stmt_list);
break;
case TOK_FUNC_CALL:
parse_func_call(stmt_list);
parse_stmt_list(stmt_list);
break;
case TOK_PAREN_CLOSE:
case TOK_TERMINATE:
break;
default:
printf("error, tok_type is %s \n", tok_type_tostring(lookahead->tok_type));
}
}
void parse_func_call(NodeStmtList *stmt_list) {
Token *id;
switch (lookahead->tok_type) {
case TOK_FUNC_CALL:
id = lookahead;
match(TOK_FUNC_CALL);
NodeFuncCall *node_call = malloc(sizeof(NodeFuncCall));
node_call->id = id;
Node *node = generate_node(AST_FUNC_CALL, node_call);
queue_enqueue(stmt_list->stmts, node);
match(TOK_SEMICOLON);
break;
default:
break;
}
}
void parse_func_def(NodeStmtList *stmt_list) {
switch (lookahead->tok_type) {
case TOK_FUNC_DEF:
match(TOK_FUNC_DEF);
NodeFuncDef *node_fun = malloc(sizeof(NodeFuncDef));
node_fun->local_sym = hash_new();
node_fun->id = lookahead;
match(TOK_ID);
while (lookahead->tok_type != TOK_PAREN_OPEN) {
hash_add(node_fun->local_sym, lookahead->lexem_one, NULL);
match(TOK_ID);
}
match(TOK_PAREN_OPEN);
NodeStmtList *node_fun_block = malloc(sizeof(NodeStmtList));
node_fun_block->stmts = queue_new();
parse_stmt_list(node_fun_block);
match(TOK_PAREN_CLOSE);
node_fun->body = generate_node(AST_STMT_LIST, node_fun_block);
Node *node = generate_node(AST_FUNC_DEF, node_fun);
queue_enqueue(stmt_list->stmts, node);
break;
default:
break;
}
}
void parse_times(NodeStmtList *stmt_list) {
switch (lookahead->tok_type) {
case TOK_TIMES:
match(TOK_TIMES);
expr_queue_state = queue_new();
parse_expr();
struct NExpr *expr = shunting_yard();
match(TOK_PAREN_OPEN);
NodeStmtList *stmt_list_block = malloc(sizeof(NodeStmtList));
stmt_list_block->stmts = queue_new();
parse_stmt_list(stmt_list_block);
Node *node_block = generate_node(AST_STMT_LIST, stmt_list_block);
match(TOK_PAREN_CLOSE);
NodeTimes *node_times = malloc(sizeof(NodeTimes));
node_times->times = expr;
node_times->block = node_block;
Node *n = generate_node(AST_TIMES, node_times);
queue_enqueue(stmt_list->stmts, n);
break;
default:
break;
}
}
void parse_is(NodeStmtList *stmt_list) {
switch (lookahead->tok_type) {
case TOK_IS:
match(TOK_IS);
expr_queue_state = queue_new();
parse_expr();
struct NExpr *expr1 = shunting_yard();
Token *token_cmp = lookahead;
match(TOK_CMP);
expr_queue_state = queue_new();
parse_expr();
struct NExpr *expr2 = shunting_yard();
match(TOK_PAREN_OPEN);
NodeCmp *node_cmp = malloc(sizeof(NodeCmp));
node_cmp->cmp = token_cmp;
node_cmp->expr1 = expr1;
node_cmp->expr2 = expr2;
NodeStmtList *then_stmt_list = malloc(sizeof(NodeStmtList));
then_stmt_list->stmts = queue_new();
Node *then = generate_node(AST_STMT_LIST, then_stmt_list);
node_cmp->then = then;
Node *node = generate_node(AST_CMP, node_cmp);
parse_stmt_list(then_stmt_list);
queue_enqueue(stmt_list->stmts, node);
match(TOK_PAREN_CLOSE);
break;
default:
printf("error parse_is...");
}
}
void parse_print(NodeStmtList *stmt_list) {
switch (lookahead->tok_type) {
case TOK_P:
match(TOK_P);
if(lookahead->tok_type == TOK_STRING) {
char *string = lookahead->lexem_one;
match(TOK_STRING);
NodePrint *node_print = malloc(sizeof(NodePrint));
node_print->string = string;
Node *node = generate_node(AST_PRINT, node_print);
queue_enqueue(stmt_list->stmts, node);
match(TOK_SEMICOLON);
} else {
expr_queue_state = queue_new();
parse_expr();
struct NExpr *expr = shunting_yard();
match(TOK_SEMICOLON);
NodePrint *node_print = (NodePrint *) malloc(sizeof(NodePrint));
node_print->expr = expr;
Node *node = generate_node(AST_PRINT, node_print);
queue_enqueue(stmt_list->stmts, node);
}
break;
default:
break;
}
}
void parse_assignment(NodeStmtList *stmt_list) {
Token *lvalue;
switch (lookahead->tok_type) {
case TOK_ID:
lvalue = lookahead;
match(TOK_ID);
match(TOK_EQ);
if(lookahead->tok_type == TOK_STRING) {
char *string = lookahead->lexem_one;
match(TOK_STRING);
NodeAssign *node_assign = malloc(sizeof(NodeAssign));
node_assign->lvalue = lvalue;
node_assign->string = string;
Node *node = generate_node(AST_ASSIGNMENT, node_assign);
queue_enqueue(stmt_list->stmts, node);
match(TOK_SEMICOLON);
// id, number, (, - sollte im folgenden gematched werden
} else {
expr_queue_state = queue_new();
parse_expr();
struct NExpr *expr = shunting_yard();
match(TOK_SEMICOLON);
NodeAssign *node_assign = malloc(sizeof(NodeAssign));
node_assign->expr = expr;
node_assign->lvalue = lvalue;
Node *node = generate_node(AST_ASSIGNMENT, node_assign);
queue_enqueue(stmt_list->stmts, node);
}
break;
default:
syntax_error(TOK_ID);
}
}
// E --> P A
// FIRST(E) = id number ( -
void parse_expr() {
switch (lookahead->tok_type) {
case TOK_ID:
case TOK_NUMBER:
case TOK_PAREN_OPEN:
case TOK_MINUS:
parse_p();
parse_a();
break;
default:
printf("fehler parse_expr");
}
}
// Exp-Operator ^ noch nicht implementiert!!
// A -> BPA | Epsilon
// FIRST(A) = + - * / ^ Epsilon
// FOLLOW(A) = TOK_SEMICOLON, TOK_PAREN_OPEN, TOK_CMP
void parse_a() {
switch (lookahead->tok_type) {
case TOK_BINOP:
parse_b();
parse_p();
parse_a(); // rechtsrekursiver aufruf
break;
case TOK_SEMICOLON:
case TOK_PAREN_OPEN:
case TOK_CMP:
// Epsilon-Produktion d.h. lookahead nicht in FIRST(a), daher muss es in FOLLOW(a) in gültigem Satz sein
// Epsilon ist letztes Symbol der rechtsrekursiven Aufrufe
break;
default:
printf("fehler in parse_a, is: %s \n", tok_type_tostring(lookahead->tok_type));
}
}
// P --> V | "(" E ")" | U P
// FIRST(P) = id number ( -
void parse_p() {
switch (lookahead->tok_type) {
case TOK_ID:
case TOK_NUMBER:
parse_v();
break;
case TOK_PAREN_OPEN:
queue_enqueue(expr_queue_state, lookahead);
match(TOK_PAREN_OPEN);
parse_expr();
queue_enqueue(expr_queue_state, lookahead);
match(TOK_PAREN_CLOSE);
break;
case TOK_MINUS:
parse_u();
parse_p();
break;
default:
printf("fehler in parse_p");
}
}
// B --> "+" | "-" | "*" | "/" | "^"
void parse_b() {
switch (lookahead->tok_type) {
case TOK_BINOP:
queue_enqueue(expr_queue_state, lookahead);
match(TOK_BINOP);
break;
default:
syntax_error(TOK_BINOP);
}
}
// U --> "-"
void parse_u() {
switch (lookahead->tok_type) {
case TOK_MINUS:
queue_enqueue(expr_queue_state, lookahead);
match(TOK_MINUS);
break;
default:
syntax_error(TOK_MINUS);
}
}
// V = id | number
void parse_v() {
switch (lookahead->tok_type) {
case TOK_NUMBER:
queue_enqueue(expr_queue_state, lookahead);
match(TOK_NUMBER);
break;
case TOK_ID:
queue_enqueue(expr_queue_state, lookahead);
match(TOK_ID);
break;
default:
printf("fehler bei parse_v");
}
}
void test_tokens() {
for (Token *token = scanner(); token != NULL; token = scanner()) {
printf("typ: %s, lexem: %s\n", tok_type_tostring(token->tok_type), token->lexem_one);
}
}
void match(int token) {
if(lookahead->tok_type != token) {
syntax_error(token);
}
consume_token();
}
Token *consume_token() {
lookahead = scanner();
return lookahead;
}
void syntax_error(int token_expected) {
printf("-- Syntaxerror at %i : %i --\n", lookahead->line, lookahead->row);
printf("-- Message --\n");
printf("Is: %s, Expected: %s\n", tok_type_tostring(lookahead->tok_type), tok_type_tostring(token_expected));
exit(EXIT_FAILURE);
}
char * string_replace(char *search, char *replace, char *string) {
char *tempString, *searchStart;
int len=0;
// preuefe ob Such-String vorhanden ist
searchStart = strstr(string, search);
if(searchStart == NULL) {
return string;
}
// Speicher reservieren
tempString = (char*) malloc(strlen(string) * sizeof(char));
if(tempString == NULL) {
return NULL;
}
strcpy(tempString, string);
len = searchStart - string;
string[len] = '\0';
strcat(string, replace);
len += strlen(search);
strcat(string, (char*)tempString+len);
// Speicher freigeben
free(tempString);
return string;
}