-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.h
683 lines (603 loc) · 16.2 KB
/
parser.h
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
#ifndef PARSER_H_
#define PARSER_H_
#include <iostream>
using std::istream;
using std::ostream;
#include <string>
using std::string;
using std::stoi;
#include <map>
using std::map;
#include "lexer.h"
// indicates if parse errors were present
extern bool hasParseErrors;
extern void error(int linenum, const string& message);
enum TypeForNode { INT_TYPE, STRING_TYPE, ERROR_TYPE, EMPTY_TYPE };
struct Value
{
TypeForNode type;
int intValue;
string stringValue;
static Value Integer(int v = 0)
{
return Value(v);
}
static Value String(string s = "")
{
return Value(s);
}
static Value Error(string error = "")
{
Value val(ERROR_TYPE);
val.stringValue = error;
return val;
}
static Value Empty()
{
return Value(EMPTY_TYPE);
}
Value() : type(EMPTY_TYPE) {}
private:
Value(int v)
: type(INT_TYPE),
intValue(v)
{
}
Value(string s)
: type(STRING_TYPE),
stringValue(s)
{
}
Value(TypeForNode type)
: type(type)
{
}
};
inline ostream& operator<<(ostream& os, const Value& v)
{
if (v.type == INT_TYPE)
{
return os << v.intValue;
}
else if (v.type == STRING_TYPE)
{
return os << v.stringValue;
}
return os;
}
inline Value operator+(const Value& u, const Value& v)
{
if (u.type == INT_TYPE && v.type == INT_TYPE)
{
return Value::Integer(u.intValue + v.intValue);
}
if (u.type == STRING_TYPE && v.type == STRING_TYPE)
{
return Value::String(u.stringValue + v.stringValue);
}
return Value::Error();
}
inline Value operator-(const Value& u, const Value& v)
{
if (u.type == INT_TYPE && v.type == INT_TYPE)
{
return Value::Integer(u.intValue - v.intValue);
}
return Value::Error();
}
inline Value operator*(const Value& u, const Value& v)
{
if (u.type == INT_TYPE && v.type == INT_TYPE)
{
return Value::Integer(u.intValue * v.intValue);
}
if (u.type == INT_TYPE && v.type == STRING_TYPE)
{
string accum = "";
int count = u.intValue;
while (count--)
{
accum += v.stringValue;
}
return Value::String(accum);
}
if (v.type == INT_TYPE && u.type == STRING_TYPE)
{
string accum = "";
int count = v.intValue;
while (count--)
{
accum += u.stringValue;
}
return Value::String(accum);
}
return Value::Error();
}
inline Value operator/(const Value& u, const Value& v)
{
if (u.type == INT_TYPE && v.type == INT_TYPE)
{
if(v.intValue != 0)
{
return Value::Integer(u.intValue / v.intValue);
}
else
{
return Value::Error("DIVIDE BY ZERO");
}
}
if (u.type == STRING_TYPE && v.type == STRING_TYPE)
{
size_t pos = u.stringValue.find(v.stringValue);
if (pos == string::npos)
{
return u;
}
Value result = Value::String(u.stringValue);
result.stringValue.replace(pos, v.stringValue.size(), "");
return result;
}
return Value::Error();
}
extern map<string, Value> symbolTable;
extern map<string, TypeForNode> typeTable;
// forward declaration of visitor class
// ParseTree needs it, but visitor also needs classes depending on ParseTree
class ParseTreeVisitor;
class ParseTree {
int linenumber;
ParseTree *left;
ParseTree *right;
public:
ParseTree(int n, ParseTree *l = 0, ParseTree *r = 0) : linenumber(n), left(l), right(r) {}
virtual ~ParseTree() {}
ParseTree* getLeft() const { return left; }
ParseTree* getRight() const { return right; }
int getLineNumber() const { return linenumber; }
virtual TypeForNode GetType() const { return ERROR_TYPE; }
virtual int GetIntValue() const { throw "no integer value"; }
virtual string GetStringValue() const { throw "no string value"; }
virtual Value Evaluate() const = 0;
// accept visitor
// derived classes override this method to perform tree traversal
virtual void accept(ParseTreeVisitor *visitor) const = 0;
};
// forward declaration of all the classes that ParseTreeVisitor needs
// it's required, because all of these classes depend on methods of ParseTreeVisitor
class StatementList;
class Addition;
class Subtraction;
class Multiplication;
class Division;
class PrintCommand;
class VariableAssignment;
class VariableDeclaration;
class Identifier;
class IntegerConstant;
class StringConstant;
// implementation of visitor pattern
// when tree node accepts a visitor, it calls type-appropriate version of beginVisit/endVisit methods
class ParseTreeVisitor
{
protected:
// default methods, in case the deriving visitor doesn't care about the types
virtual bool beginVisitNode(const ParseTree *tree)
{
return true;
}
virtual void endVisitNode(const ParseTree *tree) {}
public:
virtual ~ParseTreeVisitor() {}
// for each ParseTree type, there is a pair beginVisit/endVisit
virtual bool beginVisit(const StatementList *stmts)
{
return beginVisitNode((ParseTree*)stmts);
}
virtual bool beginVisit(const Addition *add)
{
return beginVisitNode((ParseTree*)add);
}
virtual bool beginVisit(const Subtraction *sub)
{
return beginVisitNode((ParseTree*)sub);
}
virtual bool beginVisit(const Multiplication *mul)
{
return beginVisitNode((ParseTree*)mul);
}
virtual bool beginVisit(const Division *div)
{
return beginVisitNode((ParseTree*)div);
}
virtual bool beginVisit(const PrintCommand *print)
{
return beginVisitNode((ParseTree*)print);
}
virtual bool beginVisit(const VariableAssignment *varAssign)
{
return beginVisitNode((ParseTree*)varAssign);
}
virtual bool beginVisit(const VariableDeclaration *varDecl)
{
return beginVisitNode((ParseTree*)varDecl);
}
virtual bool beginVisit(const Identifier *id)
{
return beginVisitNode((ParseTree*)id);
}
virtual bool beginVisit(const IntegerConstant *intConst)
{
return beginVisitNode((ParseTree*)intConst);
}
virtual bool beginVisit(const StringConstant *strConst)
{
return beginVisitNode((ParseTree*)strConst);
}
virtual void endVisit(const StatementList *stmts)
{
endVisitNode((ParseTree*)stmts);
}
virtual void endVisit(const Addition *add)
{
endVisitNode((ParseTree*)add);
}
virtual void endVisit(const Subtraction *sub)
{
endVisitNode((ParseTree*)sub);
}
virtual void endVisit(const Multiplication *mul)
{
endVisitNode((ParseTree*)mul);
}
virtual void endVisit(const Division *div)
{
endVisitNode((ParseTree*)div);
}
virtual void endVisit(const PrintCommand *print)
{
endVisitNode((ParseTree*)print);
}
virtual void endVisit(const VariableAssignment *varAssign)
{
endVisitNode((ParseTree*)varAssign);
}
virtual void endVisit(const VariableDeclaration *varDecl)
{
endVisitNode((ParseTree*)varDecl);
}
virtual void endVisit(const Identifier *id)
{
endVisitNode((ParseTree*)id);
}
virtual void endVisit(const IntegerConstant *intConst)
{
endVisitNode((ParseTree*)intConst);
}
virtual void endVisit(const StringConstant *strConst)
{
endVisitNode((ParseTree*)strConst);
}
};
// these are straight-forward
// all types of syntax constructs are represented here
// note that only minimum of methods required for this particular assignment is supported,
// e.g. semantic check only cares about variable names, so we don't implement type-returning methods, etc.
class StatementList : public ParseTree {
public:
StatementList(ParseTree *first, ParseTree *rest) : ParseTree(0, first, rest) {}
virtual Value Evaluate() const
{
if (getLeft() != NULL)
{
if (getLeft()->Evaluate().type != EMPTY_TYPE)
return Value::Error();
}
if (getRight() != NULL)
{
return getRight()->Evaluate();
}
return Value::Empty();
}
virtual void accept(ParseTreeVisitor *visitor) const
{
if (visitor->beginVisit(this))
{
if (getLeft() != 0)
{
getLeft()->accept(visitor);
}
if (getRight() != 0)
{
getRight()->accept(visitor);
}
}
visitor->endVisit(this);
}
};
class Addition : public ParseTree {
public:
Addition(int line, ParseTree *op1, ParseTree *op2) : ParseTree(line, op1, op2) {}
virtual void accept(ParseTreeVisitor *visitor) const
{
if (visitor->beginVisit(this))
{
getLeft()->accept(visitor);
getRight()->accept(visitor);
}
visitor->endVisit(this);
}
virtual Value Evaluate() const
{
return getLeft()->Evaluate() + getRight()->Evaluate();
}
virtual TypeForNode GetType() const
{
if (getLeft()->GetType() == getRight()->GetType())
{
return getLeft()->GetType();
}
return ERROR_TYPE;
}
};
class Subtraction : public ParseTree {
public:
Subtraction(int line, ParseTree *op1, ParseTree *op2) : ParseTree(line, op1, op2) {}
virtual void accept(ParseTreeVisitor *visitor) const
{
if (visitor->beginVisit(this))
{
getLeft()->accept(visitor);
getRight()->accept(visitor);
}
visitor->endVisit(this);
}
virtual Value Evaluate() const
{
return getLeft()->Evaluate() - getRight()->Evaluate();
}
virtual TypeForNode GetType() const
{
if (getLeft()->GetType() == INT_TYPE && getRight()->GetType() == INT_TYPE)
{
return INT_TYPE;
}
return ERROR_TYPE;
}
};
class Multiplication : public ParseTree {
public:
Multiplication(int line, ParseTree *op1, ParseTree *op2) : ParseTree(line, op1, op2) {}
virtual void accept(ParseTreeVisitor *visitor) const
{
if (visitor->beginVisit(this))
{
getLeft()->accept(visitor);
getRight()->accept(visitor);
}
visitor->endVisit(this);
}
virtual Value Evaluate() const
{
return getLeft()->Evaluate() * getRight()->Evaluate();
}
virtual TypeForNode GetType() const
{
if (getLeft()->GetType() == INT_TYPE)
{
return getRight()->GetType();
}
if (getLeft()->GetType() == STRING_TYPE && getRight()->GetType() == INT_TYPE)
{
return STRING_TYPE;
}
return ERROR_TYPE;
}
};
class Division : public ParseTree {
public:
Division(int line, ParseTree *op1, ParseTree *op2) : ParseTree(line, op1, op2) {}
virtual void accept(ParseTreeVisitor *visitor) const
{
if (visitor->beginVisit(this))
{
getLeft()->accept(visitor);
getRight()->accept(visitor);
}
visitor->endVisit(this);
}
virtual Value Evaluate() const
{
Value val = getLeft()->Evaluate() / getRight()->Evaluate();
if (val.type == ERROR_TYPE && val.stringValue.size() > 0)
{
error(getLineNumber(), val.stringValue);
}
return val;
}
virtual TypeForNode GetType() const
{
if (getLeft()->GetType() == getRight()->GetType())
{
return getLeft()->GetType();
}
return ERROR_TYPE;
}
};
class IntegerConstant : public ParseTree
{
int value;
public:
IntegerConstant(const Token& tok) : ParseTree(tok.GetLinenum()) {
value = stoi( tok.GetLexeme() );
}
virtual TypeForNode GetType() const { return INT_TYPE; }
virtual int GetIntValue() const { return value; }
virtual Value Evaluate() const
{
return Value::Integer(value);
}
virtual void accept(ParseTreeVisitor *visitor) const
{
visitor->beginVisit(this);
visitor->endVisit(this);
}
};
class StringConstant : public ParseTree
{
string value;
public:
StringConstant(const Token& token)
: ParseTree(token.GetLinenum())
{
value = token.GetLexeme().substr(1, token.GetLexeme().size() -2);
}
virtual TypeForNode GetType() const { return STRING_TYPE; }
virtual string GetStringValue() const { return value; }
virtual Value Evaluate() const
{
return Value::String(value);
}
virtual void accept(ParseTreeVisitor *visitor) const
{
visitor->beginVisit(this);
visitor->endVisit(this);
}
};
class Identifier : public ParseTree {
string identifier;
public:
Identifier(const Token& id)
: ParseTree(id.GetLinenum()),
identifier(id.GetLexeme())
{
}
string getName() const
{
return identifier;
}
virtual void accept(ParseTreeVisitor *visitor) const
{
visitor->beginVisit(this);
visitor->endVisit(this);
}
virtual Value Evaluate() const
{
return symbolTable[identifier];
}
virtual TypeForNode GetType() const
{
if (typeTable.find(identifier) != typeTable.end())
{
return typeTable[identifier];
}
return ERROR_TYPE;
}
};
class VariableDeclaration : public ParseTree
{
const TypeForNode type;
Identifier *identifier;
public:
VariableDeclaration(const Token& keyword, Identifier *identifier)
: ParseTree(keyword.GetLinenum()),
identifier(identifier),
type(keyword.GetTokenType() == T_INT ? INT_TYPE : STRING_TYPE)
{
}
virtual Value Evaluate() const
{
symbolTable[identifier->getName()] = type == INT_TYPE ? Value::Integer() : Value::String();
return Value::Empty();
}
virtual TypeForNode GetType() const
{
return type;
}
Identifier* getIdentifier() const
{
return identifier;
}
virtual void accept(ParseTreeVisitor *visitor) const
{
visitor->beginVisit(this);
visitor->endVisit(this);
}
};
class VariableAssignment : public ParseTree
{
Identifier *identifier;
public:
VariableAssignment(const Token& keyword, Identifier *identifier, ParseTree *expr)
: ParseTree(keyword.GetLinenum(), expr),
identifier(identifier)
{
}
virtual Value Evaluate() const
{
Value val = getLeft()->Evaluate();
if (val.type != ERROR_TYPE)
{
symbolTable[identifier->getName()] = val;
return Value::Empty();
}
return Value::Error();
}
Identifier* getIdentifier() const
{
return identifier;
}
virtual void accept(ParseTreeVisitor *visitor) const
{
if (visitor->beginVisit(this))
{
getLeft()->accept(visitor);
}
visitor->endVisit(this);
}
};
class PrintCommand : public ParseTree
{
const TokenType tokenType;
public:
PrintCommand(const Token& keyword, ParseTree *expr)
: ParseTree(keyword.GetLinenum(), expr),
tokenType(keyword.GetTokenType())
{
}
virtual Value Evaluate() const
{
Value val = getLeft()->Evaluate();
if (val.type != ERROR_TYPE)
{
std::cout << val;
if (IsNewline())
{
std::cout << std::endl;
}
return Value::Empty();
}
return Value::Error();
}
bool IsNewline() const
{
return tokenType == T_PRINTLN;
}
virtual void accept(ParseTreeVisitor *visitor) const
{
if (visitor->beginVisit(this))
{
getLeft()->accept(visitor);
}
visitor->endVisit(this);
}
};
extern ParseTree * Prog(istream* in);
extern ParseTree * StmtList(istream* in);
extern ParseTree * Stmt(istream* in);
extern ParseTree * Decl(istream* in);
extern ParseTree * Set(istream* in);
extern ParseTree * Print(istream* in);
extern ParseTree * Expr(istream* in);
extern ParseTree * Term(istream* in);
extern ParseTree * Primary(istream* in);
#endif /* PARSER_H_ */