1 import java_cup.runtime.*;
4 /* August 1999 - modified by Gerwin Klein <lsf@jflex.de>
5 to interface with JFlex scanners,
6 allows empty semicolon in class decls.
10 class_member_declaration ::=
14 | interface_declaration
18 interface_member_declaration ::=
20 | abstract_method_declaration
22 | interface_declaration
29 /* Java 1.2 parser for CUP.
30 * Copyright (C) 1998 C. Scott Ananian <cananian@alumni.princeton.edu>
31 * This program is released under the terms of the GPL; see the file
32 * COPYING for more details. There is NO WARRANTY on this code.
36 JDK 1.2 Features added:
38 explicit_constructor_invocation ::= ...
39 | primary DOT THIS LPAREN argument_list_opt RPAREN SEMICOLON ;
41 | name DOT SUPER DOT IDENTIFIER ;
42 method_invocation ::= ...
43 | name DOT SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN ;
47 public void report_error(String message, Object info) {
48 StringBuffer m = new StringBuffer("Error ");
50 if (info instanceof java_cup.runtime.Symbol)
51 m.append( "("+info.toString()+")" );
53 m.append(" : "+message);
55 System.out.println(m);
58 public void report_fatal_error(String message, Object info) {
59 report_error(message, info);
60 throw new RuntimeException("Fatal Syntax Error");
64 terminal BOOLEAN; // primitive_type
65 terminal BYTE, SHORT, INT, LONG, CHAR; // integral_type
66 terminal FLOAT, DOUBLE; // floating_point_type
67 terminal LBRACK, RBRACK; // array_type
68 terminal DOT; // qualified_name
69 terminal SEMICOLON, MULT, COMMA, LBRACE, RBRACE, EQ, LPAREN, RPAREN, COLON;
70 terminal PACKAGE; // package_declaration
71 terminal IMPORT; // import_declaration
72 terminal PUBLIC, PROTECTED, PRIVATE; // modifier
73 terminal STATIC; // modifier
74 terminal ABSTRACT, FINAL, NATIVE, SYNCHRONIZED, TRANSIENT, VOLATILE;
75 terminal CLASS; // class_declaration
76 terminal EXTENDS; // super
77 terminal IMPLEMENTS; // interfaces
78 terminal VOID; // method_header
79 terminal THROWS; // throws
80 terminal THIS, SUPER; // explicit_constructor_invocation
81 terminal INTERFACE; // interface_declaration
82 terminal IF, ELSE; // if_then_statement, if_then_else_statement
83 terminal SWITCH; // switch_statement
84 terminal CASE, DEFAULT; // switch_label
85 terminal DO, WHILE; // while_statement, do_statement
86 terminal FOR; // for_statement
87 terminal BREAK; // break_statement
88 terminal CONTINUE; // continue_statement
89 terminal RETURN; // return_statement
90 terminal THROW; // throw_statement
91 terminal TRY; // try_statement
92 terminal CATCH; // catch_clause
93 terminal FINALLY; // finally
94 terminal NEW; // class_instance_creation_expression
95 terminal PLUSPLUS; // postincrement_expression
96 terminal MINUSMINUS; // postdecrement_expression
97 terminal PLUS, MINUS, COMP, NOT, DIV, MOD;
98 terminal LSHIFT, RSHIFT, URSHIFT; // shift_expression
99 terminal LT, GT, LTEQ, GTEQ, INSTANCEOF; // relational_expression
100 terminal EQEQ, NOTEQ; // equality_expression
101 terminal AND; // and_expression
102 terminal XOR; // exclusive_or_expression
103 terminal OR; // inclusive_or_expression
104 terminal ANDAND; // conditional_and_expression
105 terminal OROR; // conditional_or_expression
106 terminal QUESTION; // conditional_expression
107 terminal MULTEQ, DIVEQ, MODEQ, PLUSEQ, MINUSEQ; // assignment_operator
108 terminal LSHIFTEQ, RSHIFTEQ, URSHIFTEQ; // assignment_operator
109 terminal ANDEQ, XOREQ, OREQ; // assignment_operator
111 terminal java.lang.Number INTEGER_LITERAL;
112 terminal java.lang.Number FLOATING_POINT_LITERAL;
113 terminal java.lang.Boolean BOOLEAN_LITERAL;
114 terminal java.lang.Character CHARACTER_LITERAL;
115 terminal java.lang.String STRING_LITERAL;
116 terminal java.lang.String IDENTIFIER; // name
117 terminal NULL_LITERAL;
119 // strictfp keyword, new in Java 1.2
122 // Reserved but unused:
123 terminal CONST, GOTO;
125 // 19.2) The Syntactic Grammar
127 // 19.3) Lexical Structure
128 non terminal literal;
129 // 19.4) Types, Values, and Variables
130 non terminal type, primitive_type, numeric_type;
131 non terminal integral_type, floating_point_type;
132 non terminal reference_type;
133 non terminal class_or_interface_type;
134 non terminal class_type, interface_type;
135 non terminal array_type;
137 non terminal name, simple_name, qualified_name;
139 non terminal compilation_unit;
140 non terminal package_declaration_opt, package_declaration;
141 non terminal import_declarations_opt, import_declarations;
142 non terminal type_declarations_opt, type_declarations;
143 non terminal import_declaration;
144 non terminal single_type_import_declaration;
145 non terminal type_import_on_demand_declaration;
146 non terminal type_declaration;
147 // 19.7) Productions used only in the LALR(1) grammar
148 non terminal modifiers_opt, modifiers, modifier;
149 // 19.8.1) Class Declaration
150 non terminal class_declaration, super, super_opt;
151 non terminal interfaces, interfaces_opt, interface_type_list;
152 non terminal class_body;
153 non terminal class_body_declarations, class_body_declarations_opt;
154 non terminal class_body_declaration, class_member_declaration;
155 // 19.8.2) Field Declarations
156 non terminal field_declaration, variable_declarators, variable_declarator;
157 non terminal variable_declarator_id, variable_initializer;
158 // 19.8.3) Method Declarations
159 non terminal method_declaration, method_header, method_declarator;
160 non terminal formal_parameter_list_opt, formal_parameter_list;
161 non terminal formal_parameter;
162 non terminal throws_opt, throws;
163 non terminal class_type_list, method_body;
164 // 19.8.4) Static Initializers
165 non terminal static_initializer;
166 // 19.8.5) Constructor Declarations
167 non terminal constructor_declaration, constructor_declarator;
168 non terminal constructor_body;
169 non terminal explicit_constructor_invocation;
170 // 19.9.1) Interface Declarations
171 non terminal interface_declaration;
172 non terminal extends_interfaces_opt, extends_interfaces;
173 non terminal interface_body;
174 non terminal interface_member_declarations_opt, interface_member_declarations;
175 non terminal interface_member_declaration, constant_declaration;
176 non terminal abstract_method_declaration;
178 non terminal array_initializer;
179 non terminal variable_initializers;
180 // 19.11) Blocks and Statements
182 non terminal block_statements_opt, block_statements, block_statement;
183 non terminal local_variable_declaration_statement, local_variable_declaration;
184 non terminal statement, statement_no_short_if;
185 non terminal statement_without_trailing_substatement;
186 non terminal empty_statement;
187 non terminal labeled_statement, labeled_statement_no_short_if;
188 non terminal expression_statement, statement_expression;
189 non terminal if_then_statement;
190 non terminal if_then_else_statement, if_then_else_statement_no_short_if;
191 non terminal switch_statement, switch_block;
192 non terminal switch_block_statement_groups;
193 non terminal switch_block_statement_group;
194 non terminal switch_labels, switch_label;
195 non terminal while_statement, while_statement_no_short_if;
196 non terminal do_statement;
197 non terminal for_statement, for_statement_no_short_if;
198 non terminal for_init_opt, for_init;
199 non terminal for_update_opt, for_update;
200 non terminal statement_expression_list;
201 non terminal identifier_opt;
202 non terminal break_statement, continue_statement;
203 non terminal return_statement, throw_statement;
204 non terminal synchronized_statement, try_statement;
205 non terminal catches_opt, catches, catch_clause;
206 non terminal finally;
207 // 19.12) Expressions
208 non terminal primary, primary_no_new_array;
209 non terminal class_instance_creation_expression;
210 non terminal argument_list_opt, argument_list;
211 non terminal array_creation_expression;
212 non terminal dim_exprs, dim_expr, dims_opt, dims;
213 non terminal field_access, method_invocation, array_access;
214 non terminal postfix_expression;
215 non terminal postincrement_expression, postdecrement_expression;
216 non terminal unary_expression, unary_expression_not_plus_minus;
217 non terminal preincrement_expression, predecrement_expression;
218 non terminal cast_expression;
219 non terminal multiplicative_expression, additive_expression;
220 non terminal shift_expression, relational_expression, equality_expression;
221 non terminal and_expression, exclusive_or_expression, inclusive_or_expression;
222 non terminal conditional_and_expression, conditional_or_expression;
223 non terminal conditional_expression, assignment_expression;
224 non terminal assignment;
225 non terminal left_hand_side;
226 non terminal assignment_operator;
227 non terminal expression_opt, expression;
228 non terminal constant_expression;
233 // 19.2) The Syntactic Grammar
234 goal ::= compilation_unit
237 // 19.3) Lexical Structure.
238 literal ::= INTEGER_LITERAL
239 | FLOATING_POINT_LITERAL
246 // 19.4) Types, Values, and Variables
247 type ::= primitive_type
254 numeric_type::= integral_type
255 | floating_point_type
264 floating_point_type ::=
270 class_or_interface_type
273 class_or_interface_type ::= name;
275 class_type ::= class_or_interface_type;
276 interface_type ::= class_or_interface_type;
278 array_type ::= primitive_type dims
286 simple_name ::= IDENTIFIER
294 package_declaration_opt
295 import_declarations_opt
296 type_declarations_opt
298 package_declaration_opt ::= package_declaration | ;
299 import_declarations_opt ::= import_declarations | ;
300 type_declarations_opt ::= type_declarations | ;
302 import_declarations ::=
304 | import_declarations import_declaration
306 type_declarations ::=
308 | type_declarations type_declaration
310 package_declaration ::=
311 PACKAGE name SEMICOLON
313 import_declaration ::=
314 single_type_import_declaration
315 | type_import_on_demand_declaration
317 single_type_import_declaration ::=
318 IMPORT name SEMICOLON
320 type_import_on_demand_declaration ::=
321 IMPORT name DOT MULT SEMICOLON
325 | interface_declaration
329 // 19.7) Productions used only in the LALR(1) grammar
333 modifiers ::= modifier
336 modifier ::= PUBLIC | PROTECTED | PRIVATE
338 | ABSTRACT | FINAL | NATIVE | SYNCHRONIZED | TRANSIENT | VOLATILE
339 | STRICTFP // note that semantic analysis must check that the
340 // context of the modifier allows strictfp.
345 // 19.8.1) Class Declaration:
346 class_declaration ::=
347 modifiers_opt CLASS IDENTIFIER super_opt interfaces_opt class_body
349 super ::= EXTENDS class_type
354 interfaces ::= IMPLEMENTS interface_type_list
359 interface_type_list ::=
361 | interface_type_list COMMA interface_type
363 class_body ::= LBRACE class_body_declarations_opt RBRACE
365 class_body_declarations_opt ::=
366 | class_body_declarations ;
367 class_body_declarations ::=
368 class_body_declaration
369 | class_body_declarations class_body_declaration
371 class_body_declaration ::=
372 class_member_declaration
374 | constructor_declaration
377 class_member_declaration ::=
380 /* repeat the prod for 'class_declaration' here: */
381 | modifiers_opt CLASS IDENTIFIER super_opt interfaces_opt class_body
382 | interface_declaration
386 // 19.8.2) Field Declarations
387 field_declaration ::=
388 modifiers_opt type variable_declarators SEMICOLON
390 variable_declarators ::=
392 | variable_declarators COMMA variable_declarator
394 variable_declarator ::=
395 variable_declarator_id
396 | variable_declarator_id EQ variable_initializer
398 variable_declarator_id ::=
400 | variable_declarator_id LBRACK RBRACK
402 variable_initializer ::=
407 // 19.8.3) Method Declarations
408 method_declaration ::=
409 method_header method_body
412 modifiers_opt type method_declarator throws_opt
413 | modifiers_opt VOID method_declarator throws_opt
415 method_declarator ::=
416 IDENTIFIER LPAREN formal_parameter_list_opt RPAREN
417 | method_declarator LBRACK RBRACK // deprecated
418 // be careful; the above production also allows 'void foo() []'
420 formal_parameter_list_opt ::=
421 | formal_parameter_list
423 formal_parameter_list ::=
425 | formal_parameter_list COMMA formal_parameter
428 type variable_declarator_id
429 | FINAL type variable_declarator_id
434 throws ::= THROWS class_type_list
438 | class_type_list COMMA class_type
440 method_body ::= block
444 // 19.8.4) Static Initializers
445 static_initializer ::=
449 // 19.8.5) Constructor Declarations
450 constructor_declaration ::=
451 modifiers_opt constructor_declarator throws_opt
454 constructor_declarator ::=
455 simple_name LPAREN formal_parameter_list_opt RPAREN
458 LBRACE explicit_constructor_invocation
459 block_statements RBRACE
460 | LBRACE explicit_constructor_invocation RBRACE
461 | LBRACE block_statements RBRACE
464 explicit_constructor_invocation ::=
465 THIS LPAREN argument_list_opt RPAREN SEMICOLON
466 | SUPER LPAREN argument_list_opt RPAREN SEMICOLON
467 | primary DOT THIS LPAREN argument_list_opt RPAREN SEMICOLON
468 | primary DOT SUPER LPAREN argument_list_opt RPAREN SEMICOLON
473 // 19.9.1) Interface Declarations
474 interface_declaration ::=
475 modifiers_opt INTERFACE IDENTIFIER extends_interfaces_opt
478 extends_interfaces_opt ::=
481 extends_interfaces ::=
482 EXTENDS interface_type
483 | extends_interfaces COMMA interface_type
486 LBRACE interface_member_declarations_opt RBRACE
488 interface_member_declarations_opt ::=
489 | interface_member_declarations
491 interface_member_declarations ::=
492 interface_member_declaration
493 | interface_member_declarations interface_member_declaration
495 interface_member_declaration ::=
497 | abstract_method_declaration
499 | interface_declaration
502 constant_declaration ::=
505 abstract_method_declaration ::=
506 method_header SEMICOLON
510 array_initializer ::=
511 LBRACE variable_initializers COMMA RBRACE
512 | LBRACE variable_initializers RBRACE
513 | LBRACE COMMA RBRACE
516 variable_initializers ::=
518 | variable_initializers COMMA variable_initializer
521 // 19.11) Blocks and Statements
522 block ::= LBRACE block_statements_opt RBRACE
524 block_statements_opt ::=
529 | block_statements block_statement
532 local_variable_declaration_statement
535 | interface_declaration
537 local_variable_declaration_statement ::=
538 local_variable_declaration SEMICOLON
540 local_variable_declaration ::=
541 type variable_declarators
542 | FINAL type variable_declarators
544 statement ::= statement_without_trailing_substatement
547 | if_then_else_statement
551 statement_no_short_if ::=
552 statement_without_trailing_substatement
553 | labeled_statement_no_short_if
554 | if_then_else_statement_no_short_if
555 | while_statement_no_short_if
556 | for_statement_no_short_if
558 statement_without_trailing_substatement ::=
561 | expression_statement
567 | synchronized_statement
574 labeled_statement ::=
575 IDENTIFIER COLON statement
577 labeled_statement_no_short_if ::=
578 IDENTIFIER COLON statement_no_short_if
580 expression_statement ::=
581 statement_expression SEMICOLON
583 statement_expression ::=
585 | preincrement_expression
586 | predecrement_expression
587 | postincrement_expression
588 | postdecrement_expression
590 | class_instance_creation_expression
592 if_then_statement ::=
593 IF LPAREN expression RPAREN statement
595 if_then_else_statement ::=
596 IF LPAREN expression RPAREN statement_no_short_if
599 if_then_else_statement_no_short_if ::=
600 IF LPAREN expression RPAREN statement_no_short_if
601 ELSE statement_no_short_if
604 SWITCH LPAREN expression RPAREN switch_block
607 LBRACE switch_block_statement_groups switch_labels RBRACE
608 | LBRACE switch_block_statement_groups RBRACE
609 | LBRACE switch_labels RBRACE
612 switch_block_statement_groups ::=
613 switch_block_statement_group
614 | switch_block_statement_groups switch_block_statement_group
616 switch_block_statement_group ::=
617 switch_labels block_statements
621 | switch_labels switch_label
624 CASE constant_expression COLON
629 WHILE LPAREN expression RPAREN statement
631 while_statement_no_short_if ::=
632 WHILE LPAREN expression RPAREN statement_no_short_if
635 DO statement WHILE LPAREN expression RPAREN SEMICOLON
638 FOR LPAREN for_init_opt SEMICOLON expression_opt SEMICOLON
639 for_update_opt RPAREN statement
641 for_statement_no_short_if ::=
642 FOR LPAREN for_init_opt SEMICOLON expression_opt SEMICOLON
643 for_update_opt RPAREN statement_no_short_if
648 for_init ::= statement_expression_list
649 | local_variable_declaration
654 for_update ::= statement_expression_list
656 statement_expression_list ::=
658 | statement_expression_list COMMA statement_expression
666 BREAK identifier_opt SEMICOLON
669 continue_statement ::=
670 CONTINUE identifier_opt SEMICOLON
673 RETURN expression_opt SEMICOLON
676 THROW expression SEMICOLON
678 synchronized_statement ::=
679 SYNCHRONIZED LPAREN expression RPAREN block
683 | TRY block catches_opt finally
688 catches ::= catch_clause
689 | catches catch_clause
692 CATCH LPAREN formal_parameter RPAREN block
694 finally ::= FINALLY block
697 // 19.12) Expressions
698 primary ::= primary_no_new_array
699 | array_creation_expression
701 primary_no_new_array ::=
704 | LPAREN expression RPAREN
705 | class_instance_creation_expression
709 | primitive_type DOT CLASS
711 | array_type DOT CLASS
715 class_instance_creation_expression ::=
716 NEW class_type LPAREN argument_list_opt RPAREN
717 | NEW class_type LPAREN argument_list_opt RPAREN class_body
718 | primary DOT NEW IDENTIFIER
719 LPAREN argument_list_opt RPAREN
720 | primary DOT NEW IDENTIFIER
721 LPAREN argument_list_opt RPAREN class_body
723 argument_list_opt ::=
728 | argument_list COMMA expression
730 array_creation_expression ::=
731 NEW primitive_type dim_exprs dims_opt
732 | NEW class_or_interface_type dim_exprs dims_opt
733 | NEW primitive_type dims array_initializer
734 | NEW class_or_interface_type dims array_initializer
736 dim_exprs ::= dim_expr
739 dim_expr ::= LBRACK expression RBRACK
744 dims ::= LBRACK RBRACK
748 primary DOT IDENTIFIER
749 | SUPER DOT IDENTIFIER
750 | name DOT SUPER DOT IDENTIFIER
752 method_invocation ::=
753 name LPAREN argument_list_opt RPAREN
754 | primary DOT IDENTIFIER LPAREN argument_list_opt RPAREN
755 | SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
756 | name DOT SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
759 name LBRACK expression RBRACK
760 | primary_no_new_array LBRACK expression RBRACK
762 postfix_expression ::=
765 | postincrement_expression
766 | postdecrement_expression
768 postincrement_expression ::=
769 postfix_expression PLUSPLUS
771 postdecrement_expression ::=
772 postfix_expression MINUSMINUS
775 preincrement_expression
776 | predecrement_expression
777 | PLUS unary_expression
778 | MINUS unary_expression
779 | unary_expression_not_plus_minus
781 preincrement_expression ::=
782 PLUSPLUS unary_expression
784 predecrement_expression ::=
785 MINUSMINUS unary_expression
787 unary_expression_not_plus_minus ::=
789 | COMP unary_expression
790 | NOT unary_expression
794 LPAREN primitive_type dims_opt RPAREN unary_expression
795 | LPAREN expression RPAREN unary_expression_not_plus_minus
796 | LPAREN name dims RPAREN unary_expression_not_plus_minus
798 multiplicative_expression ::=
800 | multiplicative_expression MULT unary_expression
801 | multiplicative_expression DIV unary_expression
802 | multiplicative_expression MOD unary_expression
804 additive_expression ::=
805 multiplicative_expression
806 | additive_expression PLUS multiplicative_expression
807 | additive_expression MINUS multiplicative_expression
811 | shift_expression LSHIFT additive_expression
812 | shift_expression RSHIFT additive_expression
813 | shift_expression URSHIFT additive_expression
815 relational_expression ::=
817 | relational_expression LT shift_expression
818 | relational_expression GT shift_expression
819 | relational_expression LTEQ shift_expression
820 | relational_expression GTEQ shift_expression
821 | relational_expression INSTANCEOF reference_type
823 equality_expression ::=
824 relational_expression
825 | equality_expression EQEQ relational_expression
826 | equality_expression NOTEQ relational_expression
830 | and_expression AND equality_expression
832 exclusive_or_expression ::=
834 | exclusive_or_expression XOR and_expression
836 inclusive_or_expression ::=
837 exclusive_or_expression
838 | inclusive_or_expression OR exclusive_or_expression
840 conditional_and_expression ::=
841 inclusive_or_expression
842 | conditional_and_expression ANDAND inclusive_or_expression
844 conditional_or_expression ::=
845 conditional_and_expression
846 | conditional_or_expression OROR conditional_and_expression
848 conditional_expression ::=
849 conditional_or_expression
850 | conditional_or_expression QUESTION expression
851 COLON conditional_expression
853 assignment_expression ::=
854 conditional_expression
857 assignment ::= left_hand_side assignment_operator assignment_expression
864 assignment_operator ::=
881 expression ::= assignment_expression
883 constant_expression ::=