-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.java
478 lines (426 loc) · 10.5 KB
/
Parser.java
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package parser;
import java.util.Scanner;
public final class Parser {
//Token Types
public static final int NONE=0;
public static final int DELIMITER=1;
public final int VARIABLE=2;
public static final int NUMBER=3;
//Types of errors which can occur during parsing
public static final int SYNTAX=0;
public static final int UNBALANCED_PARANS=1;
public static final int NOEXP=2;
public static final int DIVBYZERO=3;
//Token to indicate end of expression, indicating the parser should stop.
static final String EOE="\0";
//Expression reference String
private String exp;
//Next token index into the expression to be parsed
private int expIdx;
//Current Token
private String token;
//Token type, [NONE,DELIMITER,VARIABLE,NUMBER]
private int tokType;
//Array of values for variables, A-Z
double[] variables=new double[26];
public Parser(String expression)
{
this.exp=expression;
}
//Empty constructor to allow changing the expression to be parsed after a default initialization
public Parser()
{
this.exp="";
}
//Set or change the expression the parser should evaluate, soft-resets the parser, but does not affect assigned variables
public void setExpression(String exp)
{
this.exp=exp;
this.expIdx=0;
this.token="";
this.tokType=Parser.NONE;
}
//Finds and returns the value for variables A-Z
private double findValue(String name) throws ParserException
{
if(!Character.isLetter(name.charAt(0)))
{
handleError(Parser.SYNTAX);
return 0.0;
}
double value=variables[Character.toUpperCase(name.charAt(0))-'A'];
return value;
}
//Checks if input character is a delimiter
private static boolean isDelimiter(char ch)
{
if(" +-/*^=() ".indexOf(ch)!=-1)
{
return true;
}
return false;
}
//Gets the next token, by accessing it via
private void getToken()
{
this.token="";
this.tokType=NONE;
if(expIdx==exp.length())
{
//End of expression reached
this.token=EOE;
return;
}
//skip over all whitespaces after ensuring EOE is not already reached
while(expIdx<exp.length()&&Character.isWhitespace(exp.charAt(expIdx)))
{
++expIdx;
}
if(expIdx==exp.length())
{
//The expression ended with a whitespace.
this.token=EOE;
return;
}
if(isDelimiter(exp.charAt(expIdx)))
{
this.token=exp.charAt(expIdx)+"";
this.tokType=DELIMITER;
++expIdx;
}
else if(Character.isLetter(exp.charAt(expIdx)))
{
while(!isDelimiter(exp.charAt(expIdx)))
{
if(Character.isDigit(exp.charAt(expIdx)))
{
this.token=EOE;
//this.tokType=NONE;
//ERROR: INVALID SYNTAX
System.err.println("Invalid syntax");
return;
}
this.token+=exp.charAt(expIdx);
++expIdx;
if(expIdx>=exp.length())
{
break;
}
}
this.tokType=VARIABLE;
}
else if(Character.isDigit(exp.charAt(expIdx)))
{
while(!isDelimiter(exp.charAt(expIdx)))
{
if(Character.isLetter(exp.charAt(expIdx)))
{
this.token=EOE;
//this.tokType=NONE;
//ERROR: INVALID SYNTAX
System.err.println("Invalid syntax");
return;
}
this.token+=exp.charAt(expIdx);
++expIdx;
if(expIdx>=exp.length())
{
break;
}
}
this.tokType=NUMBER;
}
else
{
//Invalid character
this.token=EOE;
//ERROR:INVALID SYNTAX
System.err.println("Illegal character");
return;
}
}
//returns the type associated with <int> token type
private static String type(int tktype)
{
switch(tktype)
{
case 0:
return "NONE";
case 1:
return "DELIMITER";
case 2:
return "VARIABLE";
case 3:
return "NUMBER";
default:
return null;
}
}
private static boolean isIllegalCharacter(char c)
{
if(!(Character.isDigit(c)||Character.isLetter(c)||Parser.isDelimiter(c)||Character.isWhitespace(c)||c=='.'))
{
return true;
}
return false;
}
//main method for parser execution
public static void main(String[] args) {
//Test Tokenization:
// String expression="A +100-(B*C)/2";
//// String expression="1000+B=C";
// if(expression.trim().isEmpty()) {
// System.out.println("Empty expression");
// return;
// }
// for(char c:expression.toCharArray())
// {
// if(Parser.isIllegalCharacter(c))
// {
// System.err.println("Illegal character detected!");
// return;
// }
// }
// Parser parser=new Parser(expression);
// parser.getToken();
// while(!parser.token.equals(EOE))
// {
// System.out.println(parser.token+" "+type(parser.tokType));
// parser.getToken();
// }
//
// }
Parser obj=new Parser();
Scanner sc=new Scanner(System.in);
PARSER_LOOP:
while(true)
{
System.out.println("Input expression to be evaluated:");
String expression=sc.nextLine();
obj.setExpression(expression);
try {
double result=obj.evaluate();
System.out.println(expression +"="+result);
}
catch(ParserException ex)
{
sc.close();
ex.printStackTrace();
return;
}
System.out.println("Continue? y/n");
if(!sc.nextLine().equalsIgnoreCase("y"))
{
break PARSER_LOOP;
}
// System.out.println();
}
sc.close();
}
//throws a ParserException whenever the parser encounters a predefined error.
private static void handleError(int error) throws ParserException
{
String[] err_list= {
"Synatx error in Expression!",
"Unbalanced/Unmatched Paranthesis!",
"Empty Expression!",
"Division by Zero",
};
throw new ParserException(err_list[error]);
}
//Parses a number
private double atom() throws ParserException
{
double result=0.0;
switch(tokType)
{
case NUMBER:
try
{
result= Double.parseDouble(token);
}
catch(NumberFormatException ex)
{
handleError(SYNTAX);
}
getToken();
break;
case VARIABLE:
result=this.findValue(token);//index variable and find it's value
getToken();
break;
default:
handleError(SYNTAX);
}
return result;
}
//evaluates an assignment, if one is present. If not, it simply continues evaluation via add/sub eval.
//lowest priority, 1st in recursive descent parsing.
private double evaluateAssignment() throws ParserException
{
double result;
int var_index;
int tempType;
String tempToken;
if(this.tokType==VARIABLE)
{
//save the token, in case next operator is not assignment(=)
tempToken=new String(this.token);
tempType=this.tokType;
//index at which to store assignment value.
var_index=Character.toUpperCase(this.token.charAt(0))-'A';
getToken(); //Proceed to next Token
//If next token is an assignment, proceed, if not put back the token and move the index pointer back the required number of
//spaces
if(!token.equals("="))
{
//Restore index pointer for expression
if(token!=EOE)
{
for(int i=0;i<token.length();++i)
{
this.expIdx--;
}
}
//restore old token along with it's type
this.token=new String(tempToken);
this.tokType=tempType;
}
else
{
getToken();//get expression after assignment operator to evaluate
result=this.evaluateAddSub();
this.variables[var_index]=result;
return result;
}
}
return this.evaluateAddSub();
}
//Evaluates Parenthesis in an expression-6th in order for recursive descent calls
private double evaluateParans() throws ParserException
{
double result=0.0;
if(token.equals("("))
{
getToken();
result=evaluateAddSub();
if(!token.equals(")"))
{
//Expression had an opening parenthesis but no closing parenthesis to match.
handleError(Parser.UNBALANCED_PARANS);
}
getToken();//Move to next Token
}
else
{
//Token is not a parenthesis and all other operations have already been checked in the recursion chain,
//so it must be a number
result=atom();
}
return result;
}
//Evaluate a unary operator (+/-), 5th in order for recursive descent calls
private double evaluateUnary() throws ParserException
{
double result=0.0;
String operator="";
if(tokType==Parser.DELIMITER&&(token.equals("+")||token.equals("-")))
{
operator=this.token;
getToken();
}
result=this.evaluateParans();//Recursive call to 6th method for evaluating paranthesis,higher priority than
//unary operators.
if(operator.equals("-"))
{
result=-result;
}
return result;
}
//Evaluate an exponent,4th in order for recursive descent calls
private double evaluateExponent() throws ParserException
{
double result=0.0;
double partialResult=0.0;
result=this.evaluateUnary();//call to 5th method in the recursive descent parser chain.
if(token.equals("^"))
{
getToken();//exponent can be invoked multiple times
partialResult=evaluateExponent();//recursive call to calculate exponent, if exponentiation symbol repeats itself.
result=Math.pow(result, partialResult);//Final calculation for exponentiation.
}
return result;
}
//Evaluates multiplication and division, 3rd in the parse tree chain
private double evaluateMultiDiv() throws ParserException
{
char op;
double result;
double partialResult;
result=this.evaluateExponent();
while(((op=token.charAt(0))=='*')||(op=='/')||(op=='%'))
{
getToken();
partialResult=evaluateExponent();
switch(op)
{
case '*':
result=result*partialResult;
break;
case '/':
if(partialResult==0)
{
Parser.handleError(DIVBYZERO);
}
result=result/partialResult;
break;
case '%':
if(partialResult==0)
{
Parser.handleError(DIVBYZERO);
}
result=result%partialResult;
}
}
return result;
}
//Evaluate Addition and Subtraction-This function is second in the chain of recursive calls.
private double evaluateAddSub() throws ParserException
{
double result=0.0;
double partialResult=0.0;
char op;
result=this.evaluateMultiDiv();//Call to higher priority multiplication and division evaluation.
while(((op=token.charAt(0))=='+')||(op=='-'))
{
getToken();
partialResult=this.evaluateMultiDiv();
switch(op)
{
case '+':
result= result+partialResult;
break;
case '-':
result=result-partialResult;
break;
}
}
return result;
}
//Evaluate the mathematical expression defined within this Parser Object
public double evaluate() throws ParserException
{
double result=0.0;
getToken(); //Get the first token
if(token.equals(Parser.EOE))
{
handleError(Parser.NOEXP);
}
result=this.evaluateAssignment();
if(!token.equals(EOE))
{
Parser.handleError(SYNTAX);
}
return result;
}
}