-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain (2).cpp
798 lines (679 loc) · 20.9 KB
/
main (2).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
//By: Ben Lanoue, Kristin Tragarz, Nick Dea
#include <cstdlib>
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
class Expr;
class Stmt;
// Runtime Global Variables
int pc; // program counter
vector<string> lexemes;
vector<string> tokens;
vector<string>::iterator lexitr;
vector<string>::iterator tokitr;
map<string, int> vartable; // map of variables and their values
vector<Stmt *> insttable; // table of instructions
map<string, string> symboltable; // map of variables to datatype (i.e. sum t_integer)
class Expr{ // expressions are evaluated!
public:
virtual int eval() = 0;
virtual string toString() = 0;
virtual ~Expr(){}
};
//Kristin Tragarz
class ConstExpr : public Expr{
private:
int value;
public:
//pre: 1st parameter is a valid number or boolean from file
//post: ConstExpr is initialized with the number or boolean
ConstExpr(int val){
value = val;
};
//pre: none
//post: number or boolean is returned
int eval(){
return value;
};
//pre: none
//post: Expr contents are converted to a string and returned
string toString(){
string temp;
temp = "" + value;
return(temp);
};
};
//Ben Lanoue
class IdExpr : public Expr{
private:
string id;
public:
//pre: valid variable name as s
//post: id is initiated to s
IdExpr(string s){
id = s;
}
//pre:id needs to be in the vartable map
//post: returns the value of id from vartable map
int eval(){
map<string,int>::iterator a = vartable.find(id);
return a->second;
}
//pre:none
//post:returns a string that shows id value and its value
//on the vartable map
string toString(){
string asnString = "id: " + id + " " + "value: " + to_string(eval()) + "\n";
return asnString;
}
};
//Nick Dea
class InFixExpr : public Expr {
private:
vector<Expr *> exprs;
vector<string> ops; // tokens of operators
vector<Expr *>::iterator exprsitr;
vector<string>::iterator opsitr;
public:
// pre:Valid expression pointer vector and vector of strings containing operators
// post:InFixExpr is initialized
InFixExpr(vector<Expr *> exp, vector<string> o) {
exprs = exp;
ops = o;
exprsitr = exprs.begin();
opsitr = ops.begin();
}
// pre:none
// post:Deletes the expression pointers
~InFixExpr() {
for (int i = 0; i < exprs.size(); i++) {
delete exprs.at(i);
}
}
// pre:none
// post:Evaluates the infix expression(s); returning an integer.
int eval() {
int result = (**exprsitr).eval();
exprsitr++;
while (exprsitr != exprs.end()) {
if (*opsitr == "+") {
result = result + (**exprsitr).eval();
exprsitr++;
opsitr++;
} else if (*opsitr == "-") {
result = result - (**exprsitr).eval();
exprsitr++;
opsitr++;
} else if (*opsitr == "*") {
result = result * (**exprsitr).eval();
exprsitr++;
opsitr++;
} else if (*opsitr == "/") {
result = result / (**exprsitr).eval();
exprsitr++;
opsitr++;
} else if (*opsitr == "%") {
result = result % (**exprsitr).eval();
exprsitr++;
opsitr++;
}
}
return result;
}
// pre:none
// post:Prints the expressions, operators, and what the result is.
string toString() {
string expressions = "";
for (int i = 0; i < exprs.size(); i++) {
expressions = expressions + " " + to_string((*exprs.at(i)).eval());
}
string operators = "";
for (int i = 0; i < ops.size(); i++) {
expressions = expressions + " " + ops.at(i);
}
return "Expressions: " + expressions +
"\n" + "Operators: " + operators
+ "\n" + "After eval(): " + to_string(eval());
}
};
// Runtime Global Methods
// prints vartable, instable, symboltable
class Stmt{ // statements are executed!
private:
string name;
public:
Stmt(){};
Stmt(string n){
name = n;
}
virtual ~Stmt(){};
virtual string toString() = 0;
virtual void execute() = 0;
void setName(string n){
name = n;
}
string getName(){
return name;
}
};
//Kristin Tragarz
class AssignStmt : public Stmt{
private:
string var;
Expr* p_expr;
public:
//pre: none
//post: AssignStmt object is initialized
AssignStmt(){};
//pre: 1st parameter is valid token name
//post: AssignStmt object is initialized with a name
AssignStmt(string n):Stmt(n)
{}
//pre: 1st parameter is valid token name, 2nd parameter is valid id name, 3rd parameter is
// a created expr pointer
//post: AssignStmt object is initialized
AssignStmt(string n, string v, Expr* p):Stmt(n){
var = v;
p_expr = p;
}
//pre: none
//post: allocated memory for p_expr is released
~AssignStmt(){
delete p_expr;
}
//pre: none
//post: contents of assignment statement instruction are converted to a string and returned
string toString(){
string asnString;
asnString = "name: " + getName() + " var: " + var + " expr: " + p_expr->toString() + "\n";
return(asnString);
}
//pre: none
//post: assignment statement is executed
void execute(){
int temp = p_expr->eval();
vartable.erase(var);
vartable.insert({var, temp});
}
};
//Ben Lanoue
class InputStmt : public Stmt{
private:
string var;
public:
//pre: valid variable name, valid token
//post: initializes name and var
InputStmt(string n, string v):Stmt(n)
{
var = v;
}
//pre: none
//post: none
~InputStmt(){}
//pre:none
//post:returns the values of name and var into a string
string toString(){
string asnString = "name: " + getName() + " var: " + var;
return asnString;
}
//pre: name and var initialized, var is on the vartable map
//post: the key var will have a value of of whatever int
//the user inputs in vartable
void execute(){
int temp;
cin >> temp;
vartable.erase(var);
vartable.insert({var,temp});
}
};
//Nick Dea
class StrOutStmt : public Stmt {
private:
string value;
public:
// pre:Valid token name and value
// post:StrOutStmt is initialized
StrOutStmt(string n, string v):Stmt(n)
{
value = v;
}
// pre:none
// post:none
~StrOutStmt() {
}
// pre:none
// post:returns a string with the name and value of the statement
string toString() {
return "Name: " + getName() + " Value: " + value + "\n";
}
// pre:none
// post:prints out the value to the console
void execute() {
cout << value << endl;
}
};
//Ben Lanoue
class ExprOutStmt : public Stmt{
private:
Expr* p_expr;
public:
//pre:valid name token, valid expression
//post: initializes name and p_expr
ExprOutStmt(string n, Expr* p):Stmt(n)
{
p_expr = p;
}
//pre: p_expr points to an Expr object
//post: p_expr deallocates the memory of p_expr
~ExprOutStmt(){
delete p_expr;
}
//pre: none
//post: returns a string with the value of name and
//p_expr values
string toString(){
string asnString;
asnString = "name: " + getName() + " " + p_expr->toString();
}
//pre: none
//post: sets p_expr to p
void setPointer(Expr* p){
p_expr = p;
}
//pre: p_expr is valid
//post: p_expr prints out 0 or 1 (false or true respectivaly)
void execute(){
cout << (p_expr->eval()) << endl;
}
};
//Kristin Tragarz
class IfStmt : public Stmt{
private:
Expr* p_expr;
int elsetarget;
public:
//pre: 1st parameter is valid token name and 2nd parameter is a Expr pointer already created
//post: IfStmt is initialized
IfStmt(string n, Expr* p):Stmt(n)
{
p_expr = p;
}
//pre: none
//post: pointer memory is released
~IfStmt(){
delete p_expr;
}
//pre: none
//post: elsetarget is returned
int getTarget(){
return elsetarget;
}
//pre: 1st parameter is a positive integer
//post: elsetarget is set
void setTarget(int t){
elsetarget = t;
}
//pre: none
//post: content of IfStmt is converted to string and returned
string toString(){
string ifStmt;
ifStmt = "name: " + getName() + " p_expr: " + p_expr->toString() + " target: " + to_string(elsetarget) + "\n";
}
//pre: none
//post: expression is evaluated and returned to call if true to continue else set the pc to the elsetarget
void execute(){
if(p_expr->eval() != 1)
pc = elsetarget;
}
};
//Nick Dea
class WhileStmt : public Stmt {
private:
Expr* p_expr;
int elsetarget;
public:
// pre:Valid token name and expression pointer
// post:StrOutStmt is initialized
WhileStmt(string n, Expr* p):Stmt(n)
{
p_expr = p;
}
// pre:none
// post:frees up the expression pointer
~WhileStmt() {
delete p_expr;
}
// pre:none
// post:prints the name, expression, and elsetarget
string toString() {
return "Name: " + getName() + " Expression: " + p_expr->toString() + " Target: " + to_string(elsetarget);
}
// pre:none
// post:returns a boolean if the while statement can continue
// and moves the program counter back to elsetarget
void execute() {
if (p_expr->eval() != 1)
pc = elsetarget;
}
// pre:valid expression pointer
// post:sets p_expr to the input pointer
void setPointer(Expr* p) {
p_expr = p;
}
// pre:valid elsetarget
// post:sets the elsetarget to the input elsetarget
void setTarget(int t) {
elsetarget = t;
}
// pre:none
// post:returns the elsetarget
int getTarget() {
return elsetarget;
}
};
class Compiler{
private:
//Kristin Tragarz
//pre: none
//post: an IfStmt object is created and added to the instruction table
void buildIf(){
tokitr++; lexitr++;
if(*tokitr != "s_lparen")
return;
string name = "t_if";
string express = "";
tokitr++; lexitr++;
while(*tokitr != "s_rparen"){
express = express + *lexitr;
tokitr++;
lexitr++;
}
Expr* exP = buildExpr(express);
IfStmt* ifs = new IfStmt(name,exP);
insttable.push_back(ifs);
return;
}
//Nick Dea
//pre:None
//post:Creates a WhileStmt object and adds it to the instruction table
void buildWhile() {
tokitr++;
lexitr++;
if (*tokitr != "s_lparen") {
return;
}
string name = "t_while";
string express = "";
tokitr++;
lexitr++;
while (*tokitr != "s_rparen") {
express = express + *lexitr;
tokitr++;
lexitr++;
}
Expr* exprP = buildExpr(express);
WhileStmt* whs = new WhileStmt(name, exprP);
insttable.push_back(whs);
}
/*
//Ben Lanoue
//pre: a valid token name
//post: returns a Stmt object
void buildStmt(){ // parent class, declares name
string b = *tokitr;
Stmt p = Stmt(b);
return p;
}*/
//Kristin Tragarz
//pre: none
//post: an AssignStmt object is created and added to the instruction table
void buildAssign(){
string var = *tokitr--;
string name = "s_assign";
tokitr++;
lexitr++;
string express = "";
while(*tokitr != "s_semi"){
express = express + *lexitr;
tokitr++;
lexitr++;
}
Expr* exP = buildExpr(express);
AssignStmt* as = new AssignStmt(name,var,exP);
insttable.push_back(as);
}
//Ben Lanoue
//pre: none
//post: makes a InputStmt object and puts it in the instruction vector
void buildInput(){
lexitr++;lexitr++;
tokitr++;tokitr++;
string b = *lexitr;
InputStmt* p = new InputStmt("t_input", b);
insttable.push_back(p);
}
//Kristin Tragarz
//pre: none
//post: either an ExprOutout or StmtOutput object is created and added to the instruction table
void buildOutput(){
//skip past left paren
tokitr++; lexitr++;
tokitr++; lexitr++;
string express = "";
//now at inside the ()
while(*tokitr != "s_rparen"){
express = express + *lexitr;
tokitr;
lexitr;
}
//determine which type of statement is contained within the ()
bool whatExprs = isOperator(express);
if(whatExprs == true){
StrOutStmt* stmtOut = new StrOutStmt("t_output", express);
insttable.push_back(stmtOut);
}
else{
Expr* press = buildExpr(express);
ExprOutStmt* expOut = new ExprOutStmt("t_output",press);
insttable.push_back(expOut);
}
}
//Kristin Tragarz
//pre: string is comprised of letters/numbers within the () of an output statement
//post: returns false if an operator is found, true otherwise
bool isOperator(string s){
bool check;
if (s[0] == '>' || '<' || '+' || '-' || '*' || '/' ){
check = false;
return check;
}
else{
string substr = "";
int size = s.size();
for(int i = 1; i < size; i++){
substr = substr + s[i];
}
check = true;
return(isOperator(substr));
}
return check;
}
//Ben Lanoue
//pre: string b, which is the expression
//post: returns a pointer to a Expr object this method made
Expr* buildExpr(string b){
int i = 0;
map<string,int>::iterator v;
v = vartable.find(b);
bool isConst = true;
bool isVar = (v != vartable.end());
bool isInFix = false;
while(i != b.size()) {
if(b.at(i) < 48 && b.at(i) > 57){
isConst = false;
}
i++;
}
string::size_type sz; // alias of size_t
if(isConst){
int b2 = stoi (b,&sz);
//int b2 = (int) b;
Expr* p = new ConstExpr(b2);
return p;
}
else if(isVar){
Expr* p = new IdExpr(b);
return p;
}
else{
//This does not work, but returns 0/false
//so the code continues
Expr* p = new IdExpr("0");
return p;
}
}
//Kristin, Ben, Nick
//pre: 1st parameter to be a valid, open file
//post: token/lexeme vectors are created
void populateTokenLexemes(istream& infile){
string line, tok, lex;
int pos;
getline(infile, line);
while (!infile.eof()) {
pos = line.find(":");
tok = line.substr(0, pos - 1);
lex = line.substr(pos + 2, line.length() - pos - 2);
tokens.push_back(tok);
lexemes.push_back(lex);
getline(infile, line);
}
tokitr = tokens.begin();
lexitr = lexemes.begin();
}
//Kristin, Ben, Nick
//pre: 1st parameter needs to be a valid, open file
//post: symboltable is created
void populateSymbolTable(istream& infile){
string line, key, value;
int pos;
getline(infile, line);
while (!infile.eof()) {
pos = line.find(":");
value = line.substr(0, pos - 1);
key = line.substr(pos + 2, line.length() - pos - 2);
symboltable.insert({key,value});
getline(infile, line);
}
}
public:
Compiler(istream& source, istream& symbols){
populateTokenLexemes(source);
populateSymbolTable(symbols);
}
//Kristin Tragarz
//pre: tok/lex vectors and symboltable has been constructed
//post: instruction table is built
bool compile(){
//Iterates until we hit begin on the vectors.
while((*tokitr) != "t_begin"){
tokitr++;
lexitr++;
}
tokitr++;
lexitr++;
//starts with the first line after begin, checks cases, builds
while(tokitr != tokens.end()){
if((*tokitr) == "s_assign"){
buildAssign();
}
if((*tokitr) == "t_if"){
buildIf();
}
if((*tokitr) == "t_while"){
buildWhile();
}
if((*tokitr) == "t_output"){
buildOutput();
}
if((*tokitr) == "t_input"){
buildInput();
}
tokitr++;
lexitr++;
}
return true;
}
//Ben Lanoue
//pre: none
//post: runs the insttable
void run(){ // executes the instruction table
pc = 0;
for (pc; pc < insttable.size(); pc++){
if((insttable.at(pc)->getName()).compare("t_if") == 0){
(*insttable.at(pc)).execute();
pc--;
}
else if (insttable.at(pc)->getName().compare("t_while") == 0){
(*insttable.at(pc)).execute();
pc--;
}
else{
(*insttable.at(pc)).execute();
}
}
}
};
// Nick Dea
// pre:none
// post:prints vartable, instable, symboltable
void dump() {
map<string, int>::iterator vtabitr = vartable.begin();
vector<Stmt *>::iterator itabitr = insttable.begin();
map<string, string>::iterator stabitr = symboltable.begin();
cout << endl;
cout << "Variable table:" << endl;
while (vtabitr != vartable.end()) {
cout << vtabitr->first << " " << vtabitr->second << endl;
vtabitr++;
}
cout << endl;
cout << "Instruction table:" << endl;
while (itabitr != insttable.end()) {
cout << (*itabitr)->toString() << endl;
itabitr++;
}
cout << endl;
cout << "Symbol table:" << endl;
while (stabitr != symboltable.end()) {
cout << stabitr->first << " " << stabitr->second << endl;
stabitr++;
}
return;
}
//Kristin Tragarz
int main(int argc, char** argv) {
string sourceF;
string symbolF;
cout << "Enter the name of the source code file: ";
cin >> sourceF;
ifstream sourcefile(sourceF);
if(!sourcefile){
cout << "File could not be found. Ending program" << endl;
exit(-1);
}
cout << "\nEnter the name of the symbol table file: ";
cin >> symbolF;
ifstream symbolfile(symbolF);
if(!symbolfile){
cout << "File could not be found. Ending program" << endl;
exit(-1);
}
Compiler c(sourcefile, symbolfile);
c.compile();
c.run();
dump();
return 0;
}