-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReference(Par)3.txt
322 lines (297 loc) · 11.9 KB
/
Reference(Par)3.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
package com.company;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.*;
import java.io.FileNotFoundException;
public class parserFile
{
private FileWriter Output;
private String filePath;
private int tokenIndex;
private String holder;
private int ahead;
private int behind;
private int twoBehind;
private int twoAhead;
private String currentToken;
private String nextToken;
private ArrayList<String> tokenInfo = new ArrayList<>();
private List<String> lineNumbers = new ArrayList<>();
private int lineCount = 1;
private boolean EOF;
private int binaryTreePosition;
TreeMap<Integer, String> binaryTree = new TreeMap<Integer, String>();
parserFile(ArrayList<String> tokenInfo, List<String> lineNumbers) {
this.lineNumbers = lineNumbers;
tokenIndex = 0;
this.tokenInfo = tokenInfo;
currentToken = tokenInfo.get(tokenIndex);
binaryTreePosition = 0;
try {
Files.deleteIfExists(Paths.get("parserOutput.txt"));
Output = new FileWriter("parserOutput.txt", true);
}
catch (NoSuchFileException e) {
System.out.println("No such file/directory exists");
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
// System.out.println("--------BINARY TREE-----------");
}
public void createBinaryTree() {
int position = 1;
while (tokenIndex < tokenInfo.size())
{
nextToken = lookAhead();
outerloop:
for (; position <= tokenInfo.size() - 1; position++)//checking for everything begins here
{
//region error checking
if (currentToken.equals("INTEGER")) {
if (!(lookTwoAhead().equals("+") || lookTwoAhead().equals("-") || lookTwoAhead().equals("*") || lookTwoAhead().equals("/") || lookTwoAhead().equals("=") || lookTwoAhead().equals("LET") || !lineNumbersContain(lookTwoAhead()) || lookTwoAhead().equals(")") || lookTwoAhead().equals("EXP"))) {
for (; lineCount < lineNumbers.size();) {
System.out.println("Error: Integer needs to be followed by an arithmetic function"); //throw an error
return;
}
}
}
else if (currentToken.equals("FLOAT")) {
if (!(lookTwoAhead().equals("+") || lookTwoAhead().equals("-") || lookTwoAhead().equals("*") || lookTwoAhead().equals("/") || lookTwoAhead().equals("=") || lookTwoAhead().equals("LET") || !lineNumbersContain(lookTwoAhead()))) {
for (; lineCount < lineNumbers.size();) {
String temp = lookTwoAhead();
System.out.println("Error: FLOAT needs to be followed by an arithmetic function"); //throw an error
return;
}
}
}
//PRINT string, int, or variable
else if (currentToken.equals("PRINT")) {
if (!(lookAhead().equals("VAR") || lookAhead().equals("\"\"") || lookAhead().equals("INTEGER"))) {
System.out.println("Error: Print command must be followed by a String, VAR, or INTEGER"); //throw an error
return;
}
String temp = lookTwoAhead();
}
else if (currentToken.equals("LET")) {
if ((lookAhead().equals("INTEGER")))
{
System.out.println("Error: Let keyword needs to be followed by a character"); //throw an error
return;
}
}
else if (currentToken.equals("=")) {
if (!(lookAhead().equals("INTEGER") || lookAhead().equals("VAR") || lookAhead().equals("\"\"") || lookAhead().equals("EXP") || lookAhead().equals("INT"))) {
System.out.println("Error: = operator must be followed by INT,VAR or STRING."); //throw an error
return;
}
}
else if (currentToken.equals("NEXT")) {
if (!(lookAhead().equals("VAR"))) {
System.out.println("Error: NEXT must be followed by VAR."); //throw an error
return;
}
}
else if (currentToken.equals("EXP"))
{
if (!(lookAhead().equals("("))) {
System.out.println("Error: Unexpected token after EXP."); //throw an error
return;
}
}
else if (currentToken.equals("SQR"))
{
if (!(lookAhead().equals("("))) {
System.out.println("Error: Unexpected token after SQR ."); //throw an error
return;
}
}
else if (currentToken.equals("END"))
{
String temp = lookAhead();
}
//endregion
for (int x = 0; x < lineNumbers.size(); x++)
{
if (currentToken.equals(lineNumbers.get(x)))
{
currentToken = getNextToken();
continue outerloop;
}
}
if (currentToken.equals("INTEGER")) {
currentToken = getNextToken();
binaryTree.put(binaryTreePosition++, currentToken);
currentToken = getNextToken();
break;
}
else if (currentToken.equals("FLOAT")) {
currentToken = getNextToken();
binaryTree.put(binaryTreePosition++, currentToken);
currentToken = getNextToken();
break;
}
else if (currentToken.equals("PRINT"))
{
binaryTree.put(binaryTreePosition++, currentToken);
currentToken = getNextToken();
break;
}
else if (currentToken.equals("\"\""))
{
currentToken = getNextToken();
binaryTree.put(binaryTreePosition++, currentToken);
currentToken = getNextToken();
break;
}
else if(currentToken.equals("+"))
{
binaryTree.put(binaryTreePosition++, currentToken);
currentToken = getNextToken();
break;
}
else if(currentToken.equals("LET"))
{
binaryTree.put(binaryTreePosition++, currentToken);
currentToken = getNextToken();
break;
}
else if(currentToken.equals("="))
{
binaryTree.put(binaryTreePosition++, currentToken);
currentToken = getNextToken();
break;
}
else if(currentToken.equals("VAR"))
{
currentToken = getNextToken();
binaryTree.put(binaryTreePosition++, currentToken);
currentToken = getNextToken();
break;
}
else if(currentToken.equals("NEXT"))
{
binaryTree.put(binaryTreePosition++, currentToken);
currentToken = getNextToken();
break;
}
else if(currentToken.equals("END")) {
binaryTree.put(binaryTreePosition++, currentToken);
currentToken = getNextToken();
break;
}
else if(currentToken.equals("(")) {
binaryTree.put(binaryTreePosition++, currentToken);
currentToken = getNextToken();
break;
}
else if(currentToken.equals(")")) {
binaryTree.put(binaryTreePosition++, currentToken);
currentToken = getNextToken();
break;
}
else if(currentToken.equals("EXP")) {
binaryTree.put(binaryTreePosition++, currentToken);
currentToken = getNextToken();
break;
}
else if(currentToken.equals("SQR")) {
binaryTree.put(binaryTreePosition++, currentToken);
currentToken = getNextToken();
break;
}
else if(currentToken.equals("^")) {
binaryTree.put(binaryTreePosition++, currentToken);
currentToken = getNextToken();
break;
}
else if(currentToken.equals("-")) {
binaryTree.put(binaryTreePosition++, currentToken);
currentToken = getNextToken();
break;
}
else if(currentToken.equals("/")) {
binaryTree.put(binaryTreePosition++, currentToken);
currentToken = getNextToken();
break;
}
else if(currentToken.equals("INT")) {
binaryTree.put(binaryTreePosition++, currentToken);
currentToken = getNextToken();
break;
}
}
}
for(Map.Entry m:binaryTree.entrySet())
{
// System.out.println("Key:"+m.getKey() + " Value:"+m.getValue());
for (int count = 0; count < 1; count++) {
try {
Output.write("Key:"+m.getKey() + " Value:"+m.getValue()+"\n");
break;
} catch (IOException e) {
e.printStackTrace();
}
}
}
// System.out.println("--------BINARY TREE END-----------");
try {
Output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private String getNextToken() {
tokenIndex += 1;//calling this increments tokenIndex
if (tokenIndex < tokenInfo.size())
currentToken = tokenInfo.get(tokenIndex);
return currentToken;
}
private String lookAhead() {
if (tokenIndex < tokenInfo.size()-1) {
ahead = tokenIndex + 1;
holder = tokenInfo.get(ahead);
return holder;
} else {
return "Error: no can do buckaroo";
}
}
private String lookTwoAhead() {
if (tokenIndex < tokenInfo.size() - 2) {
twoAhead = tokenIndex + 2;
holder = tokenInfo.get(twoAhead);
return holder;
} else {
return "Error: no can do buckaroo";
}
}
private String lookBehind() {
behind = tokenIndex - 1;
holder = tokenInfo.get(behind);
return holder;
}
private String lookTwoBehind() {
twoBehind = tokenIndex - 2;
holder = tokenInfo.get(twoBehind);
return holder;
}
private boolean lineNumbersContain(String x)
{
for (int i = 0; i < lineNumbers.size()-1; i++)
{
if (x.equals(lineNumbers))
{
return true;
}
}
return false;
}
public TreeMap<Integer, String> getBinaryTree(){
return binaryTree;
}
}