-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.java
More file actions
269 lines (227 loc) · 5.99 KB
/
Parser.java
File metadata and controls
269 lines (227 loc) · 5.99 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
package com.example;
import java.util.ArrayList;
import java.util.Stack;
/**
* Parser
*/
class Parser {
private final ArrayList<Token> tokens;
private int p = 0;
Parser(ArrayList<Token> tokens) {
this.tokens = tokens;
}
/**
* Return the operator of literal token
*/
Op from(String l) {
return switch (l) {
case "+" -> Op.ADD;
case "-" -> Op.SUB;
case "*" -> Op.MUL;
case "/" -> Op.DIV;
default -> {
throw new IllegalStateException("Unexpected value: " + l);
}
};
}
/**
* Return the precedence of operator
*/
int getPrecedence(Op op) {
return switch (op) {
case ADD, SUB -> 1;
case MUL, DIV -> 2;
};
}
/**
* Set precedence and the difference is who calculates L or r first
*/
Expr setPrecedence(Op op, BE b, Expr expr) {
if (getPrecedence(op) > getPrecedence(b.P)) {
// If the priority of OP is higher than that of BE append to right
b.R = new BE(b.R, op, expr);
} else {
// Expression of the same level or priority less than, generate new
return new BE(b, op, expr);
}
return b;
}
/**
* Append expression
*/
void append(Op op, Stack<Expr> l, Stack<Expr> s) {
if (op == null)
return; // Must have an operator to append
System.out.printf("-> OP: %s L: %s S: %s\n", op, l, s);
// Append expression
if (l.empty()) {
if (s.size() > 1) {
Expr y = s.pop();
Expr x = s.pop();
if (x instanceof BE) {
// Precedence
s.push(this.setPrecedence(op, (BE) x, y));
return;
}
// BE Expr
s.push(new BE(x, op, y));
return;
}
// Unary Expr
if (s.size() == 1) {
s.push(new UE(op, s.pop()));
return;
}
}
// Operand
Expr er = l.pop();
if (l.empty()) {
// Unary Expr
if (s.empty()) {
s.push(new UE(op, er));
return;
}
Expr e = s.pop(); // To stack of expression
// Binary expression
if (e instanceof BE) {
// Set precedence
s.push(this.setPrecedence(op, (BE) e, er));
}
// Group expr
// Unary
// Call
// Get
else if (
e instanceof GRE
|| e instanceof UE
|| e instanceof CE
|| e instanceof GE
) {
s.push(new BE(e, op, er)); // To BE Expr
}
} else {
// Two operands of pop generate BE
s.push(new BE(l.pop(), op, er));
}
}
// Return the previous of token literal is equal?
boolean previous(String k) {
if (
p - 1 == -1
) {
return false;
} else {
return this.tokens.
get(p - 1).k.equals(k); // Previous
}
}
// Return expression of L or S stack
Expr getExpr(Stack<Expr> l, Stack<Expr> s) {
if (l.empty() &&
s.empty()
) {
return null;
} else {
return l.empty() ?
s.pop() : l.pop(); // POP it
}
}
/**
* Parse expression and return
*/
Expr parse() {
Op p = null; // TOP operator
Stack<Expr> l = new Stack<>(); // Stack of operand
Stack<Expr> s = new Stack<>(); // Stack of expression
ArrayList<Expr> args = new ArrayList<>(); // Arguments for CE Expr
while (this.p < tokens.size()) {
Token token = tokens.get(this.p); // Current token
switch (token.k) {
// Literal Expr
case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" -> {
// Push a new literal expression
l.push(new I(Integer.parseInt(token.k)));
// Append expression
append(p, l, s);
// After appending set the top operator to null
p = null;
}
// Operators
case "+", "-", "*", "/" -> p = from(token.k);
// Group expression
case "(" -> {
// CallExpr
boolean callE =
previous("a")
|| previous("b")
|| previous("c")
|| previous("x")
|| previous("y")
|| previous("z")
|| previous(")");
this.p++; // Skip left paren symbol
if (callE) {
CE ce = new CE(); // Call Expression
ce.callee = getExpr(l, s);
ce.arguments = this.parse();
s.push(ce);
} else {
// Generate GRE expression after recursive parsing
s.push(new GRE(this.parse()));
append(p, l, s);
p = null;
}
}
// Exit the current parsing when look right paren symbol
case ")" -> {
// Current parsing CE expression.
if (!args.isEmpty()) {
ArrayList<Expr> list = new ArrayList<>(args); // Retold
list.add(getExpr(l, s)); // back argument
return new ARG(list);
} else {
return getExpr(l, s);
}
}
// Arguments split
case "," -> args.add(getExpr(l, s));
// Name expression
case "a", "b", "c", "x", "y", "z" -> {
l.push(new NE(token));
// Append expression
append(p, l, s);
p = null;
}
// Assign expression
case "=" -> {
Expr expr = getExpr(l, s);
this.p++; // Skip assignment symbol
s.push(new AE(expr, this.parse()));
}
// Get expression
case "." -> {
Expr expr = getExpr(l, s);
this.p++; // Skip dot symbol
s.push(new GE(expr, tokens.get(this.p)));
}
// Index expression
case "[" -> {
Expr expr = getExpr(l, s);
this.p++; // Skip left bracket symbol
s.push(new IE(expr, this.parse()));
}
// Exit the current parsing when look right bracket symbol
case "]" -> {
return getExpr(l, s);
}
}
this.p++; // For loop
// Dissemble message
if (!s.empty() && p == null) {
System.out.println(s.peek());
}
}
// Return the final expression at the top of the stack
return getExpr(l, s);
}
}