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 //failure aware computation keywords
110 non terminal ParseNode flag_declaration;
112 // 19.2) The Syntactic Grammar
113 non terminal ParseNode goal;
114 // 19.3) Lexical Structure
115 non terminal ParseNode literal;
116 // 19.4) Types, Values, and Variables
117 non terminal ParseNode type, primitive_type, numeric_type;
118 non terminal ParseNode integral_type, floating_point_type;
119 non terminal ParseNode reference_type;
120 non terminal ParseNode class_or_interface_type;
121 non terminal ParseNode class_type;
122 //non terminal ParseNode interface_type;
123 non terminal ParseNode array_type;
125 non terminal ParseNode name, simple_name, qualified_name;
127 non terminal ParseNode compilation_unit;
128 //non terminal ParseNode package_declaration_opt, package_declaration;
129 //non terminal ParseNode import_declarations_opt, import_declarations;
130 non terminal ParseNode type_declarations_opt, type_declarations;
131 //non terminal ParseNode import_declaration;
132 //non terminal ParseNode single_type_import_declaration;
133 //non terminal ParseNode type_import_on_demand_declaration;
134 non terminal ParseNode type_declaration;
135 // 19.7) Productions used only in the LALR(1) grammar
136 non terminal ParseNode modifiers_opt, modifiers, modifier;
137 // 19.8.1) Class Declaration
138 non terminal ParseNode class_declaration, super, super_opt;
139 //non terminal interfaces, interfaces_opt, interface_type_list;
140 non terminal ParseNode class_body;
141 non terminal ParseNode class_body_declarations, class_body_declarations_opt;
142 non terminal ParseNode class_body_declaration, class_member_declaration;
143 // 19.8.2) Field Declarations
144 non terminal ParseNode field_declaration, variable_declarators, variable_declarator;
145 non terminal ParseNode variable_declarator_id;
146 non terminal ParseNode variable_initializer;
147 // 19.8.3) Method Declarations
148 non terminal ParseNode method_declaration, method_header, method_declarator;
149 non terminal ParseNode formal_parameter_list_opt, formal_parameter_list;
150 non terminal ParseNode formal_parameter;
151 //non terminal ParseNode throws_opt;
152 //non terminal ParseNode throws;
153 //non terminal ParseNode class_type_list;
154 non terminal ParseNode method_body;
155 // 19.8.4) Static Initializers
156 //non terminal ParseNode static_initializer;
157 // 19.8.5) Constructor Declarations
158 non terminal ParseNode constructor_declaration, constructor_declarator;
159 non terminal ParseNode constructor_body;
160 //non terminal ParseNode explicit_constructor_invocation;
161 // 19.9.1) Interface Declarations
162 //non terminal ParseNode interface_declaration;
163 //non terminal ParseNode extends_interfaces_opt, extends_interfaces;
164 //non terminal ParseNode interface_body;
165 //non terminal ParseNode interface_member_declarations_opt, interface_member_declarations;
166 //non terminal ParseNode interface_member_declaration, constant_declaration;
167 //non terminal ParseNode abstract_method_declaration;
169 //non terminal ParseNode array_initializer;
170 //non terminal ParseNode variable_initializers;
171 // 19.11) Blocks and Statements
172 non terminal ParseNode block;
173 non terminal ParseNode block_statements_opt, block_statements, block_statement;
174 non terminal ParseNode local_variable_declaration_statement, local_variable_declaration;
175 non terminal ParseNode statement, statement_no_short_if;
176 non terminal ParseNode statement_without_trailing_substatement;
177 non terminal ParseNode empty_statement;
178 //non terminal ParseNode labeled_statement, labeled_statement_no_short_if;
179 non terminal ParseNode expression_statement, statement_expression;
180 non terminal ParseNode if_then_statement;
181 non terminal ParseNode if_then_else_statement, if_then_else_statement_no_short_if;
182 //non terminal ParseNode switch_statement, switch_block;
183 //non terminal ParseNode switch_block_statement_groups;
184 //non terminal ParseNode switch_block_statement_group;
185 //non terminal ParseNode switch_labels, switch_label;
186 non terminal ParseNode while_statement, while_statement_no_short_if;
187 non terminal ParseNode do_statement;
188 non terminal ParseNode for_statement, for_statement_no_short_if;
189 non terminal ParseNode for_init_opt, for_init;
190 non terminal ParseNode for_update_opt, for_update;
191 non terminal ParseNode statement_expression_list;
192 //non terminal ParseNode identifier_opt;
193 non terminal ParseNode break_statement, continue_statement;
194 non terminal ParseNode return_statement;
195 //non terminal ParseNode throw_statement;
196 //non terminal ParseNode synchronized_statement, try_statement;
197 //non terminal ParseNode catches_opt;
198 //non terminal ParseNode catches, catch_clause;
199 //non terminal ParseNode finally;
200 //non terminal ParseNode assert_statement;
201 // 19.12) Expressions
202 non terminal ParseNode primary, primary_no_new_array;
203 non terminal ParseNode class_instance_creation_expression;
204 non terminal ParseNode argument_list_opt, argument_list;
205 //non terminal ParseNode array_creation_init;
206 non terminal ParseNode array_creation_uninit;
207 non terminal ParseNode dim_exprs, dim_expr;
208 non terminal Integer dims_opt, dims;
209 non terminal ParseNode field_access, method_invocation;
210 non terminal ParseNode array_access;
211 non terminal ParseNode postfix_expression;
212 non terminal ParseNode postincrement_expression, postdecrement_expression;
213 non terminal ParseNode unary_expression, unary_expression_not_plus_minus;
214 non terminal ParseNode preincrement_expression, predecrement_expression;
215 non terminal ParseNode cast_expression;
216 non terminal ParseNode multiplicative_expression, additive_expression;
217 non terminal ParseNode shift_expression, relational_expression, equality_expression;
218 non terminal ParseNode and_expression, exclusive_or_expression, inclusive_or_expression;
219 non terminal ParseNode conditional_and_expression, conditional_or_expression;
220 non terminal ParseNode conditional_expression;
221 non terminal ParseNode assignment_expression;
222 non terminal ParseNode assignment;
223 non terminal ParseNode assignment_operator;
224 non terminal ParseNode expression_opt, expression;
225 //non terminal ParseNode constant_expression;
229 // 19.2) The Syntactic Grammar
230 goal ::= compilation_unit:cu
236 // 19.3) Lexical Structure.
239 literal ::= INTEGER_LITERAL:integer_lit
241 ParseNode pn=new ParseNode("literal");
242 pn.addChild("integer").setLiteral(integer_lit);
245 | FLOATING_POINT_LITERAL:float_lit
247 ParseNode pn=new ParseNode("literal");
248 pn.addChild("float").setLiteral(float_lit);
251 | BOOLEAN_LITERAL:boolean_lit
253 ParseNode pn=new ParseNode("literal");
254 pn.addChild("boolean").setLiteral(boolean_lit);
257 | CHARACTER_LITERAL:char_lit
259 ParseNode pn=new ParseNode("literal");
260 pn.addChild("char").setLiteral(char_lit);
263 | STRING_LITERAL:string_lit
265 ParseNode pn=new ParseNode("literal");
266 pn.addChild("string").setLiteral(string_lit);
271 RESULT=(new ParseNode("literal")).addChild("null").getRoot();
275 // 19.4) Types, Values, and Variables
276 type ::= primitive_type:type {: RESULT=type; :}
277 | reference_type:type {: RESULT=type; :}
281 numeric_type:type {: RESULT=type; :}
282 | BOOLEAN {: RESULT=(new ParseNode("type")).addChild("boolean").getRoot(); :}
284 numeric_type::= integral_type:type {: RESULT=type; :}
285 | floating_point_type:type {: RESULT=type; :}
288 BYTE {: RESULT=(new ParseNode("type")).addChild("byte").getRoot(); :}
289 | SHORT {: RESULT=(new ParseNode("type")).addChild("short").getRoot(); :}
290 | INT {: RESULT=(new ParseNode("type")).addChild("int").getRoot(); :}
291 | LONG {: RESULT=(new ParseNode("type")).addChild("long").getRoot(); :}
292 | CHAR {: RESULT=(new ParseNode("type")).addChild("char").getRoot(); :}
294 floating_point_type ::=
295 FLOAT {: RESULT=(new ParseNode("type")).addChild("float").getRoot(); :}
296 | DOUBLE {: RESULT=(new ParseNode("type")).addChild("double").getRoot(); :}
300 class_or_interface_type:type {: RESULT=type; :}
301 | array_type:type {: RESULT=type; :}
303 class_or_interface_type ::= name:name {:
304 RESULT=(new ParseNode("type")).addChild("class").addChild(name).getRoot();
307 class_type ::= class_or_interface_type:type {: RESULT=type; :};
308 //interface_type ::= class_or_interface_type;
310 array_type ::= primitive_type:prim dims:dims {:
311 ParseNode pn=(new ParseNode("type")).addChild("array");
312 pn.addChild("basetype").addChild(prim);
313 pn.addChild("dims").setLiteral(dims);
316 | name:name dims:dims {:
317 ParseNode pn=(new ParseNode("type")).addChild("array");
318 pn.addChild("basetype").addChild("type").addChild("class").addChild(name);
319 pn.addChild("dims").setLiteral(dims);
325 name ::= simple_name:name {: RESULT=name; :}
326 | qualified_name:name {: RESULT=name; :}
328 simple_name ::= IDENTIFIER:id {:
329 RESULT=(new ParseNode("name")).addChild("identifier").addChild(id).getRoot();
332 qualified_name ::= name:name DOT IDENTIFIER:id {:
333 ParseNode pn=new ParseNode("name");
334 pn.addChild("base").addChild(name);
335 pn.addChild("identifier").addChild(id);
342 // package_declaration_opt
343 // import_declarations_opt
344 type_declarations_opt:tdo {:
345 ParseNode pn=new ParseNode("compilation_unit");
350 //package_declaration_opt ::= package_declaration | ;
351 //import_declarations_opt ::= import_declarations | ;
352 type_declarations_opt ::= type_declarations:tds {:
355 {: RESULT=new ParseNode("empty"); :}
358 //import_declarations ::=
359 // import_declaration
360 // | import_declarations import_declaration
363 type_declarations ::=
364 type_declaration:td {:
365 ParseNode pn=new ParseNode("type_declaration_list");
369 | type_declarations:tds type_declaration:td {:
375 //package_declaration ::=
376 // PACKAGE name SEMICOLON
378 //import_declaration ::=
379 // single_type_import_declaration
380 // | type_import_on_demand_declaration
382 //single_type_import_declaration ::=
383 // IMPORT name SEMICOLON
385 //type_import_on_demand_declaration ::=
386 // IMPORT name DOT MULT SEMICOLON
394 // | interface_declaration
395 | SEMICOLON {: RESULT=new ParseNode("empty"); :}
398 // 19.7) Productions used only in the LALR(1) grammar
400 {: RESULT=new ParseNode("empty"); :}
405 modifiers ::= modifier:mo {:
406 ParseNode pn=new ParseNode("modifier_list");
410 | modifiers:mos modifier:mo {:
416 PUBLIC {: RESULT=new ParseNode("public"); :}|
417 PROTECTED {: RESULT=new ParseNode("protected"); :}|
418 PRIVATE {: RESULT=new ParseNode("private"); :}|
419 STATIC {: RESULT=new ParseNode("static"); :} |
421 FINAL {: RESULT=new ParseNode("final"); :}|
422 NATIVE {: RESULT=new ParseNode("native"); :}
426 // STRICTFP // note that semantic analysis must check that the
427 // context of the modifier allows strictfp.
432 // 19.8.1) Class Declaration:
433 class_declaration ::=
434 modifiers_opt:mo CLASS IDENTIFIER:id super_opt:so //interfaces_opt
437 ParseNode pn=new ParseNode("class_declaration");
438 pn.addChild("modifiers").addChild(mo);
439 pn.addChild("name").addChild(id);
440 pn.addChild("super").addChild(so);
441 pn.addChild("classbody").addChild(body);
445 super ::= EXTENDS class_type:classtype {:
450 {: RESULT=new ParseNode("empty"); :}
456 //interfaces ::= IMPLEMENTS interface_type_list
461 //interface_type_list ::=
463 // | interface_type_list COMMA interface_type
466 class_body ::= LBRACE class_body_declarations_opt:cbdo RBRACE {: RESULT=cbdo; :}
469 class_body_declarations_opt ::=
470 {: RESULT=new ParseNode("empty"); :}
471 | class_body_declarations:cbd {: RESULT=cbd; :};
473 class_body_declarations ::=
474 class_body_declaration:cbd {:
475 ParseNode pn=new ParseNode("class_body_declaration_list");
479 | class_body_declarations:cbds class_body_declaration:cbd {:
485 class_body_declaration ::=
486 class_member_declaration:member {:
487 RESULT=(new ParseNode("member")).addChild(member).getRoot();
489 // | static_initializer
490 | constructor_declaration:constructor {:
491 RESULT=(new ParseNode("constructor")).addChild(constructor).getRoot();
494 RESULT=(new ParseNode("block")).addChild(block).getRoot();
497 class_member_declaration ::=
498 //failure aware computation
499 flag_declaration:flag {:
500 RESULT=(new ParseNode("flag")).addChild(flag).getRoot();
503 field_declaration:field {:
504 RESULT=(new ParseNode("field")).addChild(field).getRoot();
506 | method_declaration:method {:
507 RESULT=(new ParseNode("method")).addChild(method).getRoot();
509 /* repeat the prod for 'class_declaration' here: */
510 // | modifiers_opt CLASS IDENTIFIER super_opt class_body
511 // | interface_declaration
512 | SEMICOLON {: RESULT=new ParseNode("empty"); :}
515 //Failure aware computation
517 FLAG IDENTIFIER:id SEMICOLON {:
518 ParseNode pn=new ParseNode("flag_declaration");
519 pn.addChild("name").addChild(id);
524 // 19.8.2) Field Declarations
525 field_declaration ::=
526 modifiers_opt:mo type:type variable_declarators:var SEMICOLON {:
527 ParseNode pn=new ParseNode("field_declaration");
528 pn.addChild("modifier").addChild(mo);
529 pn.addChild("type").addChild(type);
530 pn.addChild("variables").addChild(var);
535 variable_declarators ::=
536 variable_declarator:vd {:
537 ParseNode pn=new ParseNode("variable_declarators_list");
541 | variable_declarators:vds COMMA variable_declarator:vd {:
546 variable_declarator ::=
547 variable_declarator_id:id {:
548 ParseNode pn=new ParseNode("variable_declarator");
552 | variable_declarator_id:id EQ variable_initializer:init {:
553 ParseNode pn=new ParseNode("variable_declarator");
555 pn.addChild("initializer").addChild(init);
559 variable_declarator_id ::=
561 RESULT=(new ParseNode("single")).addChild(id).getRoot();:}
562 | variable_declarator_id:id LBRACK RBRACK {:
563 RESULT=(new ParseNode("array")).addChild(id).getRoot();:}
565 variable_initializer ::=
566 expression:exp {: RESULT=exp; :}
567 // | array_initializer
570 // 19.8.3) Method Declarations
571 method_declaration ::=
572 method_header:header method_body:body {:
573 ParseNode pn=new ParseNode("method_declaration");
575 pn.addChild("body").addChild(body);
580 modifiers_opt:mo type:type method_declarator:decl //throws_opt
582 ParseNode pn=new ParseNode("method_header");
583 pn.addChild("modifiers").addChild(mo);
584 pn.addChild("returntype").addChild(type);
588 | modifiers_opt:mo VOID method_declarator:decl //throws_opt
590 ParseNode pn=new ParseNode("method_header");
591 pn.addChild("modifiers").addChild(mo);
596 method_declarator ::=
597 IDENTIFIER:id LPAREN formal_parameter_list_opt:params RPAREN {:
598 ParseNode pn=new ParseNode("method_declarator");
599 pn.addChild("name").addChild(id);
600 pn.addChild("parameters").addChild(params);
603 // | method_declarator LBRACK RBRACK // deprecated
604 // be careful; the above production also allows 'void foo() []'
606 formal_parameter_list_opt ::=
607 {: RESULT=new ParseNode("empty"); :}
608 | formal_parameter_list:fpl {:
612 formal_parameter_list ::=
613 formal_parameter:fp {:
614 ParseNode pn=new ParseNode("formal_parameter_list");
618 | formal_parameter_list:fpl COMMA formal_parameter:fp {:
624 type:type variable_declarator_id:name {:
625 ParseNode pn=new ParseNode("formal_parameter");
630 // | FINAL type variable_declarator_id
635 //throws ::= THROWS class_type_list
637 //class_type_list ::=
639 // | class_type_list COMMA class_type
641 method_body ::= block:block {:
644 | SEMICOLON {: RESULT=new ParseNode("empty"); :}
647 // 19.8.4) Static Initializers
648 //static_initializer ::=
652 // 19.8.5) Constructor Declarations
653 constructor_declaration ::=
654 modifiers_opt:mo constructor_declarator:cd
656 constructor_body:body {:
657 ParseNode pn=new ParseNode("constructor_declaration");
658 pn.addChild("modifiers").addChild(mo);
660 pn.addChild("body").addChild(body);
664 constructor_declarator ::=
665 simple_name:name LPAREN formal_parameter_list_opt:fplo RPAREN {:
666 ParseNode pn=new ParseNode("constructor_declarator");
668 pn.addChild("parameters").addChild(fplo);
673 // LBRACE explicit_constructor_invocation:eci block_statements:bs RBRACE |
674 // LBRACE explicit_constructor_invocation RBRACE |
675 LBRACE block_statements:block RBRACE {:
676 ParseNode pn=new ParseNode("constructor_body");
680 | LBRACE RBRACE {: RESULT=new ParseNode("empty"); :}
682 //explicit_constructor_invocation ::=
683 // THIS LPAREN argument_list_opt RPAREN SEMICOLON
684 // | SUPER LPAREN argument_list_opt RPAREN SEMICOLON
685 // | primary DOT THIS LPAREN argument_list_opt RPAREN SEMICOLON
686 // | primary DOT SUPER LPAREN argument_list_opt RPAREN SEMICOLON
691 // 19.9.1) Interface Declarations
692 //interface_declaration ::=
693 // modifiers_opt INTERFACE IDENTIFIER extends_interfaces_opt
696 //extends_interfaces_opt ::=
697 // | extends_interfaces
699 //extends_interfaces ::=
700 // EXTENDS interface_type
701 // | extends_interfaces COMMA interface_type
704 // LBRACE interface_member_declarations_opt RBRACE
706 //interface_member_declarations_opt ::=
707 // | interface_member_declarations
709 //interface_member_declarations ::=
710 // interface_member_declaration
711 // | interface_member_declarations interface_member_declaration
713 //interface_member_declaration ::=
714 // constant_declaration
715 // | abstract_method_declaration
716 // | class_declaration
717 // | interface_declaration
720 //constant_declaration ::=
722 // // need to semantically check that modifiers of field declaration
723 // // include only PUBLIC, STATIC, or FINAL. Other modifiers are
726 //abstract_method_declaration ::=
727 // method_header SEMICOLON
732 //array_initializer ::=
733 // LBRACE variable_initializers COMMA RBRACE
734 // | LBRACE variable_initializers RBRACE
735 // | LBRACE COMMA RBRACE
738 //variable_initializers ::=
739 // variable_initializer
740 // | variable_initializers COMMA variable_initializer
743 // 19.11) Blocks and Statements
744 block ::= LBRACE block_statements_opt:bso RBRACE {:
748 block_statements_opt ::=
749 {: RESULT=new ParseNode("empty"); :}
750 | block_statements:bs {:
755 block_statement:bs {:
756 ParseNode pn=new ParseNode("block_statement_list");
760 | block_statements:bss block_statement:bs {:
766 local_variable_declaration_statement:lvds {:
769 | statement:statement {:
772 // | class_declaration
773 // | interface_declaration
775 local_variable_declaration_statement ::=
776 local_variable_declaration:lvd SEMICOLON {:
780 local_variable_declaration ::=
781 type:type variable_declarators:var {:
782 ParseNode pn=new ParseNode("local_variable_declaration");
787 // | FINAL type variable_declarators
789 statement ::= statement_without_trailing_substatement:st {:
792 // | labeled_statement:st {: RESULT=st; :}
793 | if_then_statement:st {: RESULT=st; :}
794 | if_then_else_statement:st {: RESULT=st; :}
795 | while_statement:st {: RESULT=st; :}
796 | for_statement:st {: RESULT=st; :}
798 statement_no_short_if ::=
799 statement_without_trailing_substatement:st {: RESULT=st; :}
800 // | labeled_statement_no_short_if:st {: RESULT=st; :}
801 | if_then_else_statement_no_short_if:st {: RESULT=st; :}
802 | while_statement_no_short_if:st {: RESULT=st; :}
803 | for_statement_no_short_if:st {: RESULT=st; :}
805 statement_without_trailing_substatement ::=
806 block:st {: RESULT=st; :}
807 | empty_statement:st {: RESULT=st; :}
808 | expression_statement:st {: RESULT=st; :}
809 // | switch_statement
811 | break_statement:st {: RESULT=st; :}
812 | continue_statement:st {: RESULT=st; :}
813 | return_statement:st {: RESULT=st; :}
814 // | synchronized_statement
817 // | assert_statement
820 SEMICOLON {: RESULT=new ParseNode("nop"); :}
822 //labeled_statement ::=
823 // IDENTIFIER COLON statement
825 //labeled_statement_no_short_if ::=
826 // IDENTIFIER COLON statement_no_short_if
828 expression_statement ::=
829 statement_expression:se SEMICOLON {:
830 ParseNode pn=new ParseNode("expression");
834 statement_expression ::=
835 assignment:st {: RESULT=st; :}
836 | preincrement_expression:st {: RESULT=st; :}
837 | predecrement_expression:st {: RESULT=st; :}
838 | postincrement_expression:st {: RESULT=st; :}
839 | postdecrement_expression:st {: RESULT=st; :}
840 | method_invocation:st {: RESULT=st; :}
841 | class_instance_creation_expression:st {: RESULT=st; :}
843 if_then_statement ::=
844 IF LPAREN expression:exp RPAREN statement:st {:
845 ParseNode pn=new ParseNode("ifstatement");
846 pn.addChild("condition").addChild(exp);
847 pn.addChild("statement").addChild(st);
851 if_then_else_statement ::=
852 IF LPAREN expression:exp RPAREN statement_no_short_if:st
853 ELSE statement:else_st {:
854 ParseNode pn=new ParseNode("ifstatement");
855 pn.addChild("condition").addChild(exp);
856 pn.addChild("statement").addChild(st);
857 pn.addChild("else_statement").addChild(else_st);
861 if_then_else_statement_no_short_if ::=
862 IF LPAREN expression:exp RPAREN statement_no_short_if:st
863 ELSE statement_no_short_if:else_st {:
864 ParseNode pn=new ParseNode("ifstatement");
865 pn.addChild("condition").addChild(exp);
866 pn.addChild("statement").addChild(st);
867 pn.addChild("else_statement").addChild(else_st);
871 //switch_statement ::=
872 // SWITCH LPAREN expression RPAREN switch_block
875 // LBRACE switch_block_statement_groups switch_labels RBRACE
876 // | LBRACE switch_block_statement_groups RBRACE
877 // | LBRACE switch_labels RBRACE
880 //switch_block_statement_groups ::=
881 // switch_block_statement_group
882 // | switch_block_statement_groups switch_block_statement_group
884 //switch_block_statement_group ::=
885 // switch_labels block_statements
889 // | switch_labels switch_label
892 // CASE constant_expression COLON
897 WHILE LPAREN expression:exp RPAREN statement:st {:
898 ParseNode pn=new ParseNode("whilestatement");
899 pn.addChild("condition").addChild(exp);
900 pn.addChild("statement").addChild(st);
904 while_statement_no_short_if ::=
905 WHILE LPAREN expression:exp RPAREN statement_no_short_if:st {:
906 ParseNode pn=new ParseNode("whilestatement");
907 pn.addChild("condition").addChild(exp);
908 pn.addChild("statement").addChild(st);
913 DO statement:st WHILE LPAREN expression:exp RPAREN SEMICOLON {:
914 ParseNode pn=new ParseNode("dowhilestatement");
915 pn.addChild("condition").addChild(exp);
916 pn.addChild("statement").addChild(st);
921 FOR LPAREN for_init_opt:init SEMICOLON expression_opt:exp SEMICOLON
922 for_update_opt:update RPAREN statement:st {:
923 ParseNode pn=new ParseNode("forstatement");
924 pn.addChild("initializer").addChild(init);
925 pn.addChild("condition").addChild(exp);
926 pn.addChild("update").addChild(update);
927 pn.addChild("statement").addChild(st);
931 for_statement_no_short_if ::=
932 FOR LPAREN for_init_opt:init SEMICOLON expression_opt:exp SEMICOLON
933 for_update_opt:update RPAREN statement_no_short_if:st {:
934 ParseNode pn=new ParseNode("forstatement");
935 pn.addChild("initializer").addChild(init);
936 pn.addChild("condition").addChild(exp);
937 pn.addChild("update").addChild(update);
938 pn.addChild("statement").addChild(st);
943 {: RESULT=new ParseNode("empty"); :}
944 | for_init:init {: RESULT=init; :}
946 for_init ::= statement_expression_list:list {: RESULT=list; :}
947 | local_variable_declaration:decl {: RESULT=decl; :}
950 {: RESULT=new ParseNode("empty"); :}
951 | for_update:update {: RESULT=update; :}
953 for_update ::= statement_expression_list:list {: RESULT=list; :}
955 statement_expression_list ::=
956 statement_expression:expr {:
957 RESULT=(new ParseNode("statement_expression_list")).addChild(expr).getRoot();
959 | statement_expression_list:list COMMA statement_expression:expr {:
972 SEMICOLON {: RESULT=new ParseNode("break"); :}
975 continue_statement ::=
979 {: RESULT=new ParseNode("continue"); :}
982 RETURN expression_opt:exp SEMICOLON {:
983 RESULT=(new ParseNode("return")).addChild(exp).getRoot(); :}
985 //throw_statement ::=
986 // THROW expression SEMICOLON
988 //synchronized_statement ::=
989 // SYNCHRONIZED LPAREN expression RPAREN block
993 // | TRY block catches_opt finally
998 //catches ::= catch_clause
999 // | catches catch_clause
1002 // CATCH LPAREN formal_parameter RPAREN block
1004 //finally ::= FINALLY block
1006 //assert_statement ::=
1007 // ASSERT expression SEMICOLON
1008 // | ASSERT expression COLON expression SEMICOLON
1011 // 19.12) Expressions
1012 primary ::= primary_no_new_array:st {:
1014 // | array_creation_init:st {:
1017 | array_creation_uninit:st {:
1021 primary_no_new_array ::=
1022 literal:lit {: RESULT=lit; :}
1023 | THIS {: RESULT=new ParseNode("this"); :}
1024 | LPAREN expression:exp RPAREN {: RESULT=exp; :}
1025 | class_instance_creation_expression:exp {: RESULT=exp; :}
1026 | field_access:exp {: RESULT=exp; :}
1027 | method_invocation:exp {: RESULT=exp; :}
1028 | array_access:exp {: RESULT=exp; :}
1029 // | primitive_type DOT CLASS
1031 // | array_type DOT CLASS
1035 class_instance_creation_expression ::=
1036 NEW class_or_interface_type:type LPAREN argument_list_opt:args RPAREN {:
1037 ParseNode pn=new ParseNode("createobject");
1042 // | NEW class_or_interface_type LPAREN argument_list_opt RPAREN class_body
1043 // | primary DOT NEW IDENTIFIER
1044 // LPAREN argument_list_opt RPAREN {:
1047 // | primary DOT NEW IDENTIFIER
1048 // LPAREN argument_list_opt RPAREN class_body
1049 // | name DOT NEW IDENTIFIER
1050 // LPAREN argument_list_opt RPAREN
1051 // | name DOT NEW IDENTIFIER
1052 // LPAREN argument_list_opt RPAREN class_body
1054 argument_list_opt ::=
1055 {: RESULT=new ParseNode("empty"); :}
1056 | argument_list:args {: RESULT=args; :}
1060 ParseNode pn=new ParseNode("argument_list");
1064 | argument_list:list COMMA expression:exp {:
1069 array_creation_uninit ::=
1070 NEW primitive_type:type dim_exprs:dimexpr dims_opt:dims {:
1071 ParseNode pn=new ParseNode("createarray");
1073 pn.addChild(dimexpr);
1074 pn.addChild("dims_opt").setLiteral(dims);
1077 | NEW class_or_interface_type:type dim_exprs:dimexpr dims_opt:dims {:
1078 ParseNode pn=new ParseNode("createarray");
1080 pn.addChild(dimexpr);
1081 pn.addChild("dims_opt").setLiteral(dims);
1085 //array_creation_init ::=
1086 // NEW primitive_type dims array_initializer
1087 // | NEW class_or_interface_type dims array_initializer
1089 dim_exprs ::= dim_expr:exp {:
1090 ParseNode pn=new ParseNode("dim_exprs");
1093 | dim_exprs:base dim_expr:exp {:
1098 dim_expr ::= LBRACK expression:exp RBRACK {: RESULT=exp; :}
1100 dims_opt ::= {: RESULT=null; :}
1101 | dims:dims {: RESULT = dims; :}
1104 dims ::= LBRACK RBRACK {: RESULT=new Integer(0); :}
1105 | dims:dims LBRACK RBRACK {: RESULT=new Integer(dims.intValue()+1); :}
1109 primary:base DOT IDENTIFIER:id {:
1110 ParseNode pn=new ParseNode("fieldaccess");
1111 pn.addChild("base").addChild(base);
1112 pn.addChild("field").addChild(id);
1115 // | SUPER DOT IDENTIFIER
1116 // | name DOT SUPER DOT IDENTIFIER
1118 method_invocation ::=
1119 name:name LPAREN argument_list_opt:args RPAREN {:
1120 ParseNode pn=new ParseNode("methodinvoke1");
1125 | primary:base DOT IDENTIFIER:name LPAREN argument_list_opt:args RPAREN {:
1126 ParseNode pn=new ParseNode("methodinvoke2");
1127 pn.addChild("base").addChild(base);
1128 pn.addChild("id").addChild(name);
1132 // | SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
1133 // | name DOT SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
1136 name:name LBRACK expression:exp RBRACK {:
1137 ParseNode pn=new ParseNode("arrayaccess");
1138 pn.addChild("base").addChild(name);
1139 pn.addChild("index").addChild(exp);
1142 | primary_no_new_array:base LBRACK expression:exp RBRACK {:
1143 ParseNode pn=new ParseNode("arrayaccess");
1144 pn.addChild("base").addChild(base);
1145 pn.addChild("index").addChild(exp);
1148 // | array_creation_init:init LBRACK expression:exp RBRACK {:
1149 // ParseNode pn=new ParseNode("arrayaccess");
1150 // pn.addChild("init").addChild(init);
1151 // pn.addChild("index").addChild(exp);
1155 postfix_expression ::=
1158 | name:exp {: RESULT=exp; :}
1159 | postincrement_expression:exp {: RESULT=exp; :}
1160 | postdecrement_expression:exp {: RESULT=exp; :}
1162 postincrement_expression ::=
1163 postfix_expression:exp PLUSPLUS
1164 {: RESULT=(new ParseNode("postinc")).addChild(exp).getRoot(); :}
1166 postdecrement_expression ::=
1167 postfix_expression:exp MINUSMINUS
1168 {: RESULT=(new ParseNode("postdec")).addChild(exp).getRoot(); :}
1170 unary_expression ::=
1171 preincrement_expression:exp {: RESULT=exp; :}
1172 | predecrement_expression:exp {: RESULT=exp; :}
1173 | PLUS unary_expression:exp
1174 {: RESULT=(new ParseNode("unaryplus")).addChild(exp).getRoot(); :}
1175 | MINUS unary_expression:exp
1176 {: RESULT=(new ParseNode("unaryminus")).addChild(exp).getRoot(); :}
1177 | unary_expression_not_plus_minus:exp {:
1180 preincrement_expression ::=
1181 PLUSPLUS unary_expression:exp
1182 {: RESULT=(new ParseNode("preinc")).addChild(exp).getRoot(); :}
1184 predecrement_expression ::=
1185 MINUSMINUS unary_expression:exp
1186 {: RESULT=(new ParseNode("predec")).addChild(exp).getRoot(); :}
1188 unary_expression_not_plus_minus ::=
1189 postfix_expression:exp {:
1191 // | COMP unary_expression
1192 | NOT unary_expression:exp
1193 {: RESULT=(new ParseNode("not")).addChild(exp).getRoot(); :}
1194 | cast_expression:exp {: RESULT=exp; :}
1197 LPAREN primitive_type:type
1199 RPAREN unary_expression:exp {:
1200 ParseNode pn=new ParseNode("cast1");
1201 pn.addChild("type").addChild(type);
1202 pn.addChild("exp").addChild(exp);
1205 | LPAREN expression:type RPAREN unary_expression_not_plus_minus:exp {:
1206 ParseNode pn=new ParseNode("cast2");
1207 pn.addChild("type").addChild(type);
1208 pn.addChild("exp").addChild(exp);
1212 // | LPAREN name dims RPAREN unary_expression_not_plus_minus
1214 multiplicative_expression ::=
1215 unary_expression:exp {:
1217 | multiplicative_expression:exp1 MULT unary_expression:exp2 {:
1218 ParseNode pn=new ParseNode("mult");
1223 | multiplicative_expression:exp1 DIV unary_expression:exp2 {:
1224 ParseNode pn=new ParseNode("div");
1229 | multiplicative_expression:exp1 MOD unary_expression:exp2 {:
1230 ParseNode pn=new ParseNode("mod");
1236 additive_expression ::=
1237 multiplicative_expression:exp {:
1239 | additive_expression:exp1 PLUS multiplicative_expression:exp2 {:
1240 ParseNode pn=new ParseNode("add");
1245 | additive_expression:exp1 MINUS multiplicative_expression:exp2 {:
1246 ParseNode pn=new ParseNode("sub");
1252 shift_expression ::=
1253 additive_expression:exp {:
1255 | shift_expression:exp1 LSHIFT additive_expression:exp2 {:
1256 ParseNode pn=new ParseNode("leftshift");
1261 | shift_expression:exp1 RSHIFT additive_expression:exp2 {:
1262 ParseNode pn=new ParseNode("rightshift");
1267 // | shift_expression URSHIFT additive_expression
1269 relational_expression ::=
1270 shift_expression:exp {:
1272 | relational_expression:exp1 LT shift_expression:exp2 {:
1273 ParseNode pn=new ParseNode("comp_lt");
1278 | relational_expression:exp1 GT shift_expression:exp2 {:
1279 ParseNode pn=new ParseNode("comp_gt");
1284 | relational_expression:exp1 LTEQ shift_expression:exp2 {:
1285 ParseNode pn=new ParseNode("comp_lte");
1290 | relational_expression:exp1 GTEQ shift_expression:exp2 {:
1291 ParseNode pn=new ParseNode("comp_gte");
1296 // | relational_expression INSTANCEOF reference_type
1299 equality_expression ::=
1300 relational_expression:exp {:
1302 | equality_expression:exp1 EQEQ relational_expression:exp2 {:
1303 ParseNode pn=new ParseNode("equal");
1308 | equality_expression:exp1 NOTEQ relational_expression:exp2 {:
1309 ParseNode pn=new ParseNode("not_equal");
1316 equality_expression:exp {:
1318 | and_expression:exp1 AND equality_expression:exp2 {:
1319 ParseNode pn=new ParseNode("bitwise_and");
1325 exclusive_or_expression ::=
1326 and_expression:expr {:
1329 | exclusive_or_expression:exp1 XOR and_expression:exp2 {:
1330 ParseNode pn=new ParseNode("bitwise_xor");
1336 inclusive_or_expression ::=
1337 exclusive_or_expression:exclor {:
1339 | inclusive_or_expression:exp1 OR exclusive_or_expression:exp2 {:
1340 ParseNode pn=new ParseNode("bitwise_or");
1346 conditional_and_expression ::=
1347 inclusive_or_expression:inclor {:
1349 | conditional_and_expression:exp1 ANDAND inclusive_or_expression:exp2 {:
1350 ParseNode pn=new ParseNode("logical_and");
1356 conditional_or_expression ::=
1357 conditional_and_expression:condand {:
1359 | conditional_or_expression:exp1 OROR conditional_and_expression:exp2 {:
1360 ParseNode pn=new ParseNode("logical_or");
1366 conditional_expression ::=
1367 conditional_or_expression:condor {:
1369 // | conditional_or_expression QUESTION expression
1370 // COLON conditional_expression
1372 assignment_expression ::=
1373 conditional_expression:expr {:
1375 assignment:assign {:
1378 // semantic check necessary here to ensure a valid left-hand side.
1379 // allowing a parenthesized variable here on the lhs was introduced in
1380 // JLS 2; thanks to Eric Blake for pointing this out.
1381 assignment ::= postfix_expression:lvalue assignment_operator:op assignment_expression:rvalue {:
1382 ParseNode pn=new ParseNode("assignment");
1383 pn.addChild("op").addChild(op);
1384 ParseNode pnargs=pn.addChild("args");
1385 pnargs.addChild(lvalue);
1386 pnargs.addChild(rvalue);
1390 assignment_operator ::=
1391 EQ {: RESULT=new ParseNode("eq"); :}
1392 | MULTEQ {: RESULT=new ParseNode("multeq"); :}
1393 | DIVEQ {: RESULT=new ParseNode("diveq"); :}
1394 | MODEQ {: RESULT=new ParseNode("modeq"); :}
1395 | PLUSEQ {: RESULT=new ParseNode("pluseq"); :}
1396 | MINUSEQ {: RESULT=new ParseNode("minuseq"); :}
1397 | LSHIFTEQ {: RESULT=new ParseNode("lshifteq"); :}
1398 | RSHIFTEQ {: RESULT=new ParseNode("rshifteq"); :}
1399 // | URSHIFTEQ {: RESULT=new ParseNode("urshifteq"); :}
1400 | ANDEQ {: RESULT=new ParseNode("andeq"); :}
1401 | XOREQ {: RESULT=new ParseNode("xoreq"); :}
1402 | OREQ {: RESULT=new ParseNode("oreq"); :}
1405 {: RESULT=new ParseNode("empty"); :}
1409 expression ::= assignment_expression:exp {:
1412 //constant_expression ::=