-
Notifications
You must be signed in to change notification settings - Fork 13
/
parser.c
1698 lines (1387 loc) · 41.7 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
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include "parser.h"
/* Preliminary Definitions
* Each function `is_<TokenType>` is defined as
* int is_<TokenType> (struct TokenList** tok, struct ParseTree** new)
* - **tok points to the current token in the sequence. The function moves it ahead CHANGING the pointer location.
* - **new points to a ALREADY-ALLOCATED ParseTree. It will contain the resulting subtree, or stays empty.
* Therefore:
* - The caller must alloc the ParseTree** new
* - The called must assign *new->data, child and sibling
* - The caller is responsible for freeing memory
* Return a integer flag with the operation final status (see parser.h).
*/
int is_Obj (struct TokenList** tok, struct ParseTree** tree);
int is_List (struct TokenList** tok, struct ParseTree** tree);
int is_ListExpr (struct TokenList** tok, struct ParseTree** tree);
int is_Line(struct TokenList** tok, struct ParseTree** tree);
int is_Program(struct TokenList** head, struct ParseTree** tree);
int is_Expr(struct TokenList** head, struct ParseTree** tree);
int is_BaseExpr(struct TokenList** head, struct ParseTree** tree);
int is_Term(struct TokenList** head, struct ParseTree** tree);
struct ParseTree* alloc_ParseTree() {
struct ParseTree* tree;
tree = malloc(sizeof(struct ParseTree));
if (tree == NULL)
return NULL;
tree->data = NULL;
tree->child = NULL;
tree->sibling = NULL;
return tree;
}
struct ParseTree* new_ParseTree(struct Token* c) {
struct ParseTree* tree;
tree = alloc_ParseTree();
if (tree == NULL)
return NULL;
tree->data = new_Token(c->lexeme, c->type);
if (tree->data == NULL) {
free(tree);
return NULL;
}
tree->child = NULL;
tree->sibling = NULL;
return tree;
}
void print_PT(struct ParseTree* tree, int indent) {
if (tree == NULL)
return;
// This is a Depth-First print
if (indent > 0) {
char space[indent + 1];
memset(space, ' ', indent * sizeof(char));
memset(space+indent, '\0', sizeof(char));
printf("%s", space);
}
print_Token(tree->data);
struct ParseTree* sibling;
struct ParseTree* child;
sibling = tree->sibling;
child = tree->child;
print_PT(child, indent + 2);
print_PT(sibling, indent);
}
void print_ParseTree(struct ParseTree* tree) {
print_PT(tree, 0);
}
void free_ParseTree(struct ParseTree* tree) {
struct ParseTree* sibling;
struct ParseTree* child;
sibling = tree->sibling;
child = tree->child;
free_Token(tree->data);
free(tree);
if (sibling != NULL)
free_ParseTree(sibling);
if (child != NULL)
free_ParseTree(child);
}
/*
* Template function for easy-to-check Tokens
*
* Match the current Token in the TokenList** tok, with the given TokenType type.
* If char* lexeme != NULL, then also checks that the current Token's lexeme matches it.
*
* First, all WS are skipped. Then, if the check fails *tok unchanged. Otherwise it's advanced.
*
* It ASSUMES WS are already removed
*/
int __single_Token_template (struct TokenList** tok, struct ParseTree** new, enum TokenType type, char* lexeme, int hasEndline) {
if (*tok == NULL)
return PARSING_ERROR;
struct TokenList* current = *tok;
if (current->token->type != type) {
printf("Expecting <%s>, Found <%s>\n", type2char(type), type2char(current->token->type));
return PARSING_ERROR;
}
if (lexeme != NULL)
if(strcmp(current->token->lexeme, lexeme) != 0) {
printf("Expecting %s, Found %s\n", lexeme, current->token->lexeme);
return PARSING_ERROR;
}
// As by definition above, new is already allocated
struct Token* newTok = new_Token(current->token->lexeme, current->token->type);
if (newTok == NULL)
return MEMORY_ERROR;
(*new)->data = newTok;
(*new)->child = NULL;
(*new)->sibling = NULL;
*tok = current->next;
if (hasEndline && *tok==NULL){
printf("Did you forget a Endline Token (semicolon)?\n");
return PARSING_ERROR;
}
return SUBTREE_OK;
}
int _single_Token_template (struct TokenList** tok, struct ParseTree** new, enum TokenType type, char* lexeme) {
return __single_Token_template(tok, new, type, lexeme, 1);
}
int is_Endline (struct TokenList** tok, struct ParseTree** new) {
return __single_Token_template(tok, new, Endline, NULL, 0);
}
int is_Int (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Int, NULL);
}
int is_Dot (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Dot, NULL);
}
int is_Var (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Var, NULL);
}
int is_readIn (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, ReadIn, NULL);
}
int is_writeOut (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, WriteOut, "writeOut");
}
int is_If (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, If, "if");
}
int is_Else (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Else, "else");
}
int is_While (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, While, "while");
}
int is_Break (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Break, "break");
}
int is_Continue (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Continue, "continue");
}
int is_Lbrack (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Lbrack, NULL);
}
int is_Rbrack (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Rbrack, NULL);
}
int is_Lpar (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Lpar, NULL);
}
int is_Rpar (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Rpar, NULL);
}
int is_Comma (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Comma, NULL);
}
int is_EqEq (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, EqEq, NULL);
}
int is_Equal (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Equal, NULL);
}
int is_Plus (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Plus, NULL);
}
int is_Minus (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Minus, NULL);
}
int is_NotEq (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, NotEq, NULL);
}
int is_Greater (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Greater, NULL);
}
int is_GreaterEq (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, GreaterEq, NULL);
}
int is_Lesser (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Lesser, NULL);
}
int is_LesserEq (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, LesserEq, NULL);
}
int is_Star (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Star, NULL);
}
int is_Div (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Div, NULL);
}
int is_FloatDiv (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, FloatDiv, NULL);
}
int is_Percent (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Percent, NULL);
}
int is_Or (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Or, NULL);
}
int is_And (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, And, NULL);
}
int is_Pow (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Pow, NULL);
}
int is_Null (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Null, NULL);
}
int is_Bool (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, Bool, NULL);
}
int is_CharSeq (struct TokenList** tok, struct ParseTree** new) {
return _single_Token_template(tok, new, QuotedStr, NULL);
}
int is_QuotedStr (struct TokenList** tok, struct ParseTree** new) {
if (*tok == NULL)
return PARSING_ERROR;
struct ParseTree *charseq, *comma, *obj, *last;
int status, nVar, nObj;
struct Token *newTok;
status = SUBTREE_OK;
nVar = nObj = 0;
newTok = new_Token((char[1]){'\0'}, QuotedStr);
if (newTok == NULL)
return MEMORY_ERROR;
(*new)->data = newTok;
charseq = alloc_ParseTree();
if (charseq == NULL)
return MEMORY_ERROR;
status = is_CharSeq(tok, &charseq);
if (status != SUBTREE_OK) {
free_ParseTree(charseq);
return status;
}
(*new)->child = charseq;
last = charseq;
// Count the variables to interpolate
int i = 0;
while (charseq->data->lexeme[i] != '\0') {
if (charseq->data->lexeme[i] == '%') {
if (charseq->data->lexeme[i+1] == '%') // escape char
i += 2;
else if (charseq->data->lexeme[i+1] == 's') {
nVar += 1;
i += 2;
}
else i++;
}
else i++;
}
while ((*tok)->token->type == Comma) {
comma = alloc_ParseTree();
if (comma == NULL)
return MEMORY_ERROR;
status = is_Comma(tok, &comma);
if (status != SUBTREE_OK) {
free_ParseTree(comma);
break;
}
last->sibling = comma;
last = comma;
// Now there must be a Obj
obj = alloc_ParseTree();
if (obj == NULL)
return MEMORY_ERROR;
status = is_Obj(tok, &obj);
if (status != SUBTREE_OK) {
free_ParseTree(obj);
break;
}
last->sibling = obj;
last = obj;
nObj ++;
}
if (nObj != nVar) {
printf("QuotedStr with #obj != #interpolation (%d != %d)\n", nObj, nVar);
return PARSING_ERROR;
}
return status;
}
int is_CondOp (struct TokenList** tok, struct ParseTree** new) {
enum TokenType type;
type = (*tok)->token->type;
if (type == And)
return is_And(tok, new);
else if (type == Or)
return is_Or(tok, new);
else if (type == EqEq)
return is_EqEq(tok, new);
else if (type == NotEq)
return is_NotEq(tok, new);
else if (type == Greater)
return is_Greater(tok, new);
else if (type == GreaterEq)
return is_GreaterEq(tok, new);
else if (type == Lesser)
return is_Lesser(tok, new);
else if (type == LesserEq)
return is_LesserEq(tok, new);
else
return PARSING_ERROR;
}
int is_Operator (struct TokenList** tok, struct ParseTree** new) {
enum TokenType type;
type = (*tok)->token->type;
if (type == Plus)
return is_Plus(tok, new);
else if (type == Minus)
return is_Minus(tok, new);
else if (type == Star)
return is_Star(tok, new);
else if (type == Div)
return is_Div(tok, new);
else if (type == FloatDiv)
return is_FloatDiv(tok, new);
else if (type == Percent)
return is_Percent(tok, new);
else
return is_CondOp(tok, new);
}
int match_CondOp_type (enum TokenType type) {
return (type == EqEq ||
type == NotEq ||
type == Greater ||
type == GreaterEq ||
type == Lesser ||
type == LesserEq ||
type == And ||
type == Or);
}
int match_AritmOp_type (enum TokenType type) {
return (type == Plus ||
type == Minus ||
type == Star ||
type == Div ||
type == FloatDiv ||
type == Percent);
}
int match_TermOp_type (enum TokenType type) {
return (type == Star ||
type == Div ||
type == FloatDiv ||
type == Percent);
}
int match_operator_type (enum TokenType type) {
return (match_AritmOp_type(type) ||
match_CondOp_type(type));
}
int is_BaseExpr (struct TokenList** tok, struct ParseTree** new) {
if (*tok == NULL)
return PARSING_ERROR;
int status;
struct ParseTree *lpar, *rpar, *subexpr, *obj;
struct Token *newTok;
status = SUBTREE_OK;
newTok = new_Token((char[1]){'\0'}, BaseExpr);
if (newTok == NULL)
return MEMORY_ERROR;
(*new)->data = newTok;
if ((*tok)->token->type == Lpar) {
// is a sub expression
lpar = alloc_ParseTree();
if (lpar == NULL)
return MEMORY_ERROR;
status = is_Lpar(tok, &lpar);
if (status != SUBTREE_OK){
free_ParseTree(lpar);
return status;
}
(*new)->child = lpar;
subexpr = alloc_ParseTree();
if (subexpr == NULL)
return MEMORY_ERROR;
status = is_Expr(tok, &subexpr);
if (status != SUBTREE_OK){
free_ParseTree(subexpr);
return status;
}
lpar->sibling = subexpr;
rpar = alloc_ParseTree();
if (rpar == NULL)
return MEMORY_ERROR;
status = is_Rpar(tok, &rpar);
if (status != SUBTREE_OK){
free_ParseTree(rpar);
return status;
}
subexpr->sibling = rpar;
}
else {
// just a Obj
obj = alloc_ParseTree();
if (obj == NULL)
return MEMORY_ERROR;
status = is_Obj(tok, &obj);
if (status != SUBTREE_OK) {
free_ParseTree(obj);
return status;
}
(*new)->child = obj;
}
return status;
}
int is_Term (struct TokenList** tok, struct ParseTree** new) {
if (*tok == NULL)
return PARSING_ERROR;
int status;
enum TokenType type;
struct ParseTree *base, *op, *term;
struct Token *newTok;
status = SUBTREE_OK;
newTok = new_Token((char[1]){'\0'}, Term);
if (newTok == NULL)
return MEMORY_ERROR;
(*new)->data = newTok;
base = alloc_ParseTree();
if (base == NULL)
return MEMORY_ERROR;
status = is_BaseExpr(tok, &base);
if (status != SUBTREE_OK){
free_ParseTree(base);
return status;
}
(*new)->child = base;
// The remainder of a Term is optional
type = (*tok)->token->type;
if (match_TermOp_type(type)){
op = alloc_ParseTree();
if (op == NULL)
return MEMORY_ERROR;
status = is_Operator(tok, &op);
if (status != SUBTREE_OK){
free_ParseTree(op);
return status;
}
base->sibling = op;
term = alloc_ParseTree();
if (term == NULL)
return MEMORY_ERROR;
status = is_Term(tok, &term);
if (status != SUBTREE_OK){
free_ParseTree(term);
return status;
}
op->sibling = term;
}
return status;
}
int is_Pred (struct TokenList** tok, struct ParseTree** new) {
if (*tok == NULL)
return PARSING_ERROR;
struct ParseTree *term, *op, *pred;
struct Token *newTok;
enum TokenType type;
int status;
status = SUBTREE_OK;
newTok = new_Token((char[1]){'\0'}, Pred);
if (newTok == NULL)
return MEMORY_ERROR;
(*new)->data = newTok;
term = alloc_ParseTree();
if (term == NULL)
return MEMORY_ERROR;
status = is_Term(tok, &term);
if (status != SUBTREE_OK){
free_ParseTree(term);
return status;
}
(*new)->child = term;
// The remainder of Pred is optional
// (only if there is '+' | '-')
type = (*tok)->token->type;
if (match_AritmOp_type(type) &&
!match_TermOp_type(type)){
op = alloc_ParseTree();
if (op == NULL)
return MEMORY_ERROR;
status = is_Operator(tok, &op);
if (status != SUBTREE_OK){
free_ParseTree(op);
return status;
}
term->sibling = op;
pred = alloc_ParseTree();
if (pred == NULL)
return MEMORY_ERROR;
status = is_Pred(tok, &pred);
if (status != SUBTREE_OK){
free_ParseTree(pred);
return status;
}
op->sibling = pred;
}
return status;
}
int is_Expr (struct TokenList** tok, struct ParseTree** new) {
if (*tok == NULL)
return PARSING_ERROR;
struct ParseTree *pred, *op, *expr;
struct Token *newTok;
enum TokenType type;
int status;
status = SUBTREE_OK;
newTok = new_Token((char[1]){'\0'}, Expr);
if (newTok == NULL)
return MEMORY_ERROR;
(*new)->data = newTok;
pred = alloc_ParseTree();
if (pred == NULL)
return MEMORY_ERROR;
status = is_Pred(tok, &pred);
if (status != SUBTREE_OK){
free_ParseTree(pred);
return status;
}
(*new)->child = pred;
// The remainder of Expr is optional
// (only if there is a conditional operator)
type = (*tok)->token->type;
if (match_CondOp_type(type)){
op = alloc_ParseTree();
if (op == NULL)
return MEMORY_ERROR;
status = is_Operator(tok, &op);
if (status != SUBTREE_OK){
free_ParseTree(op);
return status;
}
pred->sibling = op;
expr = alloc_ParseTree();
if (expr == NULL)
return MEMORY_ERROR;
status = is_Expr(tok, &expr);
if (status != SUBTREE_OK){
free_ParseTree(expr);
return status;
}
op->sibling = expr;
}
return status;
}
int is_List (struct TokenList** tok, struct ParseTree** new) {
struct ParseTree *open, *listexpr, *close;
int status, has_expr;
struct Token* newTok;
newTok = new_Token((char[1]){'\0'}, List);
if (newTok == NULL)
return MEMORY_ERROR;
(*new)->data = newTok;
status = SUBTREE_OK;
has_expr = 0;
open = alloc_ParseTree();
if (open == NULL)
return MEMORY_ERROR;
status = is_Lbrack(tok, &open);
if (status != SUBTREE_OK) {
free_ParseTree(open);
return status;
}
(*new)->child = open;
if ((*tok)->token->type != Rbrack) {
listexpr = alloc_ParseTree();
if (listexpr == NULL)
return MEMORY_ERROR;
status = is_ListExpr(tok, &listexpr);
if (status != SUBTREE_OK) {
free_ParseTree(listexpr);
return status;
}
has_expr = 1;
open->sibling = listexpr;
}
close = alloc_ParseTree();
if (close == NULL)
return MEMORY_ERROR;
status = is_Rbrack(tok, &close);
if (status != SUBTREE_OK) {
free_ParseTree(close);
return status;
}
if (has_expr)
listexpr->sibling = close;
else
open->sibling = close;
return status;
}
int is_ListElem(struct TokenList** tok, struct ParseTree** new) {
struct ParseTree *var, *lbrack, *idx, *rbrack;
int status;
struct Token *newTok;
status = SUBTREE_OK;
newTok = new_Token((char[1]){'\0'}, ListElem);
if (newTok == NULL)
return MEMORY_ERROR;
(*new)->data = newTok;
var = alloc_ParseTree();
if (var == NULL)
return MEMORY_ERROR;
status = is_Var(tok, &var);
if (status != SUBTREE_OK) {
free_ParseTree(var);
return status;
}
(*new)->child = var;
lbrack = alloc_ParseTree();
if (lbrack == NULL)
return MEMORY_ERROR;
status = is_Lbrack(tok, &lbrack);
if (status != SUBTREE_OK) {
free_ParseTree(lbrack);
return status;
}
var->sibling = lbrack;
idx = alloc_ParseTree();
if (idx == NULL)
return MEMORY_ERROR;
// only int and var are allowed as list index
if ((*tok)->token->type == Int)
status = is_Int(tok, &idx);
else
status = is_Var(tok, &idx);
if (status != SUBTREE_OK) {
free_ParseTree(idx);
return status;
}
lbrack->sibling = idx;
rbrack = alloc_ParseTree();
if (rbrack == NULL)
return MEMORY_ERROR;
status = is_Rbrack(tok, &rbrack);
if (status != SUBTREE_OK) {
free_ParseTree(rbrack);
return status;
}
idx->sibling = rbrack;
return status;
}
int is_ListExpr(struct TokenList** tok, struct ParseTree** new) {
struct ParseTree *obj, *comma, *last;
int status;
struct Token* newTok;
status = SUBTREE_OK;
newTok = new_Token((char[1]){'\0'}, ListExpr);
if (newTok == NULL)
return MEMORY_ERROR;
(*new)->data = newTok;
obj = alloc_ParseTree();
if (obj == NULL)
return MEMORY_ERROR;
status = is_Obj(tok, &obj);
if (status != SUBTREE_OK) {
free_ParseTree(obj);
return status;
}
(*new)->child = obj;
last = obj;
while( (*tok)->token->type == Comma) {
comma = alloc_ParseTree();
if (comma == NULL)
return MEMORY_ERROR;
status = is_Comma(tok, &comma);
if (status != SUBTREE_OK) {
free_ParseTree(comma);
return status;
}
last->sibling = comma;
last = comma;
obj = alloc_ParseTree();
if (obj == NULL)
return MEMORY_ERROR;
status = is_Obj(tok, &obj);
if (status != SUBTREE_OK) {
free_ParseTree(obj);
return status;
}
last->sibling = obj;
last = obj;
}
return status;
}
int is_Str (struct TokenList** tok, struct ParseTree** new) {
if (*tok == NULL)
return PARSING_ERROR;
struct ParseTree *quotedstr, *plus, *last;
int status;
struct Token* newTok;
newTok = new_Token((char[1]){'\0'}, Str);
if (newTok == NULL)
return MEMORY_ERROR;
(*new)->data = newTok;
status = SUBTREE_OK;
quotedstr = alloc_ParseTree();
if (quotedstr == NULL)
return MEMORY_ERROR;
status = is_QuotedStr(tok, "edstr);
if (status != SUBTREE_OK) {
free_ParseTree(quotedstr);
return status;
}
(*new)->child = quotedstr;
last = quotedstr;
while((*tok)->token->type == Plus) {
plus = alloc_ParseTree();
if (plus == NULL)
return MEMORY_ERROR;
status = is_Plus(tok, &plus);
if (status != SUBTREE_OK) {
free_ParseTree(plus);
return status;
}
last->sibling = plus;
last = plus;
if ((*tok)->token->type != QuotedStr)
return PARSING_ERROR;
quotedstr = alloc_ParseTree();
if (quotedstr == NULL)
return MEMORY_ERROR;
status = is_QuotedStr(tok, "edstr);
if (status != SUBTREE_OK) {
free_ParseTree(quotedstr);
return status;
}
last->sibling = quotedstr;
last = quotedstr;
}
return status;
}
int is_Frac (struct TokenList** tok, struct ParseTree** new) {
struct ParseTree *dot, *integer;
int status;
struct Token* newTok;
newTok = new_Token((char[1]){'\0'}, Frac);
if (newTok == NULL)
return MEMORY_ERROR;
(*new)->data = newTok;
status = SUBTREE_OK;
dot = alloc_ParseTree();
if (dot == NULL)
return MEMORY_ERROR;
status = is_Dot(tok, &dot);
if (status != SUBTREE_OK) {
free_ParseTree(dot);
return status;
}
(*new)->child = dot;
integer = alloc_ParseTree();
if (integer == NULL)
return MEMORY_ERROR;
status = is_Int(tok, &integer);
if (status != SUBTREE_OK) {
free_ParseTree(integer);
return status;
}
dot->sibling = integer;
return status;
}
int is_Exp (struct TokenList** tok, struct ParseTree** new) {
int status, has_sign;
struct ParseTree *pow, *sign, *integer;
struct Token* newTok;
newTok = new_Token((char[1]){'\0'}, Pow);
if (newTok == NULL)
return MEMORY_ERROR;
(*new)->data = newTok;
status = SUBTREE_OK;
has_sign = 0;
pow = alloc_ParseTree();
if (pow == NULL)
return MEMORY_ERROR;
status = is_Pow(tok, &pow);
if (status != SUBTREE_OK) {
free_ParseTree(pow);
return status;
}
(*new)->child = pow;
sign = alloc_ParseTree();
if (sign == NULL)
return MEMORY_ERROR;
if ((*tok)->token->type == Plus) {
status = is_Plus(tok, &sign);
has_sign = 1;
}
else if ((*tok)->token->type == Minus) {
status = is_Minus(tok, &sign);
has_sign = 1;
}