-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_cup_cup.cup
More file actions
431 lines (330 loc) · 18.6 KB
/
java_cup_cup.cup
File metadata and controls
431 lines (330 loc) · 18.6 KB
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
import semantic.*;
import java_cup.runtime.*;
parser code {:
public static Context mContext = new Context();
public void report_error(String message, Object info) {
StringBuilder m = new StringBuilder("Error ");
if (info instanceof java_cup.runtime.Symbol)
m.append( "("+info.toString()+")" );
m.append(" : "+message);
System.out.println(m);
}
public void report_fatal_error(String message, Object info) {
report_error(message, info);
throw new RuntimeException("Fatal Syntax Error");
}
:};
// terminais
terminal CLASS, EXTENDS, IMPLEMENTS, INTERFACE, NEW, BREAK, CASE;
terminal CONTINUE, DEFAULT, DO, IF, ELSE, FOR, RETURN, SWITCH;
terminal WHILE, TRY, CATCH, FINALLY, THROW, IMPORT;
terminal PACKAGE, BYTE, ABSTRACT, FINAL, NATIVE, SYNCHRONIZED;
terminal TRANSIENT, VOLATILE,SUPER, STRICTFP, PUBLIC, PROTECTED, PRIVATE;
terminal STATIC, VOID, NULL, THIS, INSTANCEOF, BOOLEAN;
terminal INTEGER_LITERAL, FLOAT_LITERAL;
terminal CHAR, SHORT, INT, FLOAT, LONG, DOUBLE, STRING, TRUE, FALSE;
terminal LPAREN, RPAREN, LBRACE, RBRACE, LBRACK, RBRACK, SEMICOLON;
terminal COMMA, DOT, EQUAL, GREATER, LESS, NOT, BITNOT, QUESTION;
terminal COLON, EQEQ, LTEQ, GTEQ, NOTEQ, ANDAND, OROR, PLUSPLUS;
terminal MINUSMINUS, PLUS, MINUS, MULT, DIV, AND, OR, XOR, MOD;
terminal LSHIFT, RSHIFT, URSHIFT, PLUSEQ, MINUSEQ, MULTEQ, DIVEQ;
terminal ANDEQ, OREQ, XOREQ, MODEQ, LSHIFTEQ, RSHIFTEQ, ID;
terminal STRING_LITERAL;
// nao terminais
non terminal compilation_unit, identifier, package_name, package_statement_e_aux;
non terminal package_statement, import_statement_e_aux, import_statement_e;
non terminal import_statement, type_declaration_e, type_declaration_e_aux;
non terminal type_declaration, type, class_declaration, interface_declaration;
non terminal field_declaration_e_aux,modifier_e, modifier, modifier_e_aux;
non terminal extends_e_aux, implements_e_aux, field_declaration_e, field_declaration;
non terminal arglist, interface_name_e, simple_interface_name_e, compound_interface_name_e;
non terminal interface_name, bracks_e_aux, argslist_e_aux, extends_interfaces, static_initializer;
non terminal expression, numeric_expression, testing_expression, logical_expression, bit_expression;
non terminal casting_expression, creating_expression, expression_end, expression_e_aux, return_expression;
non terminal throw_expression, parentheses_expression, literal, statement, if_else_statement;
non terminal braced_statement, partial_statement_end, no_statement, if_nested_statement;
non terminal if_statement, do_statement, while_statement, for_statement, try_statement;
non terminal catch_e_aux, finally_e_aux, catch_sentence, catches_sentences, switch_statement;
non terminal switch_cases_default, case_sentence, case_sentences, default_sentences, default_sentence;
non terminal for_statement_begin, for_statement_others, statements_within_braces, statement_within_braces;
non terminal statements_within_braces_e_aux, constructor_declaration, variable_declaration, variable_declarator;
non terminal variable_initializer, variable_initializers_e_aux, variable_initializers, variable_declarators;
non terminal statement_variable_declaration, variable_declarator_this_super;
non terminal method_declaration_end, this_super_prefix, parameter, parameter_e_aux, parameter_e, method_declaration, error1;
start with compilation_unit;
//#############package##############//
compilation_unit ::= package_statement_e_aux:p import_statement_e_aux:i type_declaration_e_aux:t
{:if(p != null && i != null){
System.out.println("PACKAGE RECONIZED: "+ p + " IMPORT RECONIZED: "+i);
}else if(p== null && i != null){
System.out.println("IMPORT RECONIZED: "+ i) ;
}else if(p != null && i == null){
System.out.println("PACKAGE RECONIZED: "+ p );
};:};
package_statement_e_aux ::= | package_statement:p {:RESULT = p;:};
package_statement ::= PACKAGE:p package_name:e SEMICOLON
{: System.out.println("PACKAGE RECONIZED: "+ p );:};
//#############end package##############//
identifier ::= ID:id {:RESULT = id;:};
package_name ::= identifier:id {:RESULT = id;:}
| package_name:pk DOT identifier:id {:RESULT = pk + "." + id;:};
//#############imports##############//
import_statement_e_aux ::= | import_statement_e;
import_statement_e ::= import_statement | import_statement_e import_statement;
import_statement ::= IMPORT package_name:i SEMICOLON
{:System.out.println("IMPORT RECONIZED: "+ i) ;:};
//#############end imports##############//
literal ::= NULL:n {: RESULT = new Variable(n.toString(), null, "null_literal", n); :}
| STRING_LITERAL:sl {: RESULT = new Variable(sl.toString(), null, "string_literal", sl); :}
| FLOAT_LITERAL: fl {: RESULT = new Variable(fl.toString(), null, "floating_literal", fl); :}
|INTEGER_LITERAL: il {: RESULT = new Variable(il.toString(), null, "integer_literal", il); :}
| TRUE :t {: RESULT = new Variable(t.toString(), null, "bool_literal", t); :}
| FALSE:f {: RESULT = new Variable(f.toString(), null, "bool_literal", f); :}
;
type ::= BOOLEAN:bl {: RESULT = bl; :}
| BYTE:b {: RESULT = b; :}
| CHAR:c {: RESULT = c; :}
| SHORT:s {: RESULT = s; :}
| INT:i {: RESULT = new Variable(i.toString(), null, "integer_literal", i); :}
| FLOAT:f {: RESULT = f; :}
| LONG:l {: RESULT = l; :}
| DOUBLE:d {: RESULT = d; :}
| STRING:s {: RESULT = s; :}
| VOID;
//#############begin class or interface##############//
type_declaration_e_aux ::= | type_declaration_e:t {:RESULT = t;:};
type_declaration_e ::= type_declaration:t {:RESULT = t;:}
| type_declaration_e type_declaration;
type_declaration ::= class_declaration
| interface_declaration;
class_declaration ::= modifier_e_aux CLASS identifier extends_e_aux implements_e_aux LBRACE field_declaration_e_aux RBRACE;
interface_declaration ::= modifier_e_aux INTERFACE identifier extends_interfaces LBRACE field_declaration_e RBRACE;
//#############end class or interface##############//
//###################################################################################//
//###################################################################################//
//############# constructor of class ##############//
constructor_declaration ::= modifier_e_aux identifier LPAREN parameter_e_aux RPAREN braced_statement;
//############# end constructor of class ##############//
//###################################################################################//
//###################################################################################//
//############# variables declarations ##############//
variable_declaration ::= modifier_e_aux type:t variable_declarators:v SEMICOLON
{:RESULT = new Variable(v.toString(), null, t.toString(), v) ;
System.out.println("Variable declareted: " + v);
:}
| modifier_e_aux variable_declarators:v SEMICOLON
{: System.err.println("SEMANTIC ERROR: Variable not declareted, name: " + v);:}
| modifier_e_aux package_name variable_declarators SEMICOLON;
variable_declarators ::= variable_declarator:v {:RESULT = v;:} | variable_declarators COMMA variable_declarator ;
variable_declarator ::= identifier:id bracks_e_aux variable_initializers_e_aux:var
{:RESULT = id;
if(var != null){
System.out.println("Variable initialized: " + id);
}else{
System.out.println("WARNING: Variable not initialized " + id);
}:};
variable_initializers_e_aux ::= | variable_initializer:v{:RESULT = v;:};
variable_initializers ::= variable_initializers:v COMMA variable_initializer
{:RESULT = v;:}
| variable_initializer:v
{:RESULT = v;:};
variable_initializer ::= EQUAL expression:e {:RESULT = e;:}
| EQUAL LBRACE variable_initializers RBRACE | EQUAL LBRACE RBRACE;
//############# end variables declarations ##############//
//###################################################################################//
//###################################################################################//
//#############declaration methods##############//
method_declaration ::= modifier_e_aux type identifier LPAREN parameter_e_aux RPAREN
method_declaration_end | modifier_e_aux package_name
identifier LPAREN parameter_e_aux RPAREN method_declaration_end;
method_declaration_end ::= braced_statement:b {:RESULT = b;:}
| SEMICOLON;
modifier_e_aux ::= | modifier_e:m {:RESULT = m;:};
modifier_e ::= modifier:m {:RESULT = m;:}
| modifier modifier_e;
modifier ::= ABSTRACT:m {:RESULT = m;:}
| FINAL:m {:RESULT = m;:}
| NATIVE:m {:RESULT = m;:}
| SYNCHRONIZED:m {:RESULT = m;:}
| TRANSIENT:m {:RESULT = m;:}
| STATIC:m {:RESULT = m;:}
| PUBLIC:m {:RESULT = m;:}
| PROTECTED:m {:RESULT = m;:}
| PRIVATE:m {:RESULT = m;:}
| STRICTFP:m {:RESULT = m;:}
| VOLATILE:m {:RESULT = m;:};
//#############end declaration methods##############//
//#############extends and/or implements##############//
extends_e_aux ::= | EXTENDS package_name;
implements_e_aux ::= | IMPLEMENTS interface_name_e;
interface_name_e ::= simple_interface_name_e:si {:RESULT = si;:}
| compound_interface_name_e:ci {:RESULT = ci;:};
simple_interface_name_e ::= interface_name:i {:RESULT = i;:};
compound_interface_name_e ::= interface_name_e COMMA interface_name;
interface_name ::= identifier:i {:RESULT = i;:} |
package_name DOT identifier;
//#############end extends and/or implements##############//
field_declaration_e_aux ::= | field_declaration_e:f {:RESULT = f;:};
field_declaration_e ::= field_declaration:f {:RESULT = f;:} | field_declaration_e field_declaration;
field_declaration ::= variable_declaration | method_declaration | constructor_declaration
| static_initializer | error1 ;
static_initializer ::= STATIC braced_statement;
extends_interfaces ::= | EXTENDS interface_name_e;
bracks_e_aux ::= | bracks_e_aux LBRACK RBRACK;
expression_e_aux ::= | expression:e {:RESULT = e;:};
expression ::= numeric_expression:n {:RESULT = n;:}| expression LPAREN expression RPAREN;
numeric_expression ::= testing_expression:te {:RESULT = te;:}//{:System.out.println("Testing: "+te);:}//{:RESULT = te;:}
| numeric_expression:e1 PLUS:p testing_expression:e2 {:System.out.println(RESULT + " ");:}
{: RESULT = SemanticAnalyzer.checkAdditiveExp(e2.toString(), p, e1);:}
| numeric_expression:e1 MINUS:m testing_expression:e2
{: RESULT = SemanticAnalyzer.checkAdditiveExp(e1.toString(), m, e2); :}
| numeric_expression:e1 MULT:m testing_expression:e2
{: RESULT = SemanticAnalyzer.checkMultiplicativeExp(e1.toString(), m, e2); :}
| numeric_expression:e1 DIV:d testing_expression:e2
{: RESULT = SemanticAnalyzer.checkMultiplicativeExp(e1.toString(), d, e2); :}
| numeric_expression:e1 MOD:m testing_expression:e2
{: RESULT = SemanticAnalyzer.checkMultiplicativeExp(e1.toString(), m, e2); :}
| numeric_expression DIVEQ testing_expression
| numeric_expression PLUSEQ testing_expression
| numeric_expression MINUSEQ testing_expression
| numeric_expression MULTEQ testing_expression
| numeric_expression MODEQ testing_expression;
testing_expression ::= logical_expression:le {:RESULT = le;:}
| testing_expression:e1 GREATER:g logical_expression:e2
// {: RESULT = SemanticAnalyzer.checkRelationalExp(e1.toString(), g, e2); :}
| testing_expression:e1 LESS:l logical_expression:e2
// {: RESULT = SemanticAnalyzer.checkRelationalExp(e1.toString(), l, e2); :}
| testing_expression:e1 GTEQ:g logical_expression:e2
// {: RESULT = SemanticAnalyzer.checkRelationalExp(e1.toString(), g, e2); :}
| testing_expression:e1 LTEQ:l logical_expression:e2
// {: RESULT = SemanticAnalyzer.checkRelationalExp(e1.toString(), l, e2); :}
| testing_expression EQEQ logical_expression
| testing_expression NOTEQ logical_expression;
logical_expression ::= bit_expression:be {:RESULT = be;:}
| NOT bit_expression
| logical_expression AND bit_expression
| logical_expression ANDEQ bit_expression
| logical_expression OR bit_expression
| logical_expression OREQ bit_expression
| logical_expression XOR bit_expression
| logical_expression XOREQ bit_expression
| logical_expression ANDAND bit_expression
| logical_expression OROR bit_expression
| logical_expression QUESTION expression COLON bit_expression;
bit_expression ::= casting_expression:ce {:RESULT = ce;:}
| BITNOT casting_expression
| bit_expression RSHIFTEQ casting_expression
| bit_expression RSHIFT casting_expression
| bit_expression LSHIFT casting_expression
| bit_expression URSHIFT casting_expression
| bit_expression LSHIFTEQ casting_expression;
casting_expression ::= creating_expression:ce {:RESULT = ce;:}
| LPAREN type RPAREN creating_expression
| parentheses_expression identifier;
creating_expression ::= expression_end:e {:RESULT = e;:}
| NEW package_name LPAREN argslist_e_aux RPAREN
| type LBRACK expression RBRACK bracks_e_aux
| package_name LBRACK expression RBRACK bracks_e_aux
| PLUSPLUS expression_end
| MINUSMINUS expression_end
| MINUS expression_end
| creating_expression PLUSPLUS
| creating_expression MINUSMINUS;
expression_end ::= identifier:id {:RESULT = new Variable(id.toString(), null, "integer_literal", id);:}
| SUPER | THIS | parentheses_expression
| LBRACK expression RBRACK | package_name INSTANCEOF package_name
| literal:l {:RESULT = l;:};
argslist_e_aux ::= | arglist;
arglist ::= expression | arglist COMMA expression;
parentheses_expression ::= LPAREN expression RPAREN;
statement ::= statement_variable_declaration
| if_else_statement
| if_statement
| partial_statement_end
| while_statement
| try_statement
| switch_statement
| for_statement;
partial_statement_end ::= braced_statement
| do_statement
| expression SEMICOLON
| CONTINUE SEMICOLON
| BREAK SEMICOLON
| identifier COLON partial_statement_end
| return_expression
| throw_expression
| SYNCHRONIZED LPAREN expression RPAREN partial_statement_end
| no_statement;
braced_statement ::= LBRACE statements_within_braces_e_aux RBRACE;
statements_within_braces_e_aux ::= | statements_within_braces;
statements_within_braces ::= statement_within_braces
| statements_within_braces statement_within_braces;
statement_within_braces ::= statement;
no_statement ::= SEMICOLON;
return_expression ::= RETURN expression_e_aux SEMICOLON;
throw_expression ::= THROW expression_e_aux SEMICOLON;
//#############condição##############//
if_else_statement ::= IF LPAREN expression RPAREN if_nested_statement ELSE statement;
if_nested_statement ::= partial_statement_end;
if_statement ::= IF LPAREN expression RPAREN statement;
//#############end condição##############//
//#############Laço while (ou do while)##############//
do_statement ::= DO statement WHILE LPAREN expression RPAREN SEMICOLON;
while_statement ::= WHILE LPAREN expression:e RPAREN statement
{:if(!(e.toString().equals("bool_literal true = true")
|| (e.toString().equals("bool_literal false = false")))
){
System.err.println("SEMANTIC ERROR: Expression '" + e + "' may not be boolean");
}:}
;
common_while ::= WHILE LPAREN;
//############# end Laço while##############//
//#############Laço for##############//
for_statement ::= FOR LPAREN for_statement_begin for_statement_others SEMICOLON for_statement_others RPAREN statement;
for_statement_begin ::= SEMICOLON | variable_declaration;
for_statement_others ::= expression_e_aux;
//#############end Laço for##############//
try_statement ::= TRY braced_statement catch_e_aux finally_e_aux;
catch_e_aux ::=
| catches_sentences;
catches_sentences ::= catch_sentence
| catches_sentences catch_sentence;
catch_sentence ::= CATCH LPAREN parameter RPAREN braced_statement;
finally_e_aux ::= | FINALLY braced_statement;
//#############switch case##############//
switch_statement ::= SWITCH LPAREN expression RPAREN LBRACE switch_cases_default RBRACE | SWITCH LPAREN expression RPAREN LBRACE RBRACE;
switch_cases_default ::= case_sentences | default_sentences | switch_cases_default statement;
case_sentences ::= case_sentence | switch_cases_default case_sentence;
case_sentence ::= CASE expression COLON;
default_sentences ::= default_sentence | switch_cases_default default_sentence;
default_sentence ::= DEFAULT COLON;
//############# end switch case##############//
//###################################################################################//
//###################################################################################//
parameter ::= type identifier bracks_e_aux | package_name identifier bracks_e_aux;
parameter_e_aux ::= | parameter_e;
parameter_e ::= parameter | parameter_e COMMA parameter;
statement_variable_declaration ::= FINAL type:t variable_declarators:v SEMICOLON
{:RESULT = new Variable(v.toString(), null, t.toString(), v) ;
System.out.println("FInal Variable declareted: " + v);
:}
| type:t variable_declarators:v SEMICOLON
{:RESULT = new Variable(v.toString(), null, t.toString(), v) ;
System.out.println("Variable declareted: " + v);
:}
| variable_declarator_this_super SEMICOLON
| package_name EQUAL expression SEMICOLON
// | variable_declarators:v SEMICOLON
// {: System.err.println("Variable not declareted, name: " + v);:}
| EQUAL expression:e SEMICOLON
{: System.err.println("SYNTACTIC ERROR: Missing package name or id at: " + e );:}
| package_name:p EQUAL SEMICOLON
{: System.err.println("SYNTACTIC ERROR: Missing expression after '" + p + " =' " );:}
// | type:t variable_declarators:v
// {: System.err.println("Missing semicolon at " + v);:}
;
variable_declarator_this_super ::= this_super_prefix identifier bracks_e_aux variable_initializers_e_aux;
this_super_prefix ::= THIS DOT | SUPER DOT;
//################################## errors ############################
error1 ::= BREAK SEMICOLON {:
System.err.println("SYNTACTIC ERROR: Break statement found, cannot use 'break' without an iteration");:};