public class ParsingDirector private enum Lexem NUM VAR MUL DIV LPARCE

  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
public class ParsingDirector {
private enum Lexem { NUM, VAR, MUL, DIV, LPARCE, RPARCE, ADD, SUB }
private class LexemStack {
private Lexem ident;
private int value;
private LexemStack next;
LexemStack(Lexem ident) {
this.ident = ident;
value = 0;
}
LexemStack(Lexem ident, int value) {
this.ident = ident;
this.value = value;
}
public Lexem getIdent() {
return ident;
}
public int getValue() {
return value;
}
public LexemStack getNext() {
return next;
}
public void setNext(LexemStack next) {
this.next = next;
}
}
private StackMachine machine;
private LexemStack top, tail;
public ParsingDirector(StackMachine evaluator) {
machine = evaluator;
top = null;
tail = null;
}
private void addLexem(LexemStack elem) {
if (top == null) {
top = tail = elem;
} else {
tail.setNext(elem);
tail = elem;
}
}
private void nextLexem() {
top = top.getNext();
}
private void F() {
switch (top.getIdent()) {
case NUM:
machine.pushConst(top.getValue());
nextLexem();
break;
case VAR:
machine.pushVar();
nextLexem();
break;
case SUB:
nextLexem();
F();
machine.neg();
break;
case LPARCE:
nextLexem();
analysis();
nextLexem();
break;
default:
break;
}
}
private void Ttilt() {
if (top != null) {
switch (top.getIdent()) {
case MUL:
nextLexem();
F();
machine.mul();
Ttilt();
break;
case DIV:
nextLexem();
F();
machine.div();
Ttilt();
break;
default:
break;
}
}
}
private void T() {
F();
Ttilt();
}
private void Etilt() {
if (top != null) {
switch (top.getIdent()) {
case ADD:
nextLexem();
T();
machine.add();
Etilt();
break;
case SUB:
nextLexem();
T();
machine.sub();
Etilt();
break;
default:
break;
}
}
}
private void analysis() {
T();
Etilt();
}
public void parse(String s) {
int temp = 0, cur;
char symbol;
LexemStack curLexem;
boolean change = false;
for (cur = 0; cur < s.length(); cur++) {
symbol = s.charAt(cur);
if ((symbol <= '9') && (symbol >= '0')) {
temp = temp*10+symbol-'0';
change = true;
continue;
}
if (change) {
curLexem = new LexemStack(Lexem.NUM, temp);
addLexem(curLexem);
temp = 0;
change = false;
}
switch (symbol) {
case 'x':
curLexem = new LexemStack(Lexem.VAR);
addLexem(curLexem);
break;
case '(':
curLexem = new LexemStack(Lexem.LPARCE);
addLexem(curLexem);
break;
case ')':
curLexem = new LexemStack(Lexem.RPARCE);
addLexem(curLexem);
break;
case '+':
curLexem = new LexemStack(Lexem.ADD);
addLexem(curLexem);
break;
case '-':
curLexem = new LexemStack(Lexem.SUB);
addLexem(curLexem);
break;
case '*':
curLexem = new LexemStack(Lexem.MUL);
addLexem(curLexem);
break;
case '/':
curLexem = new LexemStack(Lexem.DIV);
addLexem(curLexem);
break;
}
}
if (change) {
curLexem = new LexemStack(Lexem.NUM, temp);
addLexem(curLexem);
}
analysis();
}
}