forked from paceuniversity/cs361-paceuniversity-classroom-21e866-kay-parser-2024-Kay-Parser-Starter-Code-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.java
326 lines (256 loc) · 7.29 KB
/
Program.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
// Program.java
// NOT TO BE MODIFIED (except if you are sure there is a bug!)
// Abstract syntax of JAY
// Display methods are added to facilitate debugging and experimentation
import java.util.Vector;
public class Program {
// Program = Declarations decpart ; Block body
public Declarations decpart;
public Block body;
public String display() {
int level = 0;
Indenter indent = new Indenter(level);
String s = indent.display("Abstract syntax of the KAY Program: ");
String dec = decpart.display(level + 1);
String bod = body.display(level + 1);
String nl = "\n";
return s + dec + bod + nl;
}
}
class Indenter {
// To help displaying the abstract tree using levels
public int level;
public Indenter(int nextlevel) {
level = nextlevel;
}
public String display(String message) {
// Displays a message on the next line at the current level
String tab = "";
String nl = "\n";
for (int i = 0; i < level; i++)
tab = tab + " ";
return nl + tab + message;
}
}
class Declarations extends Vector {
// Declarations = Declaration *
// (a Vector of Declarations d1, d2, ..., dn)
public String display(int level) {
Indenter indent = new Indenter(level);
String s = indent.display(getClass().toString().substring(6) + ": ");
String s1 = indent.display(" Declarations = {");
String s2 = "";
for (int i = 0; i < size(); i++) {
s2 = s2 + ((Declaration) elementAt(i)).display();
if (i < size() - 1)
s2 = s2 + ", ";
}
return s + s1 + s2 + "}";
}
}
class Declaration {
// Declaration = Variable v; Type t
public Variable v;
public Type t;
public String display() {
return "<" + v.id + ", " + t.id + ">";
}
}
class Type {
// Type = int | bool | undef
public String id;
final static String INTEGER = "int";
final static String BOOLEAN = "bool";
final static String UNDEFINED = "undef";
public Type(String t) {
id = t;
}
public boolean isBoolean() {
return id.equals(BOOLEAN);
}
public boolean isInteger() {
return id.equals(INTEGER);
}
public boolean isUndefined() {
return id.equals(UNDEFINED);
}
}
class Statement {
// Statement = Skip | Block | Assignment | Conditional | Loop
public String display(int level) {
Indenter indent = new Indenter(level);
return indent.display(getClass().toString().substring(6) + ": ");
}
}
class Skip extends Statement {
public String display(int level) {
return super.display(level);
}
}
class Block extends Statement {
// Block = Statement*
// (a Vector of members)
public Vector blockmembers = new Vector();
public String display(int level) {
String s = super.display(level);
for (int i = 0; i < blockmembers.size(); i++)
s = s + ((Statement) blockmembers.elementAt(i)).display(level + 1);
return s;
}
}
class Assignment extends Statement {
// Assignment = Variable target; Expression source
public Variable target;
public Expression source;
public String display(int level) {
String s = super.display(level);
String tar = target.display(level + 1);
String sour = source.display(level + 1);
return s + tar + sour;
}
}
class Conditional extends Statement {
// Conditional = Expression test; Statement thenbranch, elsebranch
public Expression test;
public Statement thenbranch, elsebranch;
// elsebranch == null means "if... then" Statement
public String display(int level) {
String s = super.display(level);
String cond = test.display(level + 1);
String then = thenbranch.display(level + 1);
String els = "";
if (elsebranch != null)
els = els + elsebranch.display(level + 1);
return s + cond + then + els;
}
}
class Loop extends Statement {
// Loop = Expression test; Statement body
public Expression test;
public Statement body;
public String display(int level) {
String s = super.display(level);
String t = test.display(level + 1);
String b = body.display(level + 1);
return s + t + b;
}
}
class Expression {
// Expression = Variable | Value | Binary | Unary
public String display(int level) {
Indenter indent = new Indenter(level);
return indent.display(getClass().toString().substring(6) + ": ");
}
}
class Variable extends Expression {
// Variable = String id
public String id;
public boolean equals(Object obj) {
String s = ((Variable) obj).id;
return id.equalsIgnoreCase(s); // case-insensitive identifiers
}
public int hashCode() {
return id.hashCode();
}
public String display(int level) {
String s = super.display(level);
return s + id;
}
}
class Value extends Expression {
// Value = int intValue | bool boolValue
public Type type;
public int intValue; // if the type is an int intValue is used otherwise
// boolValue is used
public boolean boolValue;
public Value(int i) {
type = new Type(Type.INTEGER);
intValue = i;
}
public Value(boolean b) {
type = new Type(Type.BOOLEAN);
boolValue = b;
}
public Value() {
type = new Type(Type.UNDEFINED);
}
public String display(int level) {
String s = super.display(level);
if (type.isInteger())
s = s + intValue;
else if (type.isBoolean())
s = s + boolValue;
return s;
}
}
class Binary extends Expression {
// Binary = Operator op; Expression term1, term2
public Operator op;
public Expression term1, term2;
public String display(int level) {
String s = super.display(level);
String sop = op.display(level + 1);
String t1 = term1.display(level + 1);
String t2 = term2.display(level + 1);
return s + sop + t1 + t2;
}
}
class Unary extends Expression {
// Unary = Operator op; Expression term
public Operator op;
public Expression term;
public String display(int level) {
String s = super.display(level);
String sop = op.display(level + 1);
String t = term.display(level + 1);
return s + sop + t;
}
}
class Operator {
public String val; // value of the operator - one of the constant above
// Operator = BooleanOp | RelationalOp | ArithmeticOp | Unary1Op
// BooleanOp = && | ||
final static String AND = "&&";
final static String OR = "||";
// RelationalOp = < | <= | == | <> | >= | >
final static String LT = "<";
final static String LE = "<=";
final static String EQ = "==";
final static String NE = "<>";
final static String GT = ">";
final static String GE = ">=";
// ArithmeticOp = + | - | * | /
final static String PLUS = "+";
final static String MINUS = "-";
final static String TIMES = "*";
final static String DIV = "/";
// UnaryOp = !
final static String NOT = "!";
public Operator(String s) {
val = s;
}
public boolean BooleanOp() {
return val.equals(AND) || val.equals(OR);
}
public boolean RelationalOp() {
return val.equals(LT)
|| val.equals(LE)
|| val.equals(EQ)
|| val.equals(NE)
|| val.equals(GT)
|| val.equals(GE);
}
public boolean ArithmeticOp() {
return val.equals(PLUS)
|| val.equals(MINUS)
|| val.equals(TIMES)
|| val.equals(DIV);
}
public boolean UnaryOp() {
return val.equals(NOT);
}
public String display(int level) {
Indenter indent = new Indenter(level);
return indent.display(getClass().toString().substring(6) + ": " + val);
}
}