3 import java_cup.runtime.*;
5 /* Java 1.1 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.1 Features added:
14 anonymous array expressions.
16 blank finals; final local variables.
18 block ::= class/interface decl
19 primary_no_new_array ::= class_name DOT THIS
20 class_instance_creation_expresion ::= ...
21 explicit_constructor_invocation ::= ...
26 public Grm11(Lexer l) {
31 public void syntax_error(java_cup.runtime.Symbol current) {
32 report_error("Syntax error (" + current.sym + ")", current);
34 public void report_error(String message, java_cup.runtime.Symbol info) {
35 lexer.errorMsg(message, info);
39 scan with {: return lexer.nextToken(); :};
41 terminal BOOLEAN; // primitive_type
42 terminal BYTE, SHORT, INT, LONG, CHAR; // integral_type
43 terminal FLOAT, DOUBLE; // floating_point_type
44 terminal LBRACK, RBRACK; // array_type
45 terminal java.lang.String IDENTIFIER; // name
46 terminal DOT; // qualified_name
47 terminal SEMICOLON, MULT, COMMA, LBRACE, RBRACE, EQ, LPAREN, RPAREN, COLON;
48 terminal PACKAGE; // package_declaration
49 terminal IMPORT; // import_declaration
50 terminal PUBLIC, PROTECTED, PRIVATE; // modifier
51 terminal STATIC; // modifier
52 terminal ABSTRACT, FINAL, NATIVE, SYNCHRONIZED, TRANSIENT, VOLATILE;
53 terminal CLASS; // class_declaration
54 terminal EXTENDS; // super
55 terminal IMPLEMENTS; // interfaces
56 terminal VOID; // method_header
57 terminal THROWS; // throws
58 terminal THIS, SUPER; // explicit_constructor_invocation
59 terminal INTERFACE; // interface_declaration
60 terminal IF, ELSE; // if_then_statement, if_then_else_statement
61 terminal SWITCH; // switch_statement
62 terminal CASE, DEFAULT; // switch_label
63 terminal DO, WHILE; // while_statement, do_statement
64 terminal FOR; // for_statement
65 terminal BREAK; // break_statement
66 terminal CONTINUE; // continue_statement
67 terminal RETURN; // return_statement
68 terminal THROW; // throw_statement
69 terminal TRY; // try_statement
70 terminal CATCH; // catch_clause
71 terminal FINALLY; // finally
72 terminal NEW; // class_instance_creation_expression
73 terminal PLUSPLUS; // postincrement_expression
74 terminal MINUSMINUS; // postdecrement_expression
75 terminal PLUS, MINUS, COMP, NOT, DIV, MOD;
76 terminal LSHIFT, RSHIFT, URSHIFT; // shift_expression
77 terminal LT, GT, LTEQ, GTEQ, INSTANCEOF; // relational_expression
78 terminal EQEQ, NOTEQ; // equality_expression
79 terminal AND; // and_expression
80 terminal XOR; // exclusive_or_expression
81 terminal OR; // inclusive_or_expression
82 terminal ANDAND; // conditional_and_expression
83 terminal OROR; // conditional_or_expression
84 terminal QUESTION; // conditional_expression
85 terminal MULTEQ, DIVEQ, MODEQ, PLUSEQ, MINUSEQ; // assignment_operator
86 terminal LSHIFTEQ, RSHIFTEQ, URSHIFTEQ; // assignment_operator
87 terminal ANDEQ, XOREQ, OREQ; // assignment_operator
89 terminal java.lang.Number INTEGER_LITERAL;
90 terminal java.lang.Number FLOATING_POINT_LITERAL;
91 terminal java.lang.Boolean BOOLEAN_LITERAL;
92 terminal java.lang.Character CHARACTER_LITERAL;
93 terminal java.lang.String STRING_LITERAL;
94 terminal NULL_LITERAL;
96 // Reserved but unused:
98 // lexer compatibility with Java 1.2:
100 // lexer compatibility with Java 1.4
102 // lexer compatibility with Java 1.5
106 // 19.2) The Syntactic Grammar
108 // 19.3) Lexical Structure
109 non terminal literal;
110 // 19.4) Types, Values, and Variables
111 non terminal type, primitive_type, numeric_type;
112 non terminal integral_type, floating_point_type;
113 non terminal reference_type;
114 non terminal class_or_interface_type;
115 non terminal class_type, interface_type;
116 non terminal array_type;
118 non terminal name, simple_name, qualified_name;
120 non terminal compilation_unit;
121 non terminal package_declaration_opt, package_declaration;
122 non terminal import_declarations_opt, import_declarations;
123 non terminal type_declarations_opt, type_declarations;
124 non terminal import_declaration;
125 non terminal single_type_import_declaration;
126 non terminal type_import_on_demand_declaration;
127 non terminal type_declaration;
128 // 19.7) Productions used only in the LALR(1) grammar
129 non terminal modifiers_opt, modifiers, modifier;
130 // 19.8.1) Class Declaration
131 non terminal class_declaration, super, super_opt;
132 non terminal interfaces, interfaces_opt, interface_type_list;
133 non terminal class_body;
134 non terminal class_body_declarations, class_body_declarations_opt;
135 non terminal class_body_declaration, class_member_declaration;
136 // 19.8.2) Field Declarations
137 non terminal field_declaration, variable_declarators, variable_declarator;
138 non terminal variable_declarator_id, variable_initializer;
139 // 19.8.3) Method Declarations
140 non terminal method_declaration, method_header, method_declarator;
141 non terminal formal_parameter_list_opt, formal_parameter_list;
142 non terminal formal_parameter;
143 non terminal throws_opt, throws;
144 non terminal class_type_list, method_body;
145 // 19.8.4) Static Initializers
146 non terminal static_initializer;
147 // 19.8.5) Constructor Declarations
148 non terminal constructor_declaration, constructor_declarator;
149 non terminal constructor_body;
150 non terminal explicit_constructor_invocation;
151 // 19.9.1) Interface Declarations
152 non terminal interface_declaration;
153 non terminal extends_interfaces_opt, extends_interfaces;
154 non terminal interface_body;
155 non terminal interface_member_declarations_opt, interface_member_declarations;
156 non terminal interface_member_declaration, constant_declaration;
157 non terminal abstract_method_declaration;
159 non terminal array_initializer;
160 non terminal variable_initializers;
161 // 19.11) Blocks and Statements
163 non terminal block_statements_opt, block_statements, block_statement;
164 non terminal local_variable_declaration_statement, local_variable_declaration;
165 non terminal statement, statement_no_short_if;
166 non terminal statement_without_trailing_substatement;
167 non terminal empty_statement;
168 non terminal labeled_statement, labeled_statement_no_short_if;
169 non terminal expression_statement, statement_expression;
170 non terminal if_then_statement;
171 non terminal if_then_else_statement, if_then_else_statement_no_short_if;
172 non terminal switch_statement, switch_block;
173 non terminal switch_block_statement_groups;
174 non terminal switch_block_statement_group;
175 non terminal switch_labels, switch_label;
176 non terminal while_statement, while_statement_no_short_if;
177 non terminal do_statement;
178 non terminal for_statement, for_statement_no_short_if;
179 non terminal for_init_opt, for_init;
180 non terminal for_update_opt, for_update;
181 non terminal statement_expression_list;
182 non terminal identifier_opt;
183 non terminal break_statement, continue_statement;
184 non terminal return_statement, throw_statement;
185 non terminal synchronized_statement, try_statement;
186 non terminal catches_opt, catches, catch_clause;
187 non terminal finally;
188 // 19.12) Expressions
189 non terminal primary, primary_no_new_array;
190 non terminal class_instance_creation_expression;
191 non terminal argument_list_opt, argument_list;
192 non terminal array_creation_expression;
193 non terminal dim_exprs, dim_expr, dims_opt, dims;
194 non terminal field_access, method_invocation, array_access;
195 non terminal postfix_expression;
196 non terminal postincrement_expression, postdecrement_expression;
197 non terminal unary_expression, unary_expression_not_plus_minus;
198 non terminal preincrement_expression, predecrement_expression;
199 non terminal cast_expression;
200 non terminal multiplicative_expression, additive_expression;
201 non terminal shift_expression, relational_expression, equality_expression;
202 non terminal and_expression, exclusive_or_expression, inclusive_or_expression;
203 non terminal conditional_and_expression, conditional_or_expression;
204 non terminal conditional_expression, assignment_expression;
205 non terminal assignment;
206 non terminal left_hand_side;
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
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
397 formal_parameter_list_opt ::=
398 | formal_parameter_list
400 formal_parameter_list ::=
402 | formal_parameter_list COMMA formal_parameter
405 type variable_declarator_id
406 | FINAL type variable_declarator_id
411 throws ::= THROWS class_type_list
415 | class_type_list COMMA class_type
417 method_body ::= block
421 // 19.8.4) Static Initializers
422 static_initializer ::=
426 // 19.8.5) Constructor Declarations
427 constructor_declaration ::=
428 modifiers_opt constructor_declarator throws_opt
431 constructor_declarator ::=
432 simple_name LPAREN formal_parameter_list_opt RPAREN
435 LBRACE explicit_constructor_invocation
436 block_statements RBRACE
437 | LBRACE explicit_constructor_invocation RBRACE
438 | LBRACE block_statements RBRACE
441 explicit_constructor_invocation ::=
442 THIS LPAREN argument_list_opt RPAREN SEMICOLON
443 | SUPER LPAREN argument_list_opt RPAREN SEMICOLON
444 | primary DOT SUPER LPAREN argument_list_opt RPAREN SEMICOLON
449 // 19.9.1) Interface Declarations
450 interface_declaration ::=
451 modifiers_opt INTERFACE IDENTIFIER extends_interfaces_opt
454 extends_interfaces_opt ::=
457 extends_interfaces ::=
458 EXTENDS interface_type
459 | extends_interfaces COMMA interface_type
462 LBRACE interface_member_declarations_opt RBRACE
464 interface_member_declarations_opt ::=
465 | interface_member_declarations
467 interface_member_declarations ::=
468 interface_member_declaration
469 | interface_member_declarations interface_member_declaration
471 interface_member_declaration ::=
473 | abstract_method_declaration
475 | interface_declaration
478 constant_declaration ::=
480 // need to semantically check that modifiers of field declaration
481 // include only PUBLIC, STATIC, or FINAL. Other modifiers are
484 abstract_method_declaration ::=
485 method_header SEMICOLON
489 array_initializer ::=
490 LBRACE variable_initializers COMMA RBRACE
491 | LBRACE variable_initializers RBRACE
492 | LBRACE COMMA RBRACE
495 variable_initializers ::=
497 | variable_initializers COMMA variable_initializer
500 // 19.11) Blocks and Statements
501 block ::= LBRACE block_statements_opt RBRACE
503 block_statements_opt ::=
508 | block_statements block_statement
511 local_variable_declaration_statement
514 | interface_declaration
516 local_variable_declaration_statement ::=
517 local_variable_declaration SEMICOLON
519 local_variable_declaration ::=
520 type variable_declarators
521 | FINAL type variable_declarators
523 statement ::= statement_without_trailing_substatement
526 | if_then_else_statement
530 statement_no_short_if ::=
531 statement_without_trailing_substatement
532 | labeled_statement_no_short_if
533 | if_then_else_statement_no_short_if
534 | while_statement_no_short_if
535 | for_statement_no_short_if
537 statement_without_trailing_substatement ::=
540 | expression_statement
546 | synchronized_statement
553 labeled_statement ::=
554 IDENTIFIER COLON statement
556 labeled_statement_no_short_if ::=
557 IDENTIFIER COLON statement_no_short_if
559 expression_statement ::=
560 statement_expression SEMICOLON
562 statement_expression ::=
564 | preincrement_expression
565 | predecrement_expression
566 | postincrement_expression
567 | postdecrement_expression
569 | class_instance_creation_expression
571 if_then_statement ::=
572 IF LPAREN expression RPAREN statement
574 if_then_else_statement ::=
575 IF LPAREN expression RPAREN statement_no_short_if
578 if_then_else_statement_no_short_if ::=
579 IF LPAREN expression RPAREN statement_no_short_if
580 ELSE statement_no_short_if
583 SWITCH LPAREN expression RPAREN switch_block
586 LBRACE switch_block_statement_groups switch_labels RBRACE
587 | LBRACE switch_block_statement_groups RBRACE
588 | LBRACE switch_labels RBRACE
591 switch_block_statement_groups ::=
592 switch_block_statement_group
593 | switch_block_statement_groups switch_block_statement_group
595 switch_block_statement_group ::=
596 switch_labels block_statements
600 | switch_labels switch_label
603 CASE constant_expression COLON
608 WHILE LPAREN expression RPAREN statement
610 while_statement_no_short_if ::=
611 WHILE LPAREN expression RPAREN statement_no_short_if
614 DO statement WHILE LPAREN expression RPAREN SEMICOLON
617 FOR LPAREN for_init_opt SEMICOLON expression_opt SEMICOLON
618 for_update_opt RPAREN statement
620 for_statement_no_short_if ::=
621 FOR LPAREN for_init_opt SEMICOLON expression_opt SEMICOLON
622 for_update_opt RPAREN statement_no_short_if
627 for_init ::= statement_expression_list
628 | local_variable_declaration
633 for_update ::= statement_expression_list
635 statement_expression_list ::=
637 | statement_expression_list COMMA statement_expression
645 BREAK identifier_opt SEMICOLON
648 continue_statement ::=
649 CONTINUE identifier_opt SEMICOLON
652 RETURN expression_opt SEMICOLON
655 THROW expression SEMICOLON
657 synchronized_statement ::=
658 SYNCHRONIZED LPAREN expression RPAREN block
662 | TRY block catches_opt finally
667 catches ::= catch_clause
668 | catches catch_clause
671 CATCH LPAREN formal_parameter RPAREN block
673 finally ::= FINALLY block
676 // 19.12) Expressions
677 primary ::= primary_no_new_array
678 | array_creation_expression
680 primary_no_new_array ::=
683 | LPAREN expression RPAREN
684 | class_instance_creation_expression
688 | primitive_type DOT CLASS
690 | array_type DOT CLASS
694 class_instance_creation_expression ::=
695 NEW class_type LPAREN argument_list_opt RPAREN
696 | NEW class_type LPAREN argument_list_opt RPAREN class_body
697 | primary DOT NEW IDENTIFIER
698 LPAREN argument_list_opt RPAREN
699 | primary DOT NEW IDENTIFIER
700 LPAREN argument_list_opt RPAREN class_body
702 argument_list_opt ::=
707 | argument_list COMMA expression
709 array_creation_expression ::=
710 NEW primitive_type dim_exprs dims_opt
711 | NEW class_or_interface_type dim_exprs dims_opt
712 | NEW primitive_type dims array_initializer
713 | NEW class_or_interface_type dims array_initializer
715 dim_exprs ::= dim_expr
718 dim_expr ::= LBRACK expression RBRACK
723 dims ::= LBRACK RBRACK
727 primary DOT IDENTIFIER
728 | SUPER DOT IDENTIFIER
730 method_invocation ::=
731 name LPAREN argument_list_opt RPAREN
732 | primary DOT IDENTIFIER LPAREN argument_list_opt RPAREN
733 | SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
736 name LBRACK expression RBRACK
737 | primary_no_new_array LBRACK expression RBRACK
739 postfix_expression ::=
742 | postincrement_expression
743 | postdecrement_expression
745 postincrement_expression ::=
746 postfix_expression PLUSPLUS
748 postdecrement_expression ::=
749 postfix_expression MINUSMINUS
752 preincrement_expression
753 | predecrement_expression
754 | PLUS unary_expression
755 | MINUS unary_expression
756 | unary_expression_not_plus_minus
758 preincrement_expression ::=
759 PLUSPLUS unary_expression
761 predecrement_expression ::=
762 MINUSMINUS unary_expression
764 unary_expression_not_plus_minus ::=
766 | COMP unary_expression
767 | NOT unary_expression
771 LPAREN primitive_type dims_opt RPAREN unary_expression
772 | LPAREN expression RPAREN unary_expression_not_plus_minus
773 | LPAREN name dims RPAREN unary_expression_not_plus_minus
775 multiplicative_expression ::=
777 | multiplicative_expression MULT unary_expression
778 | multiplicative_expression DIV unary_expression
779 | multiplicative_expression MOD unary_expression
781 additive_expression ::=
782 multiplicative_expression
783 | additive_expression PLUS multiplicative_expression
784 | additive_expression MINUS multiplicative_expression
788 | shift_expression LSHIFT additive_expression
789 | shift_expression RSHIFT additive_expression
790 | shift_expression URSHIFT additive_expression
792 relational_expression ::=
794 | relational_expression LT shift_expression
795 | relational_expression GT shift_expression
796 | relational_expression LTEQ shift_expression
797 | relational_expression GTEQ shift_expression
798 | relational_expression INSTANCEOF reference_type
800 equality_expression ::=
801 relational_expression
802 | equality_expression EQEQ relational_expression
803 | equality_expression NOTEQ relational_expression
807 | and_expression AND equality_expression
809 exclusive_or_expression ::=
811 | exclusive_or_expression XOR and_expression
813 inclusive_or_expression ::=
814 exclusive_or_expression
815 | inclusive_or_expression OR exclusive_or_expression
817 conditional_and_expression ::=
818 inclusive_or_expression
819 | conditional_and_expression ANDAND inclusive_or_expression
821 conditional_or_expression ::=
822 conditional_and_expression
823 | conditional_or_expression OROR conditional_and_expression
825 conditional_expression ::=
826 conditional_or_expression
827 | conditional_or_expression QUESTION expression
828 COLON conditional_expression
830 assignment_expression ::=
831 conditional_expression
834 assignment ::= left_hand_side assignment_operator assignment_expression
841 assignment_operator ::=
858 expression ::= assignment_expression
860 constant_expression ::=