-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion7.java
179 lines (159 loc) · 4.29 KB
/
Question7.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
public class Question7{
// Symbols:
Object nextToken = "";
String inc_op = "++";
String dec_op = "--";
String mult_op = "*";
String greaterEq_op = ">=";
String bitand_op = "&";
String plus_op = "+";
String minus_op = "-";
String lessEq_op = "<=";
String mod_op = "%";
String greater_op = ">";
String less_op = "<";
String and_op = "&&";
String div_op = "/";
String not_op = "!";
String or_op = "||";
String xor_op = "~|";
String assign_op = "=";
String divEq_op = "/=";
String id = "id";
NumLit num_lit = new NumLit();
// Other Methods:
// assign()
// expr()
// term1()
// term2()
// term3()
// term4()
// factor1()
// factor2()
// pre()
// post()
// error()
// lex()
void assign() {
if (nextToken != id){
/* Get the next token */
error();
}else{
lex();
if (nextToken != assign_op || nextToken != divEq_op){
error();
}else{
lex();
expr();
}
}
}
void expr() {
/* Parse the first term */
term1();
/* As long as the next token is + or -, get
the next token and parse the next term */
while (nextToken == or_op || nextToken == xor_op) {
lex();
term1();
}
}
void term1(){
/* Parse the first factor */
term2();
/* As long as the next token is * or /, get the
next token and parse the next factor */
while (nextToken == and_op || nextToken == div_op || nextToken == not_op) {
lex();
term2();
}
}
void term2(){
/* Parse the first factor */
term3();
/* As long as the next token is * or /, get the
next token and parse the next factor */
while (nextToken == greater_op || nextToken == less_op) {
lex();
term3();
}
}
void term3(){
/* Parse the first factor */
term4();
/* As long as the next token is * or /, get the
next token and parse the next factor */
while (nextToken == plus_op || nextToken == minus_op || nextToken == mod_op) {
lex();
term4();
}
}
void term4(){
/* Parse the first factor */
factor1();
/* As long as the next token is * or /, get the
next token and parse the next factor */
while (nextToken == plus_op || nextToken == minus_op || nextToken == lessEq_op) {
lex();
factor1();
}
}
void factor1(){
/* Parse the first factor */
factor2();
/* As long as the next token is * or /, get the
next token and parse the next factor */
while (nextToken == mult_op || nextToken == greaterEq_op || nextToken == bitand_op) {
lex();
factor2();
}
}
void factor2(){
/* Parse the first factor */
pre();
/* As long as the next token is * or /, get the
next token and parse the next factor */
while (nextToken == inc_op || nextToken == dec_op) {
lex();
pre();
}
}
void pre(){
/* Parse the first factor */
post();
/* As long as the next token is * or /, get the
next token and parse the next factor */
while (nextToken == inc_op || nextToken == dec_op) {
lex();
post();
}
}
void post(){
/* Determine which RHS */
if (nextToken == id || nextToken == num_lit)
/* Get the next token */
lex();
/* If the RHS is ( <expr> ), call lex to pass over the
left parenthesis, call expr, and check for the right
parenthesis */
else { if (nextToken == "(") {
lex();
expr();
if (nextToken == ")")
lex();
else
error();
}
/* It was not an id, an integer literal, or a left
parenthesis */
else
error();
}
}
void error(){
}
void lex(){
}
public static void main(String[] args){
}
}