3 import java_cup.runtime.*;
5 /* Java 1.5 (JSR-14 + JSR-201) parser for CUP.
6 * (Well, Java 1.5 as of 28 Jul 2003; it may change before official release)
7 * Copyright (C) 2003 C. Scott Ananian <cananian@alumni.princeton.edu>
8 * This program is released under the terms of the GPL; see the file
9 * COPYING for more details. There is NO WARRANTY on this code.
13 JSR-14 Features added:
14 * parameterized types, including corrections from the spec released
15 with the 2.2 prototype of the JSR-14 compiler. Arrays of parameterized
16 types bounded by wildcards are slated to be added to Java 1.5 (although
17 they are not supported by the 2.2 prototype); this grammar supports them.
18 "Wildcard" types are supported as of the 28 jul 2003 release.
20 JSR-201 Features added:
21 * no changes for autoboxing
24 FOR LPAREN type variable_declarator_id COLON expression RPAREN
26 // must check that first IDENTIFIER is 'each' and second IDENTIFIER
27 // is 'in' -- CSA extension; not (yet?) officially adopted
28 | FOR IDENTIFIER LPAREN type variable_declarator_id IDENTIFIER
29 expression RPAREN statement
31 foreach_statement_no_short_if ::=
32 FOR LPAREN type variable_declarator_id COLON expression RPAREN
34 // must check that first IDENTIFIER is 'each' and second IDENTIFIER
35 // is 'in' -- CSA extension; not (yet?) officially adopted
36 | FOR IDENTIFIER LPAREN type variable_declarator_id IDENTIFIER
37 expression RPAREN statement_no_short_if
41 statement_no_short_if ::= ...
42 | foreach_statement_no_short_if ;
45 static_single_type_import_declaration ::=
46 IMPORT STATIC name SEMICOLON
48 static_type_import_on_demand_declaration ::=
49 IMPORT STATIC name DOT MULT SEMICOLON
51 import_declaration ::= ...
52 | static_single_type_import_declaration
53 | static_type_import_on_demand_declaration
56 formal_parameter ::= ...
57 | type ELLIPSIS IDENTIFIER
58 | FINAL type ELLIPSIS IDENTIFIER
62 modifiers_opt ENUM IDENTIFIER interfaces_opt enum_body
65 LBRACE enum_constants_opt enum_body_declarations_opt RBRACE
67 enum_constants_opt ::=
72 | enum_constants COMMA enum_constant
75 IDENTIFIER enum_arguments_opt
76 | IDENTIFIER enum_arguments_opt class_body
78 enum_arguments_opt ::=
79 | LPAREN argument_list_opt RPAREN
81 enum_body_declarations_opt ::=
82 | SEMICOLON class_body_declarations_opt
88 public Grm15(Lexer l) {
93 public void syntax_error(java_cup.runtime.Symbol current) {
94 report_error("Syntax error (" + current.sym + ")", current);
96 public void report_error(String message, java_cup.runtime.Symbol info) {
97 lexer.errorMsg(message, info);
101 scan with {: return lexer.nextToken(); :};
103 terminal BOOLEAN; // primitive_type
104 terminal BYTE, SHORT, INT, LONG, CHAR; // integral_type
105 terminal FLOAT, DOUBLE; // floating_point_type
106 terminal LBRACK, RBRACK; // array_type
107 terminal java.lang.String IDENTIFIER; // name
108 terminal DOT; // qualified_name
109 terminal SEMICOLON, MULT, COMMA, LBRACE, RBRACE, EQ, LPAREN, RPAREN, COLON;
110 terminal PACKAGE; // package_declaration
111 terminal IMPORT; // import_declaration
112 terminal PUBLIC, PROTECTED, PRIVATE; // modifier
113 terminal STATIC; // modifier
114 terminal ABSTRACT, FINAL, NATIVE, SYNCHRONIZED, TRANSIENT, VOLATILE;
115 terminal CLASS; // class_declaration
116 terminal EXTENDS; // super
117 terminal IMPLEMENTS; // interfaces
118 terminal VOID; // method_header
119 terminal THROWS; // throws
120 terminal THIS, SUPER; // explicit_constructor_invocation
121 terminal INTERFACE; // interface_declaration
122 terminal IF, ELSE; // if_then_statement, if_then_else_statement
123 terminal SWITCH; // switch_statement
124 terminal CASE, DEFAULT; // switch_label
125 terminal DO, WHILE; // while_statement, do_statement
126 terminal FOR; // for_statement
127 terminal BREAK; // break_statement
128 terminal CONTINUE; // continue_statement
129 terminal RETURN; // return_statement
130 terminal THROW; // throw_statement
131 terminal TRY; // try_statement
132 terminal CATCH; // catch_clause
133 terminal FINALLY; // finally
134 terminal NEW; // class_instance_creation_expression
135 terminal PLUSPLUS; // postincrement_expression
136 terminal MINUSMINUS; // postdecrement_expression
137 terminal PLUS, MINUS, COMP, NOT, DIV, MOD;
138 terminal LSHIFT, RSHIFT, URSHIFT; // shift_expression
139 terminal LT, GT, LTEQ, GTEQ, INSTANCEOF; // relational_expression
140 terminal EQEQ, NOTEQ; // equality_expression
141 terminal AND; // and_expression
142 terminal XOR; // exclusive_or_expression
143 terminal OR; // inclusive_or_expression
144 terminal ANDAND; // conditional_and_expression
145 terminal OROR; // conditional_or_expression
146 terminal QUESTION; // conditional_expression
147 terminal MULTEQ, DIVEQ, MODEQ, PLUSEQ, MINUSEQ; // assignment_operator
148 terminal LSHIFTEQ, RSHIFTEQ, URSHIFTEQ; // assignment_operator
149 terminal ANDEQ, XOREQ, OREQ; // assignment_operator
151 terminal java.lang.Number INTEGER_LITERAL;
152 terminal java.lang.Number FLOATING_POINT_LITERAL;
153 terminal java.lang.Boolean BOOLEAN_LITERAL;
154 terminal java.lang.Character CHARACTER_LITERAL;
155 terminal java.lang.String STRING_LITERAL;
156 terminal NULL_LITERAL;
158 // Reserved but unused:
159 terminal CONST, GOTO;
160 // strictfp keyword, new in Java 1.2
162 // assert keyword, new in Java 1.4
163 terminal ASSERT; // assert_statement
164 // ellipsis token for varargs, new in Java 1.5 (JSR-201)
166 // enum keyword, new in Java 1.5 (JSR-201)
169 // 19.2) The Syntactic Grammar
171 // 19.3) Lexical Structure
172 non terminal literal;
173 // 19.4) Types, Values, and Variables
174 non terminal type, primitive_type, numeric_type;
175 non terminal integral_type, floating_point_type;
176 non terminal reference_type;
177 non terminal class_or_interface_type;
178 non terminal class_type, interface_type;
179 non terminal array_type;
181 non terminal name, simple_name, qualified_name;
183 non terminal compilation_unit;
184 non terminal package_declaration_opt, package_declaration;
185 non terminal import_declarations_opt, import_declarations;
186 non terminal type_declarations_opt, type_declarations;
187 non terminal import_declaration;
188 non terminal single_type_import_declaration;
189 non terminal type_import_on_demand_declaration;
190 non terminal static_single_type_import_declaration;
191 non terminal static_type_import_on_demand_declaration;
192 non terminal type_declaration;
193 // 19.7) Productions used only in the LALR(1) grammar
194 non terminal modifiers_opt, modifiers, modifier;
195 // 19.8.1) Class Declaration
196 non terminal class_declaration, super, super_opt;
197 non terminal interfaces, interfaces_opt, interface_type_list;
198 non terminal class_body, class_body_opt;
199 non terminal class_body_declarations, class_body_declarations_opt;
200 non terminal class_body_declaration, class_member_declaration;
201 // JSR-201) Enum Declaration
202 non terminal enum_declaration;
203 non terminal enum_body, enum_constants_opt, enum_constants, enum_constant;
204 non terminal enum_arguments_opt, enum_body_declarations_opt;
205 // 19.8.2) Field Declarations
206 non terminal field_declaration, variable_declarators, variable_declarator;
207 non terminal variable_declarator_id, variable_initializer;
208 // 19.8.3) Method Declarations
209 non terminal method_declaration, method_header, method_declarator;
210 non terminal formal_parameter_list_opt, formal_parameter_list;
211 non terminal formal_parameter;
212 non terminal throws_opt, throws;
213 non terminal class_type_list, method_body;
214 // 19.8.4) Static Initializers
215 non terminal static_initializer;
216 // 19.8.5) Constructor Declarations
217 non terminal constructor_declaration, constructor_declarator;
218 non terminal constructor_body;
219 non terminal explicit_constructor_invocation;
220 // 19.9.1) Interface Declarations
221 non terminal interface_declaration;
222 non terminal extends_interfaces_opt, extends_interfaces;
223 non terminal interface_body;
224 non terminal interface_member_declarations_opt, interface_member_declarations;
225 non terminal interface_member_declaration, constant_declaration;
226 non terminal abstract_method_declaration;
228 non terminal array_initializer;
229 non terminal variable_initializers;
230 // 19.11) Blocks and Statements
232 non terminal block_statements_opt, block_statements, block_statement;
233 non terminal local_variable_declaration_statement, local_variable_declaration;
234 non terminal statement, statement_no_short_if;
235 non terminal statement_without_trailing_substatement;
236 non terminal empty_statement;
237 non terminal labeled_statement, labeled_statement_no_short_if;
238 non terminal expression_statement, statement_expression;
239 non terminal if_then_statement;
240 non terminal if_then_else_statement, if_then_else_statement_no_short_if;
241 non terminal switch_statement, switch_block;
242 non terminal switch_block_statement_groups;
243 non terminal switch_block_statement_group;
244 non terminal switch_labels, switch_label;
245 non terminal while_statement, while_statement_no_short_if;
246 non terminal do_statement;
247 non terminal foreach_statement, foreach_statement_no_short_if;
248 non terminal for_statement, for_statement_no_short_if;
249 non terminal for_init_opt, for_init;
250 non terminal for_update_opt, for_update;
251 non terminal statement_expression_list;
252 non terminal identifier_opt;
253 non terminal break_statement, continue_statement;
254 non terminal return_statement, throw_statement;
255 non terminal synchronized_statement, try_statement;
256 non terminal catches_opt, catches, catch_clause;
257 non terminal finally;
258 non terminal assert_statement;
259 // 19.12) Expressions
260 non terminal primary, primary_no_new_array;
261 non terminal class_instance_creation_expression;
262 non terminal argument_list_opt, argument_list;
263 non terminal array_creation_init, array_creation_uninit;
264 non terminal dim_exprs, dim_expr, dims_opt, dims;
265 non terminal field_access, method_invocation, array_access;
266 non terminal postfix_expression;
267 non terminal postincrement_expression, postdecrement_expression;
268 non terminal unary_expression, unary_expression_not_plus_minus;
269 non terminal preincrement_expression, predecrement_expression;
270 non terminal cast_expression;
271 non terminal multiplicative_expression, additive_expression;
272 non terminal shift_expression, relational_expression, equality_expression;
273 non terminal and_expression, exclusive_or_expression, inclusive_or_expression;
274 non terminal conditional_and_expression, conditional_or_expression;
275 non terminal conditional_expression, assignment_expression;
276 non terminal assignment;
277 non terminal assignment_operator;
278 non terminal expression_opt, expression;
279 non terminal constant_expression;
280 // JSR-14 2.1) Type Syntax 2.3) Handling Consecutive Type Brackets
281 non terminal class_or_interface;
282 non terminal type_variable;
283 non terminal type_arguments, type_arguments_opt;
284 non terminal type_argument_list;
285 non terminal type_argument_list_1, reference_type_1;
286 non terminal type_argument_list_2, reference_type_2;
287 non terminal type_argument_list_3, reference_type_3;
288 // JSR-14 2.2) Parameterized Type Declarations 2.3) Handling Consecutive...
289 non terminal type_parameters, type_parameters_opt;
290 non terminal type_parameter, type_parameter_list;
291 non terminal type_parameter_1, type_parameter_list_1;
292 non terminal type_bound, type_bound_opt;
293 non terminal type_bound_1;
294 non terminal additional_bound_list, additional_bound_list_opt;
295 non terminal additional_bound_list_1;
296 non terminal additional_bound;
297 non terminal additional_bound_1;
298 non terminal wildcard, wildcard_1, wildcard_2, wildcard_3;
299 non terminal type_argument, type_argument_1, type_argument_2, type_argument_3;
300 // not mentioned in JSR-14: need to reduce the precedence of instanceof
301 // Alternatively, you can tweak the relational_expression production a little.
302 non terminal instanceof_expression;
303 //// expressions which are Not a Name
304 non terminal postfix_expression_nn;
305 non terminal unary_expression_nn;
306 non terminal unary_expression_not_plus_minus_nn;
307 non terminal multiplicative_expression_nn;
308 non terminal additive_expression_nn;
309 non terminal shift_expression_nn;
310 non terminal relational_expression_nn;
311 non terminal instanceof_expression_nn;
312 non terminal equality_expression_nn;
313 non terminal and_expression_nn;
314 non terminal exclusive_or_expression_nn;
315 non terminal inclusive_or_expression_nn;
316 non terminal conditional_and_expression_nn;
317 non terminal conditional_or_expression_nn;
318 non terminal conditional_expression_nn;
319 non terminal assignment_expression_nn;
320 non terminal expression_nn;
324 // 19.2) The Syntactic Grammar
325 goal ::= compilation_unit
328 // 19.3) Lexical Structure.
329 literal ::= INTEGER_LITERAL
330 | FLOATING_POINT_LITERAL
337 // 19.4) Types, Values, and Variables
338 type ::= primitive_type
345 numeric_type::= integral_type
346 | floating_point_type
355 floating_point_type ::=
361 class_or_interface_type
362 /* note that the 'type_variable' production will come out of the grammar
363 * as a 'class_or_interface_type' with a 'simple_name'. The semantic
364 * checker will have to resolve whether this is a class name or a type
371 class_or_interface ::=
373 | class_or_interface LT type_argument_list_1 DOT name
375 class_or_interface_type ::=
377 | class_or_interface LT type_argument_list_1
380 class_type ::= class_or_interface_type;
381 interface_type ::= class_or_interface_type;
383 array_type ::= primitive_type dims
384 // we have class_or_interface_type here even though only unbounded
385 // wildcards are really allowed in the parameterization.
386 // we have to expand this to avoid lookahead problems.
388 | class_or_interface LT type_argument_list_1 DOT name dims
389 | class_or_interface LT type_argument_list_1 dims
392 type_arguments_opt ::= type_arguments | ;
395 LT type_argument_list_1
397 wildcard ::= QUESTION
398 | QUESTION EXTENDS reference_type
399 | QUESTION SUPER reference_type
401 wildcard_1 ::= QUESTION GT
402 | QUESTION EXTENDS reference_type_1
403 | QUESTION SUPER reference_type_1
405 wildcard_2 ::= QUESTION RSHIFT
406 | QUESTION EXTENDS reference_type_2
407 | QUESTION SUPER reference_type_2
409 wildcard_3 ::= QUESTION URSHIFT
410 | QUESTION EXTENDS reference_type_3
411 | QUESTION SUPER reference_type_3
415 | class_or_interface LT type_argument_list_2
418 reference_type RSHIFT
419 | class_or_interface LT type_argument_list_3
422 reference_type URSHIFT
424 type_argument_list ::=
426 | type_argument_list COMMA type_argument
428 type_argument_list_1 ::=
430 | type_argument_list COMMA type_argument_1
432 type_argument_list_2 ::=
434 | type_argument_list COMMA type_argument_2
436 type_argument_list_3 ::=
438 | type_argument_list COMMA type_argument_3
461 simple_name ::= IDENTIFIER
469 package_declaration_opt
470 import_declarations_opt
471 type_declarations_opt
473 package_declaration_opt ::= package_declaration | ;
474 import_declarations_opt ::= import_declarations | ;
475 type_declarations_opt ::= type_declarations | ;
477 import_declarations ::=
479 | import_declarations import_declaration
481 type_declarations ::=
483 | type_declarations type_declaration
485 package_declaration ::=
486 PACKAGE name SEMICOLON
488 import_declaration ::=
489 single_type_import_declaration
490 | type_import_on_demand_declaration
491 | static_single_type_import_declaration
492 | static_type_import_on_demand_declaration
494 single_type_import_declaration ::=
495 IMPORT name SEMICOLON
497 static_single_type_import_declaration ::=
498 IMPORT STATIC name SEMICOLON
500 type_import_on_demand_declaration ::=
501 IMPORT name DOT MULT SEMICOLON
503 static_type_import_on_demand_declaration ::=
504 IMPORT STATIC name DOT MULT SEMICOLON
509 | interface_declaration
513 // 19.7) Productions used only in the LALR(1) grammar
517 modifiers ::= modifier
520 modifier ::= PUBLIC | PROTECTED | PRIVATE
522 | ABSTRACT | FINAL | NATIVE | SYNCHRONIZED | TRANSIENT | VOLATILE
523 | STRICTFP // note that semantic analysis must check that the
524 // context of the modifier allows strictfp.
529 // 19.8.1) Class Declaration:
530 class_declaration ::=
531 modifiers_opt CLASS IDENTIFIER type_parameters_opt
532 super_opt interfaces_opt class_body
534 super ::= EXTENDS class_type
539 interfaces ::= IMPLEMENTS interface_type_list
544 interface_type_list ::=
546 | interface_type_list COMMA interface_type
548 class_body ::= LBRACE class_body_declarations_opt RBRACE
552 class_body_declarations_opt ::=
553 | class_body_declarations ;
554 class_body_declarations ::=
555 class_body_declaration
556 | class_body_declarations class_body_declaration
558 class_body_declaration ::=
559 class_member_declaration
561 | constructor_declaration
564 class_member_declaration ::=
567 /* repeat the prod for 'class_declaration' here: */
568 | modifiers_opt CLASS IDENTIFIER type_parameters_opt super_opt interfaces_opt class_body
570 | interface_declaration
574 // JSR-201) Enum Declaration
576 modifiers_opt ENUM IDENTIFIER interfaces_opt enum_body
579 LBRACE enum_constants_opt enum_body_declarations_opt RBRACE
581 enum_constants_opt ::=
586 | enum_constants COMMA enum_constant
589 IDENTIFIER enum_arguments_opt
590 | IDENTIFIER enum_arguments_opt class_body
592 enum_arguments_opt ::=
593 | LPAREN argument_list_opt RPAREN
595 enum_body_declarations_opt ::=
596 | SEMICOLON class_body_declarations_opt
599 // 19.8.2) Field Declarations
600 field_declaration ::=
601 modifiers_opt type variable_declarators SEMICOLON
603 variable_declarators ::=
605 | variable_declarators COMMA variable_declarator
607 variable_declarator ::=
608 variable_declarator_id
609 | variable_declarator_id EQ variable_initializer
611 variable_declarator_id ::=
613 | variable_declarator_id LBRACK RBRACK
615 variable_initializer ::=
620 // 19.8.3) Method Declarations
621 method_declaration ::=
622 method_header method_body
625 // have to expand type_parameters_opt here so that we don't
626 // force an early decision of whether this is a field_declaration
627 // or a method_declaration (the type_parameters_opt would have to
628 // be reduced when we see the 'type' if this was a method declaration,
629 // but it might still turn out to be a field declaration).
630 modifiers_opt type method_declarator throws_opt
631 | modifiers_opt LT type_parameter_list_1 type method_declarator throws_opt
632 | modifiers_opt VOID method_declarator throws_opt
633 | modifiers_opt LT type_parameter_list_1 VOID method_declarator throws_opt
635 method_declarator ::=
636 IDENTIFIER LPAREN formal_parameter_list_opt RPAREN
637 | method_declarator LBRACK RBRACK // deprecated
638 // be careful; the above production also allows 'void foo() []'
640 formal_parameter_list_opt ::=
641 | formal_parameter_list
643 formal_parameter_list ::=
645 | formal_parameter_list COMMA formal_parameter
648 type variable_declarator_id
649 | FINAL type variable_declarator_id
650 // careful, productions below allow varargs in non-final positions.
651 | type ELLIPSIS IDENTIFIER
652 | FINAL type ELLIPSIS IDENTIFIER
657 throws ::= THROWS class_type_list
661 | class_type_list COMMA class_type
663 method_body ::= block
667 // 19.8.4) Static Initializers
668 static_initializer ::=
672 // 19.8.5) Constructor Declarations
673 constructor_declaration ::=
674 modifiers_opt constructor_declarator
675 throws_opt constructor_body
676 | modifiers_opt LT type_parameter_list_1 constructor_declarator
677 throws_opt constructor_body
679 constructor_declarator ::=
680 simple_name LPAREN formal_parameter_list_opt RPAREN
683 LBRACE explicit_constructor_invocation
684 block_statements RBRACE
685 | LBRACE explicit_constructor_invocation RBRACE
686 | LBRACE block_statements RBRACE
689 explicit_constructor_invocation ::=
690 THIS LPAREN argument_list_opt RPAREN SEMICOLON
691 | type_arguments THIS LPAREN argument_list_opt RPAREN SEMICOLON
692 | SUPER LPAREN argument_list_opt RPAREN SEMICOLON
693 | type_arguments SUPER LPAREN argument_list_opt RPAREN SEMICOLON
694 | primary DOT SUPER LPAREN argument_list_opt RPAREN SEMICOLON
695 | primary DOT type_arguments SUPER
696 LPAREN argument_list_opt RPAREN SEMICOLON
697 | name DOT SUPER LPAREN argument_list_opt RPAREN SEMICOLON
698 | name DOT type_arguments SUPER LPAREN argument_list_opt RPAREN SEMICOLON
703 // 19.9.1) Interface Declarations
704 interface_declaration ::=
705 modifiers_opt INTERFACE IDENTIFIER type_parameters_opt
706 extends_interfaces_opt interface_body
708 extends_interfaces_opt ::=
711 extends_interfaces ::=
712 EXTENDS interface_type
713 | extends_interfaces COMMA interface_type
716 LBRACE interface_member_declarations_opt RBRACE
718 interface_member_declarations_opt ::=
719 | interface_member_declarations
721 interface_member_declarations ::=
722 interface_member_declaration
723 | interface_member_declarations interface_member_declaration
725 interface_member_declaration ::=
727 | abstract_method_declaration
730 | interface_declaration
733 constant_declaration ::=
735 // need to semantically check that modifiers of field declaration
736 // include only PUBLIC, STATIC, or FINAL. Other modifiers are
739 abstract_method_declaration ::=
740 method_header SEMICOLON
744 array_initializer ::=
745 LBRACE variable_initializers COMMA RBRACE
746 | LBRACE variable_initializers RBRACE
747 | LBRACE COMMA RBRACE
750 variable_initializers ::=
752 | variable_initializers COMMA variable_initializer
755 // 19.11) Blocks and Statements
756 block ::= LBRACE block_statements_opt RBRACE
758 block_statements_opt ::=
763 | block_statements block_statement
766 local_variable_declaration_statement
770 | interface_declaration
772 local_variable_declaration_statement ::=
773 local_variable_declaration SEMICOLON
775 /* jikes expands 'type' in production for local_variable_declaration to
776 * avoid reduce-reduce conflict: given 'name [' the grammar can't decide
777 * whether this is going to be a type (starting the local_variable_declaration)
778 * or an array access expression. */
779 local_variable_declaration ::=
780 type variable_declarators
781 // you may want to accept 'modifiers' here instead of just FINAL
782 // to produce better error messages.
783 | FINAL type variable_declarators
785 statement ::= statement_without_trailing_substatement
788 | if_then_else_statement
793 statement_no_short_if ::=
794 statement_without_trailing_substatement
795 | labeled_statement_no_short_if
796 | if_then_else_statement_no_short_if
797 | while_statement_no_short_if
798 | for_statement_no_short_if
799 | foreach_statement_no_short_if
801 statement_without_trailing_substatement ::=
804 | expression_statement
810 | synchronized_statement
818 labeled_statement ::=
819 IDENTIFIER COLON statement
821 labeled_statement_no_short_if ::=
822 IDENTIFIER COLON statement_no_short_if
824 expression_statement ::=
825 statement_expression SEMICOLON
827 statement_expression ::=
829 | preincrement_expression
830 | predecrement_expression
831 | postincrement_expression
832 | postdecrement_expression
834 | class_instance_creation_expression
836 if_then_statement ::=
837 IF LPAREN expression RPAREN statement
839 if_then_else_statement ::=
840 IF LPAREN expression RPAREN statement_no_short_if
843 if_then_else_statement_no_short_if ::=
844 IF LPAREN expression RPAREN statement_no_short_if
845 ELSE statement_no_short_if
848 SWITCH LPAREN expression RPAREN switch_block
851 LBRACE switch_block_statement_groups switch_labels RBRACE
852 | LBRACE switch_block_statement_groups RBRACE
853 | LBRACE switch_labels RBRACE
856 switch_block_statement_groups ::=
857 switch_block_statement_group
858 | switch_block_statement_groups switch_block_statement_group
860 switch_block_statement_group ::=
861 switch_labels block_statements
865 | switch_labels switch_label
868 CASE constant_expression COLON
873 WHILE LPAREN expression RPAREN statement
875 while_statement_no_short_if ::=
876 WHILE LPAREN expression RPAREN statement_no_short_if
879 DO statement WHILE LPAREN expression RPAREN SEMICOLON
881 foreach_statement ::=
882 FOR LPAREN type variable_declarator_id COLON expression RPAREN
884 // must check that first IDENTIFIER is 'each' and second IDENTIFIER
886 | FOR IDENTIFIER LPAREN type variable_declarator_id IDENTIFIER
887 expression RPAREN statement
889 foreach_statement_no_short_if ::=
890 FOR LPAREN type variable_declarator_id COLON expression RPAREN
891 statement_no_short_if
892 // must check that first IDENTIFIER is 'each' and second IDENTIFIER
894 | FOR IDENTIFIER LPAREN type variable_declarator_id IDENTIFIER
895 expression RPAREN statement_no_short_if
898 FOR LPAREN for_init_opt SEMICOLON expression_opt SEMICOLON
899 for_update_opt RPAREN statement
901 for_statement_no_short_if ::=
902 FOR LPAREN for_init_opt SEMICOLON expression_opt SEMICOLON
903 for_update_opt RPAREN statement_no_short_if
908 for_init ::= statement_expression_list
909 | local_variable_declaration
914 for_update ::= statement_expression_list
916 statement_expression_list ::=
918 | statement_expression_list COMMA statement_expression
926 BREAK identifier_opt SEMICOLON
929 continue_statement ::=
930 CONTINUE identifier_opt SEMICOLON
933 RETURN expression_opt SEMICOLON
936 THROW expression SEMICOLON
938 synchronized_statement ::=
939 SYNCHRONIZED LPAREN expression RPAREN block
943 | TRY block catches_opt finally
948 catches ::= catch_clause
949 | catches catch_clause
952 CATCH LPAREN formal_parameter RPAREN block
954 finally ::= FINALLY block
957 ASSERT expression SEMICOLON
958 | ASSERT expression COLON expression SEMICOLON
961 // 19.12) Expressions
962 primary ::= primary_no_new_array
963 | array_creation_init
964 | array_creation_uninit
966 primary_no_new_array ::=
970 | LPAREN expression_nn RPAREN
971 | class_instance_creation_expression
977 // "Type DOT CLASS", but expanded
978 | primitive_type DOT CLASS
979 | primitive_type dims DOT CLASS
981 | name dims DOT CLASS
982 // the following two productions are part of the expansion of
983 // 'type DOT CLASS' but are not actually allowed, as they involve params.
984 // [see msg from Neal Gafter <3F219367.3070903@sun.com> 25-jul-2003]
985 // | class_or_interface type_arguments DOT name dims DOT CLASS
986 // | class_or_interface LT type_argument_list_1 dims DOT CLASS
988 // grammar distributed with prototype 2.2 is in error; the following is correct
989 // [ Neal Gafter, <3F2577E0.3090008@sun.com> ]
990 class_instance_creation_expression ::=
991 NEW class_or_interface_type LPAREN argument_list_opt RPAREN class_body_opt
992 | NEW type_arguments class_or_interface_type LPAREN argument_list_opt RPAREN class_body_opt
993 | primary DOT NEW type_arguments_opt IDENTIFIER type_arguments_opt
994 LPAREN argument_list_opt RPAREN class_body_opt
995 | name DOT NEW type_arguments_opt IDENTIFIER type_arguments_opt
996 LPAREN argument_list_opt RPAREN class_body_opt
998 argument_list_opt ::=
1003 | argument_list COMMA expression
1005 array_creation_uninit ::=
1006 NEW primitive_type dim_exprs dims_opt
1007 | NEW class_or_interface_type dim_exprs dims_opt
1009 array_creation_init ::=
1010 NEW primitive_type dims array_initializer
1011 | NEW class_or_interface_type dims array_initializer
1013 dim_exprs ::= dim_expr
1014 | dim_exprs dim_expr
1016 dim_expr ::= LBRACK expression RBRACK
1021 dims ::= LBRACK RBRACK
1022 | dims LBRACK RBRACK
1025 primary DOT IDENTIFIER
1026 | SUPER DOT IDENTIFIER
1027 | name DOT SUPER DOT IDENTIFIER
1029 method_invocation ::=
1030 name LPAREN argument_list_opt RPAREN
1031 // the following production appeared in the prototype 2.2 spec, but it
1032 // introduces ambiguities in the grammar (consider the expression
1034 // which could be either an invocation on E or two boolean comparisons).
1035 // Neal Gafter has assured me that this production should be removed
1036 // from the grammar. <3F256C06.7000600@sun.com>
1037 // | type_arguments name LPAREN argument_list_opt RPAREN
1038 | primary DOT IDENTIFIER LPAREN argument_list_opt RPAREN
1039 | primary DOT type_arguments IDENTIFIER LPAREN argument_list_opt RPAREN
1040 | name DOT type_arguments IDENTIFIER LPAREN argument_list_opt RPAREN
1041 | SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
1042 | SUPER DOT type_arguments IDENTIFIER LPAREN argument_list_opt RPAREN
1043 | name DOT SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
1044 | name DOT SUPER DOT type_arguments IDENTIFIER LPAREN argument_list_opt RPAREN
1047 name LBRACK expression RBRACK
1048 | primary_no_new_array LBRACK expression RBRACK
1049 | array_creation_init LBRACK expression RBRACK
1051 postfix_expression ::=
1054 | postincrement_expression
1055 | postdecrement_expression
1057 postincrement_expression ::=
1058 postfix_expression PLUSPLUS
1060 postdecrement_expression ::=
1061 postfix_expression MINUSMINUS
1063 unary_expression ::=
1064 preincrement_expression
1065 | predecrement_expression
1066 | PLUS unary_expression
1067 | MINUS unary_expression
1068 | unary_expression_not_plus_minus
1070 preincrement_expression ::=
1071 PLUSPLUS unary_expression
1073 predecrement_expression ::=
1074 MINUSMINUS unary_expression
1076 unary_expression_not_plus_minus ::=
1078 | COMP unary_expression
1079 | NOT unary_expression
1082 // This parsing technique was discovered by Eric Blake <ebb9@email.byu.edu>
1083 // We solving grammar ambiguities with between parenthesized less-than
1084 // relational operations and type casts with a slightly-more-complicated
1085 // cast_expression production.
1086 // Illustrative example: LPAREN name LT name ...
1087 // is this going to be a cast_expression or a relational_expression?
1088 // canonically, this production is:
1089 // cast_expression ::= LPAREN type RPAREN unary_expression_not_plus_minus
1091 LPAREN primitive_type dims_opt RPAREN unary_expression
1092 | LPAREN name RPAREN unary_expression_not_plus_minus
1093 | LPAREN name dims RPAREN unary_expression_not_plus_minus
1094 | LPAREN name LT type_argument_list_1 dims_opt RPAREN
1095 unary_expression_not_plus_minus
1096 | LPAREN name LT type_argument_list_1 DOT
1097 class_or_interface_type dims_opt RPAREN
1098 unary_expression_not_plus_minus
1100 multiplicative_expression ::=
1102 | multiplicative_expression MULT unary_expression
1103 | multiplicative_expression DIV unary_expression
1104 | multiplicative_expression MOD unary_expression
1106 additive_expression ::=
1107 multiplicative_expression
1108 | additive_expression PLUS multiplicative_expression
1109 | additive_expression MINUS multiplicative_expression
1111 shift_expression ::=
1113 | shift_expression LSHIFT additive_expression
1114 | shift_expression RSHIFT additive_expression
1115 | shift_expression URSHIFT additive_expression
1117 relational_expression ::=
1119 | relational_expression LT shift_expression
1120 | relational_expression GT shift_expression
1121 | relational_expression LTEQ shift_expression
1122 | relational_expression GTEQ shift_expression
1124 // we lower the precendence of instanceof to resolve a grammar ambiguity.
1125 // semantics are unchanged, since relational expressions do not operate
1126 // on boolean. Eric Blake had a different solution here, where he
1127 // used the production 'shift_expression LT shift_expression' to solve
1128 // the same problem.
1129 instanceof_expression ::=
1130 relational_expression
1131 | instanceof_expression INSTANCEOF reference_type
1133 equality_expression ::=
1134 instanceof_expression
1135 | equality_expression EQEQ instanceof_expression
1136 | equality_expression NOTEQ instanceof_expression
1140 | and_expression AND equality_expression
1142 exclusive_or_expression ::=
1144 | exclusive_or_expression XOR and_expression
1146 inclusive_or_expression ::=
1147 exclusive_or_expression
1148 | inclusive_or_expression OR exclusive_or_expression
1150 conditional_and_expression ::=
1151 inclusive_or_expression
1152 | conditional_and_expression ANDAND inclusive_or_expression
1154 conditional_or_expression ::=
1155 conditional_and_expression
1156 | conditional_or_expression OROR conditional_and_expression
1158 conditional_expression ::=
1159 conditional_or_expression
1160 | conditional_or_expression QUESTION expression
1161 COLON conditional_expression
1163 assignment_expression ::=
1164 conditional_expression
1167 // semantic check necessary here to ensure a valid left-hand side.
1168 // allowing a parenthesized variable here on the lhs was introduced in
1169 // JLS 2; thanks to Eric Blake for pointing this out.
1170 assignment ::= postfix_expression assignment_operator assignment_expression
1172 assignment_operator ::=
1189 expression ::= assignment_expression
1191 // note that this constraint must be enforced during semantic checking
1192 // 'constant_expression' should include enumerated constants.
1193 constant_expression ::=
1197 // JLS-14 productions.
1198 type_parameters_opt ::= type_parameters | ;
1200 LT type_parameter_list_1
1202 type_parameter_list ::=
1203 type_parameter_list COMMA type_parameter
1206 type_parameter_list_1 ::=
1208 | type_parameter_list COMMA type_parameter_1
1211 type_variable type_bound_opt
1213 type_parameter_1 ::=
1215 | type_variable type_bound_1
1217 type_bound_opt ::= type_bound | ;
1219 EXTENDS reference_type additional_bound_list_opt
1222 EXTENDS reference_type_1
1223 | EXTENDS reference_type additional_bound_list_1
1225 additional_bound_list_opt ::= additional_bound_list | ;
1226 additional_bound_list ::=
1227 additional_bound additional_bound_list
1230 additional_bound_list_1 ::=
1231 additional_bound additional_bound_list_1
1232 | additional_bound_1
1234 additional_bound ::=
1237 additional_bound_1 ::=
1238 AND reference_type_1
1240 //////////////////////////////////////////////
1241 // the following productions are copied from the standard ones, but
1242 // 'name' all alone is not allowed. The '_nn' stands for 'not name'.
1243 // we also expand the productions so that they recursively depend on the
1244 // '_nn' forms of their left hand side, then adding a new production
1245 // with 'name' explicit on the left-hand side.
1246 // this allows us to postpone the decision whether '(x)' is an expression
1247 // or a type-cast until we can see enough right context to make the proper
1249 postfix_expression_nn ::=
1251 // the 'name' production was removed here.
1252 | postincrement_expression
1253 | postdecrement_expression
1255 unary_expression_nn ::=
1256 preincrement_expression
1257 | predecrement_expression
1258 | PLUS unary_expression
1259 | MINUS unary_expression
1260 | unary_expression_not_plus_minus_nn
1262 unary_expression_not_plus_minus_nn ::=
1263 postfix_expression_nn
1264 | COMP unary_expression
1265 | NOT unary_expression
1268 multiplicative_expression_nn ::=
1270 | name MULT unary_expression
1271 | multiplicative_expression_nn MULT unary_expression
1272 | name DIV unary_expression
1273 | multiplicative_expression_nn DIV unary_expression
1274 | name MOD unary_expression
1275 | multiplicative_expression_nn MOD unary_expression
1277 additive_expression_nn ::=
1278 multiplicative_expression_nn
1279 | name PLUS multiplicative_expression
1280 | additive_expression_nn PLUS multiplicative_expression
1281 | name MINUS multiplicative_expression
1282 | additive_expression_nn MINUS multiplicative_expression
1284 shift_expression_nn ::=
1285 additive_expression_nn
1286 | name LSHIFT additive_expression
1287 | shift_expression_nn LSHIFT additive_expression
1288 | name RSHIFT additive_expression
1289 | shift_expression_nn RSHIFT additive_expression
1290 | name URSHIFT additive_expression
1291 | shift_expression_nn URSHIFT additive_expression
1293 relational_expression_nn ::=
1295 // note that we've tweaked the productions for LT/GT to disallow
1296 // a<b<c as a valid expression. This avoids ambiguity with
1297 // parameterized types in casts.
1298 | name LT shift_expression
1299 | shift_expression_nn LT shift_expression
1300 | name GT shift_expression
1301 | shift_expression_nn GT shift_expression
1302 | name LTEQ shift_expression
1303 | relational_expression_nn LTEQ shift_expression
1304 | name GTEQ shift_expression
1305 | relational_expression_nn GTEQ shift_expression
1307 instanceof_expression_nn ::=
1308 relational_expression_nn
1309 | name INSTANCEOF reference_type
1310 | instanceof_expression_nn INSTANCEOF reference_type
1312 equality_expression_nn ::=
1313 instanceof_expression_nn
1314 | name EQEQ instanceof_expression
1315 | equality_expression_nn EQEQ instanceof_expression
1316 | name NOTEQ instanceof_expression
1317 | equality_expression_nn NOTEQ instanceof_expression
1319 and_expression_nn ::=
1320 equality_expression_nn
1321 | name AND equality_expression
1322 | and_expression_nn AND equality_expression
1324 exclusive_or_expression_nn ::=
1326 | name XOR and_expression
1327 | exclusive_or_expression_nn XOR and_expression
1329 inclusive_or_expression_nn ::=
1330 exclusive_or_expression_nn
1331 | name OR exclusive_or_expression
1332 | inclusive_or_expression_nn OR exclusive_or_expression
1334 conditional_and_expression_nn ::=
1335 inclusive_or_expression_nn
1336 | name ANDAND inclusive_or_expression
1337 | conditional_and_expression_nn ANDAND inclusive_or_expression
1339 conditional_or_expression_nn ::=
1340 conditional_and_expression_nn
1341 | name OROR conditional_and_expression
1342 | conditional_or_expression_nn OROR conditional_and_expression
1344 conditional_expression_nn ::=
1345 conditional_or_expression_nn
1346 | name QUESTION expression COLON conditional_expression
1347 | conditional_or_expression_nn QUESTION expression
1348 COLON conditional_expression
1350 assignment_expression_nn ::=
1351 conditional_expression_nn
1354 expression_nn ::= assignment_expression_nn