-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simple_RD_parser.java
207 lines (187 loc) · 5.12 KB
/
Simple_RD_parser.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
import java.util.Scanner;
public class Simple_RD_parser{
static int judge_bexpr = 0;
static char token;
static boolean has_error = false;
static String line;
static int index = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print(">> ");
if (!scanner.hasNextLine()) break;
line = scanner.nextLine();
index = 0;
if (line.isEmpty()) break;
getChar();
print();
while (index < line.length()) {
getChar();
}
}
scanner.close();
}
static void getChar() {
if (index < line.length()) {
token = line.charAt(index++);
if (token == ' ' || token == '\t')
getChar();
} else {
token = '\n';
}
}
static void error() {
System.out.println(">> syntax error!!");
has_error = true;
}
static void lex(char c) {
if (token == c) {
getChar();
} else {
error();
}
}
static int expr() {
return bexpr();
}
static int bexpr() {
int left = aexpr();
if (token == '=' || token == '!' || token == '<' || token == '>') {
int op = relop();
int right = aexpr();
judge_bexpr = 1;
switch (op) {
case 0:
return left == right ? 1 : 0;
case 1:
return left != right ? 1 : 0;
case 2:
return left < right ? 1 : 0;
case 3:
return left > right ? 1 : 0;
case 4:
return left <= right ? 1 : 0;
case 5:
return left >= right ? 1 : 0;
default:
error();
return 0;
}
} else {
judge_bexpr = -1;
return left;
}
}
static int relop() {
switch (token) {
case '=':
lex('=');
if (token == '=') {
lex('=');
return 0;
} else {
error();
return -1;
}
case '!':
lex('!');
if (token == '=') {
lex('=');
return 1;
}
return -1;
case '<':
lex('<');
if (token == '=') {
lex('=');
return 4;
}
return 2;
case '>':
lex('>');
if (token == '=') {
lex('=');
return 5;
}
return 3;
default:
error();
return -1;
}
}
static int aexpr() {
int result = term();
while (!has_error && (token == '*' || token == '/')) {
if (token == '*') {
lex('*');
result *= term();
} else if (token == '/') {
lex('/');
result /= term();
}
}
return result;
}
static int term() {
int result = factor();
while (!has_error && (token == '+' || token == '-')) {
if (token == '+') {
lex('+');
result += factor();
} else if (token == '-') {
lex('-');
result -= factor();
}
}
return result;
}
static int factor() {
int result;
if (token == '(') {
lex('(');
result = aexpr();
if (token == ')') {
lex(')');
} else {
error();
}
} else {
result = number();
}
return result;
}
static int number() {
int result = dec();
while (!has_error && Character.isDigit(token)) {
result = 10 * result + dec();
}
return result;
}
static int dec() {
if (!Character.isDigit(token)) {
error();
return 0;
}
int result = Character.getNumericValue(token);
lex(token);
return result;
}
static void print() {
has_error = false;
int result = expr();
if (has_error) {
return;
}
if (token == '\n') {
if (judge_bexpr == 1 && result == 1) {
System.out.println(">> true");
} else if (judge_bexpr == 1 && result == 0) {
System.out.println(">> false");
} else {
System.out.println(">> " + result);
}
} else {
error();
}
}
}