3 import java_cup.runtime.*;
5 /* Java 1.2 parser for CUP.
6 * Copyright (C) 1998-2003 C. Scott Ananian <cananian@alumni.princeton.edu>
7 * This program is released under the terms of the GPL; see the file
8 * COPYING for more details. There is NO WARRANTY on this code.
12 JDK 1.2 Features added:
14 explicit_constructor_invocation ::= ...
15 | primary DOT THIS LPAREN argument_list_opt RPAREN SEMICOLON ;
17 | name DOT SUPER DOT IDENTIFIER ;
18 method_invocation ::= ...
19 | name DOT SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN ;
20 parenthesized expression, plain identifiers qualifying instance
21 creation and explicit constructors, and array creation expression
22 fixes from JLS2 (thanks to Eric Blake for pointing these out)
27 public Grm12(Lexer l) {
32 public void syntax_error(java_cup.runtime.Symbol current) {
33 report_error("Syntax error (" + current.sym + ")", current);
35 public void report_error(String message, java_cup.runtime.Symbol info) {
36 lexer.errorMsg(message, info);
40 scan with {: return lexer.nextToken(); :};
42 terminal BOOLEAN; // primitive_type
43 terminal BYTE, SHORT, INT, LONG, CHAR; // integral_type
44 terminal FLOAT, DOUBLE; // floating_point_type
45 terminal LBRACK, RBRACK; // array_type
46 terminal java.lang.String IDENTIFIER; // name
47 terminal DOT; // qualified_name
48 terminal SEMICOLON, MULT, COMMA, LBRACE, RBRACE, EQ, LPAREN, RPAREN, COLON;
49 terminal PACKAGE; // package_declaration
50 terminal IMPORT; // import_declaration
51 terminal PUBLIC, PROTECTED, PRIVATE; // modifier
52 terminal STATIC; // modifier
53 terminal ABSTRACT, FINAL, NATIVE, SYNCHRONIZED, TRANSIENT, VOLATILE;
54 terminal CLASS; // class_declaration
55 terminal EXTENDS; // super
56 terminal IMPLEMENTS; // interfaces
57 terminal VOID; // method_header
58 terminal THROWS; // throws
59 terminal THIS, SUPER; // explicit_constructor_invocation
60 terminal INTERFACE; // interface_declaration
61 terminal IF, ELSE; // if_then_statement, if_then_else_statement
62 terminal SWITCH; // switch_statement
63 terminal CASE, DEFAULT; // switch_label
64 terminal DO, WHILE; // while_statement, do_statement
65 terminal FOR; // for_statement
66 terminal BREAK; // break_statement
67 terminal CONTINUE; // continue_statement
68 terminal RETURN; // return_statement
69 terminal THROW; // throw_statement
70 terminal TRY; // try_statement
71 terminal CATCH; // catch_clause
72 terminal FINALLY; // finally
73 terminal NEW; // class_instance_creation_expression
74 terminal PLUSPLUS; // postincrement_expression
75 terminal MINUSMINUS; // postdecrement_expression
76 terminal PLUS, MINUS, COMP, NOT, DIV, MOD;
77 terminal LSHIFT, RSHIFT, URSHIFT; // shift_expression
78 terminal LT, GT, LTEQ, GTEQ, INSTANCEOF; // relational_expression
79 terminal EQEQ, NOTEQ; // equality_expression
80 terminal AND; // and_expression
81 terminal XOR; // exclusive_or_expression
82 terminal OR; // inclusive_or_expression
83 terminal ANDAND; // conditional_and_expression
84 terminal OROR; // conditional_or_expression
85 terminal QUESTION; // conditional_expression
86 terminal MULTEQ, DIVEQ, MODEQ, PLUSEQ, MINUSEQ; // assignment_operator
87 terminal LSHIFTEQ, RSHIFTEQ, URSHIFTEQ; // assignment_operator
88 terminal ANDEQ, XOREQ, OREQ; // assignment_operator
90 terminal java.lang.Number INTEGER_LITERAL;
91 terminal java.lang.Number FLOATING_POINT_LITERAL;
92 terminal java.lang.Boolean BOOLEAN_LITERAL;
93 terminal java.lang.Character CHARACTER_LITERAL;
94 terminal java.lang.String STRING_LITERAL;
95 terminal NULL_LITERAL;
97 // Reserved but unused:
99 // strictfp keyword, new in Java 1.2
101 // lexer compatibility with Java 1.4
103 // lexer compatibility with Java 1.5
107 // 19.2) The Syntactic Grammar
109 // 19.3) Lexical Structure
110 non terminal literal;
111 // 19.4) Types, Values, and Variables
112 non terminal type, primitive_type, numeric_type;
113 non terminal integral_type, floating_point_type;
114 non terminal reference_type;
115 non terminal class_or_interface_type;
116 non terminal class_type, interface_type;
117 non terminal array_type;
119 non terminal name, simple_name, qualified_name;
121 non terminal compilation_unit;
122 non terminal package_declaration_opt, package_declaration;
123 non terminal import_declarations_opt, import_declarations;
124 non terminal type_declarations_opt, type_declarations;
125 non terminal import_declaration;
126 non terminal single_type_import_declaration;
127 non terminal type_import_on_demand_declaration;
128 non terminal type_declaration;
129 // 19.7) Productions used only in the LALR(1) grammar
130 non terminal modifiers_opt, modifiers, modifier;
131 // 19.8.1) Class Declaration
132 non terminal class_declaration, super, super_opt;
133 non terminal interfaces, interfaces_opt, interface_type_list;
134 non terminal class_body;
135 non terminal class_body_declarations, class_body_declarations_opt;
136 non terminal class_body_declaration, class_member_declaration;
137 // 19.8.2) Field Declarations
138 non terminal field_declaration, variable_declarators, variable_declarator;
139 non terminal variable_declarator_id, variable_initializer;
140 // 19.8.3) Method Declarations
141 non terminal method_declaration, method_header, method_declarator;
142 non terminal formal_parameter_list_opt, formal_parameter_list;
143 non terminal formal_parameter;
144 non terminal throws_opt, throws;
145 non terminal class_type_list, method_body;
146 // 19.8.4) Static Initializers
147 non terminal static_initializer;
148 // 19.8.5) Constructor Declarations
149 non terminal constructor_declaration, constructor_declarator;
150 non terminal constructor_body;
151 non terminal explicit_constructor_invocation;
152 // 19.9.1) Interface Declarations
153 non terminal interface_declaration;
154 non terminal extends_interfaces_opt, extends_interfaces;
155 non terminal interface_body;
156 non terminal interface_member_declarations_opt, interface_member_declarations;
157 non terminal interface_member_declaration, constant_declaration;
158 non terminal abstract_method_declaration;
160 non terminal array_initializer;
161 non terminal variable_initializers;
162 // 19.11) Blocks and Statements
164 non terminal block_statements_opt, block_statements, block_statement;
165 non terminal local_variable_declaration_statement, local_variable_declaration;
166 non terminal statement, statement_no_short_if;
167 non terminal statement_without_trailing_substatement;
168 non terminal empty_statement;
169 non terminal labeled_statement, labeled_statement_no_short_if;
170 non terminal expression_statement, statement_expression;
171 non terminal if_then_statement;
172 non terminal if_then_else_statement, if_then_else_statement_no_short_if;
173 non terminal switch_statement, switch_block;
174 non terminal switch_block_statement_groups;
175 non terminal switch_block_statement_group;
176 non terminal switch_labels, switch_label;
177 non terminal while_statement, while_statement_no_short_if;
178 non terminal do_statement;
179 non terminal for_statement, for_statement_no_short_if;
180 non terminal for_init_opt, for_init;
181 non terminal for_update_opt, for_update;
182 non terminal statement_expression_list;
183 non terminal identifier_opt;
184 non terminal break_statement, continue_statement;
185 non terminal return_statement, throw_statement;
186 non terminal synchronized_statement, try_statement;
187 non terminal catches_opt, catches, catch_clause;
188 non terminal finally;
189 // 19.12) Expressions
190 non terminal primary, primary_no_new_array;
191 non terminal class_instance_creation_expression;
192 non terminal argument_list_opt, argument_list;
193 non terminal array_creation_init, array_creation_uninit;
194 non terminal dim_exprs, dim_expr, dims_opt, dims;
195 non terminal field_access, method_invocation, array_access;
196 non terminal postfix_expression;
197 non terminal postincrement_expression, postdecrement_expression;
198 non terminal unary_expression, unary_expression_not_plus_minus;
199 non terminal preincrement_expression, predecrement_expression;
200 non terminal cast_expression;
201 non terminal multiplicative_expression, additive_expression;
202 non terminal shift_expression, relational_expression, equality_expression;
203 non terminal and_expression, exclusive_or_expression, inclusive_or_expression;
204 non terminal conditional_and_expression, conditional_or_expression;
205 non terminal conditional_expression, assignment_expression;
206 non terminal assignment;
207 non terminal assignment_operator;
208 non terminal expression_opt, expression;
209 non terminal constant_expression;
213 // 19.2) The Syntactic Grammar
214 goal ::= compilation_unit
217 // 19.3) Lexical Structure.
218 literal ::= INTEGER_LITERAL
219 | FLOATING_POINT_LITERAL
226 // 19.4) Types, Values, and Variables
227 type ::= primitive_type
234 numeric_type::= integral_type
235 | floating_point_type
244 floating_point_type ::=
250 class_or_interface_type
253 class_or_interface_type ::= name;
255 class_type ::= class_or_interface_type;
256 interface_type ::= class_or_interface_type;
258 array_type ::= primitive_type dims
266 simple_name ::= IDENTIFIER
274 package_declaration_opt
275 import_declarations_opt
276 type_declarations_opt
278 package_declaration_opt ::= package_declaration | ;
279 import_declarations_opt ::= import_declarations | ;
280 type_declarations_opt ::= type_declarations | ;
282 import_declarations ::=
284 | import_declarations import_declaration
286 type_declarations ::=
288 | type_declarations type_declaration
290 package_declaration ::=
291 PACKAGE name SEMICOLON
293 import_declaration ::=
294 single_type_import_declaration
295 | type_import_on_demand_declaration
297 single_type_import_declaration ::=
298 IMPORT name SEMICOLON
300 type_import_on_demand_declaration ::=
301 IMPORT name DOT MULT SEMICOLON
305 | interface_declaration
309 // 19.7) Productions used only in the LALR(1) grammar
313 modifiers ::= modifier
316 modifier ::= PUBLIC | PROTECTED | PRIVATE
318 | ABSTRACT | FINAL | NATIVE | SYNCHRONIZED | TRANSIENT | VOLATILE
319 | STRICTFP // note that semantic analysis must check that the
320 // context of the modifier allows strictfp.
325 // 19.8.1) Class Declaration:
326 class_declaration ::=
327 modifiers_opt CLASS IDENTIFIER super_opt interfaces_opt class_body
329 super ::= EXTENDS class_type
334 interfaces ::= IMPLEMENTS interface_type_list
339 interface_type_list ::=
341 | interface_type_list COMMA interface_type
343 class_body ::= LBRACE class_body_declarations_opt RBRACE
345 class_body_declarations_opt ::=
346 | class_body_declarations ;
347 class_body_declarations ::=
348 class_body_declaration
349 | class_body_declarations class_body_declaration
351 class_body_declaration ::=
352 class_member_declaration
354 | constructor_declaration
357 class_member_declaration ::=
360 /* repeat the prod for 'class_declaration' here: */
361 | modifiers_opt CLASS IDENTIFIER super_opt interfaces_opt class_body
362 | interface_declaration
366 // 19.8.2) Field Declarations
367 field_declaration ::=
368 modifiers_opt type variable_declarators SEMICOLON
370 variable_declarators ::=
372 | variable_declarators COMMA variable_declarator
374 variable_declarator ::=
375 variable_declarator_id
376 | variable_declarator_id EQ variable_initializer
378 variable_declarator_id ::=
380 | variable_declarator_id LBRACK RBRACK
382 variable_initializer ::=
387 // 19.8.3) Method Declarations
388 method_declaration ::=
389 method_header method_body
392 modifiers_opt type method_declarator throws_opt
393 | modifiers_opt VOID method_declarator throws_opt
395 method_declarator ::=
396 IDENTIFIER LPAREN formal_parameter_list_opt RPAREN
397 | method_declarator LBRACK RBRACK // deprecated
398 // be careful; the above production also allows 'void foo() []'
400 formal_parameter_list_opt ::=
401 | formal_parameter_list
403 formal_parameter_list ::=
405 | formal_parameter_list COMMA formal_parameter
408 type variable_declarator_id
409 | FINAL type variable_declarator_id
414 throws ::= THROWS class_type_list
418 | class_type_list COMMA class_type
420 method_body ::= block
424 // 19.8.4) Static Initializers
425 static_initializer ::=
429 // 19.8.5) Constructor Declarations
430 constructor_declaration ::=
431 modifiers_opt constructor_declarator throws_opt
434 constructor_declarator ::=
435 simple_name LPAREN formal_parameter_list_opt RPAREN
438 LBRACE explicit_constructor_invocation
439 block_statements RBRACE
440 | LBRACE explicit_constructor_invocation RBRACE
441 | LBRACE block_statements RBRACE
444 explicit_constructor_invocation ::=
445 THIS LPAREN argument_list_opt RPAREN SEMICOLON
446 | SUPER LPAREN argument_list_opt RPAREN SEMICOLON
447 | primary DOT SUPER LPAREN argument_list_opt RPAREN SEMICOLON
448 | name DOT SUPER LPAREN argument_list_opt RPAREN SEMICOLON
453 // 19.9.1) Interface Declarations
454 interface_declaration ::=
455 modifiers_opt INTERFACE IDENTIFIER extends_interfaces_opt
458 extends_interfaces_opt ::=
461 extends_interfaces ::=
462 EXTENDS interface_type
463 | extends_interfaces COMMA interface_type
466 LBRACE interface_member_declarations_opt RBRACE
468 interface_member_declarations_opt ::=
469 | interface_member_declarations
471 interface_member_declarations ::=
472 interface_member_declaration
473 | interface_member_declarations interface_member_declaration
475 interface_member_declaration ::=
477 | abstract_method_declaration
479 | interface_declaration
482 constant_declaration ::=
484 // need to semantically check that modifiers of field declaration
485 // include only PUBLIC, STATIC, or FINAL. Other modifiers are
488 abstract_method_declaration ::=
489 method_header SEMICOLON
493 array_initializer ::=
494 LBRACE variable_initializers COMMA RBRACE
495 | LBRACE variable_initializers RBRACE
496 | LBRACE COMMA RBRACE
499 variable_initializers ::=
501 | variable_initializers COMMA variable_initializer
504 // 19.11) Blocks and Statements
505 block ::= LBRACE block_statements_opt RBRACE
507 block_statements_opt ::=
512 | block_statements block_statement
515 local_variable_declaration_statement
518 | interface_declaration
520 local_variable_declaration_statement ::=
521 local_variable_declaration SEMICOLON
523 local_variable_declaration ::=
524 type variable_declarators
525 | FINAL type variable_declarators
527 statement ::= statement_without_trailing_substatement
530 | if_then_else_statement
534 statement_no_short_if ::=
535 statement_without_trailing_substatement
536 | labeled_statement_no_short_if
537 | if_then_else_statement_no_short_if
538 | while_statement_no_short_if
539 | for_statement_no_short_if
541 statement_without_trailing_substatement ::=
544 | expression_statement
550 | synchronized_statement
557 labeled_statement ::=
558 IDENTIFIER COLON statement
560 labeled_statement_no_short_if ::=
561 IDENTIFIER COLON statement_no_short_if
563 expression_statement ::=
564 statement_expression SEMICOLON
566 statement_expression ::=
568 | preincrement_expression
569 | predecrement_expression
570 | postincrement_expression
571 | postdecrement_expression
573 | class_instance_creation_expression
575 if_then_statement ::=
576 IF LPAREN expression RPAREN statement
578 if_then_else_statement ::=
579 IF LPAREN expression RPAREN statement_no_short_if
582 if_then_else_statement_no_short_if ::=
583 IF LPAREN expression RPAREN statement_no_short_if
584 ELSE statement_no_short_if
587 SWITCH LPAREN expression RPAREN switch_block
590 LBRACE switch_block_statement_groups switch_labels RBRACE
591 | LBRACE switch_block_statement_groups RBRACE
592 | LBRACE switch_labels RBRACE
595 switch_block_statement_groups ::=
596 switch_block_statement_group
597 | switch_block_statement_groups switch_block_statement_group
599 switch_block_statement_group ::=
600 switch_labels block_statements
604 | switch_labels switch_label
607 CASE constant_expression COLON
612 WHILE LPAREN expression RPAREN statement
614 while_statement_no_short_if ::=
615 WHILE LPAREN expression RPAREN statement_no_short_if
618 DO statement WHILE LPAREN expression RPAREN SEMICOLON
621 FOR LPAREN for_init_opt SEMICOLON expression_opt SEMICOLON
622 for_update_opt RPAREN statement
624 for_statement_no_short_if ::=
625 FOR LPAREN for_init_opt SEMICOLON expression_opt SEMICOLON
626 for_update_opt RPAREN statement_no_short_if
631 for_init ::= statement_expression_list
632 | local_variable_declaration
637 for_update ::= statement_expression_list
639 statement_expression_list ::=
641 | statement_expression_list COMMA statement_expression
649 BREAK identifier_opt SEMICOLON
652 continue_statement ::=
653 CONTINUE identifier_opt SEMICOLON
656 RETURN expression_opt SEMICOLON
659 THROW expression SEMICOLON
661 synchronized_statement ::=
662 SYNCHRONIZED LPAREN expression RPAREN block
666 | TRY block catches_opt finally
671 catches ::= catch_clause
672 | catches catch_clause
675 CATCH LPAREN formal_parameter RPAREN block
677 finally ::= FINALLY block
680 // 19.12) Expressions
681 primary ::= primary_no_new_array
682 | array_creation_init
683 | array_creation_uninit
685 primary_no_new_array ::=
688 | LPAREN expression RPAREN
689 | class_instance_creation_expression
693 | primitive_type DOT CLASS
695 | array_type DOT CLASS
699 class_instance_creation_expression ::=
700 NEW class_or_interface_type LPAREN argument_list_opt RPAREN
701 | NEW class_or_interface_type LPAREN argument_list_opt RPAREN class_body
702 | primary DOT NEW IDENTIFIER
703 LPAREN argument_list_opt RPAREN
704 | primary DOT NEW IDENTIFIER
705 LPAREN argument_list_opt RPAREN class_body
706 | name DOT NEW IDENTIFIER
707 LPAREN argument_list_opt RPAREN
708 | name DOT NEW IDENTIFIER
709 LPAREN argument_list_opt RPAREN class_body
711 argument_list_opt ::=
716 | argument_list COMMA expression
718 array_creation_uninit ::=
719 NEW primitive_type dim_exprs dims_opt
720 | NEW class_or_interface_type dim_exprs dims_opt
722 array_creation_init ::=
723 NEW primitive_type dims array_initializer
724 | NEW class_or_interface_type dims array_initializer
726 dim_exprs ::= dim_expr
729 dim_expr ::= LBRACK expression RBRACK
734 dims ::= LBRACK RBRACK
738 primary DOT IDENTIFIER
739 | SUPER DOT IDENTIFIER
740 | name DOT SUPER DOT IDENTIFIER
742 method_invocation ::=
743 name LPAREN argument_list_opt RPAREN
744 | primary DOT IDENTIFIER LPAREN argument_list_opt RPAREN
745 | SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
746 | name DOT SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
749 name LBRACK expression RBRACK
750 | primary_no_new_array LBRACK expression RBRACK
751 | array_creation_init LBRACK expression RBRACK
753 postfix_expression ::=
756 | postincrement_expression
757 | postdecrement_expression
759 postincrement_expression ::=
760 postfix_expression PLUSPLUS
762 postdecrement_expression ::=
763 postfix_expression MINUSMINUS
766 preincrement_expression
767 | predecrement_expression
768 | PLUS unary_expression
769 | MINUS unary_expression
770 | unary_expression_not_plus_minus
772 preincrement_expression ::=
773 PLUSPLUS unary_expression
775 predecrement_expression ::=
776 MINUSMINUS unary_expression
778 unary_expression_not_plus_minus ::=
780 | COMP unary_expression
781 | NOT unary_expression
785 LPAREN primitive_type dims_opt RPAREN unary_expression
786 | LPAREN expression RPAREN unary_expression_not_plus_minus
787 | LPAREN name dims RPAREN unary_expression_not_plus_minus
789 multiplicative_expression ::=
791 | multiplicative_expression MULT unary_expression
792 | multiplicative_expression DIV unary_expression
793 | multiplicative_expression MOD unary_expression
795 additive_expression ::=
796 multiplicative_expression
797 | additive_expression PLUS multiplicative_expression
798 | additive_expression MINUS multiplicative_expression
802 | shift_expression LSHIFT additive_expression
803 | shift_expression RSHIFT additive_expression
804 | shift_expression URSHIFT additive_expression
806 relational_expression ::=
808 | relational_expression LT shift_expression
809 | relational_expression GT shift_expression
810 | relational_expression LTEQ shift_expression
811 | relational_expression GTEQ shift_expression
812 | relational_expression INSTANCEOF reference_type
814 equality_expression ::=
815 relational_expression
816 | equality_expression EQEQ relational_expression
817 | equality_expression NOTEQ relational_expression
821 | and_expression AND equality_expression
823 exclusive_or_expression ::=
825 | exclusive_or_expression XOR and_expression
827 inclusive_or_expression ::=
828 exclusive_or_expression
829 | inclusive_or_expression OR exclusive_or_expression
831 conditional_and_expression ::=
832 inclusive_or_expression
833 | conditional_and_expression ANDAND inclusive_or_expression
835 conditional_or_expression ::=
836 conditional_and_expression
837 | conditional_or_expression OROR conditional_and_expression
839 conditional_expression ::=
840 conditional_or_expression
841 | conditional_or_expression QUESTION expression
842 COLON conditional_expression
844 assignment_expression ::=
845 conditional_expression
848 // semantic check necessary here to ensure a valid left-hand side.
849 // allowing a parenthesized variable here on the lhs was introduced in
850 // JLS 2; thanks to Eric Blake for pointing this out.
851 assignment ::= postfix_expression assignment_operator assignment_expression
853 assignment_operator ::=
870 expression ::= assignment_expression
872 constant_expression ::=