-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
369 lines (316 loc) · 6.03 KB
/
Source.cpp
File metadata and controls
369 lines (316 loc) · 6.03 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 <vector>
#include <fstream>
using std::cout;
using std::cin;
const int numChars = 63;
int advance(char input, int arr[][numChars], int currentState);
//test states
enum States{start = 1, id = 2, num = 3, finish = 4, error = 5};
/*
2 ident = LU LUD * // identifier.
LU = '_' | [a..zA..Z] // Letter-Underscore.
LUD = LU | DIGIT // Letter-Underscore-Digit.
DIGIT = [0..9]
*/
//using table driven version
int table[5][numChars];
int main()
{
States state = start;
char buffer;
//fill first row with every possible valid character
table[0][0] = 95;
for (int i = 97; i < 123; i++)
{
table[0][i - 96] = i;
}
for (int i = 65; i < 91; i++)
{
table[0][i - 38] = i;
}
for (int i = 48; i < 58; i++)
{
table[0][i + 5] = i;
}
//---------------------table[1][...] Starting state----------------
table[1][0] = 2; //'_'
//a-z
for (int i = 97; i < 123; i++)
{
table[1][i - 96] = 2;
}
//A-Z
for (int i = 65; i < 91; i++)
{
table[1][i - 38] = 2;
}
//0-9
for (int i = 48; i < 58; i++)
{
table[1][i + 5] = 4;
}
//----------------------------------------------------------------
//-------------------table[2][...] ID State-----------------------
table[2][0] = 1; //'_'
//a-z
for (int i = 97; i < 123; i++)
{
table[2][i - 96] = 2;
}
//A-Z
for (int i = 65; i < 91; i++)
{
table[2][i - 38] = 2;
}
//0-9
for (int i = 48; i < 58; i++)
{
table[2][i + 5] = 2;
}
//-----------------------------------------------------------------
cout << "Test Below\n";
for (int i = 0; i < numChars; i++)
{
cout << (char)table[0][i] << ' ';
}
while (state != finish && state != error)
{
switch (state)
{
case start:
cout << "Enter character(Start State): ";
cin >> buffer;
int test;
test = States(advance(buffer, table, 1));
cout << "test: " << test << std::endl;
state = (States)test;
break;
case id:
cout << "Enter character(ID State): ";
cin >> buffer;
int test2;
test2 = States(advance(buffer, table, 2));
cout << "test2: " << test2;
break;
case num:
cout << "Enter character(num State): ";
cin >> buffer;
state = States(advance(buffer, table, 3));
break;
case finish:
cout << "Enter character(finish State): ";
cin >> buffer;
state = States(advance(buffer, table, 4));
break;
case error:
cout << "Error in code";
//exit(EXIT_FAILURE);
break;
}
}
return 0;
}
int advance(char input, int arr[][numChars], int currentState)
{
std::cout << "CurrentState: " << currentState << std::endl;
for (int i = 0; i < numChars; i++)
{
if (input == (char)arr[0][i])
{
return arr[currentState][i];
}
}
cout << "couldn't find char\n";
//store input in the lexeme* buffer
//advance the buffer index
//or refill the buffer and setindex to 0
//watching out for EOF
return -1;
}
//STEP 1 Write Regular Expressions for all tokens we need to recognize
//STEP 2 Form NFA using Thompson's construction that recognizes these tokens
//Step 3 From the NFA, the subset construction will give the DFA which can be embedded as a table in the lexical analyzer
//BELOW IS CODE THAT NEEDS TO BE MERGED INTO PRECEEDING CODE
/*
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <locale>
using namespace std;
void output();
int lineNum = 1;
string define;
string getChar();
int ID;
struct lex {
};
int main()
{
ifstream inFile;
inFile.open("lexerExamp.txt");
while (getline(inFile, define))
{
getChar();
lineNum++;
}
return 0;
}
void output()
{
/*cout << "(:Token " << lineNum << keyWD << ")" << endl;
if (option == 3)
{
cout << ":ix " << ixNum;
}
else if (option == 2)
{
cout << ":str" << "" << define << "" << ")";
}
*/
}
string getChar()
{
}
void lexerLine(string str)
{
string token;
stringstream stringStream(str);
bool commentFound;
while (stringStream >> token && !commentFound)
{
//token[0]
if (isalpha(str[0]))
//void
alphAFunc(token);
else if (isdigit(str[0]))
//void
digitFunc(token);
else
//void
commentFound = symbolFunc(token);
}
}
void alphAFunc(string token)
{
}
bool symbolFunc(string token)
{
// return if comment found
}
void digitFunc(string token)
{
if (isdigit[token])
{
setID();
}
else if (token == '.')
{
setID();
}
else
{
return 0;
}
}
void setID(string y, string orange)
{
if (y == "identifier")
ID = 2;
else if (y = "integer")
ID = 3;
else if (y = "float")
ID = 4;
else if (y = "string")
ID = 5;
//Unpaired delimiters
else if (orange = ",")
ID = 6;
else if (orange = ";")
ID = 7;
//keywords
else if (orange = "prog")
ID = 10;
else if (orange = "main")
ID = 11;
else if (orange = "fcn")
ID = 12;
else if (orange = "class")
ID = 13;
else if (orange = "float")
ID = 15;
else if (orange = "int")
ID = 16;
else if (orange = "string")
ID = 17;
else if (orange = "if")
ID = 18;
else if (orange = "elseif")
ID = 19;
else if (orange = "else")
ID = 20;
else if (orange = "while")
ID = 21;
else if (orange = "input")
ID = 22;
else if (orange = "print")
ID = 23;
else if (orange = "new")
ID = 24;
else if (orange = "return")
ID = 25;
// Paired delieters
else if (orange = "<")
ID = 31;
else if (orange = ">")
ID = 32;
else if (orange = "{")
ID = 33;
else if (orange = "}")
ID = 34;
else if (orange = "[")
ID = 35;
else if (orange = "]")
ID = 35;
else if (orange = "(")
ID = 37;
else if (orange = ")")
ID = 38;
//Other punctuation
else if (orange = "*")
ID = 41;
else if (orange = "^")
ID = 42;
else if (orange = ":")
ID = 43;
else if (orange = ".")
ID = 44;
else if (orange = "=")
ID = 45;
else if (orange = "-")
ID = 46;
else if (orange = "+")
ID = 47;
else if (orange = "/")
ID = 48;
//Multi-char operators
else if (orange = "->")
ID = 51;
else if (orange = "==")
ID = 52;
else if (orange = "!=")
ID = 53;
else if (orange = "<=")
ID = 54;
else if (orange = ">=")
ID = 55;
else if (orange = "<<")
ID = 56;
else if (orange = ">>")
ID = 57;
//miscellaeous
else if (orange = "error")
ID = 99;
}
*/