-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstage2.cpp
2992 lines (2543 loc) · 80.6 KB
/
stage2.cpp
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
// Authors: Eual Smith, Brendan Murphey, !Jacob Causer
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <stack>
#include <sstream>
#include <time.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
enum storeType { INTEGER, BOOLEAN, PROG_NAME, UNKNOWN };
enum allocation { YES, NO };
enum modes { VARIABLE, CONSTANT };
struct entry{
string internalName;
string externalName;
storeType dataType;
modes mode;
string value;
allocation alloc;
int units;
};
//Stage 0
void CreateListingHeader();
void Parser();
void CreateListingTrailer();
void PrintSymbolTable();
void Prog();
void ProgStmt();
void Consts();
void Vars();
void BeginEndStmt();
void ConstStmts();
void VarStmts();
string Ids();
void Insert(string,storeType, modes, string, allocation, int);
storeType WhichType(string);
string WhichValue(string);
string NextToken();
char NextChar();
void Error(string);
string genInternalName(storeType);
bool NonKeyID();
bool isSpecial(char);
bool isLetter(char);
bool isInteger();
bool isBoolean();
//stage 1
void EXEC_STMTS();
void EXEC_STMT();
void ASSIGN_STMT();
void READ_STMT();
void READ_LIST();
void WRITE_STMT();
void WRITE_LIST();
void EXPRESS();
void EXPRESSES();
void TERM();
void TERMS();
void FACTOR();
void FACTORS();
void PART();
bool REL_OP();
bool ADD_LEVEL_OP();
bool MULT_LEVEL_OP();
void Code(string operatr, string operand1 = "", string operand2 = "");
void pushOperator(string);
void pushOperand(string);
string popOperator();
string popOperand();
string GetTemp();
void FreeTemp();
string GetMyJump();
void EmitProgramCode();
void EmitEndCode();
void EmitReadCode(string);
void EmitWriteCode(string);
void EmitAssignCode(string, string);
void EmitAdditionCode(string, string);
void EmitSubtractionCode(string, string);
void EmitNegCode(string);
void EmitMultiplicationCode(string, string);
void EmitDivisionCode(string, string);
void EmitModuloCode(string, string);
void EmitAndCode(string, string);
void EmitOrCode(string, string);
void EmitNotCode(string);
void EmitEqualityCode(string, string);
void EmitNotEqualCode(string, string);
void EmitGreaterThanCode(string, string);
void EmitGreaterThanEqualCode(string, string);
void EmitLessThanCode(string, string);
void EmitLessThanEqualCode(string, string);
bool isEXPRESS();
//stage 2
void IF_STMT();
void ELSE_PT();
void WHILE_STMT();
void REPEAT_STMT();
void NULL_STMT();
void EmitThenCode(string);
void EmitElseCode(string);
void EmitPostIfCode(string);
void EmitWhileCode();
void EmitDoCode(string);
void EmitPostWhileCode(string, string);
void EmitRepeatCode();
void EmitUntilCode(string, string);
bool isEXEC();
//**********************************GLOBAL VARIABLES***********************************
const int MAX_SYMBOL_TABLE_SIZE = 256;
int keyCount = 19;
string keys[19] = {"program", "begin", "end", "var", "const",
"integer", "boolean", "true", "false", "not", "read", "write",
"if", "then", "else", "repeat", "while", "do", "until"};
vector<entry> symbolTable;
ifstream sourceFile;
ofstream listingFile, objectFile;
string token;
char charac;
bool errorFound = false;
int lstLineNum = 0;
char prevCharac = ' ';
int boolCount = 0;
int intCount = 0;
int overflow = 0;
bool progFound = false;
const char END_OF_FILE = '$'; // arbitrary choice
string regA = "";
int myLabelCounter = -1;
int currentTempNo = -1;
int maxTempNo = -1;
int beginCounter = 0;
stack <string> Operator;
stack <string> Operand;
//*****************************************STAGE 0 BEGIN********************************************
int main(int argc, char **argv){
sourceFile.open(argv[1]); //accepts input from argv[1]
listingFile.open(argv[2]); //generates a listing to argv[2]
objectFile.open(argv[3]); //generates an object code to argv[3]
CreateListingHeader(); //top
Parser(); //main
CreateListingTrailer(); //end
//PrintSymbolTable(); //object file
return 0; //complete
}
// Creates the header forthe listing file
void CreateListingHeader(){
time_t now = time(NULL); //time (as given)
listingFile << left << "STAGE0: " << "EJ Smith, Brendan Murphey, Jacob Causer\t" << ctime(&now) << "\n"; //names
listingFile << setw(22) << "LINE NO." << "SOURCE STATEMENT\n\n"; //line numbers and source statements should be aligned under the headings
}
// Begins the program
void Parser(){
NextChar();// charac must be initialized to the first character of the source file
if(NextToken().compare("program")) //ifthe next token
Error("keyword \"program\" expected"); // process error: keyword "program" expected;
Prog();// parser implements the grammar rules, calling first rule
}
// Creates the trailer forthe listing file
void CreateListingTrailer(){
listingFile << "\nCOMPILATION TERMINATED\t" << setw(5) << ((errorFound)? '1' : '0') << " ERRORS" << " ENCOUNTERED\n";
}
// Prints the symbol table into the object file
void PrintSymbolTable(){
vector<entry>::iterator it;
time_t now = time(NULL);
objectFile << left << "STAGE1: " << "EJ Smith, Brendan Murphey, Jacob Causer\t" << ctime(&now) << "\n";
objectFile << "Symbol Table\n\n";
for(it = symbolTable.begin(); it < symbolTable.end(); ++it)
{
objectFile << left << setw(15) << it->externalName << " ";
objectFile << left << setw(4) << it->internalName << " ";
objectFile << right << setw(9) << ((it->dataType == 2)?"PROG_NAME":(it->dataType == 1)?"BOOLEAN":(it->dataType == 0)?"INTEGER":"") << " ";
objectFile << right << setw(8) << ((it->mode == 1)?"CONSTANT":(it->mode == 0)?"VARIABLE":"") << " ";
objectFile << right << setw(15) << ((it->value == "true")?"1":((it->value=="false")? "0" : it->value)) << " ";
objectFile << right << setw(3) << right << ((it->alloc)?"NO":"YES");
objectFile << setw(3) << it->units << "\n";
}
}
// Production 1
void Prog(){ //token should be "program"
if(token != "program"){
Error("keyword 'program' expected");
}
ProgStmt();
if(token == "const"){
Consts();
}
if(token == "var"){
Vars();
}
if(token != "begin"){
Error("keyword \"const\", \"var\", or \"begin\" expected"); // 11-18-15 EJ changed
}
BeginEndStmt();
if(token[0] != END_OF_FILE){
Error("no text may follow end");
}
}
// Production 2
void ProgStmt(){ //token should be "program"
string x;
if(token != "program")
Error("keyword 'program' expected");
x = NextToken();
// note: NonKeyID only checks TOKENS
if(!NonKeyID())
Error("program name expected");
if(NextToken() != ";")
Error("semicolon expected");
NextToken();
Insert(x,PROG_NAME,CONSTANT,x,NO,0);
}
// Production 3
void Consts(){ //token should be "const"
if(token != "const")
Error("keyword \"const\" expected");
NextToken();
if(!NonKeyID())
Error("non-keyword identifier must follow \"const\"");
ConstStmts();
}
// Production 4
void Vars(){ //token should be "var"
if(token != "var")
Error("keyword 'var' expected");
NextToken();
if(!NonKeyID())
Error("non-keyword identifier must follow 'var'");
VarStmts();
}
// Production 5
void BeginEndStmt(){
if (token != "begin")
Error("keyword \"begin\" expected");
else{
if (beginCounter == 0)
Code("program");
beginCounter++;
}NextToken();
if(NonKeyID() || token == "read" || token == "write" || token == "if" || token == "while" || token == "repeat" || token == ";" || token == "begin"){
EXEC_STMTS();
}if(token != "end"){
Error("keyword \"end\" expected"); // process error: keyword "end" expected
}else{
beginCounter--;
}NextToken();
if (beginCounter == 0 && token != ".")
Error("\".\" expected");
else if (beginCounter > 0 && token != ";")
Error("\";\" expected");
else if (beginCounter < 0 )
Error("need a \"begin\" before this point");
if (token == ".")
Code("end");
NextToken();
}
// Production 6
void ConstStmts(){ //token should be NON_KEY_ID
string x,y;
int spot = -1;
if(!NonKeyID())
Error("non-keyword identifier expected");
x = token;
if(NextToken() != "="){
Error("\"=\" expected");
}
y = NextToken();
if(y != "+" && y != "-" && y !="not" && !NonKeyID() && !isBoolean() && !isInteger())
Error("token to right of \"=\" illegal");
if(y == "+" || y == "-"){
NextToken();
if(!isInteger())
Error("integer expected after sign");
y = y + token;
}
if(y == "not"){
NextToken();
for(uint i = 0; i < symbolTable.size(); i++){
if(symbolTable[i].externalName == token)
spot = i;
}
if(spot != -1){
if(symbolTable[spot].dataType != BOOLEAN)
Error("boolean expected after not");
}
else if(!isBoolean())
Error("boolean expected after not");
// token was external name?
if(spot != -1){
if(symbolTable[spot].value == "true")
y = "false";
else if(symbolTable[spot].value == "false")
y = "true";
else
Error("invalid value forBOOLEAN type");
}
else if(token == "true"){
y = "false";
}else{
y = "true";
}
}
if(NextToken() != ";")
Error("\":\" expected");
Insert(x,WhichType(y),CONSTANT,WhichValue(y),YES,1);
NextToken();
if(token != "begin" && token != "var" && !NonKeyID())
Error("non-keyword identifier, \"begin\", or \"var\" expected");
if(NonKeyID())
ConstStmts();
}
// Production 7
void VarStmts(){ //token should now be NON_KEY_ID
string x, y;
if(!NonKeyID())
Error("non-keyword identifier expected");
x = Ids();
if(token != ":")
Error("\":\" expected");
NextToken();
if(token != "integer" && token != "boolean")
Error("illegal type follows \":\"");
y = token;
if(NextToken() != ";")
Error("semicolon expected");
if(y == "integer")
Insert(x, INTEGER, VARIABLE, "", YES, 1);
else if(y == "boolean")
Insert(x, BOOLEAN, VARIABLE, "", YES, 1);
else if(y == "program")
Insert(x, PROG_NAME, VARIABLE, "", NO, 0);
else
Error("not a valid storeType");
NextToken();
//ifthis doesn't work, put nexttoken before the if, and replace it with token
if(token != "begin" && !NonKeyID())
Error("non-keyword identifier or \"begin\" expected");
if(NonKeyID())
VarStmts();
}
// Production 8
string Ids(){ //token should be NON_KEY_ID
string temp,tempString;
if(!NonKeyID())
Error("non-keyword identifier expected");
tempString = token;
temp = token;
if(NextToken() == ",")
{
NextToken();
if(!NonKeyID())
Error("non-keyword identifier expected");
tempString = temp + "," + Ids();
}
return tempString;
}
// Inserts a new entry into the symbol table
void Insert(string externalName, storeType inType, modes inMode, string inValue,
allocation inAlloc, int inUnits)
{
string name;
string::iterator end = externalName.end();
for(string::iterator a = externalName.begin(); a < externalName.end(); a++){
name = ""; //initialize a new name
while((*a != ',') && (a < end) ){ //fill in name appropriately
name += *a;
a++;
}
if(!name.empty()){
if(name.length() > 15){ //ifthe name is too big (over 15), ignore characters past 15
name = name.substr(0,15);
}
for(int i = 0; i < symbolTable.size(); i++){ //ifthe name is already there, error!
if(symbolTable[i].externalName == name){
Error("multiple name definition");
}
}
if(find(keys, keys + keyCount, name) != keys + keyCount){ //see ifname matches keys
Error("illegal use of keyword"); //ifso, error
}
else{ //otherwise, set up the push_back
entry my;
if(name.length() > 15){
my.externalName = name.substr(0,15);
}else{
my.externalName = name;
}if(isupper(name[0])){
my.internalName = name;
}else{
my.internalName = genInternalName(inType);
}
if(inValue.length() > 15){
my.value = inValue.substr(0,15);
}else{
my.value = inValue;
}
my.dataType = inType;
my.mode = inMode;
my.alloc = inAlloc;
my.units = inUnits;
symbolTable.push_back(my);
}
++overflow;
if(overflow > 256)
Error("symbol table entries have exceeded the maximum allowed value");
}
}
}
// Determines which data type "name" has
storeType WhichType(string name){
string::iterator it;
vector<entry>::iterator vit;
bool isInteger = true;
if(name == "true" || name == "false")
return BOOLEAN;
if(isdigit(name[0]) || name[0] == '+' || name[0] == '-')
{
for(it = name.begin() + 1; it < name.end(); ++it)
{
if(!isdigit(*it))
isInteger = false;
}
if(isInteger)
return INTEGER;
}
for(vit = symbolTable.begin(); vit < symbolTable.end(); ++vit)
{
if(name == (*vit).externalName)
return (*vit).dataType;
}
Error("reference to undefined constant1");
}
// Determines which value "name" has
string WhichValue(string name)
{
string::iterator it;
vector<entry>::iterator vit;
bool isLiteral = true;
if(name == "true" || name == "false")
isLiteral = true;
else if(isdigit(name[0]) || name[0] == '+' || name[0] == '-')
{
for(it = name.begin() + 1; it < name.end(); ++it)
{
if(!isdigit(*it))
isLiteral = false;
}
if(isLiteral)
return name;
}
for(vit = symbolTable.begin(); vit < symbolTable.end(); ++vit)
{
if(name == (*vit).externalName)
return (*vit).value;
}
if(name == "")
Error("reference to undefined constant2");
return name;
}
// Returns the next token or end of file marker
string NextToken(){
token = "";
while (token == ""){
if(charac == '{'){ //process comment
while(true){
NextChar();
if(charac == END_OF_FILE){
break;
}else if(charac == '}'){
NextChar();
break;
}
}if(charac == END_OF_FILE){
Error("unexpected end of file");
}else if(charac == '}'){
Error("token can't start \"}\"");
}
}else if(charac == '}'){
Error("\"}\" cannot begin token");
}else if(isspace(charac)){
NextChar();
}else if(isSpecial(charac)){
token = charac;
NextChar();
}else if(charac == ':'){
token = charac;
NextChar();
if(charac == '='){
token+=charac;
NextChar();
}
}else if(charac=='<'){
token = charac;
NextChar();
if(charac=='=' || charac == '>'){
token+=charac;
NextChar();
}
}else if(charac=='>'){
token = charac;
NextChar();
if(charac=='='){
token+=charac;
NextChar();
}
}else if(charac == '_'){ //no leading _
Error("\'_\' cannot start an identifier");
}else if(isalpha(charac)){
token = charac;
charac = NextChar();
if(charac == '_'){
Error("\'_\' cannot start an identifier");
}while(isLetter(charac)){ //search lowercase, nums, and spaces. ifit's none of these, than npos is reached.
token+=charac;
NextChar();
}
}else if(isdigit(charac)){
token = charac;
while(isdigit(NextChar())){
token += charac;
}
}else if(charac == END_OF_FILE){
token = charac;
}else{
Error("illegal symbol");
}
}if(token[0] == '_'){ //no start _
Error("\"_\" cannot start an identifier");
}if(token[token.length() - 1] == '_'){ //no end _
Error("\"_\" cannot end an identifier");
}return token;
}
//special character?
bool isSpecial(char chara){
return (chara == ',' || charac == ';' || charac == '=' || charac == '+' || charac == '-' ||
charac == '.' || charac =='(' || charac ==')' || charac == '*');
}
//lowercase
bool isLetter(char chara){
return (islower(chara) || isdigit(chara) || chara == '_');
}
char NextChar(){
char myNext;
sourceFile.get(myNext);
//http://www.cplusplus.com/reference/ios/ios/good/
if(!sourceFile.good()){
charac = END_OF_FILE;
}else{
prevCharac = charac;
charac = myNext;
if(lstLineNum == 0){
lstLineNum++;
listingFile << setw(5) << right << lstLineNum << '|';
}else if(prevCharac == '\n'){
lstLineNum++;
listingFile << setw(5) << right << lstLineNum << '|';
}
listingFile << charac;
}
return charac;
}
// Displays errors to the listing file
void Error(string error){
errorFound = true;
listingFile << "\nError: Line " << lstLineNum << ": " << error << "\n";
CreateListingTrailer();
sourceFile.close();
listingFile.close();
objectFile.close();
exit(EXIT_FAILURE);
}
// Generates the internal name
string genInternalName(storeType genType){
ostringstream myOut;
if(genType == INTEGER){
myOut << "I" << intCount;
intCount++;
}else if(genType == BOOLEAN){
myOut << "B" << boolCount;
boolCount++;
}else if(genType == PROG_NAME){
if(progFound == false){
myOut << "P0";
progFound = true;
}else{
Error("only one program name allowed");
}
}
return myOut.str();
}
// Returns true if token is a NonKeyID, and false otherwise
bool NonKeyID()
{
if(token[0]=='_')
Error("cannot begin with \"_\" ");
if(isupper(token[0]))
Error("upper case characters not allowed");
if(!isalpha(token[0]))
return false;
// go through each char
for(int x = 1; x < (int)token.length(); x++)
{
if(isupper(token[x]))
Error("upper case characters not allowed");
if( !isalpha(token[x]) && !isdigit(token[x]) && token[x] != '_' )
return false;
}
return(find(keys, keys + keyCount, token) != keys + keyCount)? false : true; //token isn't key ID
}
//is it a boolean type?
bool isBoolean(){
return(token=="true" || token == "false");
}
//all chars must be a digit forit to be an integer type. is it so?
bool isInteger(){
for(int i = 0; i < (int)token.length(); i++){
if(!isdigit(token[i]))
return false;
}
return true;
}
/******************************************* END OF STAGE 0 Code ********************************************************/
/******************************************* START STAGE 1 Code **********************************************************/
const string EXPRESSFail = "Invalid expression: \"not\", \"true\", \"false\", \"(\", \"+\", \"-\", non-key ID, or integer expected";
//2
void EXEC_STMTS()
{
if(NonKeyID() || token == "read" || token == "write" || token == "if" || token == "while" || token == "repeat" || token == "begin" || token == ";")
{
EXEC_STMT();
EXEC_STMTS();
}
else if (token == "end" || token == "until")
{
;
}
else
{
Error("non-keyword identifier, \"read\", \"write\", or \"begin\" expected");
}
}
void EXEC_STMT(){
if(NonKeyID())
{
ASSIGN_STMT(); //ifit's not a keyID, assign it!
}
else if(token == "read")
{
READ_STMT();
}
else if(token == "write")
{
WRITE_STMT();
}
else if (token == "if")
{
IF_STMT();
}
else if (token == "while")
{
WHILE_STMT();
}
else if (token == "repeat")
{
REPEAT_STMT();
}
else if(token == ";")
{
NextToken(); //advance ;
}
else if(token == "begin")
{
BeginEndStmt();
}
else
{
Error("non-keyword id, read, or write statement expected");
}
if (token == ";")
NextToken();
}
//4
void ASSIGN_STMT(){
if(!NonKeyID())//check fornon key
Error("non_key_id expected");
string t = "" + token; //holds token
pushOperand(t); //push the token and move on
NextToken();
if(token != ":=")//token HAS to be := here forassignment
Error("Expected assignment operator");
else
pushOperator(":=");
NextToken();
if(isEXPRESS())
EXPRESS();
else
Error(EXPRESSFail); //need proper keywords
if(token != ";")
Error("\";\" expected "); //expected semicolon
Code(popOperator(), popOperand(), popOperand());
}
//5
void READ_STMT(){
if(token!= "read")//to read, token must be read.
Error("expected \"read\" forread statement");
NextToken(); //see what's being read
if(token != "(")//must be (forfunction
Error("\"(\" expected");
READ_LIST(); //read the list
if(NextToken()!= ";"){ //should be over
Error("Expected ;");
}
}
//6
void READ_LIST(){
if(token!= "(")//currently must be at (
Error("Expected (");
NextToken(); //ifso, proceed
string temp = Ids();
if(token!= ")"){
Error("Expected \",\" or \")\"");
}else
Code("read", temp);
}
//7
void WRITE_STMT(){
if(token != "write")
Error("Expected \"write\"");
NextToken();
if(token != "(")
Error("\"(\" expected");
WRITE_LIST();
if(NextToken()!= ";")
Error("Expected \";\"");
}
//8
void WRITE_LIST(){
if(token != "(")
Error("Expected \"(\"");
NextToken();
string temp = Ids();
if(token != ")"){ //is "," needed to be tested here?
Error("Expected \",\" or \")\"");
}else
Code("write", temp);
}
//9
void EXPRESS(){
if(token != "not" && token != "true" && token != "false" && token != "(" && token != "+" && token != "-" && !isInteger() && !NonKeyID())
Error(EXPRESSFail);
TERM();
if(token == "<>" || token == "=" || token == "<=" || token == ">=" || token == "<" || token == ">"){
EXPRESSES();
}else if(token == ")" || token == ";" || token == "then" || token == "do" || token == "until" || token == "begin"){
;
}
else
{
Error("Invalid expression1");
}
}
//10
void EXPRESSES(){
if(token != "<>" && token != "=" && token != "<=" && token != ">=" && token != "<" && token != ">")
Error("<>, =, <=, >=, <, or > expected");
pushOperator(token);
NextToken();
if(token != "not" && token != "true" && token != "false" && token != "(" && token != "+" && token != "-" && !isInteger() && !NonKeyID() )
Error("Invalid expression: 'not', 'true', 'false', '(', '+', '-', integer, or non-keyword id expected");
else
TERM();
Code(popOperator(), popOperand(), popOperand());
if(token == "<>" || token == "=" || token == "<=" || token == ">=" || token == "<" || token == ">")
{
EXPRESSES();
}
else if(!(token == ")" || token == ";" || token == "then" || token =="do" || token =="until"))
{
Error("Invalid expression");
}
}
//11
void TERM(){
if(token != "not" && token != "true" && token != "false" && token != "(" && token != "+" && token != "-" && !isInteger() && !NonKeyID() )
Error(EXPRESSFail);
FACTOR();
if(token == "-" || token == "+" || token == "or"){
TERMS();
}else if(token == "<>" || token == "=" || token == "<=" || token == ">=" || token == "<" || token == ">" || token == ")" || token == ";" || token == "then" || token =="do" || token =="until" || token == "begin"){
;
}
else
{
Error("Invalid expression3");
}
}
//12
void TERMS(){
if(!ADD_LEVEL_OP())
Error("\"-\", \"+\", or \"or\" expected");
pushOperator(token);
NextToken();
if(!isEXPRESS())
Error(EXPRESSFail);
else
FACTOR();
Code(popOperator(), popOperand(), popOperand());
if(ADD_LEVEL_OP()){
TERMS();
}else if(!REL_OP() && token != ")" && token != ";"){
Error("Invalid expression4");
}
}
//13
void FACTOR(){
if(!isEXPRESS())
Error(EXPRESSFail);
PART();
if(MULT_LEVEL_OP()){
FACTORS();
}else if(token == "<>" || token == "=" || token == "<=" || token == ">=" || token == "<" || token == ">" || token == ")" || token == ";" || token == "-" || token == "+" || token == "or" || token == "then" || token =="do" || token =="until" || token == "begin"){
;
}
else
{
Error("Invalid expression5");
}
}
//14
void FACTORS(){
if(!MULT_LEVEL_OP())
Error("invalid FACTORS: \"*\",\" div\", \"mod\", or \"and\" expected");
pushOperator(token);
NextToken();
if(!isEXPRESS())
Error(EXPRESSFail);
else
PART();
Code(popOperator(), popOperand(), popOperand());
if(MULT_LEVEL_OP()){
FACTORS();
}else if(!REL_OP() && !(token == ")" || token == ";") && !ADD_LEVEL_OP()){
Error("Multiplicative level operator expected");
}
}
//15
void PART(){
if(token == "not"){
NextToken();
if(token == "("){
NextToken();
if(!isEXPRESS())
Error(EXPRESSFail);
EXPRESS();
if(token !=")")
Error("\")\" expected");
NextToken();
Code("not", popOperand());
}else if(isBoolean()){
if(token == "true"){
pushOperand("false");
NextToken();
}else{
pushOperand("true");
NextToken();
}
}else if(NonKeyID()){
Code("not", token);
NextToken();
}
}else if(token == "+"){
NextToken();
if(token == "("){
NextToken();
if(!isEXPRESS())
Error(EXPRESSFail);
EXPRESS();
if(token != ")"){
Error("\")\" expected");
}
NextToken();
}else if(isInteger()|| NonKeyID()){
pushOperand(token);
NextToken();
}else{
Error("\"(\", integer, or non-keyword id expected");