-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpEvaluator.java
More file actions
185 lines (159 loc) · 5.88 KB
/
Copy pathExpEvaluator.java
File metadata and controls
185 lines (159 loc) · 5.88 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
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Scanner;
public class ExpEvaluator {
private String s;
private int currIndex;
private char inputToken;
private HashMap<String, Integer> hmap = new HashMap<String, Integer>();
// HashMap <key,value>
void expEvaluator(String s) {
this.s = s.replaceAll("\\s", ""); // removing whitespace from s String
currIndex = 0;
nextToken();
}
void nextToken() { // get the next element of the String
char c;
if (!s.endsWith(";")) // check the string if it ends with ;
throw new RuntimeException("Missing ';' token exptected"); // and throw RuntimeException if ; is missing
c = s.charAt(currIndex++); // increment the index for next Character
inputToken = c;
}
void match(char token) { // checking for Parenthesis
if (inputToken == token) {
nextToken();
} else {
throw new RuntimeException("Missing Parenthesis"); // throw a RuntimeException if the closing Parenthesis is missing
}
}
int eval() { // evaluating the expression
int x = exp();
if (inputToken == ';') { // determines the assignment if it ends with ;
return x;
} else {
throw new RuntimeException("Missing ';' token expected ");
}
}
// funtion call from main whcih read the file line by line using the loop
public void run(Scanner fs) {
while (fs.hasNextLine()) {
expEvaluator(fs.nextLine());
assignment();
}
}
// this function will do the assignment for the variable initialization
// and the value will be assigned to it
void assignment() {
String var = identifier();
int operand = eval();
hmap.put(var, operand);
System.out.println(var + " = " + operand);
}
// this method is applied if the operator is + or -
int exp() {
int x = term();
while (inputToken == '+' || inputToken == '-') {
char op = inputToken;
nextToken();
int y = term();
x = apply(op, x, y);
}
return x;
}
// this method is applied if the operator is * or /
int term() {
int x = factor();
while (inputToken == '*' || inputToken == '/') {
char op = inputToken;
nextToken();
int y = factor();
x = apply(op, x, y);
}
return x;
}
// thsi method will take care of more than one assignment declaration
// and store the operaters to get executed in orderly manner
// also store duplicate operators as token and checking Parenthesis
int factor() {
int x = 0;
String temp = String.valueOf(inputToken); // get the value of input using temporary string
if (hmap.containsKey(temp)) {
x = hmap.get(temp).intValue();
nextToken();
return x;
} else if (inputToken == '(') {
nextToken();
x = exp();
match(')');
return x;
} else if (inputToken == '-') {
nextToken();
x = factor();
return -x;
} else if (inputToken == '+') {
nextToken();
x = factor();
return x;
} else if (inputToken == '0') {
nextToken();
if (Character.isDigit(inputToken))
throw new RuntimeException("ERROR !! Invalid value");
return 0;
}
temp = "";
while (Character.isDigit(inputToken)) {
temp += inputToken;
nextToken();
}
return Integer.parseInt(temp);
}
// this function is construction of string that identify the creation of variable and the assignment
// to distinct between the variable name and the value assigning to it
String identifier() {
StringBuilder sb = new StringBuilder();
if (Character.isLetter(inputToken))
sb.append(inputToken);
else
throw new RuntimeException("Invalid variable name");
nextToken();
// make sure string append is a character
// also allowing the underscore and digit
while (Character.isLetter(inputToken) || inputToken == '_' || Character.isDigit(inputToken)) {
sb.append(inputToken);
nextToken();
}
if (inputToken != '=') // and make sure it is follow by the = operator to assign
throw new RuntimeException("Not an assignment statement");
nextToken();
return sb.toString();
}
// Operation to compute depending on the signs of the operators
static int apply(char op, int x, int y) {
int z = 0;
switch (op) {
case '+':
z = x + y;
break;
case '-':
z = x - y;
break;
case '*':
z = x * y;
break;
case '/':
z = x / y;
break;
}
return z;
}
public static void main(String[] args) {
try {
Scanner ObjFile = new Scanner(new FileInputStream(args[0]));
ExpEvaluator expEval = new ExpEvaluator();
expEval.run(ObjFile);
// if the file isn't passed as parameter by the user, or file doesn't exist
} catch (Exception ecpt) {
System.out.println("ERROR: " + ecpt);
}
}
}