-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRDParser.cpp
596 lines (538 loc) · 15.6 KB
/
RDParser.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
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
/*
Dylan Jager-Kujawa
Matt Richardson
Dr. Strader
CSC 442
8 December 2015
Blocks World
RDParser.cpp
Implements the parser, which reads input from the scanner and interprets it as executable code.
*/
#define DEBUG
#undef DEBUG
#include "RDParser.hpp"
// Constructor
RDParser::RDParser(std::string fileName) {
#ifdef DEBUG
std::cerr << "In Parser constructor with filename " << fileName << std::endl;
#endif
// Initialize variables
scan = LAScanner(fileName);
errors = false;
arm_val = "";
program();
#ifdef DEBUG
std::cerr << "Leaving Parser constructor" << std::endl << std::endl;
#endif
}
// Add an error to the error list
void RDParser::error(std::string message, int lineNumber) {
#ifdef DEBUG
std::cerr << "Entering error function with message: \"" << message << "\" at line " << lineNumber << std::endl;
#endif
while (scan.peek().compare(";") != 0 && scan.peek().compare("]") != 0 \
&& scan.peek().compare("}") != 0 && scan.peek().compare("") != 0) {
scan.nextToken();
}
errList.insert(errList.end(), "Line " + std::to_string(lineNumber) + ": " + message);
errors = true;
#ifdef DEBUG
std::cerr << "Leaving error function. Next token is " << scan.peek() \
<< std::endl << std::endl;
#endif
}
// Print the input file along with the line numbers
void RDParser::printInput() {
std::cout << "Input file:" << std::endl;
std::stringstream ss(scan.getInputTable().c_str());
std::string tmp = "";
int line = 1;
// Print the file, appending the line number to the front of each line.
if (scan.getInputTable().c_str() != NULL) {
while (std::getline(ss, tmp, '\n')) {
std::cout << " " << std::setw(3) << line << " | " << tmp << std::endl;
line++;
}
}
std::cout << std::endl;
}
// Print the list of errors, if any
void RDParser::printErrors() {
#ifdef DEBUG
if (!errors) {
errors = true;
errList.insert(errList.end(), "No errors generated.");
errList.insert(errList.end(), "These messages test printErrors() functionality");
errList.insert(errList.end(), "when no real errors are present.");
}
#endif
std::cout << "Errors: " << std::endl;
if (!errors)
std::cout << " (none)" << std::endl;
else
for (int i = 0; i < errList.size(); i++)
std::cout << " " << errList[i] << std::endl;
std::cout << std::endl;
}
void RDParser::printWorld() {
#ifdef DEBUG
std::cerr << "Entering printWorld(). Adding two elements to stack at (1,1)" << std::endl;
world[0].push("debug1");
world[0].push("debug2");
#endif
// Print out a diagram of the grid, representing the # of blocks per space
std::cout << "# blocks in each position:" << std::endl;
// For each row
for (int i = 0; i <= y; i++) {
std::cout << " ";
// For each column
for (int j = 0; j <= x; j++) {
// If upper-left corner, print nothing
if (i == 0 && j == 0)
std::cout << " ";
// If column header, print column number
else if (i == 0)
std::cout << std::setw(3) << j;
// If row header, print row number
else if (j == 0)
std::cout << std::setw(3) << i;
// Otherwise, print stack size. If 0, print "-"
else {
int num = world[(j - 1) + (i - 1) * x].size();
if (num == 0)
std::cout << std::setw(3) << "-";
else
std::cout << std::setw(3) << num;
}
}
std::cout << std::endl;
}
std::cout << std::endl;
// Print out the contents of any nonempty spaces
std::cout << "Location contents:" << std::endl;
// For each row
for (int i = 0; i <= y; i++) {
// For each column
for (int j = 0; j <= x; j++) {
std::stack<std::string> current = world[(j - 1) + (i - 1) * x];
// If only one element, just print that element
if (current.size() == 1) {
std::cout << " " << "(" << j << "," << i << "):" << std::endl;
std::cout << "\t" << current.top() << std::endl;
// If stack, print all elements in a stack format
} else if (current.size() > 1) {
std::cout << " " << "(" << j << "," << i << "):" << std::endl;
std::cout << "\t" << "[top]" << std::endl;
std::stack<std::string> reverse; // Reverse of the current stack
// For every element in stack
while (current.size() > 0) {
// Print the element
std::cout << "\t " << current.top() << std::endl;
// Add the element to the reverse stack
reverse.push(current.top());
// Pop
current.pop();
}
std::cout << "\t" << "[bottom]" << std::endl;
// Restore current stack to original state
// For each element in reverse stack
while (reverse.size() > 0) {
// Add to current stack
current.push(reverse.top());
// Pop
reverse.pop();
}
}
}
}
std::cout << std::endl;
#ifdef DEBUG
std::cerr << "Leaving printWorld()" << std::endl << std::endl;
#endif
}
void RDParser::print() {
printInput();
printErrors();
if (!errors)
printWorld();
}
void RDParser::program() {
#ifdef DEBUG
std::cerr << "Entering <program>" << std::endl;
std::cerr << "Starting world declaration:" << std::endl;
#endif
printInput();
// Always get current line number before popping
int line = scan.getLineNumber();
// Expect "WORLD"
if (scan.nextToken().compare("WORLD") != 0)
error("Expected WORLD", line);
// Expect a variable
id();
int* dim = coordinate();
// Expect :
if (scan.nextToken().compare(":") != 0)
error("Expected :", line);
// Check validity of dimensions
if (*dim != -1) {
x = dim[0];
y = dim[1];
world = new std::stack<std::string> [x*y];
// If getting world dimensions failed, print errors and exit
} else {
printErrors();
exit(1);
}
#ifdef DEBUG
std::cerr << "Ending world declaration" << std::endl << std::endl;
std::cerr << "Starting block declarations" << std::endl;
#endif
// Expect declarations
declarations();
if (!errors) {
std::cout << "Initial configuration:" << std::endl;
printWorld();
}
#ifdef DEBUG
std::cerr << "Leaving block declarations" << std::endl << std::endl;
std::cerr << "Starting actions block" << std::endl;
#endif
// Expect actions; actions() returns stack machine
if (!errors)
actions();
if (!errors) {
std::cout << "Final configuration:" << std::endl;
printWorld();
} else {
printErrors();
}
#ifdef DEBUG
std::cerr << "Leaving actions block" << std::endl << std::endl;
#endif
}
void RDParser::declarations() {
// Expect BLOCKS
if (scan.nextToken().compare("BLOCKS") != 0)
error("Expected BLOCKS", scan.getLineNumber());
// Expect {
if (scan.nextToken().compare("{") != 0)
error("Expected {", scan.getLineNumber());
// While next token is not arm
while (scan.peek().compare("arm") != 0) {
// Get name
std::string name = id();
// Get location
int* loc = coordinate();
// Expect ;
if (scan.nextToken().compare(";") != 0)
error("Expected ;", scan.getLineNumber());
// Add name to world at location, assuming within range
if (*loc != -1 && loc[0] <= x && loc[1] <= y) {
world[(loc[0] - 1) + (loc[1] - 1) * x].push(name);
} else if (*loc != -1) {
error("Invalid coordinate", scan.getLineNumber());
return;
}
// If next token is }, error (arm not declared)
if (scan.peek().compare("}") == 0 || scan.peek().compare("") == 0) {
error("Expected arm declaration", scan.getLineNumber());
return;
}
}
// Expect arm
if (scan.nextToken().compare("arm") != 0) {
error("Expected arm declaration", scan.getLineNumber());
return;
}
// Get arm location
int* armloc = arm();
// If armloc is -1, set armloc to default (1,1)
if (*armloc == -1) {
armloc[0] = 1;
armloc[1] = 1;
}
arm_x = armloc[0];
arm_y = armloc[1];
// Expect ;
if (scan.nextToken().compare(";") != 0) {
error("Expected ;", scan.getLineNumber());
return;
}
// Expect }
if (scan.nextToken().compare("}") != 0) {
error("Expected }", scan.getLineNumber());
return;
}
// Expect ;
if (scan.nextToken().compare(";") != 0) {
error("Expected ;", scan.getLineNumber());
return;
}
}
// Expects a coordinate value; returns 2-element array containing {x, y}.
// Returns -1 on error.
int* RDParser::coordinate() {
int* dim = new int[2];
int line = scan.getLineNumber();
#ifdef DEBUG
std::cerr << "Entering coordinate()" << std::endl;
#endif
try {
// Expect (
if (scan.nextToken().compare("(") != 0) {
error("Expected (", line);
*dim = -1;
return dim;
// Try and get x
} else
dim[0] = stoi(scan.nextToken());
#ifdef DEBUG
std::cerr << "Got X value: " << dim[0] << std::endl;
#endif
// Expect ,
if (scan.nextToken().compare(",") != 0) {
error ("Expected ,", line);
*dim = -1;
return dim;
// Try and get y
} else
dim[1] = stoi(scan.nextToken());
#ifdef DEBUG
std::cerr << "Got Y value: " << dim[1] << std::endl;
#endif
// Expect ):
if (scan.nextToken().compare(")") != 0) {
error ("Expected )", line);
*dim = -1;
return dim;
}
} catch (std::exception &e) {
error("Expected integer within coordinate declaration", line);
// On error, return -1
dim[0] = -1;
dim[1] = -1;
}
#ifdef DEBUG
std::cerr << "Returning (" << dim[0] << "," << dim[1] << ") from coordinate()" << std::endl << std::endl;
#endif
return dim;
}
// Expects a coordinate value or empty parentheses; returns 2-element array containing {x, y}.
// Returns -1 on error.
int* RDParser::arm() {
int* dim = new int[2];
int line = scan.getLineNumber();
#ifdef DEBUG
std::cerr << "Entering arm()" << std::endl;
#endif
try {
// Expect (
if (scan.nextToken().compare("(") != 0) {
error("Expected (", line);
*dim = -1;
return dim;
}
if (scan.peek().compare(")") == 0) {
#ifdef DEBUG
std::cerr << "Arm declared using default value (1,1)" << std::endl;
#endif
scan.nextToken();
dim[0] = -1;
dim[1] = -1;
return dim;
// Try and get x
} else
dim[0] = stoi(scan.nextToken());
#ifdef DEBUG
std::cerr << "Got X value: " << dim[0] << std::endl;
#endif
// Expect ,
if (scan.nextToken().compare(",") != 0) {
error ("Expected ,", line);
*dim = -1;
return dim;
}
// Try and get y
else
dim[1] = stoi(scan.nextToken());
#ifdef DEBUG
std::cerr << "Got Y value: " << dim[1] << std::endl;
#endif
// Expect ):
if (scan.nextToken().compare(")") != 0) {
error ("Expected )", line);
*dim = -1;
return dim;
}
} catch (std::exception &e) {
error("Expected integer within arm declaration", line);
// On error, return -1
dim[0] = -1;
dim[1] = -1;
}
#ifdef DEBUG
std::cerr << "Returning (" << dim[0] << "," << dim[1] << ") from arm()" << std::endl << std::endl;
#endif
return dim;
}
std::string RDParser::id() {
int line = scan.getLineNumber();
// Get next token as c_string
const char* name = scan.nextToken().c_str();
#ifdef DEBUG
std::cerr << "Entering id() with token " << name << std::endl;
#endif
int i = 0;
// For each character in string
while (name[i]) {
// If first character is not a lowercase letter, error
if (i == 0 && !std::islower(name[i], std::locale())) {
error("Variable id expected; must begin with lowercase letter", line);
return "";
// If any character is not lowercase letter or digit, error
} else if (!std::islower(name[i], std::locale()) && !std::isdigit(name[i], std::locale())) {
error("Variable id expected; must contain only lowercase letters and digits", line);
return "";
}
i++;
}
#ifdef DEBUG
std::cerr << "Token passed id check; returning." << std::endl << std::endl;
#endif
// If no errors, return name
return name;
}
void RDParser::actions() {
#ifdef DEBUG
std::cerr << "Entering actions()" << std::endl;
#endif
int line = scan.getLineNumber();
if (scan.nextToken().compare("MOVES") != 0)
error("Expected MOVES", line);
line = scan.getLineNumber();
if (scan.nextToken().compare("[") != 0)
error("Expected [", line);
while (scan.peek().compare("]") != 0 && scan.peek().compare("") != 0) {
line = scan.getLineNumber();
action();
if (scan.nextToken().compare(";") != 0)
error("Expected ;", line);
}
scan.nextToken();
line = scan.getLineNumber();
if (scan.nextToken().compare(";") != 0)
error("Expected ;", line);
#ifdef DEBUG
std::cerr << "Leaving actions()" << std::endl;
#endif
}
void RDParser::action() {
/* Possible actions:
GRAB
UNSTACK
MOVE
DROP
STACK
PRINT
*/
int line = scan.getLineNumber();
std::string fn = scan.nextToken();
#ifdef DEBUG
std::cerr << "In action() with fn = " << fn << " at line " << line << std::endl;
#endif
if (fn.compare("GRAB") == 0) {
#ifdef DEBUG
std::cerr << "Entering GRAB checks; arm_val=" << arm_val << ", loc=(" << arm_x << "," \
<< arm_y << ")" << std::endl;
#endif
if (scan.nextToken().compare("(") != 0)
error("Expected (", scan.getLineNumber());
std::string name = id();
if (scan.nextToken().compare(")") != 0)
error("Expected )", scan.getLineNumber());
line = scan.getLineNumber();
if (arm_val.compare("") != 0)
error("Attempt to unstack while arm is full", line);
else if (world[(arm_x - 1) + (arm_y - 1) * x].size() == 0)
error("Attempt to grab from an empty space", line);
else if (world[(arm_x - 1) + (arm_y - 1) * x].size() > 1)
error("Attempt to grab from a stack", line);
else if (world[(arm_x - 1) + (arm_y - 1) * x].top().compare(name) != 0)
error("Attempt to unstack element not at specified location", line);
else {
arm_val = name;
world[(arm_x - 1) + (arm_y - 1) * x].pop();
}
} else if (fn.compare("UNSTACK") == 0) {
#ifdef DEBUG
std::cerr << "Entering UNSTACK checks; arm_val=" << arm_val << ", loc=(" << arm_x << "," \
<< arm_y << ")" << std::endl;
#endif
if (scan.nextToken().compare("(") != 0)
error("Expected (", scan.getLineNumber());
std::string name = id();
if (scan.nextToken().compare(")") != 0)
error("Expected )", scan.getLineNumber());
line = scan.getLineNumber();
if (arm_val.compare("") != 0)
error("Attempt to unstack while arm is full", line);
else if (world[(arm_x - 1) + (arm_y - 1) * x].size() <= 1)
error("Attempt to unstack from a non-stack space", line);
else if (world[(arm_x - 1) + (arm_y - 1) * x].top().compare(name) != 0)
error("Attempt to unstack element not at top of stack", line);
else {
arm_val = name;
world[(arm_x - 1) + (arm_y - 1) * x].pop();
}
} else if (fn.compare("MOVE") == 0) {
#ifdef DEBUG
std::cerr << "Entering MOVE checks; arm_val=" << arm_val << ", loc=(" << arm_x << "," \
<< arm_y << ")" << std::endl;
#endif
int* new_loc = coordinate();
if (*new_loc != -1 && new_loc[0] <= x && new_loc[1] <= y && new_loc[0] > 0 && new_loc[1] > 0) {
arm_x = new_loc[0];
arm_y = new_loc[1];
} else {
error("Attempt to move to invalid coordinate", scan.getLineNumber());
}
} else if (fn.compare("DROP") == 0) {
#ifdef DEBUG
std::cerr << "Entering DROP checks; arm_val=" << arm_val << ", loc=(" << arm_x << "," \
<< arm_y << ")" << std::endl;
#endif
line = scan.getLineNumber();
if (arm_val.compare("") == 0)
error("Attempt to drop while arm is empty", line);
else if (world[(arm_x - 1) + (arm_y - 1) * x].size() > 1)
error("Attempt to drop on a stack", line);
else {
world[(arm_x - 1) + (arm_y - 1) * x].push(arm_val);
arm_val = "";
}
} else if (fn.compare("STACK") == 0) {
#ifdef DEBUG
std::cerr << "Entering STACK checks; arm_val=" << arm_val << ", loc=(" << arm_x << "," \
<< arm_y << ")" << std::endl;
#endif
line = scan.getLineNumber();
if (arm_val.compare("") == 0)
error("Attempt to stack while arm is empty", line);
else if (world[(arm_x - 1) + (arm_y - 1) * x].size() == 0)
error("Attempt to stack on an empty space", line);
else {
world[(arm_x - 1) + (arm_y - 1) * x].push(arm_val);
arm_val = "";
}
} else if (fn.compare("PRINT") == 0) {
std::cout << "World's state as of line " << scan.getLineNumber() << ":" << std::endl;
printWorld();
}
}
int main (int c, char** v) {
if (c != 2) {
std::cout << "Usage: " << v[0] << " [input file]" << std::endl;
exit(1);
}
RDParser parser(v[1]);
}