-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzourOTHERLexer.txt
408 lines (364 loc) · 14.3 KB
/
zourOTHERLexer.txt
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
import java.util.*;
import java.nio.file.*;
import java.io.*;
// private Scanner reader;
// private final String codePath; //pathway of the file containing the Quick Basic code
// private String basicCode = "";
// private int position;
// private String currentString;
// private static List<String> inputList;
// private String previousCharacter;
// private List<String> listOfVariables;
// private FileWriter Output;
// private static parserFile parserFile;
// private String holdNextLine;
// protected static List<String> lineNumbers;
// private int tempCount;
// private String hold;
// private int posHold;
// private String currChar;
// public static ArrayList<String> tokenInfo = new ArrayList<>();
public class Lexer {
private final String codePath; // pathway of the file containing the input java code
private Scanner reader;
private String input = "";
private int position;
private String currentString;
private static List<String> inputList;
// private String previousCharacter;
// private List<String> listOfVariables;
private FileWriter Output;
// private static parserFile parserFile;
private String holdNextLine;
protected static List<String> lineNumbers;
private int tempCount;
private String hold;
// private int posHold;
// private String currChar;
public static ArrayList<String> tokenInfo = new ArrayList<>();
public static ArrayList<String> symbolTable = new ArrayList<>();
Lexer() {
codePath = "input.java";
position = -1;
// previousCharacter = "";
// listOfVariables = new LinkedList<>();
lineNumbers = new ArrayList<>();
}
protected void readFile() {
try {
reader = new Scanner(new File(codePath));
while (reader.hasNextLine()) {
holdNextLine = reader.nextLine();
if (holdNextLine.contains("//") || holdNextLine.contains(""))// '//' = comment
continue;
if (!holdNextLine.equals("")) {
lineNumbers.add(holdNextLine.substring(0, 3));
input += holdNextLine + " ";
}
}
reader.close();
inputList = new LinkedList<>(Arrays.asList(input.split(" ")));
} catch (FileNotFoundException e) {
System.out.print("File not found: " + e.getMessage());
}
}
protected void createTokens() throws IOException {
/*
* LEXEME - Sequence of characters matched by PATTERN forming the TOKEN PATTERN
* - The set of rule that define a TOKEN TOKEN - The meaningful collection of
* characters over the character set of the programming language ex:ID,
* Constant, Keywords, Operators, Punctuation, Literal String [Token] [Informal
* Description] [Sample Lexemes] if characters i, f if else characters e, l, s,
* e else comparison < or > or <= or >= or == or != <=, != id letter followed by
* letters and digits pi, score, D2 number any numeric constant 3.14159, 0,
* 6.02e23 literal anything but ", surrounded by "'s "core dumped"
*/
ArrayList<String> tokenType = new ArrayList<>();
tokenType.add("abstract"); //1
tokenType.add("boolean");// 2
tokenType.add("break");// 3
tokenType.add("byte");// 4
tokenType.add("case");// 5
tokenType.add("catch");// 6
tokenType.add("class");// 7
tokenType.add("continue");// 8
tokenType.add("const");// 9
tokenType.add("default");// 10
tokenType.add("do");// 11
tokenType.add("double");// 12
tokenType.add("else");// 13
tokenType.add("enum");// 14
tokenType.add("extends");// 15
tokenType.add("final");// 16
tokenType.add("final");// 17
tokenType.add("finally");// 18
tokenType.add("float");// 19
tokenType.add("for");// 20
tokenType.add("interface"); // 21
tokenType.add("int");// 22
tokenType.add("if");// 23
tokenType.add("long");// 24
tokenType.add("main");// 25
tokenType.add("private");// 26
tokenType.add("protected");// 27
tokenType.add("public");// 28
tokenType.add("short");// 29
tokenType.add("static");// 30
tokenType.add("switch");// 31
tokenType.add("return");// 32
tokenType.add("void"); // 33
tokenType.add("volatile"); // 34
tokenType.add("this"); // 35
tokenType.add("throw");// 36
tokenType.add("try");// 37
tokenType.add("var");// 38
tokenType.add("while");// 39
tokenType.add("+");// 40
tokenType.add("-");// 41
tokenType.add("*");// 42
tokenType.add("/");// 43
tokenType.add("\""); // 44
tokenType.add("="); // 45
tokenType.add(">");// 46
tokenType.add("<");// 47
tokenType.add(">=");// 48
tokenType.add("<=");// 49
tokenType.add(":");// 50
tokenType.add("^");// 51
tokenType.add(";");// 52
tokenType.add("(");// 53
tokenType.add(")");// 54
tokenType.add("System.out.print");// 55
tokenType.add("System.out.println");// 56
//POPULATE SYMBOL TABLE
symbolTable.add("abstract"); //java
symbolTable.add("abstract"); //C#
symbolTable.add("boolean");// java
symbolTable.add("bool");// C#
symbolTable.add("break");// Java
symbolTable.add("break");// C#
symbolTable.add("byte");// Java
symbolTable.add("byte");// C#
symbolTable.add("case");// Java
symbolTable.add("case");// C#
symbolTable.add("catch");// Java
symbolTable.add("catch");// C#
symbolTable.add("class");// Java
symbolTable.add("class");// C#
symbolTable.add("continue");// Java
symbolTable.add("continue");// C#
symbolTable.add("const");// Java
symbolTable.add("const");// C#
symbolTable.add("default");// Java
symbolTable.add("default");// C#
symbolTable.add("do");// Java
symbolTable.add("do");// C#
symbolTable.add("double");// Java
symbolTable.add("double");// C#
symbolTable.add("else");// Java
symbolTable.add("else");// c#
symbolTable.add("enum");// java
symbolTable.add("enum");// C#
symbolTable.add("extends");// Java
symbolTable.add("extends");// C#
symbolTable.add("final");// Java
symbolTable.add("sealed");// C#
symbolTable.add("final");// Java
symbolTable.add("readonly");// C# alternative!
symbolTable.add("finally");// Java
symbolTable.add("finally");// C#
symbolTable.add("float");// Java
symbolTable.add("float");// C#
symbolTable.add("for");// Java
symbolTable.add("for");// C#
symbolTable.add("interface"); // Java
symbolTable.add("interface"); // C#
symbolTable.add("int");// Java
symbolTable.add("int");// C#
symbolTable.add("if");// Java
symbolTable.add("if");// C#
symbolTable.add("long");// Java
symbolTable.add("long");// C#
symbolTable.add("main");// Java
symbolTable.add("main");// C#
symbolTable.add("private");// Java
symbolTable.add("private");// C#
symbolTable.add("protected");// Java
symbolTable.add("protected");// C#
symbolTable.add("public");// Java
symbolTable.add("public");// C#
symbolTable.add("short");// Java
symbolTable.add("short");// C#
symbolTable.add("static");// Java
symbolTable.add("static");// C#
symbolTable.add("switch");// Java
symbolTable.add("switch");// C#
symbolTable.add("return");// Java
symbolTable.add("return");// C#
symbolTable.add("void"); // Java
symbolTable.add("void"); // C#
symbolTable.add("volatile"); // Java
symbolTable.add("volatile"); // C#
symbolTable.add("this"); // Java
symbolTable.add("this"); // C#
symbolTable.add("throw");// Java
symbolTable.add("throw");// C#
symbolTable.add("try");// Java
symbolTable.add("try");// C#
symbolTable.add("var");// Java
symbolTable.add("var");// C#
symbolTable.add("while");// Java
symbolTable.add("while");// C#
symbolTable.add("+");// Java
symbolTable.add("+");// C#
symbolTable.add("-");// Java
symbolTable.add("-");// C#
symbolTable.add("*");// Java
symbolTable.add("*");// C#
symbolTable.add("/");// Java
symbolTable.add("/");// C#
symbolTable.add("\""); // Java
symbolTable.add("\""); // C#
symbolTable.add("="); // Java
symbolTable.add("="); // C#
symbolTable.add(">");// Java
symbolTable.add(">");// C#
symbolTable.add("<");// Java
symbolTable.add("<");// C#
symbolTable.add(">=");// Java
symbolTable.add(">=");// C#
symbolTable.add("<=");// Java
symbolTable.add("<=");// C#
symbolTable.add(":");// Java
symbolTable.add(":");// C#
symbolTable.add("^");// Java
symbolTable.add("^");// C#
symbolTable.add(";");// Java
symbolTable.add(";");// C#
symbolTable.add("(");// Java
symbolTable.add("(");// C#
symbolTable.add(")");// Java
symbolTable.add(")");// C#
symbolTable.add("System.out.print");// Java
symbolTable.add("Console.Write");// C#
symbolTable.add("System.out.println");// Java
symbolTable.add("Console.WriteLine");// C#
// holds token info
try {
Files.deleteIfExists(Paths.get("output.txt"));
} catch (NoSuchFileException e) {
System.out.println("No such file/directory exists");
}
// gets our first character
getNextString();
try {
Output = new FileWriter("output.txt", false);
} catch (IOException e) {
System.out.println(e.getMessage());
}
// 99:
// Stores symbolTable in array
//String[] symbolTable = createSymbolTable("symbol.txt");
// try {
// symbolTable = createSymbolTable("symbol.txt");
// } catch (FileNotFoundException e) {
// System.out.print("File not found: " + e.getMessage());
// }
// int lineCounter = 0;
while (position < inputList.size()) {
// handleColon();
/*
for (int i = 0; i < symbolTable.length; i+=2) {
if (symbolTable[i].equals("abstract") && currentString.equals(symbolTable[i + 1])) {
Output.write("Lexeme:" + symbolTable[i] + " Token:" + tokenType.get(1));
// "\n");
System.out.print("Lexeme:" + symbolTable[i] + " Token:" + tokenType.get(1));
tokenInfo.add("abstract");
getNextString();
// handleColon();
// handleSemicolon();
break;
}
}
*/
//String line = reader.nextLine().trim();
for (int i=0; i<symbolTable.size(); i++)
{
//if string in TokenType matches string in SymbolTable
if (tokenType.get(i).equals(symbolTable.get(i)))
{
tokenInfo.add(String.valueOf(i)); //add this token to verified list
Output.write("Lexeme: " + symbolTable.get(i) + " Token: " + tokenInfo.get(i));
getNextString();
}
}
Output.close();
}
}
private String[] createSymbolTable(String symbolTablePath) throws IOException {
String[] symbolTable = {};
boolean fileExists = false;
String symbolTableContents = "";
try {
reader = new Scanner(new File(symbolTablePath));
fileExists = true;
} catch (FileNotFoundException e) {
System.out.print("File not found: " + e.getMessage());
}
if (fileExists) {
while (reader.hasNextLine()) {
symbolTableContents += reader.nextLine();
Output.write(symbolTableContents);
// System.out.println("reached");
symbolTable = symbolTableContents.split("\\|");
}
} else {
System.out.println("File does not exist so symbol table could not be created");
}
return symbolTable;
}
private void handleColon() {
if (currentString.endsWith(":") && !currentString.equals(":")) {
String reformatCharacter = currentString.replace(":", "").trim();
inputList.set(position, reformatCharacter);
currentString = inputList.get(position);
inputList.add(position + 1, ":");
// dont change these position variables. add inserts before it and set replaces
// at position
}
}
private void handleSemicolon() {
if (currentString.endsWith(";") && !currentString.equals(";")) {
String reformatCharacter = currentString.replace(";", "").trim();
inputList.set(position, reformatCharacter);
currentString = inputList.get(position);
inputList.add(position + 1, ";");
// dont change these position variables. add inserts before it and set replaces
// at position
}
}
protected String getNextString() {
position += 1;
if (position < inputList.size())
currentString = inputList.get(position);
return currentString;
}
protected String getPreviousCharacter() {
if (position > 0)
tempCount = position - 1;
hold = inputList.get(tempCount);
return hold;
}
public static List<String> getinputList() {
return inputList;
}
public static ArrayList<String> getTokenInfo() {
return tokenInfo;
}
public static List<String> getLineNumbers() {
return lineNumbers;
}
/*
* public static parserFile getParserFile(){ return parserFile; }
*/
}