-
Notifications
You must be signed in to change notification settings - Fork 0
/
Admin.cpp
368 lines (324 loc) · 9.45 KB
/
Admin.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
#include "Admin.h"
#include "Scanner.h"
#include "Parser.h"
#include <iostream>
Admin::Admin(void) : linePos(0), lineCount(0), traceScanner(false),
traceParser(false), outputAST(false), almostDone(false), line(""),
fileName(""),
source(NULL), output(&cout), errOutput(&cout), sc(NULL), ps(NULL), sa(NULL),qua(NULL)
{
ASTNode::lookup = NULL;
ASTNode::sa = NULL;
}
Admin::Admin(ifstream & file, string fileName, ostream & out) : linePos(0), lineCount(0),
traceScanner(false), traceParser(false), outputAST(false),
almostDone(false), fileName(fileName),
line(""), source(&file), output(&out), errOutput(&out),
sc(new Scanner(*this)), ps(new Parser(*this, *sc)), sa(new SemanticAnalyzer(this)),
qua(new QuadrupleGenerator(this))
{
ASTNode::lookup = sc;
ASTNode::sa = sa;
}
Admin::Admin(ifstream & file, string fileName, ostream & out, bool traceEnabled) : linePos(0), lineCount(0),
traceScanner(traceEnabled), traceParser(traceEnabled),
outputAST(false),
almostDone(false), line(""), fileName(fileName), source(&file), output(&out),
errOutput(&out), sc(new Scanner(*this)), ps(new Parser(*this, *sc)), sa(new SemanticAnalyzer(this)),
qua(new QuadrupleGenerator(this))
{
ASTNode::lookup = sc;
ASTNode::sa = sa;
}
Admin::Admin(const Admin &other) : linePos(other.linePos), lineCount(other.lineCount),
traceScanner(other.traceScanner), traceParser(other.traceParser),
outputAST(other.outputAST), almostDone(other.almostDone), fileName(other.fileName),
line(other.line), source(other.source), output(other.output), errOutput(other.errOutput),
sc(new Scanner(*this)), ps(new Parser(*this, *sc)), sa(new SemanticAnalyzer(this)),
qua(new QuadrupleGenerator(this))
{
ASTNode::lookup = sc;
ASTNode::sa = sa;
}
Admin& Admin::operator= (const Admin &rhs)
{
// do the copy
linePos = rhs.linePos;
lineCount = rhs.lineCount;
traceScanner = rhs.traceScanner;
traceParser = rhs.traceParser;
outputAST = rhs.outputAST;
almostDone = rhs.almostDone;
line = rhs.line;
fileName = rhs.fileName;
source = rhs.source;
output = rhs.output;
errOutput = rhs.errOutput;
sc = new Scanner(*this);
ps = new Parser(*this, *sc);
sa = new SemanticAnalyzer(this);
qua = new QuadrupleGenerator(this);
ASTNode::lookup = sc;
ASTNode::sa = sa;
// return the existing object
return *this;
}
Admin::~Admin(void)
{
vec.clear();
delete sc;
delete ps;
delete sa;
//delete qua;
}
// Returns true if a character is a whitespace character
bool Admin::isWhiteSpace(char c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r';
}
// Returns true if a character is an invisible character
bool Admin::isInvisibleCharacter(char c) {
return (c >= 0 && c < 32) || c == 127 || c == 129 || c == 141 || c == 143
|| c == 144 || c == 157;
}
// Returns true if the output AST is designated to be displayed
bool Admin::getOutputAST() {
return outputAST;
}
int Admin:: getLineNumber() {
return lineCount;
}
void Admin::setOutputAST(bool outputAST) {
this->outputAST = outputAST;
}
void Admin::setOutputStream(ostream &out) {
output = &out;
}
void Admin::setErrOutputStream(ostream &out) {
errOutput = &out;
}
// Process up to the desired point as specified by processTo
void Admin::compile(int processTo) {
ASTNode * top = NULL;
switch(processTo) {
case 1:
ps->loopScanner();
break;
case 2:
ps->startParsing();
break;
case 3:
top = ps->startParsing();
if(top != NULL && ps->getErrorCount() == 0)
{
sa->semAnalyze(top);
}
break;
case 4:
top = ps->startParsing();
if(top != NULL && ps->getErrorCount() == 0)
{
sa->semAnalyze(top);
}
if(sa->getErrorCount() == 0) {
qua->GenerateQuadruples(top);
}
break;
default:
top = ps->startParsing();
if(top != NULL && ps->getErrorCount() == 0)
{
sa->semAnalyze(top);
}
if(sa->getErrorCount() == 0) {
qua->GenerateQuadruples(top);
}
//daniel ? why do we need?? i commented it
// NO COMMENTING THIS. IT HOLDS THE WORLD TOGETHER
int t = 4;
}
}
void Admin::outputExecutable(vector<Quadruple> quads) {
for(int i = 0; i < quads.size(); i++) {
*output << quads[i].getQuadrupleTuple() << endl;
}
}
void Admin::enableOutputAST() {
outputAST = true;
}
// Interfaces with the input file. Reads a whole line, then passes out characters as needed until end of line.
char Admin::getCh(bool skipWs) {
char c;
// Load another line if finished the current one
while(linePos >= line.length()) {
if(almostDone) {
return EOF;
}
if(!skipWs) {
linePos++;
return '\n';
}
endLine();
}
c = line[linePos];
// Skip whitespace if required by the function call
while((skipWs && isWhiteSpace(c)) || isInvisibleCharacter(c)) {
linePos++;
while(linePos >= line.length()) {
if(almostDone) {
return EOF;
}
if(!skipWs) {
linePos++;
return '\n';
}
endLine();
}
c = line[linePos];
}
linePos++;
return c;
}
// Refreshes the input line. Also logs the previous one.
void Admin::endLine() {
if(!source->eof()) {
linePos = 0;
lineCount++;
getline(*source, line);
}
else {
linePos = 0;
line = "" + EOF;
almostDone = true;
}
if(lineCount > 0 && !almostDone) { scannerLog(); }
}
// Logs scanner input.
void Admin::scannerLog() {
if(traceScanner) {
int startIndex = 0;
if(fileName != "") {
*output << fileName << ":";
}
*output << lineCount << ": ";
while(isWhiteSpace(line[startIndex])) {
startIndex++;
}
for(int i = startIndex; i < line.length(); i++) {
*output << line[i];
}
*output << endl;
}
}
// Logs scanner output. With trace disabled, only prints errors.
void Admin::scannerLogEnd() {
for(int i = 0; i < vec.size(); i++) {
Token tok = vec.at(i);
if(traceScanner && sc->namesRev[tok.getTokenType()] != "ERROR") {
*output << " " << lineCount << ": (" << sc->namesRev[tok.getTokenType()] << ", ";
}
if(sc->namesRev[tok.getTokenType()] == "ERROR") {
*errOutput << " " << lineCount << ": (" << sc->namesRev[tok.getTokenType()] << ", ";
}
// If token has values, display them
if(traceScanner && sc->namesRev[tok.getTokenType()] != "ERROR") {
if(tok.getAttributeValue() != -2) {
*output << tok.getAttributeValue() << ")";
}
else {
*output << "null)";
}
}
if(sc->namesRev[tok.getTokenType()] == "ERROR") {
if(tok.getAttributeValue() != -2) {
*errOutput << tok.getAttributeValue() << ")";
}
else {
*errOutput << "null)";
}
}
// Display name if token is an identifier
if(traceScanner && sc->namesRev[tok.getTokenType()] == "ID") {
*output << " => \"" << sc->getIdentifierName(tok.getAttributeValue()) << "\"";
}
// Bump the error counter
if(sc->namesRev[tok.getTokenType()] == "ERROR") {
*errOutput << " => \"" << sc->getErrorName(tok.getAttributeValue()) << "\"";
}
if(traceScanner && sc->namesRev[tok.getTokenType()] != "ERROR") {
*output << endl;
}
if(sc->namesRev[tok.getTokenType()] == "ERROR") {
*errOutput << endl;
}
// No longer used
/*if(sc->namesRev[tok.getTokenType()] == "ENDFILE") {
int errors = sc->getErrorCount();
if(errors > 0) {
*output << "BUILD FAILED (" << errors << " errors)";
}
else {
*output << "BUILD SUCCEEDED";
}
*output << endl;
}*/
}
vec.clear();
}
// Log entry/exit of derivation tree functions
void Admin::parserLog(string functionName, int mode) {
if(traceParser) {
if(mode == PARSER_ENTER) {
*output << "Entering " << functionName << endl;
}
else if(mode == PARSER_EXIT) {
*output << "Leaving " << functionName << endl;
}
}
}
// Log matching/loading of tokens by parser
void Admin::parserLog(int type, int mode) {
if(traceParser) {
if(mode == PARSER_MATCH) {
*output << "Matched " << sc->namesRev[type] << endl;
}
else if(mode == PARSER_LOAD) {
*output << "Loaded " << sc->namesRev[type] << endl;
}
}
}
// Recursively print the abstract syntax tree (AST) produced by the parser
void Admin::parserLog(ASTNode * topNode) {
if(outputAST) {
*output << endl;
topNode->printNode(0, output);
}
}
// Called if the AST was not built due to syntax errors
void Admin::cancelAST() {
*output << endl;
*output << "One or more syntax errors detected. AST could not be built" << endl;
}
// Go back one character in the input line.
void Admin::unget() { linePos--; }
string Admin::getIdentifierName(int id) {
return sc->getIdentifierName(id);
}
/* syntaxError - prints syntax error information. In the basic parser,
* this results in the parser terminating abruptly
* @param expected token type expected to be found at current parse locn
* @param found token type found at current parse locn
*/
void Admin::syntaxError(string expected, int found){
if(fileName != "") {
*errOutput << fileName << ":";
}
*errOutput << lineCount << ": Syntax error. Found " << sc->namesRev[found]
<< " (expected " << expected << ")" << endl;
}
void Admin::semanticError(string desc, int lineNumber) {
if(fileName != "") {
*errOutput << fileName << ":";
}
*errOutput << lineNumber << ": Semantic error. " << desc << endl;
sa->incError();
}