-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractSyntax.java
662 lines (530 loc) · 16.3 KB
/
AbstractSyntax.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
// Abstract syntax for the language C++Lite,
// exactly as it appears in Appendix B.
import java.util.*;
class Indent {
final static int INDENT = 4;
}
class Program {
// Program = Declarations decpart ; Block body
Declarations decpart;
Block body;
Program (Declarations d, Block b) {
decpart = d;
body = b;
}
void display() {
System.out.println("Program (abstract syntax):");
decpart.display(Indent.INDENT);
body.display(Indent.INDENT);
}
}
class Declarations extends ArrayList<Declaration> {
// Declarations = Declaration*
// (a list of declarations d1, d2, ..., dn)
public ArrayList<Declaration> members = new ArrayList<Declaration>();
void display(int numOfIndent) {
for(int i = 0; i < numOfIndent; ++i ) {
System.out.print(" ");
}
System.out.println("Declarations: ");
for(int i = 0; i < numOfIndent + Indent.INDENT; ++i ) {
System.out.print(" ");
}
System.out.print("Declarations ={ ");
for( Declaration temp : members ) {
temp.display();
System.out.print(" ");
}
System.out.println("}");
}
}
class Declaration {
// Declaration = Variable v; Type t
Variable v;
Type t;
Declaration (Variable var, Type type) {
v = var; t = type;
} // declaration */
void display() {
System.out.print("<");
v.display();
System.out.print(", ");
t.display();
System.out.print(">");
}
}
class Type {
// Type = int | bool | char | float
final static Type INT = new Type("int");
final static Type BOOL = new Type("bool");
final static Type CHAR = new Type("char");
final static Type FLOAT = new Type("float");
// final static Type UNDEFINED = new Type("undef");
private String id;
private Type (String t) { id = t; }
public String toString ( ) { return id; }
void display() {
System.out.print(id);
}
}
abstract class Statement {
// Statement = Skip | Block | Assignment | Conditional | Loop
abstract void display(int numOfIndent);
abstract String strReturn();
}
class Skip extends Statement {
void display(int numOfIndent) {
for(int i = 0; i < numOfIndent; ++i ) {
System.out.print(" ");
}
System.out.println("Skip:");
}
String strReturn() { return " "; }
}
class Block extends Statement {
// Block = Statement*
// (a Vector of members)
public ArrayList<Statement> members = new ArrayList<Statement>();
void display(int numOfIndent) {
for(int i = 0; i < numOfIndent; ++i ) {
System.out.print(" ");
}
System.out.println("Block:");
for( Statement temp : members ) {
temp.display(numOfIndent + Indent.INDENT);
}
}
String strReturn() { return " "; }
}
class Assignment extends Statement {
// Assignment = Variable target; Expression source
Variable target;
Expression source;
Assignment (Variable t, Expression e) {
target = t;
source = e;
}
void display(int numOfIndent) {
for(int i = 0; i < numOfIndent; ++i ) {
System.out.print(" ");
}
System.out.println("Assignment:");
target.display(numOfIndent + Indent.INDENT);
source.display(numOfIndent + Indent.INDENT);
}
String strReturn() { return " "; }
}
class Conditional extends Statement {
// Conditional = Expression test; Statement thenbranch, elsebranch
Expression test;
Statement thenbranch, elsebranch;
// elsebranch == null means "if... then"
Conditional (Expression t, Statement tp) {
test = t; thenbranch = tp; elsebranch = new Skip( );
}
Conditional (Expression t, Statement tp, Statement ep) {
test = t; thenbranch = tp; elsebranch = ep;
}
void display(int numOfIndent) {
for(int i = 0; i < numOfIndent; ++i ) {
System.out.print(" ");
}
System.out.println("Conditional:");
test.display(numOfIndent + Indent.INDENT);
thenbranch.display(numOfIndent + Indent.INDENT);
elsebranch.display(numOfIndent + Indent.INDENT);
}
String strReturn() {
String testDisp = test.strReturn();
return testDisp;
}
}
class Loop extends Statement {
// Loop = Expression test; Statement body
Expression test;
Statement body;
Loop (Expression t, Statement b) {
test = t; body = b;
}
void display(int numOfIndent) {
for(int i = 0; i < numOfIndent; ++i ) {
System.out.print(" ");
}
System.out.println("Loop:");
test.display(numOfIndent + Indent.INDENT);
body.display(numOfIndent + Indent.INDENT);
}
String strReturn() {
String testDisp = test.strReturn();
return testDisp;
}
}
abstract class Expression {
// Expression = Variable | Value | Binary | Unary
abstract void display(int numOfIndent);
abstract void display();
abstract String strReturn();
}
class Variable extends Expression {
// Variable = String id
private String id;
Variable (String s) { id = s; }
public String toString( ) { return id; }
public boolean equals (Object obj) {
String s = ((Variable) obj).id;
return id.equals(s); // case-sensitive identifiers
}
public int hashCode ( ) { return id.hashCode( ); }
String strReturn() {
return id;
}
void display() {
System.out.print(id);
}
void display(int numOfIndent) {
for(int i = 0; i < numOfIndent; ++i ) {
System.out.print(" ");
}
System.out.println("Variable: " + id);
}
}
abstract class Value extends Expression {
// Value = IntValue | BoolValue |
// CharValue | FloatValue
protected Type type;
protected boolean undef = true;
int intValue ( ) {
assert false : "should never reach here";
return 0;
}
boolean boolValue ( ) {
assert false : "should never reach here";
return false;
}
char charValue ( ) {
assert false : "should never reach here";
return ' ';
}
float floatValue ( ) {
assert false : "should never reach here";
return 0.0f;
}
boolean isUndef( ) { return undef; }
Type type ( ) { return type; }
static Value mkValue (Type type) {
if (type == Type.INT) return new IntValue( );
if (type == Type.BOOL) return new BoolValue( );
if (type == Type.CHAR) return new CharValue( );
if (type == Type.FLOAT) return new FloatValue( );
throw new IllegalArgumentException("Illegal type in mkValue");
}
abstract void display(int numOfIndent);
abstract void display();
abstract String strReturn();
}
class IntValue extends Value {
private int value = 0;
IntValue ( ) { type = Type.INT; }
IntValue (int v) { this( ); value = v; undef = false; }
int intValue ( ) {
assert !undef : "reference to undefined int value";
return value;
}
public String toString( ) {
if (undef) return "undef";
return "" + value;
}
void display(int numOfIndent) {
for(int i = 0; i < numOfIndent; ++i ) {
System.out.print(" ");
}
System.out.println("IntValue: " + value);
}
String strReturn() {
String val = Integer.toString(value);
return val;
}
void display() {
System.out.print(value);
}
}
class BoolValue extends Value {
private boolean value = false;
BoolValue ( ) { type = Type.BOOL; }
BoolValue (boolean v) { this( ); value = v; undef = false; }
boolean boolValue ( ) {
assert !undef : "reference to undefined bool value";
return value;
}
int intValue ( ) {
assert !undef : "reference to undefined bool value";
return value ? 1 : 0;
}
public String toString( ) {
if (undef) return "undef";
return "" + value;
}
String strReturn() {
String val = "false";
if(value == true) {
val = "true";
}
return val;
}
void display(int numOfIndent) {
for(int i = 0; i < numOfIndent; ++i ) {
System.out.print(" ");
}
System.out.println("BoolValue: " + toString());
}
void display() {
System.out.print(value);
}
}
class CharValue extends Value {
private char value = ' ';
CharValue ( ) { type = Type.CHAR; }
CharValue (char v) { this( ); value = v; undef = false; }
char charValue ( ) {
assert !undef : "reference to undefined char value";
return value;
}
public String toString( ) {
if (undef) return "undef";
return "" + value;
}
String strReturn() {
String val = String.valueOf(value);
return val;
}
void display(int numOfIndent) {
for(int i = 0; i < numOfIndent; ++i ) {
System.out.print(" ");
}
System.out.println("CharValue: " + value);
}
void display() {
System.out.print(value);
}
}
class FloatValue extends Value {
private float value = 0;
FloatValue ( ) { type = Type.FLOAT; }
FloatValue (float v) { this( ); value = v; undef = false; }
float floatValue ( ) {
assert !undef : "reference to undefined float value";
return value;
}
public String toString( ) {
if (undef) return "undef";
return "" + value;
}
String strReturn() {
String val = Float.toString(value);
return val;
}
void display(int numOfIndent) {
for(int i = 0; i < numOfIndent; ++i ) {
System.out.print(" ");
}
System.out.println("FloatValue: " + value);
}
void display() {
System.out.print(value);
}
}
class Binary extends Expression {
// Binary = Operator op; Expression term1, term2
Operator op;
Expression term1, term2;
Binary (Operator o, Expression l, Expression r) {
op = o; term1 = l; term2 = r;
} // binar
void display(int numOfIndent) {
for(int i = 0; i < numOfIndent; ++i ) {
System.out.print(" ");
}
System.out.println("Binary:");
op.display(numOfIndent + Indent.INDENT);
term1.display(numOfIndent + Indent.INDENT);
term2.display(numOfIndent + Indent.INDENT);
}
void display() {
};
String strReturn() {
String t1 = term1.strReturn();
String t2 = term2.strReturn();
String opDisp = op.strReturn();
String wholeOp = t1 + " " + opDisp + " " + t2;
return wholeOp;
}
}
class Unary extends Expression {
// Unary = Operator op; Expression term
Operator op;
Expression term;
Unary (Operator o, Expression e) {
op = o; term = e;
} // unary
void display(int numOfIndent) {
for(int i = 0; i < numOfIndent; ++i ) {
System.out.print(" ");
}
System.out.println("Unary:");
op.display(numOfIndent + Indent.INDENT);
term.display(numOfIndent + Indent.INDENT);
}
void display() {
};
String strReturn() {
String opDisp = op.strReturn();
String t = term.strReturn();
return opDisp + " " + t;
}
}
class Operator {
// Operator = BooleanOp | RelationalOp | ArithmeticOp | UnaryOp
// 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 = "!";
final static String NEG = "-";
// CastOp = int | float | char
final static String INT = "int";
final static String FLOAT = "float";
final static String CHAR = "char";
// Typed Operators
// BoolOp = && | ||
final static String AND_2 = "AND";
final static String OR_2 = "OR";
// RelationalOp = < | <= | == | != | >= | >
final static String INT_LT = "INT<";
final static String INT_LE = "INT<=";
final static String INT_EQ = "INT==";
final static String INT_NE = "INT!=";
final static String INT_GT = "INT>";
final static String INT_GE = "INT>=";
// ArithmeticOp = + | - | * | /
final static String INT_PLUS = "INT+";
final static String INT_MINUS = "INT-";
final static String INT_TIMES = "INT*";
final static String INT_DIV = "INT/";
// UnaryOp = !
final static String INT_NEG = "INTU-";
// RelationalOp = < | <= | == | != | >= | >
final static String FLOAT_LT = "FLOAT<";
final static String FLOAT_LE = "FLOAT<=";
final static String FLOAT_EQ = "FLOAT==";
final static String FLOAT_NE = "FLOAT!=";
final static String FLOAT_GT = "FLOAT>";
final static String FLOAT_GE = "FLOAT>=";
// ArithmeticOp = + | - | * | /
final static String FLOAT_PLUS = "FLOAT+";
final static String FLOAT_MINUS = "FLOAT-";
final static String FLOAT_TIMES = "FLOAT*";
final static String FLOAT_DIV = "FLOAT/";
// UnaryOp = !
final static String FLOAT_NEG = "FLOATU-";
// RelationalOp = < | <= | == | != | >= | >
final static String CHAR_LT = "CHAR<";
final static String CHAR_LE = "CHAR<=";
final static String CHAR_EQ = "CHAR==";
final static String CHAR_NE = "CHAR!=";
final static String CHAR_GT = "CHAR>";
final static String CHAR_GE = "CHAR>=";
// RelationalOp = < | <= | == | != | >= | >
final static String BOOL_LT = "BOOL<";
final static String BOOL_LE = "BOOL<=";
final static String BOOL_EQ = "BOOL==";
final static String BOOL_NE = "BOOL!=";
final static String BOOL_GT = "BOOL>";
final static String BOOL_GE = "BOOL>=";
// Type specific cast
final static String I2F = "I2F";
final static String F2I = "F2I";
final static String C2I = "C2I";
final static String I2C = "I2C";
String val;
Operator (String s) { val = s; }
public String toString( ) { return val; }
public boolean equals(Object obj) { return val.equals(obj); }
boolean BooleanOp ( ) { return val.equals(AND) || val.equals(OR); }
boolean RelationalOp ( ) {
return val.equals(LT) || val.equals(LE) || val.equals(EQ)
|| val.equals(NE) || val.equals(GT) || val.equals(GE);
}
boolean ArithmeticOp ( ) {
return val.equals(PLUS) || val.equals(MINUS)
|| val.equals(TIMES) || val.equals(DIV);
}
boolean NotOp ( ) { return val.equals(NOT) ; }
boolean NegateOp ( ) { return val.equals(NEG) ; }
boolean intOp ( ) { return val.equals(INT); }
boolean floatOp ( ) { return val.equals(FLOAT); }
boolean charOp ( ) { return val.equals(CHAR); }
final static String intMap[ ] [ ] = {
{PLUS, INT_PLUS}, {MINUS, INT_MINUS},
{TIMES, INT_TIMES}, {DIV, INT_DIV},
{EQ, INT_EQ}, {NE, INT_NE}, {LT, INT_LT},
{LE, INT_LE}, {GT, INT_GT}, {GE, INT_GE},
{NEG, INT_NEG}, {FLOAT, I2F}, {CHAR, I2C}
};
final static String floatMap[ ] [ ] = {
{PLUS, FLOAT_PLUS}, {MINUS, FLOAT_MINUS},
{TIMES, FLOAT_TIMES}, {DIV, FLOAT_DIV},
{EQ, FLOAT_EQ}, {NE, FLOAT_NE}, {LT, FLOAT_LT},
{LE, FLOAT_LE}, {GT, FLOAT_GT}, {GE, FLOAT_GE},
{NEG, FLOAT_NEG}, {INT, F2I}
};
final static String charMap[ ] [ ] = {
{EQ, CHAR_EQ}, {NE, CHAR_NE}, {LT, CHAR_LT},
{LE, CHAR_LE}, {GT, CHAR_GT}, {GE, CHAR_GE},
{INT, C2I}
};
final static String boolMap[ ] [ ] = {
{EQ, BOOL_EQ}, {NE, BOOL_NE}, {LT, BOOL_LT},
{LE, BOOL_LE}, {GT, BOOL_GT}, {GE, BOOL_GE},
{AND, AND_2}, {OR, OR_2}
};
final static private Operator map (String[][] tmap, String op) {
for (int i = 0; i < tmap.length; i++)
if (tmap[i][0].equals(op))
return new Operator(tmap[i][1]);
assert false : "should never reach here";
return null;
}
final static public Operator intMap (String op) {
return map (intMap, op);
}
final static public Operator floatMap (String op) {
return map (floatMap, op);
}
final static public Operator charMap (String op) {
return map (charMap, op);
}
final static public Operator boolMap (String op) {
return map (boolMap, op);
}
void display(int numOfIndent) {
for(int i = 0; i < numOfIndent; ++i ) {
System.out.print(" ");
}
System.out.println("Operator: " + val);
}
String strReturn() {
return val;
}
}