3 import java_cup.runtime.*;
7 /* Java 1.4 parser for CUP.
8 * Copyright (C) 2002-2003 C. Scott Ananian <cananian@alumni.princeton.edu>
9 * This program is released under the terms of the GPL; see the file
10 * COPYING for more details. There is NO WARRANTY on this code.
14 JDK 1.4 Features added:
16 statement_without_trailing_substatement ::= ...
19 ASSERT expression SEMICOLON
20 | ASSERT expression COLON expression SEMICOLON
26 public Parser(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 // strictfp keyword, new in Java 1.2
100 // assert keyword, new in Java 1.4
101 terminal ASSERT; // assert_statement
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 non terminal assert_statement;
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 THIS LPAREN argument_list_opt RPAREN SEMICOLON
448 | primary 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
558 labeled_statement ::=
559 IDENTIFIER COLON statement
561 labeled_statement_no_short_if ::=
562 IDENTIFIER COLON statement_no_short_if
564 expression_statement ::=
565 statement_expression SEMICOLON
567 statement_expression ::=
569 | preincrement_expression
570 | predecrement_expression
571 | postincrement_expression
572 | postdecrement_expression
574 | class_instance_creation_expression
576 if_then_statement ::=
577 IF LPAREN expression RPAREN statement
579 if_then_else_statement ::=
580 IF LPAREN expression RPAREN statement_no_short_if
583 if_then_else_statement_no_short_if ::=
584 IF LPAREN expression RPAREN statement_no_short_if
585 ELSE statement_no_short_if
588 SWITCH LPAREN expression RPAREN switch_block
591 LBRACE switch_block_statement_groups switch_labels RBRACE
592 | LBRACE switch_block_statement_groups RBRACE
593 | LBRACE switch_labels RBRACE
596 switch_block_statement_groups ::=
597 switch_block_statement_group
598 | switch_block_statement_groups switch_block_statement_group
600 switch_block_statement_group ::=
601 switch_labels block_statements
605 | switch_labels switch_label
608 CASE constant_expression COLON
613 WHILE LPAREN expression RPAREN statement
615 while_statement_no_short_if ::=
616 WHILE LPAREN expression RPAREN statement_no_short_if
619 DO statement WHILE LPAREN expression RPAREN SEMICOLON
622 FOR LPAREN for_init_opt SEMICOLON expression_opt SEMICOLON
623 for_update_opt RPAREN statement
625 for_statement_no_short_if ::=
626 FOR LPAREN for_init_opt SEMICOLON expression_opt SEMICOLON
627 for_update_opt RPAREN statement_no_short_if
632 for_init ::= statement_expression_list
633 | local_variable_declaration
638 for_update ::= statement_expression_list
640 statement_expression_list ::=
642 | statement_expression_list COMMA statement_expression
650 BREAK identifier_opt SEMICOLON
653 continue_statement ::=
654 CONTINUE identifier_opt SEMICOLON
657 RETURN expression_opt SEMICOLON
660 THROW expression SEMICOLON
662 synchronized_statement ::=
663 SYNCHRONIZED LPAREN expression RPAREN block
667 | TRY block catches_opt finally
672 catches ::= catch_clause
673 | catches catch_clause
676 CATCH LPAREN formal_parameter RPAREN block
678 finally ::= FINALLY block
681 ASSERT expression SEMICOLON
682 | ASSERT expression COLON expression SEMICOLON
685 // 19.12) Expressions
686 primary ::= primary_no_new_array
687 | array_creation_init
688 | array_creation_uninit
690 primary_no_new_array ::=
693 | LPAREN expression RPAREN
694 | class_instance_creation_expression
698 | primitive_type DOT CLASS
700 | array_type DOT CLASS
704 class_instance_creation_expression ::=
705 NEW class_or_interface_type LPAREN argument_list_opt RPAREN
706 | NEW class_or_interface_type LPAREN argument_list_opt RPAREN class_body
707 | primary DOT NEW IDENTIFIER
708 LPAREN argument_list_opt RPAREN
709 | primary DOT NEW IDENTIFIER
710 LPAREN argument_list_opt RPAREN class_body
711 | name DOT NEW IDENTIFIER
712 LPAREN argument_list_opt RPAREN
713 | name DOT NEW IDENTIFIER
714 LPAREN argument_list_opt RPAREN class_body
716 argument_list_opt ::=
721 | argument_list COMMA expression
723 array_creation_uninit ::=
724 NEW primitive_type dim_exprs dims_opt
725 | NEW class_or_interface_type dim_exprs dims_opt
727 array_creation_init ::=
728 NEW primitive_type dims array_initializer
729 | NEW class_or_interface_type dims array_initializer
731 dim_exprs ::= dim_expr
734 dim_expr ::= LBRACK expression RBRACK
739 dims ::= LBRACK RBRACK
743 primary DOT IDENTIFIER
744 | SUPER DOT IDENTIFIER
745 | name DOT SUPER DOT IDENTIFIER
747 method_invocation ::=
748 name LPAREN argument_list_opt RPAREN
749 | primary DOT IDENTIFIER LPAREN argument_list_opt RPAREN
750 | SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
751 | name DOT SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
754 name LBRACK expression RBRACK
755 | primary_no_new_array LBRACK expression RBRACK
756 | array_creation_init LBRACK expression RBRACK
758 postfix_expression ::=
761 | postincrement_expression
762 | postdecrement_expression
764 postincrement_expression ::=
765 postfix_expression PLUSPLUS
767 postdecrement_expression ::=
768 postfix_expression MINUSMINUS
771 preincrement_expression
772 | predecrement_expression
773 | PLUS unary_expression
774 | MINUS unary_expression
775 | unary_expression_not_plus_minus
777 preincrement_expression ::=
778 PLUSPLUS unary_expression
780 predecrement_expression ::=
781 MINUSMINUS unary_expression
783 unary_expression_not_plus_minus ::=
785 | COMP unary_expression
786 | NOT unary_expression
790 LPAREN primitive_type dims_opt RPAREN unary_expression
791 | LPAREN expression RPAREN unary_expression_not_plus_minus
792 | LPAREN name dims RPAREN unary_expression_not_plus_minus
794 multiplicative_expression ::=
796 | multiplicative_expression MULT unary_expression
797 | multiplicative_expression DIV unary_expression
798 | multiplicative_expression MOD unary_expression
800 additive_expression ::=
801 multiplicative_expression
802 | additive_expression PLUS multiplicative_expression
803 | additive_expression MINUS multiplicative_expression
807 | shift_expression LSHIFT additive_expression
808 | shift_expression RSHIFT additive_expression
809 | shift_expression URSHIFT additive_expression
811 relational_expression ::=
813 | relational_expression LT shift_expression
814 | relational_expression GT shift_expression
815 | relational_expression LTEQ shift_expression
816 | relational_expression GTEQ shift_expression
817 | relational_expression INSTANCEOF reference_type
819 equality_expression ::=
820 relational_expression
821 | equality_expression EQEQ relational_expression
822 | equality_expression NOTEQ relational_expression
826 | and_expression AND equality_expression
828 exclusive_or_expression ::=
830 | exclusive_or_expression XOR and_expression
832 inclusive_or_expression ::=
833 exclusive_or_expression
834 | inclusive_or_expression OR exclusive_or_expression
836 conditional_and_expression ::=
837 inclusive_or_expression
838 | conditional_and_expression ANDAND inclusive_or_expression
840 conditional_or_expression ::=
841 conditional_and_expression
842 | conditional_or_expression OROR conditional_and_expression
844 conditional_expression ::=
845 conditional_or_expression
846 | conditional_or_expression QUESTION expression
847 COLON conditional_expression
849 assignment_expression ::=
850 conditional_expression
853 // semantic check necessary here to ensure a valid left-hand side.
854 // allowing a parenthesized variable here on the lhs was introduced in
855 // JLS 2; thanks to Eric Blake for pointing this out.
856 assignment ::= postfix_expression assignment_operator assignment_expression
858 assignment_operator ::=
875 expression ::= assignment_expression
877 constant_expression ::=