-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeParser.cpp
More file actions
942 lines (904 loc) · 43.6 KB
/
TypeParser.cpp
File metadata and controls
942 lines (904 loc) · 43.6 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
//
// TypeParser.cpp
// TypeParser
//
// Created by Eric Alford on 10/30/12.
// Copyright (c) 2012 Eric Alford. All rights reserved.
//
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "Type.h"
using namespace std;
string fileArray[10000];
string relop[5] = {"<",">","<=",">=","<>"};
vector<Type> types;
vector<string> declTypes, declVars, implicitTypes, explicitTypes, implicitVariables, explicitVariables;
int i, j, k, l, fileIndex=0, size = 0;
int typeNumber = 4;
bool multiTypeDecl, typeDeclVar, multiVarDecl, typeMismatch = false;
void printErrors(){
if(multiTypeDecl){
cout << "ERROR CODE 0" << endl;
exit(1);
}
if(typeDeclVar){
cout << "ERROR CODE 1" << endl;
exit(1);
}
if(multiVarDecl){
cout << "ERROR CODE 2" << endl;
exit(1);
}
if(typeMismatch){
cout << "ERROR CODE 3" << endl;
exit(1);
}
}
string getNumType(const string &s){
string numType = "false";
bool flag = true;
bool period = false;
string::const_iterator it = s.begin();
for(it=s.begin(); it<s.end(); it++){
if(!isdigit(*it) && *it != '.'){
flag = false;
numType = "NOTNUM";
}
if(*it == '.' && period){ //if there is more than one period, not a number.
numType = "NOTNUM";
flag = false;
}
if(flag && *it == '.' && !period){ //if this is the first period
numType = "REAL";
period = true;
}
}
if(!period && flag){
numType = "INT";
}
return numType;
}
bool isOperator(int op){
bool flag = false;
if(op == '+' | op == '-' | op == '*' | op == '/' | op == '=')
flag = true;
return flag;
}
void getFile(){
char c = getchar();
string word = "";
while(!feof(stdin)){
word = "";
while(isspace(c) && !feof(stdin)){
c = getchar();
}
while(!isspace(c) && !feof(stdin) && c != ';' && !isOperator(c) && c != ',' && c != '(' && c != ')'){
word += c;
c = getchar();
}
if(word != ""){
fileArray[size] = word;
size++;
}
if(c == ';' || isOperator(c)){
fileArray[size] = c;
size++;
c = getchar();
}
else
c = getchar();
}
}
void parseType(){
int index;
vector<string> typeList;
while(fileArray[fileIndex] != "VAR" && fileArray[fileIndex] != "{"){
typeList.clear();
index = -1;
while(fileArray[fileIndex] != ":"){ //add all variables to list if there are multiple on a single line.
typeList.push_back(fileArray[fileIndex]);
fileIndex++;
}
fileIndex++; //skip the : character
string type2 = fileArray[fileIndex];
fileIndex = fileIndex+2; //skip the semicolon.
for(i = 0; i<declTypes.size(); i++){ //make sure the type wasn't already declared.
for(j=0; j<typeList.size(); j++){
if(declTypes[i] == typeList[j]){
multiTypeDecl = true;
return;
}
}
}
if(!multiTypeDecl){
for(i = 0; i<types.size(); i++){ //is the right hand side a built in type?
if(types[i].typeName == type2){
index = i;
}
}
if(index != -1){ //the right hand side is a built in type
for(j=0; j<typeList.size(); j++){
if(types[index].addAlias(typeList[j])){
declTypes.push_back(typeList[j]); //add id to the list of declared types
explicitTypes.push_back(typeList[j]);
}
else{
multiTypeDecl = true; //if the type given was aready declared, we have an error.
return;
}
}
}
else{ //the right hand side is either an alias or implicitely declared
bool isAlias = false;
for(i = 0; i<types.size(); i++){
for(j=0; j<types[i].aliases.size(); j++){
if(types[i].aliases[j] == type2){
isAlias = true;
for(k=0; k<typeList.size(); k++){
if(types[i].addAlias(typeList[k])){
declTypes.push_back(typeList[k]); //add id to the list of declared types
explicitTypes.push_back(typeList[k]);
}
else{
multiTypeDecl = true; //if the type given was aready declared, we have an error.
return;
}
}
}
}
}
if(!isAlias){ //the right hand side is an implicitely declared type.
Type *newType = new Type(typeNumber, type2, true);
typeNumber++;
implicitTypes.push_back(type2);
declTypes.push_back(type2); //add type2 to the list of declared types
for(j=0; j<typeList.size(); j++){
explicitTypes.push_back(typeList[j]);
newType->addAlias(typeList[j]);
declTypes.push_back(typeList[j]); //add id to the list of declared types
}
types.push_back(*newType);
}
}
}
}
}
void parseVar(){
int index;
vector<string> varList;
while((fileArray[fileIndex] != "TYPE" && fileArray[fileIndex] != "{")){
varList.clear();
index = -1;
while(fileArray[fileIndex] != ":"){ //add all variables to list if there are multiple on a single line.
varList.push_back(fileArray[fileIndex]);
fileIndex++;
}
fileIndex++; //skip the : character
string type2 = fileArray[fileIndex]; //type2 is the right side.
fileIndex = fileIndex+2; //skip the semicolon.
for(i = 0; i<declVars.size(); i++){ //make sure the var(s) wasn't already declared.
for(j=0; j<varList.size(); j++){
if(declVars[i] == varList[j]){
multiVarDecl = true;
return;
}
}
}
for(i = 0; i<declTypes.size(); i++){ //make sure the var(s) isn't already a type name.
for(j=0; j<varList.size(); j++){
if(declTypes[i] == varList[j]){
typeDeclVar = true;
return;
}
}
}
if(!multiVarDecl){
for(i = 0; i<types.size(); i++){ //is the right hand side a built in type?
if(types[i].typeName == type2){
index = i;
}
}
if(index != -1){ //the right hand side is a built in type
for(j=0; j<varList.size(); j++){
if(types[index].addVariable(varList[j])){
declVars.push_back(varList[j]); //add id to the list of declared vars
explicitVariables.push_back(varList[j]);
}
else{
multiVarDecl = true; //if the var given was aready declared, we have an error.
return;
}
}
}
else{ //the right hand side is either an alias or implicitely declared
bool isAlias = false;
for(i = 0; i<types.size(); i++){
for(j=0; j<types[i].aliases.size(); j++){
if(types[i].aliases[j] == type2){
isAlias = true;
for(k=0; k<varList.size(); k++){
if(types[i].addVariable(varList[k])){
declVars.push_back(varList[k]); //add id to the list of declared vars
explicitVariables.push_back(varList[k]);
}
else{
multiVarDecl = true; //if the var given was aready declared, we have an error.
return;
}
}
}
}
}
if(!isAlias){ //the right hand side is an implicitely declared type.
Type *newType = new Type(typeNumber, type2, true);
typeNumber++;
implicitTypes.push_back(type2); //only add the right side to the list of implicit types/vars.
for(j=0; j<varList.size(); j++){
explicitVariables.push_back(varList[j]);
newType->addVariable(varList[j]);
declVars.push_back(varList[j]); //add id to the list of declared vars
}
declTypes.push_back(type2); //add type2 to the list of declared types
types.push_back(*newType);
}
}
}
}
}
int getType(string id){
int t = -1;
bool implicit = true;
int k, l;
for(k = 0; k<declVars.size(); k++){ //check to see if the var was declared.
if(id == declVars[k]){
implicit = false;
}
}
if(implicit){ //if it's not a variable, make sure it isn't a declared type;
for(k=0; k<declTypes.size(); k++){
if(id == declTypes[k])
t = -2; //a -2 will indicate it is a type, return error in parseBody() function.
}
}
else{
for(k=0; k<types.size(); k++){
for(l=0; l<types[k].variables.size(); l++){
if(types[k].variables[l] == id){
t = k;
}
}
}
}
return t;
}
void dropCopyType(int in, int drop){
if(in != drop){ //make sure they are not the same type
if(types[drop].typeName != ""){
types[in].aliases.push_back(types[drop].typeName);
}
for(i=0; i<types[drop].aliases.size(); i++){
types[in].aliases.push_back(types[drop].aliases[i]);
}
for(i=0; i<types[drop].variables.size(); i++){
types[in].variables.push_back(types[drop].variables[i]);
}
types.erase(types.begin()+drop); //erases the element at index drop.
}
}
bool isOperator(string op){
bool flag = false;
if(op == "+" | op == "-" | op == "*" | op == "/")
flag = true;
return flag;
}
void parseWhileExpression(){
int rightType;
fileIndex++;
//evaluate the while expression.
string leftSide = fileArray[fileIndex];
int leftType = getType(leftSide); //get the type number for the left hand side.
if(leftType == -2){ //-2 indicates the left side is a type id, we have an error.
typeDeclVar = true;
printErrors();
}
else{
string isNumLeft = getNumType(fileArray[fileIndex]);
fileIndex++;
if(fileArray[fileIndex] == "{"){ //no relop, only one id in while expression
if(isNumLeft != "NOTNUM"){ //a number is not allowed in this case.
typeMismatch = true;
printErrors();
}
if(leftType != -1 && leftType <= 3){ //leftside is not implicitely declared
if(leftType != 3){
typeMismatch = true;
printErrors();
}
}
else if(leftType == -1){ //the left side is an implicit variable, turn it into a boolean
declVars.push_back(leftSide);
types[3].variables.push_back(leftSide);
implicitVariables.push_back(leftSide);
}
else{ //leftType > 3, implicitely declared in the VAR or BODY section
dropCopyType(3, leftType);
}
fileIndex--;
}
else{ //must be a relop
bool isRelop = false;
for(i=0; i<5; i++){
if(relop[i] == fileArray[fileIndex]){
isRelop = true;
}
}
if(!isRelop){
cout << "Error. Relation Operator Expected" << endl;
exit(1);
}
fileIndex++;
rightType = getType(fileArray[fileIndex]);
string isNumRight = getNumType(fileArray[fileIndex]);
if(rightType == -2){ //-2 indicates the right side is a type id, we have an error.
typeDeclVar = true;
printErrors();
}
else if(leftType != -1 && leftType <= 3){ //left side is not implicitely declared
if(rightType != -1 && rightType <=3){ //right side is not implicitely declared
if(leftType != rightType){
typeMismatch = true;
printErrors();
}
}
else if(rightType == -1){ //right side is either a number or implicitely declared
if(isNumRight == "NOTNUM"){
declVars.push_back(fileArray[fileIndex]);
implicitVariables.push_back(fileArray[fileIndex]);
types[leftType].variables.push_back(fileArray[fileIndex]);
}
else if(isNumRight == "REAL"){
if(leftType != 1){
typeMismatch = true;
printErrors();
}
}
else{ //isNumRight is INT
if(leftType != 0){
typeMismatch = true;
printErrors();
}
}
}
else{ //rightType is > 3, implicitely declared with an implicit type.
dropCopyType(leftType, rightType);
}
}
else if(leftType == -1){ //left side is either a number or implicitely declared
if(isNumLeft == "NOTNUM"){ //left side is implicitely declared
if(rightType != -1 && rightType <=3){ //right side is not implicitely declared
declVars.push_back(leftSide);
implicitVariables.push_back(leftSide);
types[rightType].variables.push_back(leftSide);
}
else if(rightType == -1){ //right side is either a number or implicitely declared
if(isNumRight == "NOTNUM"){ //right side is implicitely declared
Type *newType = new Type(typeNumber, "", true);
typeNumber++;
newType->addVariable(leftSide);
newType->addVariable(fileArray[fileIndex]);
declVars.push_back(leftSide);
declVars.push_back(fileArray[fileIndex]);
implicitVariables.push_back(leftSide);
implicitVariables.push_back(fileArray[fileIndex]);
types.push_back(*newType);
}
else if(isNumRight == "REAL"){
declVars.push_back(leftSide);
implicitVariables.push_back(leftSide);
types[1].variables.push_back(leftSide);
}
else{ //isNumRight is INT
declVars.push_back(leftSide);
implicitVariables.push_back(leftSide);
types[0].variables.push_back(leftSide);
}
}
else{ //rightType is > 3, implicitely declared in the VAR or BODY section.
declVars.push_back(leftSide);
implicitVariables.push_back(leftSide);
types[rightType].variables.push_back(leftSide);
}
}
else if(isNumLeft == "REAL"){ //left side is a decimal number
if(rightType != -1 && rightType <=3){ //right side is not implicitely declared
if(rightType != 1){
typeMismatch = true;
printErrors();
}
}
else if(rightType == -1){ //right side is either a number or implicitely declared
if(isNumRight == "NOTNUM"){ //right side is implicitely declared
declVars.push_back(fileArray[fileIndex]);
implicitVariables.push_back(fileArray[fileIndex]);
types[1].variables.push_back(fileArray[fileIndex]);
}
else if(isNumRight == "REAL"){
//do nothing
}
else{ //isNumRight is INT
typeMismatch = true;
printErrors();
}
}
else{ //rightType is > 3, implicitely declared in the VAR or BODY section.
dropCopyType(1, rightType);
}
}
else{ //isNumLeft is an INT
if(rightType != -1 && rightType <=3){ //right side is not implicitely declared
if(rightType != 0){
typeMismatch = true;
printErrors();
}
}
else if(rightType == -1){ //right side is either a number or implicitely declared
if(isNumRight == "NOTNUM"){ //right side is implicitely declared
declVars.push_back(fileArray[fileIndex]);
implicitVariables.push_back(fileArray[fileIndex]);
types[0].variables.push_back(fileArray[fileIndex]);
}
else if(isNumRight == "REAL"){
typeMismatch = true;
printErrors();
}
else{ //isNumRight is INT
//do nothing
}
}
else{ //rightType is > 3, implicitely declared in the VAR or BODY section.
dropCopyType(0, rightType);
}
}
}
else{ //leftType > 3, implicitely declared in the VAR or BODY section.
if(rightType != -1 && rightType <=3){ //right side is not implicitely declared
dropCopyType(rightType, leftType);
}
else if(rightType == -1){ //right side is either a number or implicitely declared
if(isNumRight == "NOTNUM"){
declVars.push_back(fileArray[fileIndex]);
implicitVariables.push_back(fileArray[fileIndex]);
types[leftType].variables.push_back(fileArray[fileIndex]);
}
else if(isNumRight == "REAL"){
dropCopyType(1, leftType);
}
else{ //isNumRight is INT
dropCopyType(0, leftType);
}
}
else{ //rightType is > 3, implicitely declared with an implicit type.
dropCopyType(leftType, rightType);
}
}
}
}
}
void parseWhileBody(){
int leftType, rightType, indexTrack = 0;
while(fileArray[fileIndex] != "}"){
string leftSide = fileArray[fileIndex];
if(leftSide == "WHILE"){
parseWhileExpression();
fileIndex = fileIndex+2; //skip the { character
parseWhileBody();
fileIndex++;
}
else{
leftSide = fileArray[fileIndex];
leftType = getType(leftSide); //get the type number for the left hand side.
if(leftType == -2){ //-2 indicates the left side is a type id, we have an error.
typeDeclVar = true;
printErrors();
}
fileIndex++;
if(fileArray[fileIndex] == "="){
fileIndex++;
while(fileArray[fileIndex] != ";"){
if(indexTrack%2 > 0){ //if indexTrack is odd. fileIndex must be an operator.
if(!isOperator(fileArray[fileIndex])){
cout << "Error. Operator Expected" << endl;
exit(1);
}
fileIndex++;
indexTrack++;
}
else{ //not an operator
string isNum = getNumType(fileArray[fileIndex]);
if(leftType != -1 && leftType <= 3){ //leftside is not implicitely declared
if(isNum == "NOTNUM"){
rightType = getType(fileArray[fileIndex]);
if(rightType == -2){ //-2 indicates the right side is a type id, we have an error.
typeDeclVar = true;
printErrors();
}
if(rightType != -1 && !types[rightType].isImplicit){//right side is not
if(rightType != leftType){ //implicitely declared
typeMismatch = true;
printErrors();
}
}
//right side is implicitely declared with an implicit type
else if(rightType != -1 && types[rightType].isImplicit){
dropCopyType(leftType, rightType);
}
else{ //right side is implicitely declared
for(l=0; l<types.size(); l++){
if(types[l].typeNum == leftType)
types[l].variables.push_back(fileArray[fileIndex]);
}
}
}
else if(isNum == "REAL"){
if(leftType != 1){
typeMismatch = true;
printErrors();
}
}
else{ //isNum is type INT
if(leftType != 0){
typeMismatch = true;
printErrors();
}
}
fileIndex++;
indexTrack++;
}
else if(leftType == -1){ //left side is an implicitely declared variable
if(isNum == "NOTNUM"){
int rightType = getType(fileArray[fileIndex]);
if(rightType == -2){ //-2 indicates the right side is a type id, we have an error.
typeDeclVar = true;
printErrors();
}
if(rightType != -1){ //if rightType is not implicitely declared
for(l=0; l<types.size(); l++){
if(types[l].typeNum == rightType){
implicitVariables.push_back(leftSide); //add leftSide to implicit variable list
types[l].addVariable(leftSide);
declVars.push_back(leftSide); //add the left side to the declVars list since it now is defined.
leftType = getType(leftSide); //update the left side type for further analyzation.
}
}
}
//right side is implicitely declared with an implicit type
else if(rightType != -1 && types[rightType].isImplicit){
implicitVariables.push_back(leftSide); //add leftSide to implicit variable list
types[rightType].addVariable(leftSide);
declVars.push_back(leftSide); //add the left side to the declVars list since it now is defined.
leftType = getType(leftSide); //update the left side type for further analyzation.
}
else{ //right side is implicitely declared
Type *newType = new Type(typeNumber, "", true);
typeNumber++;
newType->addVariable(leftSide);
declVars.push_back(leftSide);
implicitVariables.push_back(leftSide);
if(leftSide != fileArray[fileIndex]){
newType->addVariable(fileArray[fileIndex]);
declVars.push_back(fileArray[fileIndex]);
implicitVariables.push_back(fileArray[fileIndex]);
}
types.push_back(*newType);
leftType = getType(leftSide); //update the left side type for further analyzation.
}
}
else if(isNum == "REAL"){
implicitVariables.push_back(leftSide);
types[1].addVariable(leftSide);
declVars.push_back(leftSide); //add the left side to the declVars list since it now is defined.
leftType = getType(leftSide); //update the left side type for further analyzation.
}
else{ //isNum is type INT
implicitVariables.push_back(leftSide);
types[0].addVariable(leftSide);
declVars.push_back(leftSide); //add the left side to the declVars list since it now is defined.
leftType = getType(leftSide); //update the left side type for further analyzation.
}
fileIndex++;
indexTrack++;
}
else{ //leftType > 3, implicitely declared in the VAR or BODY section
if(isNum == "NOTNUM"){
//update according to the else statement in the leftside implicit section (if type name is nothing)
int rightType = getType(fileArray[fileIndex]);
if(rightType == -2){ //-2 indicates the right side is a type id, we have an error.
typeDeclVar = true;
printErrors();
}
if(rightType != -1 && !types[rightType].isImplicit){//right side is not implicitely declared
dropCopyType(rightType, leftType);
leftType = getType(leftSide); //update the left side type for further analyzation.
}
//right side is implicitely declared with an implicit type
else if(rightType != -1 && types[rightType].isImplicit){
if(types[leftType].typeName != "")
dropCopyType(leftType, rightType);
else
dropCopyType(rightType, leftType);
leftType = getType(leftSide); //update the left side type for further analyzation.
}
else{ //right side is implicitely declared
types[leftType].addVariable(fileArray[fileIndex]);
declVars.push_back(fileArray[fileIndex]);
implicitVariables.push_back(fileArray[fileIndex]);
}
}
else if(isNum == "REAL"){
dropCopyType(1, leftType); //the left slide is already in the declVars list.
leftType = getType(leftSide); //update the left side type for further analyzation.
}
else{ //isNum is type INT
dropCopyType(0, leftType); //the left slide is already in the declVars list.
leftType = getType(leftSide); //update the left side type for further analyzation.
}
fileIndex++;
indexTrack++;
}
}
}
fileIndex++; //move fileIndex to the next position after the ";"
indexTrack = 0;
}
else{
cout << "Error. = Expected." << endl;
exit(1);
}
}
}
}
void parseBody(){
int leftType, rightType, indexTrack = 0;
while(fileArray[fileIndex] != "}"){
string leftSide = fileArray[fileIndex];
if(leftSide == "WHILE"){
parseWhileExpression();
fileIndex = fileIndex+2; //skip the { character
parseWhileBody();
fileIndex++;
}
else{
leftSide = fileArray[fileIndex];
leftType = getType(leftSide); //get the type number for the left hand side.
if(leftType == -2){ //-2 indicates the left side is a type id, we have an error.
typeDeclVar = true;
return;
}
fileIndex++;
if(fileArray[fileIndex] == "="){
fileIndex++;
while(fileArray[fileIndex] != ";"){
if(indexTrack%2 > 0){ //if indexTrack is odd. fileIndex must be an operator.
if(!isOperator(fileArray[fileIndex])){
cout << "Error. Operator Expected" << endl;
exit(1);
}
fileIndex++;
indexTrack++;
}
else{ //not an operator
string isNum = getNumType(fileArray[fileIndex]);
if(leftType != -1 && leftType <= 3){ //leftside is not implicitely declared
if(isNum == "NOTNUM"){
rightType = getType(fileArray[fileIndex]);
if(rightType == -2){ //-2 indicates the right side is a type id, we have an error.
typeDeclVar = true;
return;
}
if(rightType != -1 && !types[rightType].isImplicit){//right side is not
if(rightType != leftType){ //implicitely declared
typeMismatch = true;
return;
}
}
//right side is implicitely declared with an implicit type
else if(rightType != -1 && types[rightType].isImplicit){
dropCopyType(leftType, rightType);
}
else{ //right side is implicitely declared
for(l=0; l<types.size(); l++){
if(types[l].typeNum == leftType)
types[l].variables.push_back(fileArray[fileIndex]);
}
}
}
else if(isNum == "REAL"){
if(leftType != 1){
typeMismatch = true;
return;
}
}
else{ //isNum is type INT
if(leftType != 0){
typeMismatch = true;
return;
}
}
fileIndex++;
indexTrack++;
}
else if(leftType == -1){ //left side is an implicitely declared variable
if(isNum == "NOTNUM"){
int rightType = getType(fileArray[fileIndex]);
if(rightType == -2){ //-2 indicates the right side is a type id, we have an error.
typeDeclVar = true;
return;
}
if(rightType != -1){ //if rightType is not implicitely declared
for(l=0; l<types.size(); l++){
if(types[l].typeNum == rightType){
implicitVariables.push_back(leftSide); //add leftSide to implicit variable list
types[l].addVariable(leftSide);
declVars.push_back(leftSide); //add the left side to the declVars list since it now is defined.
leftType = getType(leftSide); //update the left side type for further analyzation.
}
}
}
//right side is implicitely declared with an implicit type
else if(rightType != -1 && types[rightType].isImplicit){
implicitVariables.push_back(leftSide); //add leftSide to implicit variable list
types[rightType].addVariable(leftSide);
declVars.push_back(leftSide); //add the left side to the declVars list since it now is defined.
leftType = getType(leftSide); //update the left side type for further analyzation.
}
else{ //right side is implicitely declared
Type *newType = new Type(typeNumber, "", true);
typeNumber++;
newType->addVariable(leftSide);
declVars.push_back(leftSide);
implicitVariables.push_back(leftSide);
if(leftSide != fileArray[fileIndex]){
newType->addVariable(fileArray[fileIndex]);
declVars.push_back(fileArray[fileIndex]);
implicitVariables.push_back(fileArray[fileIndex]);
}
types.push_back(*newType);
leftType = getType(leftSide); //update the left side type for further analyzation.
}
}
else if(isNum == "REAL"){
implicitVariables.push_back(leftSide);
types[1].addVariable(leftSide);
declVars.push_back(leftSide); //add the left side to the declVars list since it now is defined.
leftType = getType(leftSide); //update the left side type for further analyzation.
}
else{ //isNum is type INT
implicitVariables.push_back(leftSide);
types[0].addVariable(leftSide);
declVars.push_back(leftSide); //add the left side to the declVars list since it now is defined.
leftType = getType(leftSide); //update the left side type for further analyzation.
}
fileIndex++;
indexTrack++;
}
else{ //leftType > 3, implicitely declared in the VAR or BODY section
if(isNum == "NOTNUM"){
int rightType = getType(fileArray[fileIndex]);
if(rightType == -2){ //-2 indicates the right side is a type id, we have an error.
typeDeclVar = true;
return;
}
if(rightType != -1 && !types[rightType].isImplicit){//right side is not implicitely declared
dropCopyType(rightType, leftType);
leftType = getType(leftSide); //update the left side type for further analyzation.
}
//right side is implicitely declared with an implicit type
else if(rightType != -1 && types[rightType].isImplicit){
if(types[leftType].typeName != ""){
dropCopyType(leftType, rightType);
}
else
dropCopyType(rightType, leftType);
leftType = getType(leftSide); //update the left side type for further analyzation.
}
else{ //right side is implicitely declared
types[leftType].addVariable(fileArray[fileIndex]);
declVars.push_back(fileArray[fileIndex]);
implicitVariables.push_back(fileArray[fileIndex]);
}
}
else if(isNum == "REAL"){
dropCopyType(1, leftType); //the left slide is already in the declVars list.
leftType = getType(leftSide); //update the left side type for further analyzation.
}
else{ //isNum is type INT
dropCopyType(0, leftType); //the left slide is already in the declVars list.
leftType = getType(leftSide); //update the left side type for further analyzation.
}
fileIndex++;
indexTrack++;
}
}
}
fileIndex++; //move fileIndex to the next position after the ";"
indexTrack = 0;
}
else{
cout << "Error. = Expected." << endl;
exit(1);
}
}
}
}
int main(){
//initialize the built in types.
Type *integerType = new Type(0, "INT", false);
Type *realType = new Type(1, "REAL", false);
Type *stringType = new Type(2, "STRING", false);
Type *booleanType = new Type(3, "BOOLEAN", false);
types.push_back(*integerType);
types.push_back(*realType);
types.push_back(*stringType);
types.push_back(*booleanType);
getFile();
/*
for(i=0; i<size; i++){
cout << fileArray[i] << " ";
}
cout << endl;*/
if(fileArray[fileIndex] == "TYPE"){
fileIndex++;
parseType();
}
if(fileArray[fileIndex] == "VAR"){
fileIndex++;
parseVar();
}
if(fileArray[fileIndex] == "{"){
fileIndex++;
parseBody();
}
else{
cout << "Error. VAR, TYPE or { expected at beginning of input." << endl;
return 0;
}
printErrors();
for(i = 0; i<types.size(); i++){
if(types[i].typeName != ""){
if(types[i].aliases.size()!=0 || types[i].variables.size()!=0)
cout << types[i].typeName << " : ";
for(j=0; j<explicitTypes.size(); j++){ //print the explicit types in the order they were introduced.
for(k=0; k<types[i].aliases.size(); k++){
if(explicitTypes[j] == types[i].aliases[k])
cout << types[i].aliases[k] << " ";
}
}
for(j=0; j<implicitTypes.size(); j++){ //print the implicit types in the order they were introduced.
for(k=0; k<types[i].aliases.size(); k++){
if(implicitTypes[j] == types[i].aliases[k])
cout << types[i].aliases[k] << " ";
}
}
for(j=0; j<explicitVariables.size(); j++){ //print the explicit variables in the order they were introduced.
for(k=0; k<types[i].variables.size(); k++){
if(explicitVariables[j] == types[i].variables[k])
cout << types[i].variables[k] << " ";
}
}
for(j=0; j<implicitVariables.size(); j++){ //print the implicit variables in the order they were introduced.
for(k=0; k<types[i].variables.size(); k++){
if(implicitVariables[j] == types[i].variables[k])
cout << types[i].variables[k] << " ";
}
}
if(types[i].aliases.size()!=0 || types[i].variables.size()!=0)
cout << "#" << endl;
}
}
for(i = 0; i<types.size(); i++){
if(types[i].typeName == ""){
cout << types[i].variables[0] << " : " << types[i].variables[1] << " #" << endl;
}
}
}