forked from 2022-cs4301/compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstage1.h
207 lines (177 loc) · 6.07 KB
/
stage1.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
#ifndef STAGE1_H
#define STAGE1_H
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <stack>
using namespace std;
const char END_OF_FILE = '$'; // arbitrary choice
enum storeTypes {INTEGER, BOOLEAN, PROG_NAME, UNKNOWN};
enum modes {VARIABLE, CONSTANT};
enum allocation {YES, NO};
class SymbolTableEntry
{
public:
SymbolTableEntry(string in, storeTypes st, modes m,
string v, allocation a, int u)
{
setInternalName(in);
setDataType(st);
setMode(m);
setValue(v);
setAlloc(a);
setUnits(u);
}
string getInternalName() const
{
return internalName;
}
storeTypes getDataType() const
{
return dataType;
}
modes getMode() const
{
return mode;
}
string getValue() const
{
return value;
}
allocation getAlloc() const
{
return alloc;
}
int getUnits() const
{
return units;
}
void setInternalName(string s)
{
internalName = s;
}
void setDataType(storeTypes st)
{
dataType = st;
}
void setMode(modes m)
{
mode = m;
}
void setValue(string s)
{
value = s;
}
void setAlloc(allocation a)
{
alloc = a;
}
void setUnits(int i)
{
units = i;
}
private:
string internalName;
storeTypes dataType;
modes mode;
string value;
allocation alloc;
int units;
};
class Compiler
{
public:
Compiler(char **argv); // constructor
~Compiler(); // destructor
void createListingHeader();
void parser();
void createListingTrailer();
// Methods implementing the grammar productions
void prog(); // stage 0, production 1
void progStmt(); // stage 0, production 2
void consts(); // stage 0, production 3
void vars(); // stage 0, production 4
void beginEndStmt(); // stage 0, production 5
void constStmts(); // stage 0, production 6
void varStmts(); // stage 0, production 7
string ids(); // stage 0, production 8
void execStmts(); // stage 1, production 2
void execStmt(); // stage 1, production 3
void assignStmt(); // stage 1, production 4
void readStmt(); // stage 1, production 5
void writeStmt(); // stage 1, production 7
void express(); // stage 1, production 9
void expresses(); // stage 1, production 10
void term(); // stage 1, production 11
void terms(); // stage 1, production 12
void factor(); // stage 1, production 13
void factors(); // stage 1, production 14
void part(); // stage 1, production 15
// Helper functions for the Pascallite lexicon
bool isKeyword(string s) const; // determines if s is a keyword
bool isSpecialSymbol(char c) const; // determines if c is a special symbol
bool isNonKeyId(string s) const; // determines if s is a non_key_id
bool isInteger(string s) const; // determines if s is an integer
bool isBoolean(string s) const; // determines if s is a boolean
bool isLiteral(string s) const; // determines if s is a literal
// Action routines
void insert(string externalName, storeTypes inType, modes inMode,
string inValue, allocation inAlloc, int inUnits);
storeTypes whichType(string name); // tells which data type a name has
string whichValue(string name); // tells which value a name has
void code(string op, string operand1 = "", string operand2 = "");
void pushOperator(string op);
string popOperator();
void pushOperand(string operand);
string popOperand();
// Emit Functions
void emit(string label = "", string instruction = "", string operands = "",
string comment = "");
void emitPrologue(string progName, string = "");
void emitEpilogue(string = "", string = "");
void emitStorage();
void emitReadCode(string operand, string = "");
void emitWriteCode(string operand, string = "");
void emitAssignCode(string operand1, string operand2); // op2 = op1
void emitAdditionCode(string operand1, string operand2); // op2 + op1
void emitSubtractionCode(string operand1, string operand2); // op2 - op1
void emitMultiplicationCode(string operand1, string operand2); // op2 * op1
void emitDivisionCode(string operand1, string operand2); // op2 / op1
void emitModuloCode(string operand1, string operand2); // op2 % op1
void emitNegationCode(string operand1, string = ""); // -op1
void emitNotCode(string operand1, string = ""); // !op1
void emitAndCode(string operand1, string operand2); // op2 && op1
void emitOrCode(string operand1, string operand2); // op2 || op1
void emitEqualityCode(string operand1, string operand2); // op2 == op1
void emitInequalityCode(string operand1, string operand2); // op2 != op1
void emitLessThanCode(string operand1, string operand2); // op2 < op1
void emitLessThanOrEqualToCode(string operand1, string operand2); // op2 <= op1
void emitGreaterThanCode(string operand1, string operand2); // op2 > op1
void emitGreaterThanOrEqualToCode(string operand1, string operand2); // op2 >= op1
// Lexical routines
char nextChar(); // returns the next character or END_OF_FILE marker
string nextToken(); // returns the next token or END_OF_FILE marker
// Other routines
string genInternalName(storeTypes stype) const;
void processError(string err);
void freeTemp();
string getTemp();
string getLabel();
bool isTemporary(string s) const; // determines if s represents a temporary
private:
map<string, SymbolTableEntry> symbolTable;
ifstream sourceFile;
ofstream listingFile;
ofstream objectFile;
string token; // the next token
char ch; // the next character of the source file
uint errorCount = 0; // total number of errors encountered
uint lineNo = 0; // line numbers for the listing
stack<string> operatorStk; // operator stack
stack<string> operandStk; // operand stack
int currentTempNo = -1; // current temp number
int maxTempNo = -1; // max temp number
string contentsOfAReg; // symbolic contents of A register
};
#endif