-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
1332 lines (1273 loc) · 43.7 KB
/
parser.cpp
File metadata and controls
1332 lines (1273 loc) · 43.7 KB
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 "parser.hpp"
#include <iostream>
#include <typeinfo>
#include <vector>
Parser::Parser(Lexer * lexer)
{
this->lexer = lexer;
}
void Parser::parse()
{
Token tok = this->lexer->lex();
this->root = new Prog();
start(tok, this->root);
root->assign_indents(root, 1);
// print_ast();
}
//start -> /* empty */ | globaldeclarations
AST * Parser::start(Token tok, AST * node)
{
//std::cout<<"start() "<<lexer->name_to_str(tok.getName())<<std::endl;
if(tok.getName() == Token::T_EOF)
{
return node;
}
else
{
std::vector<AST*> glob_decls = globaldeclarations(tok);
//std::cout<<"Got return from globaldeclarations"<<std::endl;
for(auto g_d : glob_decls)
{
//std::cout<<"Start"<<std::endl;
node->add_child(g_d);
}
}
return node;
}
// globaldeclarations -> globaldeclaration globaldeclarations_p
std::vector<AST*> Parser::globaldeclarations(Token tok)
{
//std::cout<<"globaldeclarations()"<<lexer->name_to_str(tok.getName())<<std::endl;
std::vector<AST*> glob_decls;
AST* glob_decl = globaldeclaration(tok);
if(glob_decl == NULL)
{
// no global declarations, return
return glob_decls;
}
glob_decls.push_back(glob_decl);
tok = this->lexer->lex();
std::vector<AST*> prime_decls = globaldeclarations_p(tok);
//std::cout<<"Got return from globaldeclarations_p"<<std::endl;
glob_decls.insert(glob_decls.end(), prime_decls.begin(), prime_decls.end());
return glob_decls;
}
//globaldeclarations_p -> globaldeclaration globaldeclarations_p | /* empty */
std::vector<AST*> Parser::globaldeclarations_p(Token tok)
{
//std::cout<<"globaldeclarations_p()"<<lexer->name_to_str(tok.getName())<<std::endl;
std::vector<AST*> glob_decls;
if(tok.getName() == Token::T_EOF)
{
return glob_decls;
}
AST* glob_decl = globaldeclaration(tok);
if(glob_decl == NULL)
{
// no more global decls, return
return glob_decls;
}
glob_decls.push_back(glob_decl);
tok = this->lexer->lex();
std::vector<AST*> prime_decls = globaldeclarations_p(tok);
if(prime_decls.empty())
{
return glob_decls;
}
else
{
glob_decls.insert(glob_decls.end(), prime_decls.begin(), prime_decls.end());
}
return glob_decls;
}
/*
Substitute variabledeclaration, functiondeclaration, mainfunctiondeclaration
globaldeclaration -> variabledeclaration | functiondeclaration | mainfunctiondeclaration
globaldeclaration ->
type identifier ';'
| type identifier '(' functiondeclarator_p block
| VOID identifier '(' functiondeclarator_p block
| identifier '(' ')' block
globaldeclaration ->
type identifier globaldeclaration_p
| VOID identifier '(' functiondeclarator_p block
| identifier '(' ')' block
globaldeclaration_p -> ';' | '(' functiondeclarator_p block
*/
AST* Parser::globaldeclaration(Token tok)
{
//std::cout<<"globaldeclaration() "<<lexer->name_to_str(tok.getName())<<std::endl;
std::vector<AST*> glob_decls;
if(tok.getName() == Token::T_ID)
{
//this must be a main function declaratoration
//identifier '(' ')' block
std::string id = tok.getLexeme();
tok = this->lexer->lex();
if(tok.getName() != Token::T_LBRACKET)
{
std::cerr<<"error in main declaration, expected '(' after identifier around line "<<tok.getLine()<<std::endl;
exit(1);
}
tok = this->lexer->lex();
if(tok.getName() != Token::T_RBRACKET)
{
std::cerr<<"error in main declaration, expected ')' after '(' around line "<<tok.getLine()<<std::endl;
exit(1);
}
tok = this->lexer->lex();
Block* block_node = block(tok);
//create main_decl node, add formals, id , type and block nodes to children.
ID* id_node = new ID(id);
FuncDecl* main_decl = new FuncDecl(Token::T_VOID, id_node, std::vector<Formal*>(), block_node, true);
return main_decl;
}
else if(tok.getName() == Token::T_VOID)
{
//this msut be a function declarations
//VOID identifier '(' functiondeclarator_p block
tok = this->lexer->lex();
if(tok.getName() != Token::T_ID)
{
std::cerr<<"error in function declaration, expected identifier after void around line "<<tok.getLine()<<std::endl;
exit(1);
}
std::string id = tok.getLexeme();
tok = this->lexer->lex();
if(tok.getName() != Token::T_LBRACKET)
{
std::cerr<<"error in function declaration, expected '(' after void identifier around line "<<tok.getLine()<<std::endl;
exit(1);
}
tok = this->lexer->lex();
std::vector<Formal*> formals = functiondeclarator_p(tok);
tok = this->lexer->lex();
Block* block_node = block(tok);
//create func_decl node, add formals, id , type and block nodes to children.
ID* id_node = new ID(id);
FuncDecl* func_decl = new FuncDecl(Token::T_VOID, id_node, formals, block_node, false);
return func_decl;
}
else if(tok.getName() == Token::T_INT || tok.getName() == Token::T_BOOL)
{
//this could be a function or var declaration
Token::Name type = tok.getName();
tok = this->lexer->lex();
if(tok.getName() == Token::T_ID)
{
std::string id = tok.getLexeme();
tok = this->lexer->lex();
return globaldeclaration_p(type, id, tok);
}
else
{
std::cerr<<"error in global declaration, expected an identifier after type around line "<<tok.getLine()<<std::endl;
exit(1);
}
}
return NULL;
}
//globaldeclaration_p -> ';' | '(' functiondeclarator_p block
AST* Parser::globaldeclaration_p(Token::Name type, std::string id, Token tok)
{
//std::cout<<"globaldeclaration_p(), type="<<lexer->name_to_str(type)<<", id="<<id<<" "<<lexer->name_to_str(tok.getName())<<std::endl;
if(tok.getName() == Token::T_SEMICOL)
{
//this is a var declaration
// std::cout<<"Var found "<<id<<std::endl;
ID* id_node = new ID(id);
VarDecl* var_decl = new VarDecl(type, id_node, true);
return var_decl;
}
else if(tok.getName() == Token::T_LBRACKET)
{
//this is a func declaration
//type identifier '(' functiondeclarator_p block
tok = this->lexer->lex();
std::vector<Formal*> formals = functiondeclarator_p(tok);
tok = this->lexer->lex();
Block* block_node = block(tok);
// create and return func_decl node here with type, id, formal and block children.
ID* id_node = new ID(id);
FuncDecl* func_decl = new FuncDecl(type, id_node, formals, block_node, false);
return func_decl;
}
else
{
std::cerr<<"error in declaration, expected either a ';' or '(' after type identifier around line "<<tok.getLine()<<std::endl;
exit(1);
}
}
//functiondeclarator_p -> formalparameterlist ')' | ')'
std::vector<Formal*> Parser::functiondeclarator_p(Token tok)
{
//std::cout<<"functiondeclarator_p() "<<lexer->name_to_str(tok.getName())<<std::endl;
std::vector<Formal*> formals;
if(tok.getName() == Token::T_RBRACKET)
return formals;
else
{
formals = formalparameterlist(tok);
return formals;
}
}
//formalparameterlist -> formalparameter formalparameterlist_p
std::vector<Formal*> Parser::formalparameterlist(Token tok)
{
//std::cout<<"formalparameterlist() "<<lexer->name_to_str(tok.getName())<<std::endl;
std::vector<Formal*> formal_list;
Formal* formal_node = formalparameter(tok);
if(formal_node == NULL)
{
//no formals
return formal_list;
}
formal_list.push_back(formal_node);
tok = this->lexer->lex();
std::vector<Formal*> formal_p_list = formalparameterlist_p(tok);
formal_list.insert(formal_list.end(), formal_p_list.begin(), formal_p_list.end());
return formal_list;
}
//formalparameterlist_p -> ',' formalparameter formalparameterlist_p | /* empty */
std::vector<Formal*> Parser::formalparameterlist_p(Token tok)
{
//std::cout<<"formalparameterlist_p() "<<lexer->name_to_str(tok.getName())<<std::endl;
std::vector<Formal*> formal_list;
if(tok.getName() == Token::T_EOF)
{
return formal_list;
}
else if(tok.getName() == Token::T_COMMA)
{
tok = this->lexer->lex();
Formal* formal_node = formalparameter(tok);
if(formal_node == NULL)
{
//not allowed to end formal list with comma
std::cerr<<"Error in parameter list: Cannot end parameter list with ',' around line "<<tok.getLine()<<std::endl;
exit(1);
}
formal_list.push_back(formal_node);
tok = this->lexer->lex();
std::vector<Formal*> formal_p_list = formalparameterlist_p(tok);
formal_list.insert(formal_list.end(), formal_p_list.begin(), formal_p_list.end());
return formal_list;
}
else if(tok.getName() == Token::T_RBRACKET)
{
//reached end of param list
return formal_list;
}
else
{
//Got something that's not a comma or empty string, error
std::cerr<<"Error in parameter list: expected a parameter, got "
<<lexer->name_to_str(tok.getName())<<" around line "<<tok.getLine()<<std::endl;
exit(1);
}
return formal_list;
}
//formalparameter -> type identifier
Formal* Parser::formalparameter(Token tok)
{
//std::cout<<"formalparameter() "<<lexer->name_to_str(tok.getName())<<std::endl;
if(tok.getName() == Token::T_INT || tok.getName() == Token::T_BOOL)
{
Token::Name type = tok.getName();
tok = this->lexer->lex();
if(tok.getName() == Token::T_ID)
{
std::string id = tok.getLexeme();
ID* id_node = new ID(id);
Formal* formal_node = new Formal(type, id_node);
//std::cout<<"Creating new formal: "<<id<<std::endl;
return formal_node;
}
else
{
std::cerr<<"Error in parameter definition: expected an identifier after type, got "
<<lexer->name_to_str(tok.getName())<<" around line "<<tok.getLine()<<std::endl;
exit(1);
}
}
else
{
std::cerr<<"Error in parameter definition: expected a type, got "
<<lexer->name_to_str(tok.getName())<<" around line "<<tok.getLine()<<std::endl;
exit(1);
}
}
/*
block -> '{' blockp
*/
Block* Parser::block(Token tok)
{
//std::cout<<"block() "<<lexer->name_to_str(tok.getName())<<std::endl;
if(tok.getName() == Token::T_LBRACE)
{
tok = this->lexer->lex();
std::vector<AST*> block_stmts = block_p(tok);
// create block from block_stmts
tok = this->lexer->lex();
//std::cout<<"Before creating block, next token is "<<this->lexer->name_to_str(tok.getName())<<std::endl;
this->lexer->unlex(tok);
Block* block_node = new Block(block_stmts);
return block_node;
}
else
{
std::cerr<<"Error in block, expected '{' at the start of block around line "<<tok.getLine()<<std::endl;
exit(1);
}
}
//block_p -> blockstatements '}' | '}'
std::vector<AST*> Parser::block_p(Token tok)
{
//std::cout<<"block_p() "<<lexer->name_to_str(tok.getName())<<std::endl;
std::vector<AST*> block_stmts;
if(tok.getName() == Token::T_RBRACE)
{
return block_stmts;
}
block_stmts = blockstatements(tok);
//std::cout<<"Got return from blockstatements"<<std::endl;
tok = this->lexer->lex();
if(tok.getName() != Token::T_RBRACE)
{
std::cerr<<"Error in block, expected '}' at the end of block around line "<<tok.getLine()<<std::endl;
exit(1);
}
else
{
//std::cout<<"} found in block_p, consuming"<<std::endl;
}
// tok = this->lexer->lex();
return block_stmts;
}
//blockstatements -> blockstatement blockstatements_p
std::vector<AST*> Parser::blockstatements(Token tok)
{
//std::cout<<"blockstatements() "<<lexer->name_to_str(tok.getName())<<std::endl;
std::vector<AST*> block_stmts;
AST* block_stmt = blockstatement(tok);
block_stmts.push_back(block_stmt);
tok = this->lexer->lex();
if(tok.getName() == Token::T_RBRACE)
{
//std::cout<<"reached end of block, encountered } in blockstatements() "<<std::endl;
this->lexer->unlex(tok);
return block_stmts;
}
std::vector<AST*> block_p_stmts = blockstatements_p(tok);
if(!block_p_stmts.empty())
block_stmts.insert(block_stmts.end(), block_p_stmts.begin(), block_p_stmts.end());
return block_stmts;
}
//blockstatements_p -> blockstatement blockstatements_p | /* empty */
std::vector<AST*> Parser::blockstatements_p(Token tok)
{
//std::cout<<"blockstatements_p() "<<lexer->name_to_str(tok.getName())<<std::endl;
std::vector<AST*> block_stmts;
if(tok.getName() == Token::T_EOF)
{
return block_stmts;
}
if(tok.getName() == Token::T_RBRACE)
{
//std::cout<<"encountered } in blockstatements_p() "<<std::endl;
this->lexer->unlex(tok);
return block_stmts;
}
AST* block_stmt = blockstatement(tok);
block_stmts.push_back(block_stmt);
tok = this->lexer->lex();
std::vector<AST*> block_p_stmts = blockstatements_p(tok);
if(!block_p_stmts.empty())
block_stmts.insert(block_stmts.end(), block_p_stmts.begin(), block_p_stmts.end());
return block_stmts;
}
//blockstatement -> variabledeclaration | statement
AST* Parser::blockstatement(Token tok)
{
//std::cout<<"blockstatement() "<<lexer->name_to_str(tok.getName())<<std::endl;
if(tok.getName() == Token::T_INT || tok.getName() == Token::T_BOOL)
{
//var_decl-> type identifier ';'
Token::Name type = tok.getName();
tok = this->lexer->lex();
if(tok.getName() != Token::T_ID)
{
std::cerr<<"Error in variable declaration, expected identifier after type around line "<<tok.getLine()<<std::endl;
exit(1);
}
std::string id = tok.getLexeme();
tok = this->lexer->lex();
if(tok.getName() != Token::T_SEMICOL)
{
std::cerr<<"Error in variable declaration, expected ';' after identifier type around line "<<tok.getLine()<<std::endl;
exit(1);
}
//std::cout<<"Creating var "<<id<<std::endl;
ID* id_node = new ID(id);
VarDecl* var_decl = new VarDecl(type, id_node, false);
return (AST*)(var_decl);
}
else
{
return statement(tok);
}
}
/*
statement -> block
| ';'
| statementexpression ';'
| BREAK ';'
| RETURN statementret
| IF '(' expression ')' statement statementif
| WHILE '(' expression ')' statement
statementret -> expression ';' | ';'
statementif -> empty | ELSE statement
No factoring needed after some expansion
statement -> '{' blockstatements '}' | '{' '}'
| ';'
| identifier '=' assignmentexpression ';' | identifier '(' argumentlist ')' ';' | identifier '(' ')' ';'
| BREAK ';'
| RETURN statementret
| IF '(' expression ')' statement statementif
| WHILE '(' expression ')' statement
*/
AST* Parser::statement(Token tok)
{
//std::cout<<"statement() "<<lexer->name_to_str(tok.getName())<<" "<<tok.getLexeme()<<std::endl;
switch(tok.getName())
{
case Token::T_LBRACE:
{
//inner block
AST* block_node = block(tok);
return (AST*)(block_node);
}
case Token::T_SEMICOL:
{
//';' create NullStmt
NullStmt* null_node = new NullStmt();
return (AST*)(null_node);
}
case Token::T_BREAK:
{
//BREAK ';'
//match ';' create break node
tok = this->lexer->lex();
if(tok.getName() != Token::T_SEMICOL)
{
std::cerr<<"Error in statement: Expected ';' after break around line "<<tok.getLine()<<std::endl;
}
BreakStmt* break_node = new BreakStmt();
return (AST*)(break_node);
}
case Token::T_RETURN:
{
tok = this->lexer->lex();
return statementret(tok);
}
case Token::T_IF:
{
tok = this->lexer->lex();
// match '('
if(tok.getName() != Token::T_LBRACKET)
{
std::cerr<<"Expected '(' after if around line "<<tok.getLine()<<std::endl;
exit(1);
}
// get expression
tok = this->lexer->lex();
AST* exp = expression(tok);
tok = this->lexer->lex();
// match ')'
if(tok.getName() != Token::T_RBRACKET)
{
std::cerr<<"Expected ')' at end of if around line "<<tok.getLine()<<std::endl;
exit(1);
}
// get statement
tok = this->lexer->lex();
AST* stmt = statement(tok);
// return statementif
tok = this->lexer->lex();
return statementif(tok, exp, stmt);
}
case Token::T_WHILE:
{
//WHILE '(' expression ')' statement
tok = this->lexer->lex();
if(tok.getName() != Token::T_LBRACKET)
{
std::cerr<<"Expected '(' after while around line "<<tok.getLine()<<std::endl;
exit(1);
}
// call expression
tok = this->lexer->lex();
AST* exp_node = expression(tok);
// match ")"
tok = this->lexer->lex();
if(tok.getName() != Token::T_RBRACKET)
{
std::cerr<<"Expected ')' after while condition around line "<<tok.getLine()<<std::endl;
exit(1);
}
// call statement
tok = this->lexer->lex();
AST* stmt_node = statement(tok);
// TODO: create and return WhileStmt Node.
AST* while_node = new WhileStmt(exp_node, stmt_node);
return while_node;
}
case Token::T_ID:
{
AST* id_node = new ID(tok.getLexeme());
tok = this->lexer->lex();
if(tok.getName() == Token::T_EQ)
{
tok = this->lexer->lex();
AST* assn_node = assignment(tok, id_node);
tok = this->lexer->lex();
if(tok.getName() != Token::T_SEMICOL)
{
std::cerr<<"Expected ';' at end of assignment at around line "<<tok.getLine()<<std::endl;
exit(1);
}
return assn_node;
}
if(tok.getName() == Token::T_LBRACKET)
{
tok = this->lexer->lex();
AST* funcall_node = functioninvocation_p(tok, id_node);
tok = this->lexer->lex();
if(tok.getName() != Token::T_SEMICOL)
{
std::cerr<<"Expected ';' at end of assignment at around line "<<tok.getLine()<<std::endl;
exit(1);
}
return funcall_node;
}
else
{
std::cerr<<"Expected either '=' for assignment or '(' for function call after identifier at around line "<<tok.getLine()<<std::endl;
exit(1);
}
break;
}
default:
{
return NULL;
}
}
return NULL;
}
// statementret -> expression ';' | ';'
AST* Parser::statementret(Token tok)
{
//std::cout<<"statementret() "<<lexer->name_to_str(tok.getName())<<" "<<tok.getLexeme()<<std::endl;
if(tok.getName() == Token::T_SEMICOL)
{
AST* ret_node = new RetStmt();
return ret_node;
}
else
{
AST* exp_node = expression(tok);
tok = this->lexer->lex();
if(tok.getName() != Token::T_SEMICOL)
{
std::cerr<<this->lexer->name_to_str(tok.getName())<<std::endl;
std::cerr<<"Expected ';' at end of return statement around line "<<tok.getLine()<<std::endl;
exit(1);
}
RetStmt* ret_node = new RetStmt(exp_node);
return (AST*)(ret_node);
}
}
// statementif -> empty | ELSE statement
AST* Parser::statementif(Token tok, AST* exp, AST* if_stmt)
{
//std::cout<<"statementif() "<<lexer->name_to_str(tok.getName())<<" "<<tok.getLexeme()<<std::endl;
if(tok.getName() == Token::T_ELSE)
{
tok = this->lexer->lex();
AST* else_stmt = statement(tok);
IfElseStmt* if_else_node = new IfElseStmt(exp, if_stmt, else_stmt);
return (AST*)(if_else_node);
}
else
{
this->lexer->unlex(tok);
IfStmt* if_node = new IfStmt(exp, if_stmt);
return (AST*)(if_node);
}
return NULL;
}
/*
expression -> assignmentexpression
assignmentexpression -> conditionalorexpression | assignment
Full Expansion to find cases:
assignmentexpression ->
'-' unaryexpression multiplicativeexpression_p additiveexpression_p relationalexpression_p equalityexpression_p conditionalandexpression_p conditionalorexpression_p
| '!' unaryexpression multiplicativeexpression_p additiveexpression_p relationalexpression_p equalityexpression_p conditionalandexpression_p conditionalorexpression_p
| NUMBER multiplicativeexpression_p additiveexpression_p relationalexpression_p equalityexpression_p conditionalandexpression_p conditionalorexpression_p
| STRING multiplicativeexpression_p additiveexpression_p relationalexpression_p equalityexpression_p conditionalandexpression_p conditionalorexpression_p
| TRUE multiplicativeexpression_p additiveexpression_p relationalexpression_p equalityexpression_p conditionalandexpression_p conditionalorexpression_p
| FALSE multiplicativeexpression_p additiveexpression_p relationalexpression_p equalityexpression_p conditionalandexpression_p conditionalorexpression_p
| '(' expression ')' multiplicativeexpression_p additiveexpression_p relationalexpression_p equalityexpression_p conditionalandexpression_p conditionalorexpression_p
| identifier '(' functioninvocationp multiplicativeexpression_p additiveexpression_p relationalexpression_p equalityexpression_p conditionalandexpression_p conditionalorexpression_p
| identifier multiplicativeexpression_p additiveexpression_p relationalexpression_p equalityexpression_p conditionalandexpression_p conditionalorexpression_p
| identifier '=' assignmentexpression
Factor out identifier:
assignmentexpression ->
'-' unaryexpression assignmentexpression_helper
| '!' unaryexpression assignmentexpression_helper
| NUMBER assignmentexpression_helper
| STRING assignmentexpression_helper
| TRUE assignmentexpression_helper
| FALSE assignmentexpression_helper
| '(' expression ')' assignmentexpression_helper
| identifier assignmentexpression_p
assignmentexpression_p ->
|'(' functioninvocationp assignmentexpression_helper
| assignmentexpression_helper
| '=' assignmentexpression
assignmentexpression_helper -> multiplicativeexpression_p additiveexpression_p relationalexpression_p equalityexpression_p conditionalandexpression_p conditionalorexpression_p
multiplicativeexpression_p ->
'*' unaryexpression multiplicativeexpression_p
| '/' unaryexpression multiplicativeexpression_p
| '%' unaryexpression multiplicativeexpression_p
| empty
additiveexpression_p ->
'+' multiplicativeexpression additiveexpression_p
| '-' multiplicativeexpression additiveexpression_p
| empty
relationalexpression_p ->
'<' additiveexpression relationalexpression_p
| '>' additiveexpression relationalexpression_p
| LE additiveexpression relationalexpression_p
| GE additiveexpression relationalexpression_p
| empty
equalityexpression_p ->
EQ relationalexpression equalityexpression_p
| NE relationalexpression equalityexpression_p
| empty
conditionalandexpression_p ->
AND equalityexpression conditionalandexpression_p
| empty
conditionalorexpression_p ->
OR conditionalandexpression conditionalorexpression_p
| empty
*/
AST* Parser::expression(Token tok)
{
//std::cout<<"expression() "<<lexer->name_to_str(tok.getName())<<" "<<tok.getLexeme()<<std::endl;
return assignmentexpression(tok);
}
/*
assignmentexpression ->
'-' unaryexpression assignmentexpression_helper
| '!' unaryexpression assignmentexpression_helper
| NUMBER assignmentexpression_helper
| STRING assignmentexpression_helper
| TRUE assignmentexpression_helper
| FALSE assignmentexpression_helper
| '(' expression ')' assignmentexpression_helper
| identifier assignmentexpression_p
*/
AST* Parser::assignmentexpression(Token tok)
{
//std::cout<<"assignmentexpression() "<<lexer->name_to_str(tok.getName())<<" "<<tok.getLexeme()<<std::endl;
switch(tok.getName())
{
case Token::T_SUB:
{
tok = this->lexer->lex();
AST* una_node = new UnaryExp(AST::SUB, unaryexpression(tok));
tok = this->lexer->lex();
return assignmentexpression_helper(tok, una_node);
}
case Token::T_NOT:
{
tok = this->lexer->lex();
AST* una_node = new UnaryExp(AST::NOT, unaryexpression(tok));
tok = this->lexer->lex();
return assignmentexpression_helper(tok, una_node);
}
case Token::T_NUMBER:
{
AST* num_node = new Literal(AST::NUMBER, tok.getLexeme());
tok = this->lexer->lex();
return assignmentexpression_helper(tok, num_node);
}
case Token::T_STRING:
{
AST* str_node = new Literal(AST::STRING, tok.getLexeme());
tok = this->lexer->lex();
return assignmentexpression_helper(tok, str_node);
}
case Token::T_TRUE:
{
AST* true_node = new Literal(AST::TRUE, tok.getLexeme());
tok = this->lexer->lex();
return assignmentexpression_helper(tok, true_node);
}
case Token::T_FALSE:
{
AST* false_node = new Literal(AST::FALSE, tok.getLexeme());
tok = this->lexer->lex();
return assignmentexpression_helper(tok, false_node);
}
case Token::T_LBRACKET:
{
tok = this->lexer->lex();
AST* exp_node = expression(tok);
tok = this->lexer->lex();
if(tok.getName() != Token::T_RBRACKET)
{
std::cerr<<"Expected ')' after expression near line "<<tok.getLine()<<std::endl;
exit(1);
}
tok = this->lexer->lex();
return assignmentexpression_helper(tok, exp_node);
}
case Token::T_ID:
{
AST* id_node = new ID(tok.getLexeme());
tok = this->lexer->lex();
return assignmentexpression_p(tok, id_node);
}
default:
{
std::cerr<<"Expected valid expression near line "<<tok.getLine()<<std::endl;
exit(1);
return NULL;
}
}
}
/* assignmentexpression_helper ->
multiplicativeexpression_p additiveexpression_p relationalexpression_p
equalityexpression_p conditionalandexpression_p conditionalorexpression_p
*/
AST* Parser::assignmentexpression_helper(Token tok, AST* first_node)
{
//std::cout<<"assignmentexpression_helper() "<<lexer->name_to_str(tok.getName())<<" "<<tok.getLexeme()<<std::endl;
if(tok.getName() == Token::T_EOF)
return first_node;
AST* mult_node = multiplicativeexpression_p(tok, first_node);
tok = this->lexer->lex();
AST* add_node = additiveexpression_p(tok, mult_node);
tok = this->lexer->lex();
AST* rel_node = relationalexpression_p(tok, add_node);
tok = this->lexer->lex();
AST* eq_node = equalityexpression_p(tok, rel_node);
tok = this->lexer->lex();
AST* and_node = conditionalandexpression_p(tok, eq_node);
tok = this->lexer->lex();
AST* or_node = conditionalorexpression_p(tok, and_node);
return or_node;
}
/*
assignmentexpression_p ->
|'(' functioninvocation_p assignmentexpression_helper
| assignmentexpression_helper
| '=' assignmentexpression
*/
AST* Parser::assignmentexpression_p(Token tok, AST* id_node)
{
//std::cout<<"assignmentexpression_p() "<<lexer->name_to_str(tok.getName())<<" "<<tok.getLexeme()<<std::endl;
if(tok.getName() == Token::T_LBRACKET)
{
tok = this->lexer->lex();
AST* funcall_node = functioninvocation_p(tok, id_node);
tok = this->lexer->lex();
return assignmentexpression_helper(tok, funcall_node);
}
else if(tok.getName() == Token::T_EQ)
{
tok = this->lexer->lex();
return assignment(tok, id_node);
}
else
{
return assignmentexpression_helper(tok, id_node);
}
}
// assignment -> identifier '=' assignmentexpression
AST* Parser::assignment(Token tok, AST* id_node)
{
//std::cout<<"assignment() "<<lexer->name_to_str(tok.getName())<<" "<<tok.getLexeme()<<std::endl;
AST* assn_exp = assignmentexpression(tok);
AST* assn_stmt = new AssnStmt(id_node, assn_exp);
return assn_stmt;
}
/*
multiplicativeexpression -> unaryexpression multiplicativeexpression_p
*/
AST* Parser::multiplicativeexpression(Token tok)
{
//std::cout<<"multiplicativeexpression() "<<lexer->name_to_str(tok.getName())<<" "<<tok.getLexeme()<<std::endl;
AST* una_node = unaryexpression(tok);
tok = this->lexer->lex();
return multiplicativeexpression_p(tok, una_node);
}
/*
multiplicativeexpression_p ->
'*' unaryexpression multiplicativeexpression_p
| '/' unaryexpression multiplicativeexpression_p
| '%' unaryexpression multiplicativeexpression_p
| empty
*/
AST* Parser::multiplicativeexpression_p(Token tok, AST* prev_node)
{
//std::cout<<"multiplicativeexpression_p() "<<lexer->name_to_str(tok.getName())<<" "<<tok.getLexeme()<<std::endl;
switch(tok.getName())
{
case Token::T_EOF:
{
return prev_node;
}
case Token::T_MULT:
{
tok = this->lexer->lex();
AST* una_node = unaryexpression(tok);
AST* ar_node = new ArExp(AST::MULT, prev_node, una_node);
tok = this->lexer->lex();
return multiplicativeexpression_p(tok, ar_node);
}
case Token::T_DIV:
{
tok = this->lexer->lex();
AST* una_node = unaryexpression(tok);
AST* ar_node = new ArExp(AST::DIV, prev_node, una_node);
tok = this->lexer->lex();
return multiplicativeexpression_p(tok, ar_node);
}
case Token::T_MOD:
{
tok = this->lexer->lex();
AST* una_node = unaryexpression(tok);
AST* ar_node = new ArExp(AST::MOD, prev_node, una_node);
tok = this->lexer->lex();
return multiplicativeexpression_p(tok, ar_node);
}
default:
{
this->lexer->unlex(tok);
return prev_node;
}
}
}
// additiveexpression -> multiplicativeexpression additiveexpression_p
AST* Parser::additiveexpression(Token tok)
{
//std::cout<<"additiveexpression() "<<lexer->name_to_str(tok.getName())<<" "<<tok.getLexeme()<<std::endl;
AST* mult_node = multiplicativeexpression(tok);
tok = this->lexer->lex();
return additiveexpression_p(tok, mult_node);
}
/*
additiveexpression_p ->
'+' multiplicativeexpression additiveexpression_p
| '-' multiplicativeexpression additiveexpression_p
| empty
*/
AST* Parser::additiveexpression_p(Token tok, AST* prev_node)
{
//std::cout<<"additiveexpression_p() "<<lexer->name_to_str(tok.getName())<<" "<<tok.getLexeme()<<std::endl;
switch(tok.getName())
{
case Token::T_EOF:
{
return prev_node;
}
case Token::T_ADD:
{
tok = this->lexer->lex();
AST* mult_node = multiplicativeexpression(tok);
AST* ar_node = new ArExp(AST::ADD, prev_node, mult_node);
tok = this->lexer->lex();
return additiveexpression_p(tok, ar_node);
}
case Token::T_SUB:
{
tok = this->lexer->lex();
AST* mult_node = multiplicativeexpression(tok);
AST* ar_node = new ArExp(AST::SUB, prev_node, mult_node);
tok = this->lexer->lex();
return additiveexpression_p(tok, ar_node);
}
default:
{
this->lexer->unlex(tok);
return prev_node;
}
}
}
// relationalexpression -> additiveexpression relationalexpression_p
AST* Parser::relationalexpression(Token tok)
{
//std::cout<<"relationalexpression() "<<lexer->name_to_str(tok.getName())<<" "<<tok.getLexeme()<<std::endl;
AST* add_node = additiveexpression(tok);
tok = this->lexer->lex();
return relationalexpression_p(tok, add_node);
}
/*
relationalexpression_p ->
'<' additiveexpression relationalexpression_p
| '>' additiveexpression relationalexpression_p
| LE additiveexpression relationalexpression_p
| GE additiveexpression relationalexpression_p
| empty
*/
AST* Parser::relationalexpression_p(Token tok, AST* prev_node)
{
//std::cout<<"relationalexpression_p() "<<lexer->name_to_str(tok.getName())<<" "<<tok.getLexeme()<<std::endl;
switch(tok.getName())
{
case Token::T_EOF:
{
return prev_node;
}
case Token::T_LT:
{
tok = this->lexer->lex();
AST* add_node = additiveexpression(tok);
AST* rel_node = new CmpExp(AST::LT, prev_node, add_node);
tok = this->lexer->lex();
return relationalexpression_p(tok, rel_node);
}
case Token::T_LTE:
{
tok = this->lexer->lex();
AST* add_node = additiveexpression(tok);
AST* rel_node = new CmpExp(AST::LTE, prev_node, add_node);
tok = this->lexer->lex();
return relationalexpression_p(tok, rel_node);
}
case Token::T_GT:
{
tok = this->lexer->lex();
AST* add_node = additiveexpression(tok);