-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPratt.java
758 lines (652 loc) · 16.5 KB
/
Pratt.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
public class Pratt {
public static enum TokenKind implements Predicate<TokenKind> {
None, Num,
/**
* 加
*/
Add("+"),
/**
* 减
*/
Sub("-"),
/**
* 乘
*/
Star("*"),
/**
* 除
*/
Slash("/"),
/**
* 求余
*/
Percent("%"), //
/**
* 按位与
*/
Amp("&"),
/**
* 按位或
*/
Bar("|"),
/**
* 按位非
*/
Tilde("~"),
/**
* 按位异或
*/
Caret("^"),
/**
* 左移
*/
LS("<<"),
/**
* 右移
*/
RS(">>"),
/**
* 阶乘
*/
Bang("!"),
/**
* 左小括号
*/
Lpa("("),
/**
* 右小括号
*/
Rpa(")"), //
Eof,
;
public final String operator;
TokenKind() {
this(null);
}
TokenKind(String op) {
this.operator = op;
}
public int getPrecedence() {
switch (this) {
case Bang -> {
return 100;
}
case Star, Slash, Percent -> {
return 90;
}
case Add, Sub -> {
return 80;
}
case LS, RS -> {
return 70;
}
case Amp -> {
return 60;
}
case Bar -> {
return 50;
}
case Tilde -> {
return 40;
}
default -> {
return 0;
}
}
}
public boolean isPrefix() {
return this == Add || this == Sub || this == Tilde;
}
public boolean isInfix() {
return this == Add || this == Sub || this == Star || this == Slash || this == Percent || this == Amp || this == Bar || this == Caret || this == LS || this == RS;
}
public boolean isPostfix() {
return this == Bang;
}
public boolean isValue() {
return this == Num;
}
public boolean isLeftAssociative() {
return this == Add || this == Sub || this == Tilde;
}
public boolean isRightAssociative() {
return this == Bang;
}
@Override
public boolean test(TokenKind that) {
return this == that;
}
}
public static class Token {
public static final Token DUM_TOKEN = new Token(TokenKind.None, "", -1, -1);
public static final Token EOI_TOKEN = new Token(TokenKind.Eof, "", -1, -1);
public static final byte EOF = 0x1A;
private final TokenKind kind;
private final String value;
private final int column;
private final int line;
public Token(TokenKind kind, String value, int column, int line) {
this.kind = Objects.requireNonNull(kind);
this.value = value;
this.column = column;
this.line = line;
}
@Override
public String toString() {
return kind == TokenKind.Eof ? TokenKind.Eof.name() : value;
}
}
public static interface Visitor<R, C> {
R visit(ValueNode n, C ctx);
R visit(PrefixOpNode n, C ctx);
R visit(InfixOpNode n, C ctx);
R visit(PostfixOpNode n, C ctx);
}
public static abstract class ExprNode {
public abstract <R, C> R accept(Visitor<R, C> v, C ctx);
@Override
public abstract String toString();
}
public static class ValueNode extends ExprNode {
private final String value;
public ValueNode(String value) {
this.value = value;
}
@Override
public <R, C> R accept(Visitor<R, C> v, C ctx) {
return v.visit(this, ctx);
}
@Override
public String toString() {
return value;
}
}
public static class PrefixOpNode extends ExprNode {
private final Token token;
private final ExprNode rhs;
public PrefixOpNode(Token token, ExprNode rhs) {
this.token = token;
this.rhs = rhs;
}
@Override
public <R, C> R accept(Visitor<R, C> v, C ctx) {
return v.visit(this, ctx);
}
@Override
public String toString() {
return token.value + rhs.toString();
}
}
public static class InfixOpNode extends ExprNode {
private final ExprNode lhs;
private final Token token;
private final ExprNode rhs;
public InfixOpNode(ExprNode lhs, Token token, ExprNode rhs) {
this.lhs = lhs;
this.token = token;
this.rhs = rhs;
}
@Override
public <R, C> R accept(Visitor<R, C> v, C ctx) {
return v.visit(this, ctx);
}
@Override
public String toString() {
return lhs.toString() + token.value + rhs.toString();
}
}
public static class PostfixOpNode extends ExprNode {
private final ExprNode lhs;
private final Token token;
public PostfixOpNode(ExprNode lhs, Token token) {
this.lhs = lhs;
this.token = token;
}
@Override
public <R, C> R accept(Visitor<R, C> v, C ctx) {
return v.visit(this, ctx);
}
@Override
public String toString() {
return lhs.toString() + token.value;
}
}
public static class Lexer implements Iterable<Token>, Iterator<Token> {
private final Tokenizer tokenizer;
private final List<Token> tokens;
private Token prevToken;
private Token token;
private int index;
public Lexer(String input) {
this.tokenizer = new Tokenizer(input);
this.tokens = new ArrayList<>();
this.prevToken = this.token = Token.DUM_TOKEN;
}
@Override
public Iterator<Token> iterator() {
return this;
}
@Override
public boolean hasNext() {
return this.tokenizer.isAvailable();
}
@Override
public Token next() {
this.prevToken = this.token;
if (!this.tokens.isEmpty()) {
this.token = this.tokens.removeFirst();
} else {
this.token = tokenizer.readToken();
}
this.index++;
return this.token;
}
public int getIndex() {
return this.index;
}
public Token getPrevToken() {
return prevToken;
}
public void setPrevToken(Token prevToken) {
this.prevToken = prevToken;
}
public Token token() {
return token(0);
}
public Token token(int lookahead) {
if (lookahead == 0) {
return this.token;
} else {
ensureLookahead(lookahead);
return tokens.get(lookahead - 1);
}
}
private void ensureLookahead(int lookahead) {
for (int i = tokens.size(); i < lookahead; i++) {
tokens.add(tokenizer.readToken());
}
}
}
public static class Tokenizer {
private final String input;
private int pos;
private int line;
private TokenKind tk;
private char ch;
private int radix;
private StringBuilder buf = new StringBuilder();
private boolean eof = false;
public Tokenizer(String input) {
this.input = input;
this.ch = input.charAt(this.pos);
}
public Token readToken() {
String value;
Token token;
int col;
int line;
loop:
while (true) {
col = this.pos;
line = this.line;
switch (next()) {
case Token.EOF -> {
if (eof) {
this.tk = TokenKind.Eof;
}
break loop;
}
// 空白
case ' ', '\t', '\f' -> {
continue;
}
case '\r', '\n' -> {
this.line++;
continue;
}
// 数字
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' -> {
this.tk = TokenKind.Num;
backup();
scanNum();
break loop;
}
case '+' -> {
this.tk = TokenKind.Add;
break loop;
}
case '-' -> {
this.tk = TokenKind.Sub;
break loop;
}
case '*' -> {
this.tk = TokenKind.Star;
break loop;
}
case '/' -> {
this.tk = TokenKind.Slash;
break loop;
}
// 求余
case '%' -> {
this.tk = TokenKind.Percent;
break loop;
}
// 阶乘
case '!' -> {
this.tk = TokenKind.Bang;
break loop;
}
// 位运算:异或
case '^' -> {
this.tk = TokenKind.Caret;
break loop;
}
case '&' -> {
this.tk = TokenKind.Amp;
break loop;
}
case '|' -> {
this.tk = TokenKind.Bar;
break loop;
}
case '~' -> {
this.tk = TokenKind.Tilde;
break loop;
}
// 左移/右移
case '<' -> {
consume('<');
this.tk = TokenKind.LS;
break loop;
}
case '>' -> {
consume('>');
this.tk = TokenKind.RS;
break loop;
}
// 括号
case '(' -> {
this.tk = TokenKind.Lpa;
break loop;
}
case ')' -> {
this.tk = TokenKind.Rpa;
break loop;
}
default -> throw new IllegalArgumentException("unknown char: " + ch);
}
}
value = eof ? (col == this.pos ? "eof" : this.input.substring(col, this.pos)) : this.input.substring(col, this.pos);
token = new Token(this.tk, value, col, line);
return token;
}
public boolean isAvailable() {
return this.pos <= this.input.length();
}
private char next() {
if (this.pos >= this.input.length()) {
this.eof = true;
return Token.EOF;
} else {
this.ch = this.input.charAt(this.pos);
this.pos++;
if (this.ch == '\r' || this.ch == '\n') {
this.line++;
}
return this.ch;
}
}
private char peek() {
char ch = next();
backup();
return ch;
}
private void consume(char ch) {
if (ch != next()) {
throw new IllegalArgumentException("expect: " + ch + ", actual:" + get());
}
}
private char get() {
return this.ch;
}
private boolean accept(String valid) {
if (valid.indexOf(next()) >= 0) {
return true;
}
backup();
return false;
}
private void backup() {
if (!this.eof && this.pos > 0) {
char ch = this.input.charAt(this.pos - 1);
this.pos--;
if (ch == '\r' || ch == '\n') {
this.line--;
}
}
}
private void acceptRun(String valid) {
for (char ch = next(); valid.indexOf(ch) >= 0; ch = next()) {
buf.append(ch);
}
backup();
}
private void scanNum() {
buf = new StringBuilder();
accept("+-");
// Is it hex?
this.radix = 10;
var digits = "0123456789_";
if (accept("0")) {
// Note: Leading 0 does not mean octal in floats.
if (accept("xX")) {
this.radix = 16;
digits = "0123456789abcdefABCDEF_";
} else if (accept("oO")) {
this.radix = 8;
digits = "01234567_";
} else if (accept("bB")) {
this.radix = 2;
digits = "01_";
}
}
acceptRun(digits);
if (accept(".")) {
acceptRun(digits);
}
}
}
/**
* <pre>
* lbp (left binding power) : 左结合运算符
* rbp (right binding power) : 右结合运算符
* nud: 一元运算符
* led: 二元运算符
* </pre>
*/
public static class Parser {
private final Lexer lexer;
public Parser(Lexer lexer) {
this.lexer = lexer;
}
public ExprNode parse() {
return parse(0);
}
private ExprNode parse(int rbp) {
ExprNode left = this.factor();
ExprNode old;
var curr = this.lexer.token();
var next = this.lexer.next();
left = nud(curr, left);
while (rbp < next.kind.getPrecedence()) {
old = left;
left = this.led(left);
if (left == null) {
return old;
}
next = this.lexer.token();
}
return left;
}
/**
* <pre>
* expr -> term {(+|-) term}
* term -> factor {(*|/) factor}
* factor -> number | ( expr )
* number -> digit {digit}
* digit -> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
* </pre>
*/
private ExprNode factor() {
var token = this.lexer.next();
switch (token.kind) {
case Eof -> {
return null;
}
case Lpa -> {
ExprNode left = parse(0);
consume(TokenKind.Rpa);
return left;
}
case Num -> {
return new ValueNode(token.value);
}
default -> throw new IllegalArgumentException("Unknown token: " + token);
}
}
private PrefixOpNode parsePrefixExpr() {
return new PrefixOpNode(null, null);
}
private InfixOpNode parseInfixExpr() {
return new InfixOpNode(null, null, null);
}
private PostfixOpNode parsePostfixExpr() {
return new PostfixOpNode(null, null);
}
private void consume(TokenKind tk) {
var curr = this.lexer.next();
if (curr.kind != tk) {
throw new IllegalArgumentException();
}
}
/**
* 一元运算符
*
* @return 表达式
*/
private ExprNode nud(Token prev, ExprNode left) {
// -1,3!
var op = lexer.token();
if (op.kind == TokenKind.Eof) {
return left;
}
ExprNode node;
if (prev.kind.isPrefix() && !op.kind.isPrefix()) {
// 前缀: -1
node = new PrefixOpNode(op, this.parse(op.kind.getPrecedence() - 1));
} else if (op.kind.isPostfix()) {
// 后缀: 4!
node = new PostfixOpNode(left, op);
this.lexer.next();
} else if (op.kind.isInfix()) {
// 中缀: 1 + 2
node = new InfixOpNode(left, op, this.parse(op.kind.getPrecedence() - 1));
} else {
node = left;
}
return node;
}
/**
* 二元运算符
*
* @return 表达式
*/
private ExprNode led(ExprNode left) {
var op = lexer.token();
if (op.kind == TokenKind.Eof) {
return left;
}
ExprNode node;
if (op.kind.isRightAssociative()) {
node = new InfixOpNode(left, op, this.parse(op.kind.getPrecedence() - 1));
} else {
node = new InfixOpNode(left, op, this.parse(op.kind.getPrecedence()));
}
return node;
}
}
public static class Calculator {
public double calculate(String input) {
var lexer = new Lexer(input);
var parser = new Parser(lexer);
var expr = parser.parse();
return expr.accept(new CalculatorVisitor(), null);
}
}
public static class CalculatorVisitor implements Visitor<Double, Void> {
@Override
public Double visit(ValueNode n, Void ctx) {
return Double.parseDouble(n.value);
}
@Override
public Double visit(PrefixOpNode n, Void ctx) {
var rhs = n.rhs.accept(this, ctx);
double v;
switch (n.token.kind) {
case Sub -> v = -rhs;
case Add -> v = rhs;
case Tilde -> v = ~rhs.intValue();
default -> throw new IllegalArgumentException("unknown token:" + n.token);
}
return v;
}
@Override
public Double visit(InfixOpNode n, Void ctx) {
var lhs = n.lhs.accept(this, ctx);
var rhs = n.rhs.accept(this, ctx);
double v;
switch (n.token.kind) {
case Add -> v = lhs + rhs;
case Sub -> v = lhs - rhs;
case Star -> v = lhs * rhs;
case Slash -> v = lhs / rhs;
case Percent -> v = lhs % rhs;
case Amp -> v = lhs.intValue() & rhs.intValue();
case Bar -> v = lhs.intValue() | rhs.intValue();
case Caret -> v = lhs.intValue() ^ rhs.intValue();
case LS -> v = lhs.intValue() << rhs.intValue();
case RS -> v = lhs.intValue() >> rhs.intValue();
default -> throw new IllegalArgumentException("unknown token:" + n.token);
}
return v;
}
@Override
public Double visit(PostfixOpNode n, Void ctx) {
var lhs = n.lhs.accept(this, ctx);
switch (n.token.kind) {
case Bang -> {
int x = lhs.intValue();
double num = 1;
for (int i = 1; i <= x; i++) {
num *= i;
}
return num;
}
default -> throw new IllegalArgumentException("unknown token:" + n.token);
}
}
}
public static void main(String[] args) {
// 正则: Thompson算法 -> NFA
// NFA:子集构造算法 -> DFA
// DFA: Hopcroft最小化算法 -> 词法分析器
var calculator = new Calculator();
System.out.println(calculator.calculate("305 << 2 - 212 + 4 * 5!"));
}
}