-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostFix.java
88 lines (84 loc) · 2.51 KB
/
PostFix.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
/**
* Postfix table based on code provided by Prof. Allen's class notes.
* This class takes in the top of the stack and the next operator in the infix string.
* It compares the two operators and returns an int that determines wheter to pop or push the operator.
*/
public class PostFix
{
public int compareOperands(int a, int b)
{
final int POP=0;
final int PUSH=1;
final int MATCH=2;
final int ERROR=3;
final int EOL=012;
final int DONE=4;
/* wastes space but makes the table easy to generate */
int[][] table = new int[256][256];
table['(']['*']=PUSH;
table['(']['/']=PUSH;
table['(']['-']=PUSH;
table['(']['+']=PUSH;
table['(']['^']=PUSH;
table['(']['(']=PUSH;
table['('][EOL]=ERROR;
table['('][')']=MATCH;
table['+']['*']=PUSH;
table['+']['/']=PUSH;
table['+']['-']=POP;
table['+']['+']=POP;
table['+']['^']=PUSH;
table['+']['(']=PUSH;
table['+'][EOL]=POP;
table['+'][')']=PUSH;
table['-']['*']=PUSH;
table['-']['/']=POP;
table['-']['-']=PUSH;
table['-']['+']=PUSH;
table['-']['^']=PUSH;
table['-']['(']=POP;
table['-'][EOL]=POP;
table['-'][')']=POP;
table['*']['*']=POP;
table['*']['/']=POP;
table['*']['-']=POP;
table['*']['-']=POP;
table['*']['^']=PUSH;
table['*']['(']=PUSH;
table['*'][EOL]=POP;
table['*'][')']=POP;
table['^']['*']=POP;
table['^']['/']=POP;
table['^']['-']=POP;
table['^']['+']=POP;
table['^']['^']=PUSH;
table['^']['(']=PUSH;
table['^'][EOL]=POP;
table['^'][')']=POP;
table['/']['*']=PUSH;
table['/']['/']=POP;
table['/']['-']=POP;
table['/']['-']=POP;
table['/']['^']=POP;
table['/']['(']=PUSH;
table['/'][EOL]=POP;
table['/'][')']=POP;
table[')']['*']=ERROR;
table[')']['/']=ERROR;
table[')']['-']=ERROR;
table[')']['-']=ERROR;
table[')']['^']=ERROR;
table[')']['(']=ERROR;
table[')'][EOL]=ERROR;
table[')'][')']=ERROR;
table['#']['*']=PUSH;
table['#']['/']=PUSH;
table['#']['-']=PUSH;
table['#']['+']=PUSH;
table['#']['^']=PUSH;
table['#']['(']=PUSH;
table['#'][EOL]=DONE;
table['#'][')']=ERROR;
return table[a][b];
}
}