3 import java_cup.runtime.*;
5 /* Java 1.4 parser for CUP.
6 * Copyright (C) 2002-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.4 Features added:
14 statement_without_trailing_substatement ::= ...
17 ASSERT expression SEMICOLON
18 | ASSERT expression COLON expression SEMICOLON
24 public Grm14(Lexer l) {
29 public void syntax_error(java_cup.runtime.Symbol current) {
30 report_error("Syntax error (" + current.sym + ")", current);
32 public void report_error(String message, java_cup.runtime.Symbol info) {
33 lexer.errorMsg(message, info);
37 scan with {: return lexer.nextToken(); :};
39 terminal BOOLEAN; // primitive_type
40 terminal BYTE, SHORT, INT, LONG, CHAR; // integral_type
41 terminal FLOAT, DOUBLE; // floating_point_type
42 terminal LBRACK, RBRACK; // array_type
43 terminal java.lang.String IDENTIFIER; // name
44 terminal DOT; // qualified_name
45 terminal SEMICOLON, MULT, COMMA, LBRACE, RBRACE, EQ, LPAREN, RPAREN, COLON;
46 terminal PACKAGE; // package_declaration
47 terminal IMPORT; // import_declaration
48 terminal PUBLIC, PROTECTED, PRIVATE; // modifier
49 terminal STATIC; // modifier
50 terminal ABSTRACT, FINAL, NATIVE, SYNCHRONIZED, TRANSIENT, VOLATILE;
51 terminal CLASS; // class_declaration
52 terminal EXTENDS; // super
53 terminal IMPLEMENTS; // interfaces
54 terminal VOID; // method_header
55 terminal THROWS; // throws
56 terminal THIS, SUPER; // explicit_constructor_invocation
57 terminal INTERFACE; // interface_declaration
58 terminal IF, ELSE; // if_then_statement, if_then_else_statement
59 terminal SWITCH; // switch_statement
60 terminal CASE, DEFAULT; // switch_label
61 terminal DO, WHILE; // while_statement, do_statement
62 terminal FOR; // for_statement
63 terminal BREAK; // break_statement
64 terminal CONTINUE; // continue_statement
65 terminal RETURN; // return_statement
66 terminal THROW; // throw_statement
67 terminal TRY; // try_statement
68 terminal CATCH; // catch_clause
69 terminal FINALLY; // finally
70 terminal NEW; // class_instance_creation_expression
71 terminal PLUSPLUS; // postincrement_expression
72 terminal MINUSMINUS; // postdecrement_expression
73 terminal PLUS, MINUS, COMP, NOT, DIV, MOD;
74 terminal LSHIFT, RSHIFT, URSHIFT; // shift_expression
75 terminal LT, GT, LTEQ, GTEQ, INSTANCEOF; // relational_expression
76 terminal EQEQ, NOTEQ; // equality_expression
77 terminal AND; // and_expression
78 terminal XOR; // exclusive_or_expression
79 terminal OR; // inclusive_or_expression
80 terminal ANDAND; // conditional_and_expression
81 terminal OROR; // conditional_or_expression
82 terminal QUESTION; // conditional_expression
83 terminal MULTEQ, DIVEQ, MODEQ, PLUSEQ, MINUSEQ; // assignment_operator
84 terminal LSHIFTEQ, RSHIFTEQ, URSHIFTEQ; // assignment_operator
85 terminal ANDEQ, XOREQ, OREQ; // assignment_operator
87 terminal java.lang.Number INTEGER_LITERAL;
88 terminal java.lang.Number FLOATING_POINT_LITERAL;
89 terminal java.lang.Boolean BOOLEAN_LITERAL;
90 terminal java.lang.Character CHARACTER_LITERAL;
91 terminal java.lang.String STRING_LITERAL;
92 terminal NULL_LITERAL;
94 // Reserved but unused:
96 // strictfp keyword, new in Java 1.2
98 // assert keyword, new in Java 1.4
99 terminal ASSERT; // assert_statement
100 // lexer compatibility with Java 1.5
104 // 19.2) The Syntactic Grammar
106 // 19.3) Lexical Structure
107 non terminal literal;
108 // 19.4) Types, Values, and Variables
109 non terminal type, primitive_type, numeric_type;
110 non terminal integral_type, floating_point_type;
111 non terminal reference_type;
112 non terminal class_or_interface_type;
113 non terminal class_type, interface_type;
114 non terminal array_type;
116 non terminal name, simple_name, qualified_name;
118 non terminal compilation_unit;
119 non terminal package_declaration_opt, package_declaration;
120 non terminal import_declarations_opt, import_declarations;
121 non terminal type_declarations_opt, type_declarations;
122 non terminal import_declaration;
123 non terminal single_type_import_declaration;
124 non terminal type_import_on_demand_declaration;
125 non terminal type_declaration;
126 // 19.7) Productions used only in the LALR(1) grammar
127 non terminal modifiers_opt, modifiers, modifier;
128 // 19.8.1) Class Declaration
129 non terminal class_declaration, super, super_opt;
130 non terminal interfaces, interfaces_opt, interface_type_list;
131 non terminal class_body;
132 non terminal class_body_declarations, class_body_declarations_opt;
133 non terminal class_body_declaration, class_member_declaration;
134 // 19.8.2) Field Declarations
135 non terminal field_declaration, variable_declarators, variable_declarator;
136 non terminal variable_declarator_id, variable_initializer;
137 // 19.8.3) Method Declarations
138 non terminal method_declaration, method_header, method_declarator;
139 non terminal formal_parameter_list_opt, formal_parameter_list;
140 non terminal formal_parameter;
141 non terminal throws_opt, throws;
142 non terminal class_type_list, method_body;
143 // 19.8.4) Static Initializers
144 non terminal static_initializer;
145 // 19.8.5) Constructor Declarations
146 non terminal constructor_declaration, constructor_declarator;
147 non terminal constructor_body;
148 non terminal explicit_constructor_invocation;
149 // 19.9.1) Interface Declarations
150 non terminal interface_declaration;
151 non terminal extends_interfaces_opt, extends_interfaces;
152 non terminal interface_body;
153 non terminal interface_member_declarations_opt, interface_member_declarations;
154 non terminal interface_member_declaration, constant_declaration;
155 non terminal abstract_method_declaration;
157 non terminal array_initializer;
158 non terminal variable_initializers;
159 // 19.11) Blocks and Statements
161 non terminal block_statements_opt, block_statements, block_statement;
162 non terminal local_variable_declaration_statement, local_variable_declaration;
163 non terminal statement, statement_no_short_if;
164 non terminal statement_without_trailing_substatement;
165 non terminal empty_statement;
166 non terminal labeled_statement, labeled_statement_no_short_if;
167 non terminal expression_statement, statement_expression;
168 non terminal if_then_statement;
169 non terminal if_then_else_statement, if_then_else_statement_no_short_if;
170 non terminal switch_statement, switch_block;
171 non terminal switch_block_statement_groups;
172 non terminal switch_block_statement_group;
173 non terminal switch_labels, switch_label;
174 non terminal while_statement, while_statement_no_short_if;
175 non terminal do_statement;
176 non terminal for_statement, for_statement_no_short_if;
177 non terminal for_init_opt, for_init;
178 non terminal for_update_opt, for_update;
179 non terminal statement_expression_list;
180 non terminal identifier_opt;
181 non terminal break_statement, continue_statement;
182 non terminal return_statement, throw_statement;
183 non terminal synchronized_statement, try_statement;
184 non terminal catches_opt, catches, catch_clause;
185 non terminal finally;
186 non terminal assert_statement;
187 // 19.12) Expressions
188 non terminal primary, primary_no_new_array;
189 non terminal class_instance_creation_expression;
190 non terminal argument_list_opt, argument_list;
191 non terminal array_creation_init, array_creation_uninit;
192 non terminal dim_exprs, dim_expr, dims_opt, dims;
193 non terminal field_access, method_invocation, array_access;
194 non terminal postfix_expression;
195 non terminal postincrement_expression, postdecrement_expression;
196 non terminal unary_expression, unary_expression_not_plus_minus;
197 non terminal preincrement_expression, predecrement_expression;
198 non terminal cast_expression;
199 non terminal multiplicative_expression, additive_expression;
200 non terminal shift_expression, relational_expression, equality_expression;
201 non terminal and_expression, exclusive_or_expression, inclusive_or_expression;
202 non terminal conditional_and_expression, conditional_or_expression;
203 non terminal conditional_expression, assignment_expression;
204 non terminal assignment;
205 non terminal assignment_operator;
206 non terminal expression_opt, expression;
207 non terminal constant_expression;
211 // 19.2) The Syntactic Grammar
212 goal ::= compilation_unit
215 // 19.3) Lexical Structure.
216 literal ::= INTEGER_LITERAL
217 | FLOATING_POINT_LITERAL
224 // 19.4) Types, Values, and Variables
225 type ::= primitive_type
232 numeric_type::= integral_type
233 | floating_point_type
242 floating_point_type ::=
248 class_or_interface_type
251 class_or_interface_type ::= name;
253 class_type ::= class_or_interface_type;
254 interface_type ::= class_or_interface_type;
256 array_type ::= primitive_type dims
264 simple_name ::= IDENTIFIER
272 package_declaration_opt
273 import_declarations_opt
274 type_declarations_opt
276 package_declaration_opt ::= package_declaration | ;
277 import_declarations_opt ::= import_declarations | ;
278 type_declarations_opt ::= type_declarations | ;
280 import_declarations ::=
282 | import_declarations import_declaration
284 type_declarations ::=
286 | type_declarations type_declaration
288 package_declaration ::=
289 PACKAGE name SEMICOLON
291 import_declaration ::=
292 single_type_import_declaration
293 | type_import_on_demand_declaration
295 single_type_import_declaration ::=
296 IMPORT name SEMICOLON
298 type_import_on_demand_declaration ::=
299 IMPORT name DOT MULT SEMICOLON
303 | interface_declaration
307 // 19.7) Productions used only in the LALR(1) grammar
311 modifiers ::= modifier
314 modifier ::= PUBLIC | PROTECTED | PRIVATE
316 | ABSTRACT | FINAL | NATIVE | SYNCHRONIZED | TRANSIENT | VOLATILE
317 | STRICTFP // note that semantic analysis must check that the
318 // context of the modifier allows strictfp.
323 // 19.8.1) Class Declaration:
324 class_declaration ::=
325 modifiers_opt CLASS IDENTIFIER super_opt interfaces_opt class_body
327 super ::= EXTENDS class_type
332 interfaces ::= IMPLEMENTS interface_type_list
337 interface_type_list ::=
339 | interface_type_list COMMA interface_type
341 class_body ::= LBRACE class_body_declarations_opt RBRACE
343 class_body_declarations_opt ::=
344 | class_body_declarations ;
345 class_body_declarations ::=
346 class_body_declaration
347 | class_body_declarations class_body_declaration
349 class_body_declaration ::=
350 class_member_declaration
352 | constructor_declaration
355 class_member_declaration ::=
358 /* repeat the prod for 'class_declaration' here: */
359 | modifiers_opt CLASS IDENTIFIER super_opt interfaces_opt class_body
360 | interface_declaration
364 // 19.8.2) Field Declarations
365 field_declaration ::=
366 modifiers_opt type variable_declarators SEMICOLON
368 variable_declarators ::=
370 | variable_declarators COMMA variable_declarator
372 variable_declarator ::=
373 variable_declarator_id
374 | variable_declarator_id EQ variable_initializer
376 variable_declarator_id ::=
378 | variable_declarator_id LBRACK RBRACK
380 variable_initializer ::=
385 // 19.8.3) Method Declarations
386 method_declaration ::=
387 method_header method_body
390 modifiers_opt type method_declarator throws_opt
391 | modifiers_opt VOID method_declarator throws_opt
393 method_declarator ::=
394 IDENTIFIER LPAREN formal_parameter_list_opt RPAREN
395 | method_declarator LBRACK RBRACK // deprecated
396 // be careful; the above production also allows 'void foo() []'
398 formal_parameter_list_opt ::=
399 | formal_parameter_list
401 formal_parameter_list ::=
403 | formal_parameter_list COMMA formal_parameter
406 type variable_declarator_id
407 | FINAL type variable_declarator_id
412 throws ::= THROWS class_type_list
416 | class_type_list COMMA class_type
418 method_body ::= block
422 // 19.8.4) Static Initializers
423 static_initializer ::=
427 // 19.8.5) Constructor Declarations
428 constructor_declaration ::=
429 modifiers_opt constructor_declarator throws_opt
432 constructor_declarator ::=
433 simple_name LPAREN formal_parameter_list_opt RPAREN
436 LBRACE explicit_constructor_invocation
437 block_statements RBRACE
438 | LBRACE explicit_constructor_invocation RBRACE
439 | LBRACE block_statements RBRACE
442 explicit_constructor_invocation ::=
443 THIS LPAREN argument_list_opt RPAREN SEMICOLON
444 | SUPER LPAREN argument_list_opt RPAREN SEMICOLON
445 | primary DOT THIS LPAREN argument_list_opt RPAREN SEMICOLON
446 | primary DOT SUPER LPAREN argument_list_opt RPAREN SEMICOLON
451 // 19.9.1) Interface Declarations
452 interface_declaration ::=
453 modifiers_opt INTERFACE IDENTIFIER extends_interfaces_opt
456 extends_interfaces_opt ::=
459 extends_interfaces ::=
460 EXTENDS interface_type
461 | extends_interfaces COMMA interface_type
464 LBRACE interface_member_declarations_opt RBRACE
466 interface_member_declarations_opt ::=
467 | interface_member_declarations
469 interface_member_declarations ::=
470 interface_member_declaration
471 | interface_member_declarations interface_member_declaration
473 interface_member_declaration ::=
475 | abstract_method_declaration
477 | interface_declaration
480 constant_declaration ::=
482 // need to semantically check that modifiers of field declaration
483 // include only PUBLIC, STATIC, or FINAL. Other modifiers are
486 abstract_method_declaration ::=
487 method_header SEMICOLON
491 array_initializer ::=
492 LBRACE variable_initializers COMMA RBRACE
493 | LBRACE variable_initializers RBRACE
494 | LBRACE COMMA RBRACE
497 variable_initializers ::=
499 | variable_initializers COMMA variable_initializer
502 // 19.11) Blocks and Statements
503 block ::= LBRACE block_statements_opt RBRACE
505 block_statements_opt ::=
510 | block_statements block_statement
513 local_variable_declaration_statement
516 | interface_declaration
518 local_variable_declaration_statement ::=
519 local_variable_declaration SEMICOLON
521 local_variable_declaration ::=
522 type variable_declarators
523 | FINAL type variable_declarators
525 statement ::= statement_without_trailing_substatement
528 | if_then_else_statement
532 statement_no_short_if ::=
533 statement_without_trailing_substatement
534 | labeled_statement_no_short_if
535 | if_then_else_statement_no_short_if
536 | while_statement_no_short_if
537 | for_statement_no_short_if
539 statement_without_trailing_substatement ::=
542 | expression_statement
548 | synchronized_statement
556 labeled_statement ::=
557 IDENTIFIER COLON statement
559 labeled_statement_no_short_if ::=
560 IDENTIFIER COLON statement_no_short_if
562 expression_statement ::=
563 statement_expression SEMICOLON
565 statement_expression ::=
567 | preincrement_expression
568 | predecrement_expression
569 | postincrement_expression
570 | postdecrement_expression
572 | class_instance_creation_expression
574 if_then_statement ::=
575 IF LPAREN expression RPAREN statement
577 if_then_else_statement ::=
578 IF LPAREN expression RPAREN statement_no_short_if
581 if_then_else_statement_no_short_if ::=
582 IF LPAREN expression RPAREN statement_no_short_if
583 ELSE statement_no_short_if
586 SWITCH LPAREN expression RPAREN switch_block
589 LBRACE switch_block_statement_groups switch_labels RBRACE
590 | LBRACE switch_block_statement_groups RBRACE
591 | LBRACE switch_labels RBRACE
594 switch_block_statement_groups ::=
595 switch_block_statement_group
596 | switch_block_statement_groups switch_block_statement_group
598 switch_block_statement_group ::=
599 switch_labels block_statements
603 | switch_labels switch_label
606 CASE constant_expression COLON
611 WHILE LPAREN expression RPAREN statement
613 while_statement_no_short_if ::=
614 WHILE LPAREN expression RPAREN statement_no_short_if
617 DO statement WHILE LPAREN expression RPAREN SEMICOLON
620 FOR LPAREN for_init_opt SEMICOLON expression_opt SEMICOLON
621 for_update_opt RPAREN statement
623 for_statement_no_short_if ::=
624 FOR LPAREN for_init_opt SEMICOLON expression_opt SEMICOLON
625 for_update_opt RPAREN statement_no_short_if
630 for_init ::= statement_expression_list
631 | local_variable_declaration
636 for_update ::= statement_expression_list
638 statement_expression_list ::=
640 | statement_expression_list COMMA statement_expression
648 BREAK identifier_opt SEMICOLON
651 continue_statement ::=
652 CONTINUE identifier_opt SEMICOLON
655 RETURN expression_opt SEMICOLON
658 THROW expression SEMICOLON
660 synchronized_statement ::=
661 SYNCHRONIZED LPAREN expression RPAREN block
665 | TRY block catches_opt finally
670 catches ::= catch_clause
671 | catches catch_clause
674 CATCH LPAREN formal_parameter RPAREN block
676 finally ::= FINALLY block
679 ASSERT expression SEMICOLON
680 | ASSERT expression COLON expression SEMICOLON
683 // 19.12) Expressions
684 primary ::= primary_no_new_array
685 | array_creation_init
686 | array_creation_uninit
688 primary_no_new_array ::=
691 | LPAREN expression RPAREN
692 | class_instance_creation_expression
696 | primitive_type DOT CLASS
698 | array_type DOT CLASS
702 class_instance_creation_expression ::=
703 NEW class_or_interface_type LPAREN argument_list_opt RPAREN
704 | NEW class_or_interface_type LPAREN argument_list_opt RPAREN class_body
705 | primary DOT NEW IDENTIFIER
706 LPAREN argument_list_opt RPAREN
707 | primary DOT NEW IDENTIFIER
708 LPAREN argument_list_opt RPAREN class_body
709 | name DOT NEW IDENTIFIER
710 LPAREN argument_list_opt RPAREN
711 | name DOT NEW IDENTIFIER
712 LPAREN argument_list_opt RPAREN class_body
714 argument_list_opt ::=
719 | argument_list COMMA expression
721 array_creation_uninit ::=
722 NEW primitive_type dim_exprs dims_opt
723 | NEW class_or_interface_type dim_exprs dims_opt
725 array_creation_init ::=
726 NEW primitive_type dims array_initializer
727 | NEW class_or_interface_type dims array_initializer
729 dim_exprs ::= dim_expr
732 dim_expr ::= LBRACK expression RBRACK
737 dims ::= LBRACK RBRACK
741 primary DOT IDENTIFIER
742 | SUPER DOT IDENTIFIER
743 | name DOT SUPER DOT IDENTIFIER
745 method_invocation ::=
746 name LPAREN argument_list_opt RPAREN
747 | primary DOT IDENTIFIER LPAREN argument_list_opt RPAREN
748 | SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
749 | name DOT SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
752 name LBRACK expression RBRACK
753 | primary_no_new_array LBRACK expression RBRACK
754 | array_creation_init LBRACK expression RBRACK
756 postfix_expression ::=
759 | postincrement_expression
760 | postdecrement_expression
762 postincrement_expression ::=
763 postfix_expression PLUSPLUS
765 postdecrement_expression ::=
766 postfix_expression MINUSMINUS
769 preincrement_expression
770 | predecrement_expression
771 | PLUS unary_expression
772 | MINUS unary_expression
773 | unary_expression_not_plus_minus
775 preincrement_expression ::=
776 PLUSPLUS unary_expression
778 predecrement_expression ::=
779 MINUSMINUS unary_expression
781 unary_expression_not_plus_minus ::=
783 | COMP unary_expression
784 | NOT unary_expression
788 LPAREN primitive_type dims_opt RPAREN unary_expression
789 | LPAREN expression RPAREN unary_expression_not_plus_minus
790 | LPAREN name dims RPAREN unary_expression_not_plus_minus
792 multiplicative_expression ::=
794 | multiplicative_expression MULT unary_expression
795 | multiplicative_expression DIV unary_expression
796 | multiplicative_expression MOD unary_expression
798 additive_expression ::=
799 multiplicative_expression
800 | additive_expression PLUS multiplicative_expression
801 | additive_expression MINUS multiplicative_expression
805 | shift_expression LSHIFT additive_expression
806 | shift_expression RSHIFT additive_expression
807 | shift_expression URSHIFT additive_expression
809 relational_expression ::=
811 | relational_expression LT shift_expression
812 | relational_expression GT shift_expression
813 | relational_expression LTEQ shift_expression
814 | relational_expression GTEQ shift_expression
815 | relational_expression INSTANCEOF reference_type
817 equality_expression ::=
818 relational_expression
819 | equality_expression EQEQ relational_expression
820 | equality_expression NOTEQ relational_expression
824 | and_expression AND equality_expression
826 exclusive_or_expression ::=
828 | exclusive_or_expression XOR and_expression
830 inclusive_or_expression ::=
831 exclusive_or_expression
832 | inclusive_or_expression OR exclusive_or_expression
834 conditional_and_expression ::=
835 inclusive_or_expression
836 | conditional_and_expression ANDAND inclusive_or_expression
838 conditional_or_expression ::=
839 conditional_and_expression
840 | conditional_or_expression OROR conditional_and_expression
842 conditional_expression ::=
843 conditional_or_expression
844 | conditional_or_expression QUESTION expression
845 COLON conditional_expression
847 assignment_expression ::=
848 conditional_expression
851 // semantic check necessary here to ensure a valid left-hand side.
852 // allowing a parenthesized variable here on the lhs was introduced in
853 // JLS 2; thanks to Eric Blake for pointing this out.
854 assignment ::= postfix_expression assignment_operator assignment_expression
856 assignment_operator ::=
873 expression ::= assignment_expression
875 constant_expression ::=