-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLAScanner.cpp
269 lines (247 loc) · 6.78 KB
/
LAScanner.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
/*
Dylan Jager-Kujawa
Matt Richardson
Dr. Strader
CSC 442
8 December 2015
Blocks World
LAScanner.cpp
Implements the scanner, which reads input character by character and outputs a list of
tokens as defined by the grammar in the handout.
*/
#include "LAScanner.hpp"
#define DEBUG
#undef DEBUG
LAScanner::LAScanner(std::string fileName) {
#ifdef DEBUG
std::cerr << "Entering LAScanner constructor." << std::endl;
#endif
// Initialize variables
charQueue = "";
lineCount = 1;
pos = 0;
currVar = false;
std::ifstream fileIn;
try {
// Attempt to open file for reading.
fileIn.open(fileName, std::ifstream::in);
char in;
// If file not found, throw exception.
if (!fileIn)
throw FileNotFoundException();
// Read all characters and close input file.
while (fileIn.get(in))
charQueue += in;
fileIn.close();
} catch (FileNotFoundException& e) {
std::cerr << "File \"" + fileName + "\" does not exist!" << std::endl;
exit (EXIT_FAILURE);
} catch (std::exception& e) {
std::cerr << "Caught unhandled exception." << std::endl << e.what();
fileIn.close();
exit (EXIT_FAILURE);
}
#ifdef DEBUG
std::cerr << "Successfully read file. File contents: " << std::endl \
<< charQueue << std::endl;
std::cerr << "Leaving LAScanner constructor." << std::endl;
#endif
}
LAScanner::LAScanner() {}
// Return the original program as a string.
std::string LAScanner::getInputTable() {
#ifdef DEBUG
std::cerr << "In getInputTable(): charQueue is - " << std::endl \
<< charQueue << std::endl << "Leaving getInputTable()" << std::endl;
#endif
return charQueue;
}
// Return the current line number
int LAScanner::getLineNumber() {
#ifdef DEBUG
std::cerr << "In getLineNumber(): lineCount is " << lineCount << std::endl \
<< "Leaving getLineNumber()" << std::endl;
#endif
return lineCount;
}
// Peek at the next token without dequeueing it.
std::string LAScanner::peek() {
int tmp = pos;
int line_backup = lineCount;
std::string retVal = nextToken();
#ifdef DEBUG
std::cerr << "In peek()" << std::endl;
std::cerr << "nextToken is " << retVal << std::endl;
#endif
// Reset the position pointer.
pos = tmp;
// Reset lineCount
lineCount = line_backup;
#ifdef DEBUG
std::cerr << "Leaving peek()" << std::endl;
#endif
return retVal;
}
// Get the next token and reposition the pointer to "dequeue" said token.
std::string LAScanner::nextToken() {
#ifdef DEBUG
std::cerr << "Entering nextToken()." << std::endl;
#endif
char curr = '\0';
char next = '\0';
std::string retVal = "";
// Get the current character; if out of bounds, return empty string.
try {
curr = charQueue.at(pos);
} catch (std::out_of_range& e) {
#ifdef DEBUG
std::cerr << "nextToken() attempted to access char out of " \
"range while getting current character." << std::endl;
#endif
return "";
}
// Get next character; if out of bounds, increment pos.
try {
next = charQueue.at(pos + 1);
} catch (std::out_of_range& e) {
#ifdef DEBUG
std::cerr << "nextToken() attempted to access char out of " \
"range while getting next character." << std::endl;
#endif
pos++;
}
pos++;
#ifdef DEBUG
std::cerr << "Successfully retrieved curr and next: " << curr << ", & " \
<< next << ", respectively." << std::endl;
#endif
// Handle one-line comments.
if (curr == '/' && next == '/') {
try {
// Set pos to position of next newline character
while (charQueue.at(pos) != '\n')
pos++;
// Continue finding tokens
return nextToken();
} catch (std::out_of_range& e) {
#ifdef DEBUG
std::cerr << "nextToken() out of bounds while looking for \\n" << std::endl;
#endif
pos++;
}
// Handle multi-line comments.
} else if (curr == '/' && next == '*') {
try {
// Set pos to position of the * in next */
while (charQueue.at(pos) != '*' || charQueue.at(pos + 1) != '/') {
if (charQueue.at(pos) == '\n') {
lineCount++;
#ifdef DEBUG
std::cerr << "\t\tINCREMENTING LINECOUNT TO " << lineCount << std::endl;
#endif
}
pos++;
}
// Skip over */
pos += 2;
// Continue finding tokens
return nextToken();
} catch (std::out_of_range& e) {
#ifdef DEBUG
std::cerr << "nextToken() out of bounds while looking for */" << std::endl;
#endif
pos++;
}
// Handle whitespace.
} else if (isspace(curr)) {
if (curr == '\n') {
lineCount++;
#ifdef DEBUG
std::cerr << "\t\tINCREMENTING LINECOUNT TO " << lineCount << std::endl;
#endif
}
return nextToken();
// Handle letters
} else if (isalpha(curr)) {
// Current token must be some kind of variable (contains letters/digits).
currVar = true;
// If next char is alphanumeric, keep going.
if (isalnum(next)) {
retVal += curr + nextToken();
// If next char is whitespace or symbol, end of token, return.
} else if (isspace(next) || isSymbol(next)) {
retVal += curr;
// Unexpected character. Add to current token and end current token.
} else {
retVal += curr;
retVal += next;
pos++;
#ifdef DEBUG
std::cerr << "Undefined character: " << next << std::endl;
#endif
}
// Always reset currVar to false.
currVar = false;
// Handle digits
} else if (isdigit(curr)) {
// If next char is digit or if curr token is var and next char is alpha, keep going.
if (isdigit(next) || (isalpha(next) && currVar)) {
retVal += curr;
retVal += nextToken();
// If next char is whitespace or symbol, end of token, return curr.
} else if (isspace(next) || isSymbol(next)) {
retVal += curr;
// Unexpected character. Add to current token and end current token.
} else {
retVal += curr;
retVal += next;
pos++;
#ifdef DEBUG
std::cerr << "Undefined character: " << next << std::endl;
#endif
}
// Handle known symbols
} else if (isSymbol(curr)) {
retVal = curr;
// Handle all other characters
} else {
retVal += curr;
#ifdef DEBUG
std::cerr << "Undefined character: " << curr << std::endl;
#endif
}
#ifdef DEBUG
std::cerr << "Leaving nextToken(); returning token " << retVal << "." << std::endl;
#endif
return retVal;
}
// Check if character is a recognized symbol
bool LAScanner::isSymbol(char c) {
#ifdef DEBUG
std::cerr << "Entering isSymbol()" << std::endl;
#endif
switch (c) {
case '>': case '<': case '+': case '-':
case '/': case '*': case ',': case ';':
case '{': case '}': case '(': case ')':
case '[': case ']': case '=': case ':':
#ifdef DEBUG
std::cerr << "Returning true; character is: " << c << std::endl;
#endif
return true;
default:
#ifdef DEBUG
std::cerr << "Returning false; character is: " << c << std::endl;
#endif
return false;
}
}
#undef DEBUG
#ifdef DEBUG
// Used for debugging/testing the scanner by printing each token on its own line.
int main (int c, char** v) {
LAScanner scanner = LAScanner("input");
while (scanner.peek().compare("") != 0)
std::cout << scanner.nextToken() << std::endl;
}
#endif