-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.hpp
525 lines (514 loc) · 16.7 KB
/
parser.hpp
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <deque>
#include <iomanip>
using namespace std;
map<string, int> labels;
struct Instruction
{
string name;
int file_name;
string field_1 = "";
string field_2 = "";
string field_3 = "";
};
bool valid_register(string R, set<string> Register_list)
{
return Register_list.find(R) != Register_list.end();
}
bool is_integer(string s)
{
for (int j = 0; j < s.length(); j++)
{
if (isdigit(s[j]) == false && !(j == 0 and s[j] == '-'))
{
return false;
}
}
return true;
}
map<string, int> getLabels()
{
map<string, int> res = labels;
labels.clear();
return res;
}
int SearchForRegister(int starting_index, int ending_index, string file_string, set<string> Register_list)
{
//this is a helper function which searches for a register from starting index and returns the starting point of it
int start = -1;
for (int j = starting_index; j <= ending_index; j++)
{
if (file_string[j] == ' ' || file_string[j] == '\t')
{
continue;
}
else
{
start = j;
break;
}
}
if (start == -1 || start + 2 > ending_index)
{
return -1;
}
if (!valid_register(file_string.substr(start, 3), Register_list))
{
return -1;
}
return start; //else found a valid register
}
int SearchForCharacter(int starting_index, int ending_index, string file_string, char Matching)
{
//returns the position of Matching if it is the first non-whitespace character to be found, -1 otherwise
int start = -1;
for (int j = starting_index; j <= ending_index; j++)
{
if (file_string[j] == ' ' || file_string[j] == '\t')
{
continue;
}
else if (file_string[j] == Matching)
{
return j;
}
else
{
return -1;
}
}
return -1; //if no character found except whitespace
}
pair<int, int> SearchForInteger(int starting_index, int ending_index, string file_string)
{
//returns the starting and ending index of integer if found
int start = -1;
int end = -1;
bool firstMinus = true;
for (int j = starting_index; j <= ending_index; j++)
{
if ((file_string[j] == ' ' || file_string[j] == '\t') && start == -1)
{
continue;
} //removing the starting spaces and tabs}
if (isdigit(file_string[j]) || (file_string[j] == '-' && firstMinus))
{
firstMinus = false;
if (start == -1)
{
start = j;
end = j;
}
else
{
end = j;
}
}
else
{
return {start, end};
}
}
return {start, end};
}
string Match_Instruction(int start, int end, string file_string)
{
//returns the matched instruction
if (start + 3 <= end)
{
string ins = file_string.substr(start, 4);
if (ins == "addi")
{
return ins;
}
}
if (start + 2 <= end)
{
string ins = file_string.substr(start, 3);
if (ins == "add" || ins == "sub" || ins == "mul" || ins == "slt" || ins == "beq" || ins == "bne")
{
return ins;
}
}
if (start + 1 <= end)
{
string ins = file_string.substr(start, 2);
if (ins == "lw" || ins == "sw")
{
return ins;
}
}
if (start <= end)
{
string ins = file_string.substr(start, 1);
if (ins == "j")
{
return ins;
}
}
return ""; //when no valid instruction found
}
//handle the case when integer is beyond instruction memory at execution time, and case of r0
bool validLabel(string temp)
{
if (temp.size() == 0)
{
return false;
}
int first_char = (int)temp[0];
if (!(first_char == 95 || (first_char >= 65 && first_char <= 90) || (first_char >= 97 && first_char <= 122)))
{
return false;
}
//first letter can be underscore or a letter
for (int i = 1; i < temp.size(); i++)
{
if (!(first_char == 95 || (first_char >= 65 && first_char <= 90) || (first_char >= 97 && first_char <= 122) || (first_char >= 48 && first_char <= 57)))
{
//rest can be letters, underscores or digits
return false;
}
}
return true;
}
pair<bool, string> ifLabel(string ins)
{
string temp = "";
int i = 0;
while (i < ins.size() && (ins[i] == ' ' || ins[i] == '\t'))
i++;
while (i < ins.size() && ins[i] != ':' && ins[i] != ' ' && ins[i] != '\t')
{
temp += ins[i];
i++;
}
int j = i + 1;
while (j < ins.size())
{
if (ins[j] != ' ' && ins[j] != '\t')
return {false, ""};
j++;
}
if (i < ins.size() && ins[i] == ':' && validLabel(temp))
return {true, temp};
else
return {false, ""};
}
string removeComments(string file_string)
{
int i = 0;
string temp = "";
while (i < file_string.size() && file_string[i] != '#')
{
temp += file_string[i];
i++;
}
return temp;
}
string changeZero(string file_string)
{
string temp = "";
if (file_string.size() < 4)
return file_string;
else
{
int i = 0;
while (i < file_string.size())
{
if (file_string[i] != 'z')
{
temp += file_string[i];
i++;
}
else
{
if (i + 3 < file_string.size() && file_string[i + 1] == 'e' && file_string[i + 2] == 'r' && file_string[i + 3] == 'o')
{
temp += "r0";
i += 4;
}
else
{
temp += file_string[i];
i++;
}
}
}
return temp;
}
}
pair<bool, pair<string, int>> findLabel(string file_string, int pos)
{
string temp1 = "";
int i = pos;
while (i < file_string.size() && (file_string[i] == ' ' || file_string[i] == '\t'))
{
i++;
}
while (i < file_string.size() && file_string[i] != ' ' && file_string[i] != '\t')
{
temp1 += file_string[i];
i++;
}
//cout<<temp1<<endl;
if (validLabel(temp1))
return {true, {temp1, i}};
else
return {false, {"", i}};
}
bool ifEmpty(string temp)
{
for (int i = 0; i < temp.size(); i++)
{
if (temp[i] != ' ' && temp[i] != '\t' && temp[i] != '\n')
{
return false;
}
}
return true;
}
pair<int, Instruction> Create_structs(string file_string, set<string> Register_list, int pcValue)
{
int i = 0;
bool instruction_found = false;
struct Instruction new_instr;
// each line can contain atmost one instruction
//cout<<file_string.size()<<endl;
new_instr.name = ""; //default name
file_string = removeComments(file_string);
if (ifEmpty(file_string))
{
return {2, new_instr};
}
pair<bool, string> lbl = ifLabel(file_string);
if (lbl.first)
{
labels[lbl.second] = pcValue + 1;
// cout<<lbl.second<<" "<<labels[lbl.second];
return {2, new_instr};
}
file_string = changeZero(file_string);
while (i < file_string.size())
{
if (file_string[i] == ' ' || file_string[i] == '\t')
{
i++;
continue;
}
else
{
if (instruction_found)
{
//validFile = false;
//cout<<"heck";
return {0, new_instr};
} //if we have already found an instruction and a character appears, file is invalid
string ins = Match_Instruction(i, file_string.size() - 1, file_string);
if (ins == "")
{ //invalid matching
//validFile = false;
//cout<<"heck";
return {0, new_instr};
}
if (ins == "add" || ins == "sub" || ins == "mul" || ins == "slt" || ins == "beq" || ins == "bne" || ins == "addi")
{
//now, there must be three registers ahead, delimited by comma
int reg1_start;
if (ins == "addi")
{
reg1_start = SearchForRegister(i + 4, file_string.size() - 1, file_string, Register_list);
}
else
{
reg1_start = SearchForRegister(i + 3, file_string.size() - 1, file_string, Register_list);
}
if (reg1_start == -1)
{
//validFile = false;
//cout<<"heck";
return {0, new_instr};
}
string R1 = file_string.substr(reg1_start, 3);
//now first register has been found, it must be followed by a comma and there can be 0 or more whitespaces in between
int comma1Pos = SearchForCharacter(reg1_start + 3, file_string.size() - 1, file_string, ',');
if (comma1Pos == -1)
{
//validFile = false;
//cout<<"heck1";
return {0, new_instr};
}
int reg2_start = SearchForRegister(comma1Pos + 1, file_string.size() - 1, file_string, Register_list);
if (reg2_start == -1)
{
//validFile = false;
//cout<<"heck2";
return {0, new_instr};
}
string R2 = file_string.substr(reg2_start, 3);
int comma2Pos = SearchForCharacter(reg2_start + 3, file_string.size() - 1, file_string, ',');
if (comma2Pos == -1)
{
//validFile = false;
//cout<<"heck3";
return {0, new_instr};
}
int reg3_start = SearchForRegister(comma2Pos + 1, file_string.size() - 1, file_string, Register_list);
//instead of third register, we can also have an integer value
pair<int, int> integer_indices = SearchForInteger(comma2Pos + 1, file_string.size() - 1, file_string);
pair<bool, pair<string, int>> labeldata = findLabel(file_string, comma2Pos + 1);
//cout << labeldata.second.first << endl;
int index_looped;
if (reg3_start == -1 && integer_indices.first == -1 && !labeldata.first)
{
//validFile = false;
//cout<<"heck4";
return {0, new_instr};
} //neither an integer nor a string
string R3;
if (reg3_start != -1)
{
if (ins != "beq" && ins != "bne" && ins != "addi")
{ //is a register and instruction is not bne,beq or addi
R3 = file_string.substr(reg3_start, 3);
index_looped = reg3_start + 3;
}
else
{ //beq,bne and addi must have the third argument as an integer
//validFile = false;
//cout<<"heck5";
return {0, new_instr};
}
}
else if (integer_indices.first != -1)
{
if (ins == "addi" || ins == "add" || ins == "sub" || ins == "mul" || ins == "slt")
{
R3 = file_string.substr(integer_indices.first, integer_indices.second - integer_indices.first + 1);
index_looped = integer_indices.second + 1;
}
else
{
//cout<<"heck6";
return {0, new_instr};
}
}
else
{
if (ins == "bne" || ins == "beq")
{
R3 = labeldata.second.first;
index_looped = labeldata.second.second;
//cout<<R3<<endl;
}
else
{
//cout<<"heck6";
return {0, new_instr};
}
}
new_instr.name = ins;
new_instr.field_1 = R1;
new_instr.field_2 = R2;
new_instr.field_3 = R3;
i = index_looped; //increment i
instruction_found = true;
//instructs.push_back(new_instr);
continue;
}
else if (ins == "j")
{
// pair<int, int> integer_indices = SearchForInteger(i + 1, file_string.size() - 1, file_string);
// if (integer_indices.first == -1 || file_string[integer_indices.first] == '-')
// {
// //validFile = false;
// return {false, new_instr};
// }
pair<bool, pair<string, int>> lblTOpc = findLabel(file_string, i + 1);
if (!lblTOpc.first)
{
//cout<<"heck7";
return {0, new_instr};
}
new_instr.name = ins;
new_instr.field_1 = lblTOpc.second.first;
new_instr.field_2 = "";
new_instr.field_3 = "";
int index_looped = lblTOpc.second.second;
i = index_looped;
instruction_found = true;
//instructs.push_back(new_instr);
}
else if (ins == "lw" || ins == "sw")
{
//this has the format lw $t0, offset($register_name)
// first of all search for the first register
int reg1_start = SearchForRegister(i + 2, file_string.size() - 1, file_string, Register_list);
if (reg1_start == -1)
{
//validFile = false;
//cout<<"heck8";
return {0, new_instr};
}
string R1 = file_string.substr(reg1_start, 3);
//now we will search for a comma and match it
int commaPos = SearchForCharacter(reg1_start + 3, file_string.size() - 1, file_string, ',');
if (commaPos == -1)
{
//validFile = false;
//cout<<"heck9";
return {0, new_instr};
}
// now we will search for an integer offset and match it
pair<int, int> integer_indices = SearchForInteger(commaPos + 1, file_string.size() - 1, file_string);
if (integer_indices.first == -1)
{
//validFile = false;
//cout<<"hecka";
return {0, new_instr};
}
string offset = file_string.substr(integer_indices.first, integer_indices.second - integer_indices.first + 1);
// now we will match Left parenthesis
int lparenPos = SearchForCharacter(integer_indices.second + 1, file_string.size() - 1, file_string, '(');
if (lparenPos == -1)
{
//validFile = false;
//cout<<"heckb";
return {0, new_instr};
}
//now we will match a register
int reg2_start = SearchForRegister(lparenPos + 1, file_string.size() - 1, file_string, Register_list);
if (reg2_start == -1)
{
//validFile = false;
//cout<<"heckc";
return {0, new_instr};
}
string R2 = file_string.substr(reg2_start, 3);
// now we will match the right parenthesis
int rparenPos = SearchForCharacter(reg2_start + 3, file_string.size() - 1, file_string, ')');
if (rparenPos == -1)
{
//validFile = false;
//cout<<"heckd";
return {0, new_instr};
}
new_instr.name = ins;
new_instr.field_1 = R1;
new_instr.field_2 = offset;
new_instr.field_3 = R2;
i = rparenPos + 1;
instruction_found = true;
//instructs.push_back(new_instr);
}
}
}
if (new_instr.name == "")
{
return {0, new_instr};
}
return {1, new_instr};
}