forked from omaroaalj/miniJava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiniJava.g4
348 lines (311 loc) · 10.4 KB
/
MiniJava.g4
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
// Taeden & Omar - CMPT 355 - 2/17/2023
grammar MiniJava;
@parser::header {
import edu.westminstercollege.cmpt355.minijava.node.*;
import edu.westminstercollege.cmpt355.minijava.*;
import java.util.Optional;
import java.util.ArrayList;
}
goal
returns [Node n]
: classNode {
$n = $classNode.n;
}
;
classNode
returns [ClassNode n]
: (imports+=imp)*
(fields+=field)*
(methods+=method)*
main?
EOF {
var classElements = new ArrayList<Node>();
for (var classImport : $imports)
classElements.add(classImport.n);
for (var classField : $fields)
classElements.add(classField.n);
for (var classMethod : $methods)
classElements.add(classMethod.n);
if ($main.ctx != null)
classElements.add($main.n);
$n = new ClassNode($ctx, classElements);
}
;
imp
returns [Import n]
: 'import' importNames+=NAME ('.' importNames+=NAME)* ';' {
var importParts = new ArrayList<String>();
for (var importName : $importNames)
importParts.add(importName.getText());
$n = new ClassImport($ctx, importParts);
}
| 'import' importNames+=NAME '.' (importNames+=NAME '.')* '*' ';' {
var importParts = new ArrayList<String>();
for (var importName : $importNames)
importParts.add(importName.getText());
$n = new PackageImport($ctx, importParts);
}
;
field
returns [FieldDefinition n]
: type NAME ';' {
$n = new FieldDefinition($ctx, $type.n, $NAME.text, Optional.empty());
}
| type NAME '=' e=expression ';' {
$n = new FieldDefinition($ctx, $type.n, $NAME.text, Optional.of($e.n));
}
;
method
returns [MethodDefinition n]
: type NAME '(' (parameters+=parameter (',' parameters+=parameter)*)? ')' '{' methodBody '}' {
var parameterList = new ArrayList<Parameter>();
for (var p : $parameters)
parameterList.add(p.n);
$n = new MethodDefinition($ctx, $type.n, $NAME.text, parameterList, $methodBody.n, new SymbolTable(SymbolTable.Level.Method));
// DID NOT USE OPTIONALS
}
| 'void' NAME '(' (parameters+=parameter (',' parameters+=parameter)*)? ')' '{' methodBody '}' {
var parameterList = new ArrayList<Parameter>();
for (var p : $parameters)
parameterList.add(p.n);
$n = new MethodDefinition($ctx, new TypeNode($ctx, VoidType.Instance), $NAME.text, parameterList, $methodBody.n, new SymbolTable(SymbolTable.Level.Method));
}
;
main
returns [MainMethod n]
: 'main' '(' ')' '{' methodBody '}' {
$n = new MainMethod($ctx, $methodBody.n, new SymbolTable(SymbolTable.Level.Method));
}
;
methodBody
returns [Block n]
: (stmts+=statement)* {
var statements = new ArrayList<Statement>();
for(var stmt : $stmts)
statements.add(stmt.n);
$n = new Block($ctx, statements, new SymbolTable(SymbolTable.Level.Block));
}
;
parameter
returns [Parameter n]
: type NAME {
$n = new Parameter($ctx, $type.n, $NAME.text);
}
;
statement
returns [Statement n]
: ';' {
$n = new EmptyStatement($ctx);
}
| '{' stmts+=statement* '}' {
// is there a statment? if not, empty statement
var stmtList = new ArrayList<Statement>();
for(var stmt : $stmts){
stmtList.add(stmt.n);
}
$n = new Block($ctx, stmtList, new SymbolTable(SymbolTable.Level.Block));
}
// would include one or more variable declarations, possibly with initializations
| declaration {
$n = $declaration.n;
}
| expression ';' {
$n = new ExpressionStatement($ctx, $expression.n);
}
| 'return' e=expression ';' {
$n = new Return($ctx, Optional.of($e.n));
}
| 'return' ';' {
$n = new Return($ctx, Optional.empty());
}
| 'while' '(' cond=expression ')' body=statement {
$n = new While($ctx, $cond.n, $body.n);
}
| 'if' '(' cond=expression ')' body=statement {
$n = new If($ctx, $cond.n, $body.n);
}
| 'if' '(' cond=expression ')' body=statement 'else' elseBody=statement {
$n = new IfElse($ctx, $cond.n, $body.n, $elseBody.n);
}
// what goes between the ( )?
// - expression -
// - conditions like "i < 5"
// - variable call
// - method call
// as long as expression is boolean
;
// type followed by a comma-separated list of "items", each being just a name or a name = value.
declaration
returns [Declarations n] // Declaration contains TypeNode and name of variable
: type items+=decItem (',' items+=decItem)* ';' {
//Declarations parameters: TypeNode, list of Declaration
//Declaration parameters: String name, Optional<Expression>
var itemList = new ArrayList<Declaration>();
for(var item : $items)
itemList.add(item.n);
$n = new Declarations($ctx, $type.n, itemList);
}
;
decItem
returns[Declaration n] // DecItem is a name, may contain info for IntLiteral, DoubleLiteral, or VarAccess
: NAME {
$n = new Declaration($ctx, $NAME.text, Optional.empty());
}
| NAME '=' e=expression {
$n = new Declaration($ctx, $NAME.text, Optional.of($e.n));
}
;
// Other Boolean expressions:
// <, >, <=, >=, ==, != - relational operators (number -> boolean)
// == and != can be used with nonnumeric values
// == on objects is same is .equals(), != is !.equals()
// - !, &&, ||, - logic operators (boolean -> boolean)
// For objects, == means "are they the same object?"
expression
returns[Expression n]
: '_print' '(' (args+=expression (',' args+=expression)*)? ')' {
var prints = new ArrayList<Expression>();
for(var arg : $args)
prints.add(arg.n);
$n = new Print($ctx, prints);
}
| INT {
$n = new IntLiteral($ctx, $INT.text);
}
| DOUBLE {
$n = new DoubleLiteral($ctx, $DOUBLE.text);
}
| BOOLEAN {
$n = new BooleanLiteral($ctx, $BOOLEAN.text);
}
| 'this' {
$n = new This($ctx);
}
| STRING {
$n = new StringLiteral($ctx, $STRING.text);
}
// name (presumably of a variable)
| NAME {
$n = new VariableAccess($ctx, $NAME.text);
}
| e=expression '.' NAME {
$n = new FieldAccess($ctx, $e.n, $NAME.text);
}
| 'new' NAME '(' (args+=expression (',' args+=expression)*)? ')' {
var constructorArgs = new ArrayList<Expression>();
for (var arg : $args)
constructorArgs.add(arg.n);
$n = new ConstructorCall($ctx, $NAME.text, constructorArgs);
}
| e=expression '.'? NAME '(' (args+=expression (',' args+=expression)*)? ')' {
var methodArgs = new ArrayList<Expression>();
for (var arg : $args)
methodArgs.add(arg.n);
$n = new MethodCall($ctx, Optional.of($e.n), $NAME.text, methodArgs);
}
| NAME '(' (args+=expression (',' args+=expression)*)? ')' {
var methodArgs = new ArrayList<Expression>();
for (var arg : $args)
methodArgs.add(arg.n);
$n = new MethodCall($ctx, Optional.empty(), $NAME.text, methodArgs);
}
| '(' e=expression ')' {
$n = $e.n;
}
| e=expression op=('++' | '--') {
$n = new PostIncrement($ctx, $e.n, $op.text); // $op.text may be ++ or --
}
| op=('++' | '--' | '+' | '-') e=expression {
if($op.text.equals("++") || $op.text.equals("--")){
$n = new PreIncrement($ctx, $e.n, $op.text);
}
else if($op.text.equals("-")) {
$n = new Negate($ctx, $e.n);
}
else {
$n = $e.n;
}
}
| '(' type ')' e=expression {
$n = new Cast($ctx, $type.n, $e.n);
}
| l=expression op=('*' | '/' | '%') r=expression {
$n = new BinaryOp($ctx, $op.text, $l.n, $r.n);
}
| l=expression op=('+' | '-') r=expression {
$n = new BinaryOp($ctx, $op.text, $l.n, $r.n);
}
| l=expression op=('<' | '<=' | '>' | '>=') r=expression {
$n = new RelationalOp($ctx, $l.n, $r.n, $op.text);
}
| l=expression op=('==' | '!=') r=expression {
$n = new RelationalOp($ctx, $l.n, $r.n, $op.text);
}
| <assoc=right> l=expression '=' r=expression {
$n = new Assignment($ctx, $l.n, $r.n);
}
;
type
returns[TypeNode n]
: 'int' {
$n = new TypeNode($ctx, PrimitiveType.Int);
}
| 'double' {
$n = new TypeNode($ctx, PrimitiveType.Double);
}
| 'boolean' {
$n = new TypeNode($ctx, PrimitiveType.Boolean);
}
| NAME {
$n = new TypeNode($ctx, new ClassType($NAME.text));
}
;
RESERVED_WORD
: 'abstract' | 'continue' | 'for' | 'new' | 'switch'
| 'assert' | 'default' | 'if' | 'package' | 'synchronized'
| 'boolean' | 'do' | 'goto' | 'private' | 'this'
| 'break' | 'double' | 'implements' | 'protected' | 'throw'
| 'byte' | 'else' | 'import' | 'public' | 'throws'
| 'case' | 'enum' | 'instanceof' | 'return' | 'transient'
| 'catch' | 'extends' | 'int' | 'short' | 'try'
| 'char' | 'final' | 'interface' | 'static' | 'void'
| 'class' | 'finally' | 'long' | 'strictfp' | 'volatile'
| 'const' | 'float' | 'native' | 'super' | 'while'
| '_'
;
// letters, numbers, dollar signs '$', underscores '_', but not starting with a digit
WHITESPACE
: [ \n\r\t]+ -> skip
;
// fragment: doesn't generate tokens, but can be used in other lexer rules
fragment DIGITS
: [0-9]+
;
fragment FIXED_POINT
: [0-9]+ '.' [0-9]*
| [0-9]* '.' [0-9]+
;
INT
: DIGITS
;
DOUBLE
: FIXED_POINT
| FIXED_POINT [Ee] [+-]? INT
| DIGITS [Ee] [+-]? DIGITS
;
BOOLEAN
: 'true'
| 'false'
;
NAME
: [a-zA-Z_$] [a-zA-Z_$0-9]*
;
STRING
: '"' .*? '"'
;
LINE_COMMENT
: '//' .*? ('\n' | EOF) -> skip
;
BLOCK_COMMENT
: '/*' .*? '*/' -> skip
;