-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.y
1375 lines (1188 loc) · 39 KB
/
parser.y
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
%code requires {
#include "astNodes.hpp"
#include <iostream>
#include <string>
}
%union {
char *strval;
int intval;
float flval;
AstNode* astNode;
}
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int yydebug=1;
void yyerror(const char *);
extern FILE *yyin;
extern int yylex();
extern int yyparse();
AstNode* root = NULL;
int n_nodes = 0;
%}
%error-verbose
// tokens
%token ID STRING ARRAY RETURN BREAK CONTINUE GLOBAL NONLOCAL YIELD
%token INDENT DEDENT INDENTERROR NEWLINE GT LT GTE LTE EQUAL
%token INT FLOAT STR BOOL LIST INT_NUMBER FLOAT_NUMBER
%token<astNode> IF ELSE ELIF DEF WHILE FOR IN RANGE PRINT INPUT CLASS TRY EXCEPT MATCH CASE WITH AS PASS AND OR TRUE_TOK FALSE_TOK
%type<astNode> ID STRING RETURN BREAK CONTINUE GLOBAL NONLOCAL YIELD GT LT GTE LTE EQUAL INT FLOAT STR BOOL LIST
%type<astNode> INT_NUMBER FLOAT_NUMBER boolean
%type<astNode> prog statements statement function assignment expression NUMBER function_block function_stmts function_stmt_ function_sp_stmt function_compound_stmt args args_ arg simple_stmt ARRAY start function_call argsp args_p argp argsc args_c argc return_stmt global_stmt nonlocal_stmt yield_stmt
%type<astNode> func_while_stmt func_for_stmt func_for_block func_for_stmts func_for_stmt_ relation_stmt for_sp_stmt range_args for_compound_stmt
%type<astNode> func_if_stmt func_if_header func_elif_else_ func_elif_else func_else_stmt func_elif_stmts func_elif_stmt func_elif_header
%type<astNode> if_stmt if_header elif_else_ elif_else else_stmt elif_stmts elif_stmt elif_header with_statement with_statement_body with_body with_stmt_contents with_item inside_brackets target targets
%type<astNode> block stmts stmt compound_stmt pass class class_block class_stmts class_stmt_ class_sp_stmt
%type<astNode> while_stmt for_stmt for_block for_stmts for_stmt_ for_if_stmt for_if_header for_elif_else_ for_elif_else for_else_stmt for_elif_stmts for_elif_stmt for_elif_header
%type<astNode> try_except_stmt except_clauses except_clause match_stmt case_statements case_statement final_case match_block
%nonassoc '='
%left '|' '&' AND OR
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS
%%
/* Parser Grammar */
start:
prog {
// Assign the root of the AST
root = $$;
YYACCEPT;
}
;
prog
: { $$=NULL; }
| statements {
$$=$1;
}
;
statements
: statement { $$=new StatementsNode("root"); $$->add($1); }
| statements NEWLINE statement { $1->add($3); $$ = $1; }
| statements NEWLINE { }
;
statement
: if_stmt { $$ = $1; }
| while_stmt { $$ = $1; }
| for_stmt { $$ = $1; }
| function { $$ = $1; }
| function_call { $$ = $1; }
| assignment NEWLINE { $$ = $1; }
| class { $$ = $1; }
| try_except_stmt { $$ = $1; }
| match_stmt { $$ = $1; }
| with_statement { $$ = $1; }
| pass NEWLINE { $$ = $1; }
;
NUMBER
: INT_NUMBER {
std::string nname = "int" + std::to_string(n_nodes);
++n_nodes;
$1->name=nname;
$$ = $1;
}
| FLOAT_NUMBER {
std::string nname = "float" + std::to_string(n_nodes);
++n_nodes;
$1->name=nname;
$$ = $1;
}
;
boolean : TRUE_TOK {
std::string nname = "bool" + std::to_string(n_nodes);
++n_nodes;
$1 = new BoolNode();
$1->name=nname;
$1->label = "True";
$$ = $1;
}
| FALSE_TOK {
std::string nname = "bool" + std::to_string(n_nodes);
++n_nodes;
$1 = new BoolNode();
$1->name=nname;
$1->label = "False";
$$ = $1;
}
//! IF statment START (AST)
if_stmt
: if_header block elif_else_ {
//printf("if statement successfully parsed:\n");
std::string nname = "if" + std::to_string(n_nodes);
++n_nodes;
$$ = new IfStatementNode();
$$->name = nname;
$$->add($1);
$$->add($2);
if ($3 != NULL)
$$->add($3);
}
;
if_header
: IF relation_stmt { $$ = $2; }
| IF '(' relation_stmt ')' { $$ = $3; }
;
elif_else_
: { $$ = NULL; }
| elif_else { $$ = $1; }
;
elif_else
: elif_stmts else_stmt {
std::string nname = "block" + std::to_string(n_nodes);
++n_nodes;
$$=new BlockNode(nname);
$$->label = "Elif..._Else";
$$->add($1);
$$->add($2);
}
| elif_stmts { $$ = $1; }
| else_stmt { $$ = $1; }
;
else_stmt
: ELSE block {
std::string nname = "else" + std::to_string(n_nodes);
++n_nodes;
$$ = new ElseStatementNode(nname);
$$->add($2);
}
;
elif_stmts
: elif_stmt {
std::string nname = "block" + std::to_string(n_nodes);
++n_nodes;
$$=new BlockNode(nname);
$$->label = "Elif...";
$$->add($1);
}
| elif_stmts elif_stmt { $1->add($2); $$ = $1; }
;
elif_stmt
: elif_header block {
std::string nname = "elif" + std::to_string(n_nodes);
++n_nodes;
$$ = new ElIfStatementNode(nname);
$$->add($1);
$$->add($2);
}
;
elif_header
: ELIF relation_stmt { $$ = $2; }
| ELIF '(' relation_stmt ')' { $$ = $3; }
;
// IF statement END
//! FUNCTION statement START (AST)
function
: DEF ID '(' args ')' function_block {
//printf("Function successfully parsed:\n");
std::string name = "func" + std::to_string(n_nodes);
++n_nodes;
IdentifierNode* idFunc = dynamic_cast<IdentifierNode*>($2);
$$ = new FunctionNode(idFunc->value);
if ($4 != NULL)
$$->add($4);
$$->add($6);
}
| DEF ID '(' args ')' '-' GT data_type function_block {
//printf("Generic Function successfully parsed:\n");
std::string name = "func" + std::to_string(n_nodes);
++n_nodes;
IdentifierNode* idFunc = dynamic_cast<IdentifierNode*>($2);
$$ = new FunctionNode(idFunc->value + "_Generic");
if ($4 != NULL)
$$->add($4);
$$->add($9);
}
;
function_block
: NEWLINE INDENT function_stmts DEDENT { $$ = $3; }
;
function_stmts
: function_stmt_ {
std::string nname = "stmt" + std::to_string(n_nodes);
++n_nodes;
$$ = new StatementsNode(nname);
$$->add($1);
}
| function_stmts function_stmt_ { $1->add($2); $$ = $1; }
;
function_stmt_
: simple_stmt NEWLINE {
$$ = $1;
}
| function_compound_stmt {
$$ = $1;
}
| function_sp_stmt NEWLINE {
$$ = $1;
}
;
function_sp_stmt
: return_stmt{ $$ = $1; }
| global_stmt{ $$ = $1; }
| nonlocal_stmt{ $$ = $1; }
| yield_stmt{ $$ = $1; }
;
function_compound_stmt
: func_if_stmt{ $$ = $1; }
| func_while_stmt{ $$ = $1; }
| func_for_stmt{ $$ = $1; }
| try_except_stmt { $$ = $1; }
| with_statement { $$ = $1; }
| match_stmt { $$ = $1; }
| function{ $$ = $1; }
;
//!================================================================
func_while_stmt
: WHILE relation_stmt func_for_block {
//printf("while statement successfully parsed:\n");
std::string name = "while" + std::to_string(n_nodes);
++n_nodes;
$$ = new WhileStatementNode();
$$->name = name;
$$->add($2);
$$->add($3);
}
;
func_for_stmt
: FOR ID IN RANGE range_args func_for_block {
//printf("for statement successfully parsed:\n");
std::string name = "for" + std::to_string(n_nodes);
++n_nodes;
std::string nname = "range" + std::to_string(n_nodes);
++n_nodes;
$4 = new FunctionCallNode(nname);
$4->add($5);
$$ = new ForStatementNode();
$$->name = name;
$$->add($4);
$$->add($6);
}
| FOR ID IN ARRAY func_for_block {
//printf("for statement successfully parsed:\n");
}
;
func_for_block
: NEWLINE INDENT func_for_stmts DEDENT { $$ = $3; }
;
func_for_stmts
: func_for_stmt_ {
std::string nname = "stmt" + std::to_string(n_nodes);
++n_nodes;
$$ = new StatementsNode(nname);
$$->add($1);
}
| func_for_stmts func_for_stmt_ { $1->add($2); $$ = $1; }
;
func_for_stmt_
: simple_stmt NEWLINE { $$ = $1; }
| for_compound_stmt { $$ = $1; }
| for_sp_stmt NEWLINE { $$ = $1; }
| function_sp_stmt NEWLINE { $$ = $1; }
;
//!================================================================
func_if_stmt
: func_if_header function_block func_elif_else_ {
//printf("if statement successfully parsed:\n");
std::string nname = "if" + std::to_string(n_nodes);
++n_nodes;
$$ = new IfStatementNode();
$$->name = nname;
$$->add($1);
$$->add($2);
if ($3 != NULL)
$$->add($3);
}
;
func_if_header
: IF relation_stmt { $$ = $2; }
| IF '(' relation_stmt ')' { $$ = $3; }
;
func_elif_else_
: { $$ = NULL; }
| func_elif_else { $$ = $1; }
;
func_elif_else
: func_elif_stmts func_else_stmt {
std::string nname = "block" + std::to_string(n_nodes);
++n_nodes;
$$=new BlockNode(nname);
$$->label = "Elif..._Else";
$$->add($1);
$$->add($2);
}
| func_elif_stmts { $$ = $1; }
| func_else_stmt { $$ = $1; }
;
func_else_stmt
: ELSE function_block {
std::string nname = "else" + std::to_string(n_nodes);
++n_nodes;
$$ = new ElseStatementNode(nname);
$$->add($2);
}
;
func_elif_stmts
: func_elif_stmt {
std::string nname = "block" + std::to_string(n_nodes);
++n_nodes;
$$=new BlockNode(nname);
$$->label = "Elif...";
$$->add($1);
}
| func_elif_stmts func_elif_stmt { $1->add($2); $$ = $1; }
;
func_elif_stmt
: func_elif_header function_block {
std::string nname = "elif" + std::to_string(n_nodes);
++n_nodes;
$$ = new ElIfStatementNode(nname);
$$->add($1);
$$->add($2);
}
;
func_elif_header
: ELIF relation_stmt { $$ = $2; }
| ELIF '(' relation_stmt ')' { $$ = $3; }
;
//!===============================================================
//! args for define function (AST)
args
: /* empty params */ { $$ = NULL; }
| args_ { $$ = $1; }
;
args_
: arg{
std::string nname = "arg" + std::to_string(n_nodes);
++n_nodes;
$$ = new Args(nname); $$->add($1);
}
| args_ ',' arg { $1->add($3); $$ = $1; }
;
arg
: ID {
std::string nname = "iden" + std::to_string(n_nodes);
++n_nodes;
$1->name=nname;
$$ = $1;
}
| ID ':' data_type {
std::string nname = "iden" + std::to_string(n_nodes);
++n_nodes;
$1->name=nname;
$$ = $1;
}
;
//! args for print function (AST)
argsp
: /* empty params */ { $$ = NULL; }
| args_p { $$ = $1; }
;
args_p
: argp {
std::string nname = "arg" + std::to_string(n_nodes);
++n_nodes;
$$ = new Args(nname); $$->add($1);
}
| args_p argp { $1->add($2); $$ = $1; }
;
argp
: ID {
std::string nname = "iden" + std::to_string(n_nodes);
++n_nodes;
$1->name=nname;
$$ = $1;
}
| NUMBER { $$ = $1; }
| STRING {
std::string nname = "string" + std::to_string(n_nodes);
++n_nodes;
$1->name=nname;
$$ = $1;
}
;
//! args for function call (AST)
argsc
: /* empty params */ { $$ = NULL; }
| args_c { $$ = $1; }
;
args_c
: argc {
std::string nname = "arg" + std::to_string(n_nodes);
++n_nodes;
$$ = new Args(nname); $$->add($1);
}
| args_c ',' argc { $1->add($3); $$ = $1; }
;
argc
: ID {
std::string nname = "iden" + std::to_string(n_nodes);
++n_nodes;
$1->name=nname;
$$ = $1;
}
| NUMBER { $$ = $1; }
| STRING {
std::string nname = "string" + std::to_string(n_nodes);
++n_nodes;
$1->name=nname;
$$ = $1;
}
;
// FUNCTION statement END
//! WHILE statement START (AST)
while_stmt
: WHILE relation_stmt for_block {
//printf("while statement successfully parsed:\n");
std::string name = "while" + std::to_string(n_nodes);
++n_nodes;
$$ = new WhileStatementNode();
$$->name = name;
$$->add($2);
$$->add($3);
}
;
// WHILE statement END
//! FOR statement START (AST)
range_args
: '(' NUMBER ')' {
std::string nname = "arg" + std::to_string(n_nodes);
++n_nodes;
$$ = new Args(nname);
$$->add($2);
}
| '(' NUMBER ',' NUMBER')' {
std::string nname = "arg" + std::to_string(n_nodes);
++n_nodes;
$$ = new Args(nname);
$$->add($2);
$$->add($4);
}
| '(' NUMBER ',' NUMBER ',' NUMBER ')' {
std::string nname = "arg" + std::to_string(n_nodes);
++n_nodes;
$$ = new Args(nname);
$$->add($2);
$$->add($4);
$$->add($6);
}
;
for_stmt
: FOR ID IN RANGE range_args for_block {
//printf("for statement successfully parsed:\n");
std::string name = "for" + std::to_string(n_nodes);
++n_nodes;
std::string nname = "range" + std::to_string(n_nodes);
++n_nodes;
$4 = new FunctionCallNode(nname);
$4->add($5);
$$ = new ForStatementNode();
$$->name = name;
$$->add($4);
$$->add($6);
}
| FOR ID IN ARRAY for_block {
printf("for statement successfully parsed:\n");
}
;
for_block
: NEWLINE INDENT for_stmts DEDENT { $$ = $3; }
;
for_stmts
: for_stmt_ {
std::string nname = "stmt" + std::to_string(n_nodes);
++n_nodes;
$$ = new StatementsNode(nname);
$$->add($1);
}
| for_stmts for_stmt_ { $1->add($2); $$ = $1; }
;
for_stmt_
: simple_stmt NEWLINE { $$ = $1; }
| for_compound_stmt { $$ = $1; }
| for_sp_stmt NEWLINE { $$ = $1; }
;
for_sp_stmt
: BREAK {
std::string nname = "break" + std::to_string(n_nodes);
++n_nodes;
$$ = new BreakStatementNode();
$$->name = nname;
}
| CONTINUE {
std::string nname = "continue" + std::to_string(n_nodes);
++n_nodes;
$$ = new ContinueStatementNode();
$$->name = nname;
}
;
for_compound_stmt
: for_if_stmt { $$ = $1; }
| while_stmt { $$ = $1; }
| for_stmt { $$ = $1; }
| try_except_stmt { $$ = $1; }
| with_statement { $$ = $1; }
| match_stmt { $$ = $1; }
;
//!===============================================================
for_if_stmt
: for_if_header for_block for_elif_else_ {
//printf("if statement successfully parsed:\n");
std::string nname = "if" + std::to_string(n_nodes);
++n_nodes;
$$ = new IfStatementNode();
$$->name = nname;
$$->add($1);
$$->add($2);
if ($3 != NULL)
$$->add($3);
}
;
for_if_header
: IF relation_stmt { $$ = $2; }
| IF '(' relation_stmt ')' { $$ = $3; }
;
for_elif_else_
: { $$ = NULL; }
| for_elif_else { $$ = $1; }
;
for_elif_else
: for_elif_stmts for_else_stmt {
std::string nname = "block" + std::to_string(n_nodes);
++n_nodes;
$$=new BlockNode(nname);
$$->label = "Elif..._Else";
$$->add($1);
$$->add($2);
}
| for_elif_stmts { $$ = $1; }
| for_else_stmt { $$ = $1; }
;
for_else_stmt
: ELSE for_block {
std::string nname = "else" + std::to_string(n_nodes);
++n_nodes;
$$ = new ElseStatementNode(nname);
$$->add($2);
}
;
for_elif_stmts
: for_elif_stmt {
std::string nname = "block" + std::to_string(n_nodes);
++n_nodes;
$$=new BlockNode(nname);
$$->label = "Elif...";
$$->add($1);
}
| for_elif_stmts for_elif_stmt { $1->add($2); $$ = $1; }
;
for_elif_stmt
: for_elif_header for_block {
std::string nname = "elif" + std::to_string(n_nodes);
++n_nodes;
$$ = new ElIfStatementNode(nname);
$$->add($1);
$$->add($2);
}
;
for_elif_header
: ELIF relation_stmt { $$ = $2; }
| ELIF '(' relation_stmt ')' { $$ = $3; }
;
// FOR statement END
//! FUNCTION_CALL statement START (AST)
function_call
: ID '(' argsc ')' {
std::string name = "_func_call" + std::to_string(n_nodes);
++n_nodes;
IdentifierNode* idFunc = dynamic_cast<IdentifierNode*>($1);
$$ = new FunctionCallNode(idFunc->value + name);
if ($3 != NULL)
$$->add($3);
}
| PRINT '(' argsp ')' {
std::string name = "print" + std::to_string(n_nodes);
++n_nodes;
$$ = new FunctionCallNode(name);
if ($3 != NULL)
$$->add($3);
}
| INPUT '(' STRING ')' {
std::string name = "input" + std::to_string(n_nodes);
++n_nodes;
$$ = new FunctionCallNode(name);
std::string nname = "string" + std::to_string(n_nodes);
++n_nodes;
$3->name=nname;
$$->add($3);
}
;
// FUNCTION_CALL statement END
//! CLASS statement START (AST)
class
: CLASS ID class_block {
//printf("class statement successfully parsed:\n");
std::string name = "class" + std::to_string(n_nodes);
++n_nodes;
IdentifierNode* idclass = dynamic_cast<IdentifierNode*>($2);
$$ = new ClassNode(idclass->value);
$$->add($3);
}
;
class_block
: NEWLINE INDENT class_stmts DEDENT { $$ = $3; }
;
class_stmts
: class_stmt_ {
std::string nname = "stmt" + std::to_string(n_nodes);
++n_nodes;
$$ = new StatementsNode(nname);
$$->add($1);
}
| class_stmts class_stmt_ { $1->add($2); $$ = $1; }
;
class_stmt_
: simple_stmt NEWLINE { $$ = $1; }
| compound_stmt { $$ = $1; }
| class_sp_stmt NEWLINE { $$ = $1; }
| function { $$ = $1; }
;
class_sp_stmt
: global_stmt { $$ = $1; }
;
// CLASS statement END
//! TRY_EXCEPT statement START (AST)
try_except_stmt
: TRY block except_clauses {
//printf("try_except statement successfully parsed:\n");
std::string name = "try" + std::to_string(n_nodes);
++n_nodes;
$$ = new TryStatementNode();
$$->name = name;
$$->add($2);
$$->add($3);
}
;
except_clauses
: except_clause {
std::string nname = "block" + std::to_string(n_nodes);
++n_nodes;
$$=new BlockNode(nname);
$$->label = "Except...";
$$->add($1);
}
| except_clauses except_clause { $1->add($2); $$ = $1; }
;
except_clause
: EXCEPT block {
std::string nname = "except" + std::to_string(n_nodes);
++n_nodes;
$$ = new ExceptStatementNode();
$$->name = nname;
$$->add($2);
}
;
// TRY_EXCEPT statement END
//! MATCH statement START (AST)
match_stmt
: MATCH ID match_block {
//printf("match statement successfully parsed:\n");
std::string name = "match" + std::to_string(n_nodes);
++n_nodes;
std::string nname = "iden" + std::to_string(n_nodes);
++n_nodes;
$2->name=nname;
$$ = new matchStatementNode();
$$->name = name;
$$->add($2);
$$->add($3);
}
;
case_statements
:
case_statement {
std::string nname = "block" + std::to_string(n_nodes);
++n_nodes;
$$=new BlockNode(nname);
$$->label = "Case...";
$$->add($1);
}
| case_statements case_statement { $1->add($2); $$ = $1; }
;
case_statement
: CASE argp block {
std::string nname = "case" + std::to_string(n_nodes);
++n_nodes;
$$ = new CaseStatementNode(nname);
$$->add($2);
$$->add($3);
}
;
final_case
: CASE '_' block {
std::string nname = "case" + std::to_string(n_nodes);
++n_nodes;
$$ = new CaseStatementNode(nname);
$$->label = "Default Statement";
$$->add($3);
}
;
match_block
: NEWLINE INDENT case_statements final_case DEDENT {
std::string nname = "block" + std::to_string(n_nodes);
++n_nodes;
$$=new BlockNode(nname);
$$->label = "Match Block";
$$->add($3);
$$->add($4);
}
;
// MATCH statement END
//! WITH statement START (AST)
with_statement
: WITH with_statement_body block {
//printf("with statement successfully parsed:\n");
std::string nname = "with" + std::to_string(n_nodes);
n_nodes;
$$ = new withStatementNode();
$$->name = nname;
$$->add($2);
$$->add($3);
}
;
with_statement_body
: with_statement_body ',' with_body {
$1->add($3);
$$=$1;
}
| with_body {
std::string nname = "block" + std::to_string(n_nodes);
++n_nodes;
$$=new BlockNode(nname);
$$->label = "With Head";
$$->add($1);
}
;
with_body
: with_stmt_contents { $$=$1; }
|'(' with_stmt_contents ')' { $$=$2; }
;
/////////////////////////////
with_stmt_contents
: with_item { $$=$1; }
//|with_item ',' with_stmt_contents
;
with_item
: ID '(' inside_brackets ')' AS target {
std::string nname = "iden" + std::to_string(n_nodes);
n_nodes;
$1->name = nname;
std::string name = "block" + std::to_string(n_nodes);
++n_nodes;
$$=new BlockNode(name);
$$->label = "With Item";
$$->add($1);
$$->add($3);
$$->add($6);
}
//| expression { $$=$1; }
// | expression AS target {
// $1->add($3);
// $$ = $1;
// }
;
//////////////////////////////
inside_brackets
: expression {
std::string nname = "block" + std::to_string(n_nodes);
++n_nodes;
$$=new BlockNode(nname);
$$->label = "Parameters";
$$->add($1);
}
| inside_brackets ',' expression {
$1->add($3);
$$ = $1;
}
;
target
: '(' targets ')' { $$=$2; }
| target '.' ID { $1->add($3); $$=$1; }
| ID {
std::string nname = "iden" + std::to_string(n_nodes);
++n_nodes;
$1->name=nname;
$$ = $1;
}
;
targets
: target {
std::string nname = "block" + std::to_string(n_nodes);
++n_nodes;
$$=new BlockNode(nname);
$$->label = "Targets";
$$->add($1);
}
| targets ',' target { $1->add($3); $$=$1; }
;
// WITH statement END
block //: NEWLINE INDENT stmts NEWLINE DEDENT { }
: NEWLINE INDENT stmts DEDENT { $$ = $3; }
;
stmts
: stmt {
std::string nname = "stmt" + std::to_string(n_nodes);
++n_nodes;
$$ = new StatementsNode(nname);
$$->add($1);
}
| stmts stmt { $1->add($2); $$ = $1; }
;
stmt
: simple_stmt NEWLINE { $$ = $1; }
| compound_stmt { $$ = $1; }
;
simple_stmt
: assignment { $$ = $1; }
| pass { $$ = $1; }
//| return_stmt {}
//| BREAK {}
//| CONTINUE {}
//| yield_stmt {}
//| global_stmt {}
//| nonlocal_stmt {}
| function_call { $$ = $1; }
;
compound_stmt
: if_stmt { $$ = $1; }
| while_stmt { $$ = $1; }