-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexicon.cpp
299 lines (274 loc) · 8.52 KB
/
lexicon.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
#include "lexicon.hpp"
#include "iostream"
#include <fstream>
#include <cstring>
using namespace std;
string NT;
const string IDENTIFIER_TOKEN = "IDENTIFIER";
const string STRING_TOKEN = "STRING";
const string OPERATOR_TOKEN = "OPERATOR";
const string INTEGER_TOKEN = "INTEGER";
const string KEYWORD_TOKEN = "KEYWORD";
const string UNDEFINED_TOKEN = "UNDEFINED";
const string PUNCTUATION_TOKEN = "PUNCTUATION";
const char *operatorArray = "+-*<>&.@/:=~|$!#%^_[]{}\"`?";
const char *stringAllowedCharArray = "();, ";
const char *stringAllowedEscapeCharArray = "tn\\\'";
const char *eolCharArray = "\r\n";
const char *punctuationArray = "();,";
string nextTokenType = UNDEFINED_TOKEN;
/*
Checks if the end of the file has been reached using the good() function and peek()
function. The good() function checks if the stream is in a good state, and the peek()
function looks ahead to the next character without extracting it.
*/
bool checkIfEOF(ifstream &file)
{
if (!file.good() || file.peek() == EOF)
{
return true;
}
return false;
}
/*
Read an identifier/keyword token into NT.
*/
void readIdentifierToken(ifstream &file)
{
if (checkIfEOF(file))
{
// cout << "\n\nEOF reached unexpectedly without correct parsing through grammar! Will die now!!\n\n";
exit(0);
}
nextTokenType = IDENTIFIER_TOKEN;
char x; // get the next character in input stream
char peek = file.peek(); // peek and store the next character in input stream
while (isdigit(peek) || isalpha(peek) || peek == '_')
{
file.get(x);
NT += x;
peek = file.peek();
}
if (NT.compare("rec") == 0 || NT.compare("where") == 0 || NT.compare("in") == 0 || NT.compare("and") == 0 ||
NT.compare("let") == 0 || NT.compare("fn") == 0 || NT.compare("or") == 0 || NT.compare("not") == 0 ||
NT.compare("gr") == 0 || NT.compare("ge") == 0 || NT.compare("ls") == 0 || NT.compare("le") == 0 ||
NT.compare("eq") == 0 || NT.compare("ne") == 0 || NT.compare("within") == 0 || NT.compare("true") == 0 ||
NT.compare("false") == 0 || NT.compare("nil") == 0 || NT.compare("dummy") == 0 || NT.compare("aug") == 0)
{
nextTokenType = KEYWORD_TOKEN;
// cout << "\nKeyword: " << NT << "\n";
}
/*else
{
cout << '\n'
<< nextTokenType << ": " << NT << "\n";
}*/
}
void readIntegerToken(ifstream &file)
{
if (checkIfEOF(file))
{
// cout << "\n\nEOF reached unexpectedly without correct parsing through grammar! Will die now!!\n\n";
exit(0);
}
nextTokenType = INTEGER_TOKEN;
char x; // get the next character in input stream
char peek = file.peek(); // peek and store the next character input stream
while (isdigit(peek))
{
file.get(x);
NT += x;
peek = file.peek();
}
// cout << "\n" << nextTokenType << ": " << NT << "\n";
}
bool isOperator(char c)
{
if (strchr(operatorArray, c))
return true;
else
return false;
}
void readOperatorToken(ifstream &file)
{
if (checkIfEOF(file))
{
// cout << "\n\nEOF reached unexpectedly without correct parsing through grammar! Will die now!!\n\n";
exit(0);
}
nextTokenType = OPERATOR_TOKEN;
char x; // get the next character in stream in this
char peek = file.peek(); // peek and store the next character in stream in this
while (isOperator(peek))
{
file.get(x);
NT += x;
peek = file.peek();
}
// cout << '\n' << nextTokenType << ": " << NT << "\n";
}
bool isPunctuation(char c)
{
if (strchr(punctuationArray, c))
return true;
else
return false;
}
void readPunctuationChar(ifstream &file)
{
if (checkIfEOF(file))
{
// cout << "\n\nEOF reached unexpectedly without correct parsing through grammar! Will die now!!\n\n";
exit(0);
}
nextTokenType = PUNCTUATION_TOKEN;
char x; // get the next character in input stream
char peek = file.peek(); // peek and store the next character input stream
if (isPunctuation(peek))
{
file.get(x);
NT += x;
}
// cout << "\n" << nextTokenType << ": " << NT << "\n";
}
bool isStringAllowedChar(char c)
{
if (strchr(stringAllowedCharArray, c) || isdigit(c) || isalpha(c) || isOperator(c))
return true;
else
return false;
}
bool isEscapeCharInString(ifstream &file, char &peek)
{
char x; // get the next character in stream in this
// peek and store the next character in stream in this
if (peek == '\\')
{
file.get(x);
NT += x; // Add the escape backslash to the string token (as per the reference implementation)
peek = file.peek();
if (strchr(stringAllowedEscapeCharArray, peek))
{
file.get(x);
NT += x;
peek = file.peek();
return true;
}
else
{
cout << "\n\nERROR! Expected an escape character, but " << peek << " happened! Parser will DIE now!\n\n";
throw exception();
}
}
else
return false;
}
void readStringToken(ifstream &file)
{
if (checkIfEOF(file))
{
// cout << "\n\nEOF reached unexpectedly without correct parsing through grammar! Will die now!!\n\n";
exit(0);
}
nextTokenType = STRING_TOKEN;
char x; // get the next character in stream in this
char peek = file.peek(); // peek and store the next character in stream in this
if (peek == '\'')
{ // check for the single quote to start the string
file.get(x);
NT += x; // Add quotes to the token to separate the string from non-string literals with same value
peek = file.peek();
}
else
{
// cout << "\n\nERROR! Expected start of string, but " << peek << " happened! Parser will DIE now!\n\n";
throw exception();
}
while (isStringAllowedChar(peek) || (isEscapeCharInString(file, peek) && isStringAllowedChar(peek)))
{
file.get(x);
NT += x;
peek = file.peek();
}
if (peek == '\'')
{ // check for the single quote to close the string
file.get(x);
NT += x; // Add quotes to the token to separate the string from non-string literals with same value
}
else
{
// cout << "\n\nERROR! Expected close of string, but " << peek << " happened! Parser will DIE now!\n\n";
throw exception();
}
// cout << "\n" << nextTokenType << ": " << NT << "\n";
}
void scan(ifstream &file);
void resolveIfCommentOrOperator(ifstream &file)
{
char x;
file.get(x); // Move past the first '/'
char peek = file.peek();
if (peek == '/')
{
// This means it's a comment line, so keep reading/updating file stream pointer without "tokenizing" (adding to NT) until an eol.
while (!strchr(eolCharArray, peek))
{
file.get(x); // move past the whitespaces until an eol
peek = file.peek();
}
file.get(x); // Move past the EOL
// cout << "\nComment ignored";
// cout << "\nGoing to scan!";
scan(file); // call scan to get the next token
}
else
{
// this means it's an operator sequence
NT += '/'; // Add the first '/' that we moved past to the operator token
readOperatorToken(file);
}
}
void scan(ifstream &file)
{
if (checkIfEOF(file))
{
// cout << "\n\nEOF reached !\n\n";
return;
}
nextTokenType = UNDEFINED_TOKEN;
char peek = file.peek(); // peek and store the next character in stream in this
// cout << "\nIn scan, peek= '" << peek << "'";
NT.clear(); // clear NT to get the next token in file
if (isalpha(peek))
{
readIdentifierToken(file);
}
else if (isdigit(peek))
{
readIntegerToken(file);
}
else if (peek == '/')
{
resolveIfCommentOrOperator(file);
}
else if (isOperator(peek))
{
readOperatorToken(file);
}
else if (peek == '\'')
{ // Start of a string
readStringToken(file);
}
else if (iswspace(peek))
{ // ignore whiteSpace chars (space, horTab, newLine, carrReturn, verTab)
char x;
file.get(x); // further the file pointer and ignore the whitespace character (no need to tokenize it)
NT += x;
// cout << "\nGoing to scan!";
scan(file); // call scan to get the next token
}
else if (isPunctuation(peek))
{
readPunctuationChar(file);
}
}