-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cc
More file actions
895 lines (779 loc) · 17.8 KB
/
Copy pathparser.cc
File metadata and controls
895 lines (779 loc) · 17.8 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
/*
* Created by: Joshua Elkins
* Date: June 24th, 2023
*/
#include <iostream>
#include <string>
#include <set>
#include <map>
#include <vector>
#include "parser.h"
#include "lexer.h"
#include "inputbuf.h"
#include <cassert>
#include <algorithm>
using namespace std;
typedef enum {
TYPE_INT, TYPE_REAL, TYPE_BOOLEAN, TYPE_UNKNOWN
} Type;
void type_error(int line_num, string constraint) {
cout << "TYPE MISMATCH " << line_num << " " << constraint << endl;
exit(0); // TODO
}
class Graph
{
public:
void insertNode(string node)
{
if (graph.find(node) == graph.end()) {
graph[node] = set <string>();
typeTable[node] = TYPE_UNKNOWN;
variableOrder.push_back(node);
}
}
void dfsAllNeighbours(string node, vector <string>& out) {
if (visited[node]) {
return;
}
visited[node] = true;
out.push_back(node);
for (string neigh : graph[node]) {
dfsAllNeighbours(neigh, out);
}
}
vector<string> getAllNeighbours(string node) {
visited.clear();
vector <string> out;
dfsAllNeighbours(node, out);
// Sort out vector such that variables are in order they appear
vector <string> outInOrder;
for (string var : variableOrder) {
if (find(out.begin(), out.end(), var) != out.end()) {
outInOrder.push_back(var);
}
}
return outInOrder;
}
void mergeNodes(string node1, string node2, int line_num, string const& constraint)
{
Type v1 = typeTable[node1];
Type v2 = typeTable[node2];
if (v1 == v2) {
graph[node1].insert(node2);
graph[node2].insert(node1);
}
else if (v1 == TYPE_UNKNOWN) {
setValue(node1, v2, line_num, constraint);
graph[node1].insert(node2);
graph[node2].insert(node1);
}
else if (v2 == TYPE_UNKNOWN) {
setValue(node2, v1, line_num, constraint);
graph[node1].insert(node2);
graph[node2].insert(node1);
}
else {
type_error(line_num, constraint);
}
}
void setValueDfs(string node, Type type, int line_num, string const &constraint)
{
if (visited[node]) {
return;
}
visited[node] = true;
if (typeTable[node] == type) {
// pass
}
else if (typeTable[node] == TYPE_UNKNOWN) {
typeTable[node] = type;
}
else {
type_error(line_num, constraint);
}
for (string neigh : graph[node]) {
setValueDfs(neigh, type, line_num, constraint);
}
}
void setValue(string node, Type type, int line_num, string const &constraint)
{
visited.clear();
setValueDfs(node, type, line_num, constraint);
}
map <string, bool> visited;
map <string, Type> typeTable;
map <string, set<string>> graph;
vector <string> variableOrder;
};
Graph graph;
vector <string> tmp;
TokenType expressionResult;
string expressionNode;
// Syntax Error Function.
void syntax_error(){
cout << "Syntax Error\n";
exit(1);
}
/*
* Completed Function.
* Entry point to the program.
*/
int Parser::parse_program(){
#ifdef DEBUG
cout << "Entered Parse Program" << endl;
#endif
token = lexer.GetToken();
if(token.token_type == ID){
lexer.UngetToken(token);
parse_globalVars();
parse_body();
}
else if(token.token_type == LBRACE){
lexer.UngetToken(token);
parse_body();
}
else{
syntax_error();
}
return 0;
}
/*
* Completed Function.
* Acts as basic entry into the global variable list.
*/
int Parser::parse_globalVars(){
#ifdef DEBUG
cout << "Entered Parse Global Variables" << endl;
#endif
parse_vardecllist();
return 0;
}
/*
* Completed
* Loops our variable declaration list.
*/
int Parser::parse_vardecllist(){
#ifdef DEBUG
cout << "Entered Parse Variable Declaration List" << endl;
#endif
token = lexer.GetToken();
while(token.token_type == ID){
lexer.UngetToken(token);
parse_vardecl();
token = lexer.GetToken();
}
lexer.UngetToken(token);
return 0;
}
/*
* Completed Function
* Acts as a method to handle the declaration statements.
*/
int Parser::parse_vardecl(){
#ifdef DEBUG
cout << "Entered Parse Variable Declaration" << endl;
#endif
token = lexer.GetToken();
if(token.token_type != ID){
syntax_error();
}
lexer.UngetToken(token);
parse_varlist();
token = lexer.GetToken();
if(token.token_type != COLON){
syntax_error();
}
token = lexer.GetToken();
if(token.token_type == INT || token.token_type == REAL || token.token_type == BOO){
lexer.UngetToken(token);
parse_typename();
token = lexer.GetToken();
if(token.token_type != SEMICOLON){
syntax_error();
}
}
else{
syntax_error();
}
return 0;
}
/*
* Completed Function
* Acts as the gathering function for our variables
*/
int Parser::parse_varlist(){
#ifdef DEBUG
cout << "Entered Parse Variable List" << endl;
#endif
token = lexer.GetToken();
if(token.token_type != ID) {
syntax_error();
}
else{
// TOKEN
tmp.push_back(token.lexeme);
Token t2 = lexer.GetToken();
if(t2.token_type == COMMA){
while(token.token_type == ID && t2.token_type == COMMA){
// Gather ID token info here
token = lexer.GetToken();
// TOKEN
tmp.push_back(token.lexeme);
if(token.token_type != ID){
syntax_error();
}
t2 = lexer.GetToken();
}
lexer.UngetToken(t2);
}
else{
// Gather singular ID token info here
lexer.UngetToken(t2);
}
}
return 0;
}
/*
* Completed Function
* Just consumes the INT, REAL, or BOO tokens
*/
int Parser::parse_typename(){
#ifdef DEBUG
cout << "Entered Parse Type Name" << endl;
#endif
token = lexer.GetToken();
Type type = TYPE_UNKNOWN;
if(token.token_type == INT){
type = TYPE_INT;
}
else if(token.token_type == REAL){
type = TYPE_REAL;
}
else if(token.token_type == BOO){
type = TYPE_BOOLEAN;
}
else{
syntax_error();
}
for (int i = 0; i < (int)tmp.size(); ++i) {
graph.insertNode(tmp[i]);
graph.setValue(tmp[i], type, token.line_no, "C1");
}
tmp.clear();
return 0;
}
/*
* Completed Function
* Acts as the method to consume braces and enter statement list
*/
int Parser::parse_body(){
#ifdef DEBUG
cout << "Entered Parse Body" << endl;
#endif
token = lexer.GetToken();
if(token.token_type == LBRACE){
parse_stmtlist();
token = lexer.GetToken();
if(token.token_type != RBRACE){
syntax_error();
}
}
else{
syntax_error();
}
return 0;
}
/*
* Completed Function
* Acts as our looper to enter all our statements
*/
int Parser::parse_stmtlist(){
#ifdef DEBUG
cout << "Entered Parse Statement List" << endl;
#endif
token = lexer.GetToken();
while(token.token_type == ID || token.token_type == IF || token.token_type == WHILE || token.token_type == SWITCH){
lexer.UngetToken(token);
parse_stmt();
token = lexer.GetToken();
}
lexer.UngetToken(token);
return 0;
}
/*
* Completed Function
* Acts as our method to enter the specific statements
*/
int Parser::parse_stmt(){
#ifdef DEBUG
cout << "Entered Parse Statement" << endl;
#endif
token = lexer.GetToken();
if(token.token_type == ID){
lexer.UngetToken(token);
parse_assstmt();
}
else if(token.token_type == IF){
lexer.UngetToken(token);
parse_ifstmt();
}
else if(token.token_type == WHILE){
lexer.UngetToken(token);
parse_whilestmt();
}
else if(token.token_type == SWITCH){
lexer.UngetToken(token);
parse_switchstmt();
}
else{
syntax_error();
}
return 0;
}
/*
* Function Completed
* Acts as our assignment statement parser
*/
int Parser::parse_assstmt(){
#ifdef DEBUG
cout << "Entered Parse Assignment Statement" << endl;
#endif
token = lexer.GetToken();
if(token.token_type != ID){
syntax_error();
}
string lhs = token.lexeme;
graph.insertNode(lhs);
token = lexer.GetToken();
if(token.token_type != EQUAL){
syntax_error();
}
// EXPRESSION
parse_expression();
// ID, NUM, REAL, BOOL
if (expressionResult == ID) {
graph.mergeNodes(lhs, expressionNode, token.line_no, "C1");
}
else if (expressionResult == NUM) {
graph.setValue(lhs, TYPE_INT, token.line_no, "C1");
}
else if (expressionResult == REALNUM) {
graph.setValue(lhs, TYPE_REAL, token.line_no, "C1");
}
else if (expressionResult == TR || expressionResult == FA) {
graph.setValue(lhs, TYPE_BOOLEAN, token.line_no, "C1");
}
else {
cout << expressionResult << endl;
cout << "ASSIGNMENT STATEMENT ERROR\n";
}
token = lexer.GetToken();
if(token.token_type != SEMICOLON){
syntax_error();
}
return 0;
}
Type convert(TokenType type) {
switch (type) {
case NUM:
return TYPE_INT;
case REALNUM:
return TYPE_REAL;
case TR:
return TYPE_BOOLEAN;
case FA:
return TYPE_BOOLEAN;
default:
assert(0);
}
}
/*
* Completed Function
* Acts as our expression handling.
*/
int Parser::parse_expression(){
#ifdef DEBUG
cout << "Entered Parse Expression" << endl;
#endif
token = lexer.GetToken();
if(token.token_type == NOT){
auto tmpToken = token;
lexer.UngetToken(token);
parse_unaryOperator();
parse_expression();
if (expressionResult == ID) {
graph.setValue(expressionNode, TYPE_BOOLEAN, tmpToken.line_no, "C3");
}
else {
if (expressionResult != TR && expressionResult != FA) {
type_error(tmpToken.line_no, "C3");
}
}
// TYPE BOOL
// if Node, set it to bool
// or else check if bool
}
else if(token.token_type == PLUS || token.token_type == MINUS || token.token_type == MULT || token.token_type == DIV
|| token.token_type == GREATER || token.token_type == LESS || token.token_type == GTEQ || token.token_type == LTEQ || token.token_type == EQUAL || token.token_type == NOTEQUAL){
auto tmpToken = token;
lexer.UngetToken(token);
parse_binaryOperator();
parse_expression();
auto expressionResult1 = expressionResult;
auto expressionNode1 = expressionNode;
parse_expression();
auto expressionResult2 = expressionResult;
auto expressionNode2 = expressionNode;
// TYPES MUST BE SAME
// Possible types = ID (NODE), NUM, REAL, BOOL.
// NODE, NODE -> Make edge
// NODE, NUM/REAL/BOOL -> Set it to NUM/REAL/BOOL
// NUM/NUM, REAL/REAL, BOOL/BOOL -> NUM/REAL/BOOL
if (expressionResult1 == ID && expressionResult2 == ID) {
graph.mergeNodes(expressionNode1, expressionNode2, tmpToken.line_no, "C2");
expressionResult = ID;
expressionNode = expressionNode1;
}
else if (expressionResult1 == ID && (expressionResult2 == NUM || expressionResult2 == REALNUM || expressionResult2 == TR || expressionResult2 == FA)) {
graph.setValue(expressionNode1, convert(expressionResult2), tmpToken.line_no, "C2");
expressionResult = ID;
expressionNode = expressionNode1;
}
else if (expressionResult2 == ID && (expressionResult1 == NUM || expressionResult1 == REALNUM || expressionResult1 == TR || expressionResult1 == FA)) {
graph.setValue(expressionNode2, convert(expressionResult1), tmpToken.line_no, "C2");
expressionResult = ID;
expressionNode = expressionNode2;
}
else if (expressionResult1 == expressionResult2
|| (expressionResult1 == TR && expressionResult2 == FA)
|| (expressionResult1 == FA && expressionResult2 == TR)) {
expressionResult = expressionResult1;
}
else {
type_error(tmpToken.line_no, "C2");
}
if (tmpToken.token_type == GREATER || tmpToken.token_type == LESS || tmpToken.token_type == GTEQ || tmpToken.token_type == LTEQ || tmpToken.token_type == EQUAL || tmpToken.token_type == NOTEQUAL) {
expressionResult = TR;
}
}
else if(token.token_type == ID || token.token_type == NUM || token.token_type == REALNUM || token.token_type == TR || token.token_type == FA){
lexer.UngetToken(token);
parse_primary();
// NODE, NUM, REAL, BOOL
}
else{
syntax_error();
}
return 0;
}
/*
* Completed Function
* Gets our NOT token
*/
int Parser::parse_unaryOperator(){
#ifdef DEBUG
cout << "Entered Parse Unary Operator" << endl;
#endif
token = lexer.GetToken();
if(token.token_type != NOT){
syntax_error();
}
//Do something with the NOT
return 0;
}
/*
* Completed Function
* Acts as our binary handler
*/
int Parser::parse_binaryOperator(){
#ifdef DEBUG
cout << "Entered Binary Operator" << endl;
#endif
token = lexer.GetToken();
if(token.token_type == PLUS || token.token_type == MINUS || token.token_type == MULT || token.token_type == DIV){
// Do something with these Tokens
}
else if(token.token_type == GREATER || token.token_type == LESS || token.token_type == GTEQ || token.token_type == LTEQ || token.token_type == EQUAL || token.token_type == NOTEQUAL){
// Do something with these Tokens
}
else{
syntax_error();
}
return 0;
}
/*
* Completed Function
* Acts as our primary handler
*/
int Parser::parse_primary(){
#ifdef DEBUG
cout << "Entered Parse Primary" << endl;
#endif
token = lexer.GetToken();
if(token.token_type == ID || token.token_type == NUM || token.token_type == REALNUM || token.token_type == TR || token.token_type == FA){
// Do something with these Tokens
if (token.token_type == ID) {
graph.insertNode(token.lexeme);
expressionResult = ID;
expressionNode = token.lexeme;
}
else {
expressionResult = token.token_type;
}
}
else{
syntax_error();
}
return 0;
}
/*
* Completed Function
* Acts as our If Statement handler
*/
int Parser::parse_ifstmt(){
#ifdef DEBUG
cout << "Entered Parse If Statement" << endl;
#endif
token = lexer.GetToken();
Token tmpToken = token;
if(token.token_type != IF){
syntax_error();
}
token = lexer.GetToken();
if(token.token_type != LPAREN){
syntax_error();
}
parse_expression();
// must be bool
if (expressionResult == ID) {
graph.setValue(expressionNode, TYPE_BOOLEAN, tmpToken.line_no, "C4");
}
else if (expressionResult == TR || expressionResult == FA) {
// pass
}
else {
type_error(tmpToken.line_no, "C4");
}
token = lexer.GetToken();
if(token.token_type != RPAREN){
syntax_error();
}
parse_body();
return 0;
}
/*
* Completed Function
* Acts as our While Statement handler
*/
int Parser::parse_whilestmt(){
#ifdef DEBUG
cout << "Entered Parse While Statement" << endl;
#endif
token = lexer.GetToken();
Token tmpToken = token;
if(token.token_type != WHILE){
syntax_error();
}
token = lexer.GetToken();
if(token.token_type != LPAREN){
syntax_error();
}
parse_expression();
// must be bool
if (expressionResult == ID) {
graph.setValue(expressionNode, TYPE_BOOLEAN, tmpToken.line_no, "C4");
}
else if (expressionResult == TR || expressionResult == FA) {
// pass
}
else {
type_error(tmpToken.line_no, "C4");
}
token = lexer.GetToken();
if(token.token_type != RPAREN){
syntax_error();
}
parse_body();
return 0;
}
/*
* Completed Function
* Acts as out Switch Statement handler
*/
int Parser::parse_switchstmt(){
#ifdef DEBUG
cout << "Entered Switch Statement" << endl;
#endif
token = lexer.GetToken();
Token tmpToken = token;
if(token.token_type != SWITCH){
syntax_error();
}
token = lexer.GetToken();
if(token.token_type != LPAREN){
syntax_error();
}
parse_expression();
if (expressionResult == ID) {
graph.setValue(expressionNode, TYPE_INT, tmpToken.line_no, "C5");
}
else if (expressionResult == INT) {
// pass
}
else {
type_error(tmpToken.line_no, "C5");
}
token = lexer.GetToken();
if(token.token_type != RPAREN){
syntax_error();
}
token = lexer.GetToken();
if(token.token_type != LBRACE){
syntax_error();
}
parse_caselist();
token = lexer.GetToken();
if(token.token_type != RBRACE){
syntax_error();
}
return 0;
}
/*
*
*/
int Parser::parse_caselist(){
#ifdef DEBUG
cout << "Entered Parse Case List" << endl;
#endif
token = lexer.GetToken();
if(token.token_type == CASE){
while(token.token_type == CASE){
lexer.UngetToken(token);
parse_case();
token = lexer.GetToken();
}
lexer.UngetToken(token);
}
else{
syntax_error();
}
return 0;
}
int Parser::parse_case(){
#ifdef DEBUG
cout << "Entered Parse Case" << endl;
#endif
token = lexer.GetToken();
if(token.token_type != CASE){
syntax_error();
}
token = lexer.GetToken();
if(token.token_type != NUM){
syntax_error();
}
// Do something with this
token = lexer.GetToken();
if(token.token_type != COLON){
syntax_error();
}
parse_body();
return 0;
}
void printUnknown(vector <string> const& unknownVariables) {
for (int i = 0; i < (int)unknownVariables.size(); ++i) {
if (i) {
cout << ", ";
}
cout << unknownVariables[i];
}
cout << ": ? #\n";
}
int main(){
#ifdef DEBUG
cout << "Entered Main" << endl;
#endif
int i;
Parser* parseProgram = new Parser();
i = parseProgram->parse_program();
(void)i;
map <string, bool> printed;
for (string variable : graph.variableOrder) {
if (printed[variable]) {
continue;
}
if (graph.typeTable[variable] == TYPE_UNKNOWN) {
vector <string> unknown = graph.getAllNeighbours(variable);
printUnknown(unknown);
for (string v : unknown) {
printed[v] = true;
}
}
else {
printed[variable] = true;
cout << variable << ": ";
Type type = graph.typeTable[variable];
if (type == TYPE_INT) {
cout << "int";
}
else if (type == TYPE_REAL) {
cout << "real";
}
else if (type == TYPE_BOOLEAN) {
cout << "bool";
}
else if (type == TYPE_UNKNOWN) {
cout << "?";
}
else {
cout << "ERROR";
}
cout << " #" << endl;
}
}
// vector <string> unknown;
// for (string variable : graph.variableOrder) {
// if (graph.typeTable[variable] == TYPE_UNKNOWN) {
// if (unknown.empty() == false) {
// if (graph.areConnected(unknown.back(), variable)) {
// unknown.push_back(variable);
// }
// else {
// printUnknown(unknown);
// unknown.clear();
// unknown.push_back(variable);
// }
// }
// else {
// unknown.push_back(variable);
// }
// }
// else {
// if (unknown.empty() == false) {
// printUnknown(unknown);
// unknown.clear();
// }
// cout << variable << ": ";
// Type type = graph.typeTable[variable];
// if (type == TYPE_INT) {
// cout << "int";
// }
// else if (type == TYPE_REAL) {
// cout << "real";
// }
// else if (type == TYPE_BOOLEAN) {
// cout << "bool";
// }
// else if (type == TYPE_UNKNOWN) {
// cout << "?";
// }
// else {
// cout << "ERROR";
// }
// cout << " #" << endl;
// }
// }
// if (unknown.empty() == false) {
// printUnknown(unknown);
// }
return 0;
}