-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab1.cpp
More file actions
369 lines (344 loc) · 11.2 KB
/
lab1.cpp
File metadata and controls
369 lines (344 loc) · 11.2 KB
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <regex>
#include <vector>
#include <iomanip>
#include "Tokenizer.h"
using namespace std;
const int MEMORY_SIZE = 512;
const int MAX_COUNT = 16;
const int SYMBOL_LENGTH = 16;
bool eom = false;
bool PASS_2 = false;
int moduleNum = 0;
int instrNum = 0;
int instrCnt = 0;
map<string, int> defPairs;
map<string, bool> multiDef;
map<int, int> base;
map<string, bool> curUseListUsed;
map<string, int> globalUseListUsed;
map<string, int> globalUseList;
vector<string> curUseList;
vector<string> useList;
regex symbolname("([A-Z]|[a-z])([A-Z]|[a-z]|[0-9])*", regex_constants::extended);
//-- Error codes for parsing phase (pass one) --//
void __parseerror(int errcode, Tokenizer &t)
{
static string errstr[] = {
"NUM_EXPECTED", // Number expect
"SYM_EXPECTED", // Symbol Expected
"ADDR_EXPECTED", // Addressing Expected which is A/E/I/R
"SYM_TOO_LONG", // Symbol Name is too long
"TOO_MANY_DEF_IN_MODULE", // > 16
"TOO_MANY_USE_IN_MODULE", // > 16
"TOO_MANY_INSTR", // total num_instr exceeds memory size (512)
};
cout << "Parse Error line " << t.getLineCount() << " offset " << t.getTokenOffset() << ": " << errstr[errcode] << endl;
exit(1);
}
//-- Checking defcount, usecount and codecount for errors. --//
//-- Expecting 4,5,6 as category since they are the related error codes. --//
bool intErrors(int count, int category)
{
if (category == 6)
{
instrNum += count;
if (instrNum > MEMORY_SIZE)
return false;
}
else if ((category == 4 || category == 5) && count > MAX_COUNT)
return false;
return true;
}
//-- Reading any integer related input. --//
int readInt(Tokenizer &t, int category)
{
if (t.peekToken())
{
try
{
int dc = stoi(t.getToken());
if (!(intErrors(dc, category)))
__parseerror(category, t);
else
return dc;
}
catch (const invalid_argument &ia)
{
__parseerror(0, t);
}
}
else //TOKEN_ERR (eof)
{
if (!eom)
__parseerror(0, t);
else
return -1;
}
}
//-- All symbols should follow the regex rule and must be shorter than 16 chars. --//
string readSymbol(Tokenizer &t)
{
if (t.peekToken())
{
string symb = t.getToken();
if (symb.length() > SYMBOL_LENGTH)
__parseerror(3, t);
else if (!regex_match(symb, symbolname))
__parseerror(1, t);
return symb;
}
else
__parseerror(1, t);
}
//-- Instructions start with A,E,I,R follwed with a 4 digit instuction. --//
//-- inst / 1000 = op , inst % 1000 = operand. --------------------------//
void readInst(Tokenizer &t)
{
if (t.peekToken())
{
string addr = t.getToken();
if (!(addr == "A" || addr == "E" || addr == "I" || addr == "R"))
{
__parseerror(2, t);
}
else
{
int instr = readInt(t, 0);
if (PASS_2)
{
cout << setfill('0') << setw(3);
cout << instrCnt;
cout << ": ";
if (addr == "A")
{
if (instr >= 10000)
{
cout << 9999;
cout << " Error: Illegal opcode; treated as 9999";
}
else if (instr % 1000 > MEMORY_SIZE)
{
int opcode = instr / 1000;
cout << (opcode * 1000)
<< " Error: Absolute address exceeds machine size; zero used ";
}
else
cout << setfill('0') << setw(4) << instr;
}
else if (addr == "R")
{
if (instr >= 10000)
{
cout << 9999;
cout << " Error: Illegal opcode; treated as 9999";
}
else if (instr % 1000 >= base[moduleNum + 1] - base[moduleNum])
{
int opcode = instr / 1000;
cout << (opcode * 1000) + base[moduleNum]
<< " Error: Relative address exceeds module size; zero used ";
}
else
cout << setfill('0') << setw(4) << instr + base[moduleNum];
}
else if (addr == "I")
{
if (instr >= 10000)
{
cout << 9999 << " Error: Illegal immediate value; treated as 9999";
}
else
cout << setfill('0') << setw(4) << instr;
}
else if (addr == "E")
{
if (instr >= 10000)
{
cout << 9999;
cout << " Error: Illegal opcode; treated as 9999";
}
else if (instr % 1000 >= curUseList.size())
{
cout << setfill('0') << setw(4) << instr;
cout << " Error: External address exceeds length of uselist; treated as immediate";
}
else
{
int op = instr / 1000;
int operand = instr % 1000;
curUseListUsed[curUseList[operand]] = true;
if (defPairs.find(curUseList[operand]) == defPairs.end())
cout << op * 1000 << " Error: " << curUseList[operand] << " is not defined; zero used";
else
{
cout << (op * 1000) + defPairs[curUseList[operand]];
globalUseListUsed[curUseList[operand]] = moduleNum;
}
}
}
cout << endl;
}
}
}
else
__parseerror(2, t);
}
void passOne(char *fileName)
{
Tokenizer parser(fileName);
while (parser.peekToken())
{
moduleNum++;
map<string, int> curDefPairs; //for warning messages.
int defCount = readInt(parser, 4); //4 because defcount.
if (defCount == -1)
{
parser.close();
return;
}
//-- defCount = number of symbol address pairs we should parse for. --//
for (int i = 0; i < defCount; i++)
{
string sym = readSymbol(parser);
int relAdd = readInt(parser, 0); //0 because no error check.
if (defPairs.find(sym) == defPairs.end())
{
defPairs[sym] = base[moduleNum] + relAdd;
curDefPairs[sym] = relAdd;
}
else
multiDef[sym] = true; // symbol is defined multiple times.
}
int useCount = readInt(parser, 5);
for (int i = 0; i < useCount; i++)
{
string sym = readSymbol(parser);
}
int codeCount = readInt(parser, 6);
//-- Warning Message --//
for (map<string, int>::iterator iter = curDefPairs.begin(); iter != curDefPairs.end(); iter++)
{
if (iter->second >= codeCount)
{
cout << "Warning: Module " << moduleNum << ": " << iter->first
<< " too big " << iter->second << " (max=" << codeCount - 1
<< ") assume zero relative" << endl;
defPairs[iter->first] = base[moduleNum];
}
}
for (int i = 0; i < codeCount; i++)
{
readInst(parser);
}
base[moduleNum + 1] = base[moduleNum] + codeCount;
eom = true;
}
parser.close();
}
void printSymbolTable()
{
cout << "Symbol Table " << endl;
for (map<string, int>::iterator iter = defPairs.begin(); iter != defPairs.end(); iter++)
{
cout << iter->first << "=" << iter->second;
if (multiDef[iter->first])
{
cout << " Error: This variable is multiple times defined; first value used";
}
cout << endl;
}
cout << endl;
}
void passTwo(char *fileName)
{
Tokenizer parser(fileName);
cout << "Memory Map" << endl;
moduleNum = 0;
instrCnt = 0;
while (parser.peekToken())
{
moduleNum++;
int defCount = readInt(parser, 4); //4 because defcount.
if (defCount == -1)
{
for (map<string, int>::iterator iter = defPairs.begin(); iter != defPairs.end(); iter++)
{
if (globalUseListUsed.find(iter->first) == globalUseListUsed.end())
{
cout << endl << "Warning: Module " << globalUseList[iter->first] << ": " << iter->first
<< " was defined but never used" << endl;
}
}
parser.close();
return;
}
//-- defCount = number of symbol address pairs we should parse for. --//
for (int i = 0; i < defCount; i++)
{
string sym = readSymbol(parser);
int relAdd = base[moduleNum] + readInt(parser, 0); //0 because no error check.
if (defPairs[sym] == relAdd)
globalUseList[sym] = moduleNum;
}
int useCount = readInt(parser, 5);
curUseList.clear();
curUseListUsed.clear();
for (int i = 0; i < useCount; i++)
{
string sym = readSymbol(parser);
useList.push_back(sym);
curUseList.push_back(sym);
}
for (int i = 0; i < curUseList.size(); i++)
{
curUseListUsed[curUseList[i]] = false;
}
int codeCount = readInt(parser, 6);
for (int i = 0; i < codeCount; i++)
{
readInst(parser);
instrCnt++;
}
for (map<string, bool>::iterator iter = curUseListUsed.begin(); iter != curUseListUsed.end(); iter++)
{
if (iter->second == false)
cout << "Warning: Module " << moduleNum << ": " << iter->first
<< " appeared in the uselist but was not actually used" << endl;
}
}
for (map<string, int>::iterator iter = defPairs.begin(); iter != defPairs.end(); iter++)
{
if (globalUseListUsed.find(iter->first) == globalUseListUsed.end())
{
cout << "Warning: Module " << globalUseList[iter->first] << ": " << iter->first
<< " was defined but never used" << endl;
}
}
parser.close();
}
int main(int argc, char *argv[])
{
if (argc == 1)
{
cout << "You did not enter a file name.";
}
else
{
for (int i = 1; i < argc; i++)
{
base[1] = 0;
char *fileName = argv[i];
Tokenizer parser(fileName);
passOne(fileName);
printSymbolTable();
PASS_2 = true;
passTwo(fileName);
}
}
return 0;
}