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
107 non terminal ParseNode goal;
108 // 19.3) Lexical Structure
109 non terminal ParseNode literal;
110 // 19.4) Types, Values, and Variables
111 non terminal ParseNode type, primitive_type, numeric_type;
112 non terminal ParseNode integral_type, floating_point_type;
113 non terminal ParseNode reference_type;
114 non terminal ParseNode class_or_interface_type;
115 non terminal ParseNode class_type;
116 //non terminal ParseNode interface_type;
117 non terminal ParseNode array_type;
119 non terminal ParseNode name, simple_name, qualified_name;
121 non terminal ParseNode compilation_unit;
122 //non terminal ParseNode package_declaration_opt, package_declaration;
123 //non terminal ParseNode import_declarations_opt, import_declarations;
124 non terminal ParseNode type_declarations_opt, type_declarations;
125 //non terminal ParseNode import_declaration;
126 //non terminal ParseNode single_type_import_declaration;
127 //non terminal ParseNode type_import_on_demand_declaration;
128 non terminal ParseNode type_declaration;
129 // 19.7) Productions used only in the LALR(1) grammar
130 non terminal ParseNode modifiers_opt, modifiers, modifier;
131 // 19.8.1) Class Declaration
132 non terminal ParseNode class_declaration, super, super_opt;
133 //non terminal interfaces, interfaces_opt, interface_type_list;
134 non terminal ParseNode class_body;
135 non terminal ParseNode class_body_declarations, class_body_declarations_opt;
136 non terminal ParseNode class_body_declaration, class_member_declaration;
137 // 19.8.2) Field Declarations
138 non terminal ParseNode field_declaration, variable_declarators, variable_declarator;
139 non terminal ParseNode variable_declarator_id;
140 non terminal ParseNode variable_initializer;
141 // 19.8.3) Method Declarations
142 non terminal ParseNode method_declaration, method_header, method_declarator;
143 non terminal ParseNode formal_parameter_list_opt, formal_parameter_list;
144 non terminal ParseNode formal_parameter;
145 //non terminal ParseNode throws_opt;
146 //non terminal ParseNode throws;
147 //non terminal ParseNode class_type_list;
148 non terminal ParseNode method_body;
149 // 19.8.4) Static Initializers
150 //non terminal ParseNode static_initializer;
151 // 19.8.5) Constructor Declarations
152 non terminal ParseNode constructor_declaration, constructor_declarator;
153 non terminal ParseNode constructor_body;
154 //non terminal ParseNode explicit_constructor_invocation;
155 // 19.9.1) Interface Declarations
156 //non terminal ParseNode interface_declaration;
157 //non terminal ParseNode extends_interfaces_opt, extends_interfaces;
158 //non terminal ParseNode interface_body;
159 //non terminal ParseNode interface_member_declarations_opt, interface_member_declarations;
160 //non terminal ParseNode interface_member_declaration, constant_declaration;
161 //non terminal ParseNode abstract_method_declaration;
163 //non terminal ParseNode array_initializer;
164 //non terminal ParseNode variable_initializers;
165 // 19.11) Blocks and Statements
166 non terminal ParseNode block;
167 non terminal ParseNode block_statements_opt, block_statements, block_statement;
168 non terminal ParseNode local_variable_declaration_statement, local_variable_declaration;
169 non terminal ParseNode statement, statement_no_short_if;
170 non terminal ParseNode statement_without_trailing_substatement;
171 non terminal ParseNode empty_statement;
172 //non terminal ParseNode labeled_statement, labeled_statement_no_short_if;
173 non terminal ParseNode expression_statement, statement_expression;
174 non terminal ParseNode if_then_statement;
175 non terminal ParseNode if_then_else_statement, if_then_else_statement_no_short_if;
176 //non terminal ParseNode switch_statement, switch_block;
177 //non terminal ParseNode switch_block_statement_groups;
178 //non terminal ParseNode switch_block_statement_group;
179 //non terminal ParseNode switch_labels, switch_label;
180 non terminal ParseNode while_statement, while_statement_no_short_if;
181 non terminal ParseNode do_statement;
182 non terminal ParseNode for_statement, for_statement_no_short_if;
183 non terminal ParseNode for_init_opt, for_init;
184 non terminal ParseNode for_update_opt, for_update;
185 non terminal ParseNode statement_expression_list;
186 //non terminal ParseNode identifier_opt;
187 non terminal ParseNode break_statement, continue_statement;
188 non terminal ParseNode return_statement;
189 //non terminal ParseNode throw_statement;
190 //non terminal ParseNode synchronized_statement, try_statement;
191 //non terminal ParseNode catches_opt;
192 //non terminal ParseNode catches, catch_clause;
193 //non terminal ParseNode finally;
194 //non terminal ParseNode assert_statement;
195 // 19.12) Expressions
196 non terminal ParseNode primary, primary_no_new_array;
197 non terminal ParseNode class_instance_creation_expression;
198 non terminal ParseNode argument_list_opt, argument_list;
199 //non terminal ParseNode array_creation_init;
200 non terminal ParseNode array_creation_uninit;
201 non terminal ParseNode dim_exprs, dim_expr;
202 non terminal Integer dims_opt, dims;
203 non terminal ParseNode field_access, method_invocation;
204 non terminal ParseNode array_access;
205 non terminal ParseNode postfix_expression;
206 non terminal ParseNode postincrement_expression, postdecrement_expression;
207 non terminal ParseNode unary_expression, unary_expression_not_plus_minus;
208 non terminal ParseNode preincrement_expression, predecrement_expression;
209 non terminal ParseNode cast_expression;
210 non terminal ParseNode multiplicative_expression, additive_expression;
211 non terminal ParseNode shift_expression, relational_expression, equality_expression;
212 non terminal ParseNode and_expression, exclusive_or_expression, inclusive_or_expression;
213 non terminal ParseNode conditional_and_expression, conditional_or_expression;
214 non terminal ParseNode conditional_expression;
215 non terminal ParseNode assignment_expression;
216 non terminal ParseNode assignment;
217 non terminal ParseNode assignment_operator;
218 non terminal ParseNode expression_opt, expression;
219 //non terminal ParseNode constant_expression;
223 // 19.2) The Syntactic Grammar
224 goal ::= compilation_unit:cu
230 // 19.3) Lexical Structure.
233 literal ::= INTEGER_LITERAL:integer_lit
235 ParseNode pn=new ParseNode("literal");
236 pn.addChild("integer").setLiteral(integer_lit);
239 | FLOATING_POINT_LITERAL:float_lit
241 ParseNode pn=new ParseNode("literal");
242 pn.addChild("float").setLiteral(float_lit);
245 | BOOLEAN_LITERAL:boolean_lit
247 ParseNode pn=new ParseNode("literal");
248 pn.addChild("boolean").setLiteral(boolean_lit);
251 | CHARACTER_LITERAL:char_lit
253 ParseNode pn=new ParseNode("literal");
254 pn.addChild("char").setLiteral(char_lit);
257 | STRING_LITERAL:string_lit
259 ParseNode pn=new ParseNode("literal");
260 pn.addChild("string").setLiteral(string_lit);
265 RESULT=(new ParseNode("literal")).addChild("null").getRoot();
269 // 19.4) Types, Values, and Variables
270 type ::= primitive_type:type {: RESULT=type; :}
271 | reference_type:type {: RESULT=type; :}
275 numeric_type:type {: RESULT=type; :}
276 | BOOLEAN {: RESULT=(new ParseNode("type")).addChild("boolean").getRoot(); :}
278 numeric_type::= integral_type:type {: RESULT=type; :}
279 | floating_point_type:type {: RESULT=type; :}
282 BYTE {: RESULT=(new ParseNode("type")).addChild("byte").getRoot(); :}
283 | SHORT {: RESULT=(new ParseNode("type")).addChild("short").getRoot(); :}
284 | INT {: RESULT=(new ParseNode("type")).addChild("int").getRoot(); :}
285 | LONG {: RESULT=(new ParseNode("type")).addChild("long").getRoot(); :}
286 | CHAR {: RESULT=(new ParseNode("type")).addChild("char").getRoot(); :}
288 floating_point_type ::=
289 FLOAT {: RESULT=(new ParseNode("type")).addChild("float").getRoot(); :}
290 | DOUBLE {: RESULT=(new ParseNode("type")).addChild("double").getRoot(); :}
294 class_or_interface_type:type {: RESULT=type; :}
295 | array_type:type {: RESULT=type; :}
297 class_or_interface_type ::= name:name {:
298 RESULT=(new ParseNode("type")).addChild("class").addChild(name).getRoot();
301 class_type ::= class_or_interface_type:type {: RESULT=type; :};
302 //interface_type ::= class_or_interface_type;
304 array_type ::= primitive_type:prim dims:dims {:
305 ParseNode pn=(new ParseNode("type")).addChild("array");
306 pn.addChild("basetype").addChild(prim);
307 pn.addChild("dims").setLiteral(dims);
310 | name:name dims:dims {:
311 ParseNode pn=(new ParseNode("type")).addChild("array");
312 pn.addChild("basetype").addChild("type").addChild("class").addChild(name);
313 pn.addChild("dims").setLiteral(dims);
319 name ::= simple_name:name {: RESULT=name; :}
320 | qualified_name:name {: RESULT=name; :}
322 simple_name ::= IDENTIFIER:id {:
323 RESULT=(new ParseNode("name")).addChild("identifier").addChild(id).getRoot();
326 qualified_name ::= name:name DOT IDENTIFIER:id {:
327 ParseNode pn=new ParseNode("name");
328 pn.addChild("base").addChild(name);
329 pn.addChild("identifier").addChild(id);
336 // package_declaration_opt
337 // import_declarations_opt
338 type_declarations_opt:tdo {:
339 ParseNode pn=new ParseNode("compilation_unit");
344 //package_declaration_opt ::= package_declaration | ;
345 //import_declarations_opt ::= import_declarations | ;
346 type_declarations_opt ::= type_declarations:tds {:
349 {: RESULT=new ParseNode("empty"); :}
352 //import_declarations ::=
353 // import_declaration
354 // | import_declarations import_declaration
357 type_declarations ::=
358 type_declaration:td {:
359 ParseNode pn=new ParseNode("type_declaration_list");
363 | type_declarations:tds type_declaration:td {:
369 //package_declaration ::=
370 // PACKAGE name SEMICOLON
372 //import_declaration ::=
373 // single_type_import_declaration
374 // | type_import_on_demand_declaration
376 //single_type_import_declaration ::=
377 // IMPORT name SEMICOLON
379 //type_import_on_demand_declaration ::=
380 // IMPORT name DOT MULT SEMICOLON
388 // | interface_declaration
389 | SEMICOLON {: RESULT=new ParseNode("empty"); :}
392 // 19.7) Productions used only in the LALR(1) grammar
394 {: RESULT=new ParseNode("empty"); :}
399 modifiers ::= modifier:mo {:
400 ParseNode pn=new ParseNode("modifier_list");
404 | modifiers:mos modifier:mo {:
410 PUBLIC {: RESULT=new ParseNode("public"); :}|
411 PROTECTED {: RESULT=new ParseNode("protected"); :}|
412 PRIVATE {: RESULT=new ParseNode("private"); :}|
413 STATIC {: RESULT=new ParseNode("static"); :} |
415 FINAL {: RESULT=new ParseNode("final"); :}|
416 NATIVE {: RESULT=new ParseNode("native"); :}
420 // STRICTFP // note that semantic analysis must check that the
421 // context of the modifier allows strictfp.
426 // 19.8.1) Class Declaration:
427 class_declaration ::=
428 modifiers_opt:mo CLASS IDENTIFIER:id super_opt:so //interfaces_opt
431 ParseNode pn=new ParseNode("class_declaration");
432 pn.addChild("modifiers").addChild(mo);
433 pn.addChild("name").addChild(id);
434 pn.addChild("super").addChild(so);
435 pn.addChild("classbody").addChild(body);
439 super ::= EXTENDS class_type:classtype {:
444 {: RESULT=new ParseNode("empty"); :}
450 //interfaces ::= IMPLEMENTS interface_type_list
455 //interface_type_list ::=
457 // | interface_type_list COMMA interface_type
460 class_body ::= LBRACE class_body_declarations_opt:cbdo RBRACE {: RESULT=cbdo; :}
463 class_body_declarations_opt ::=
464 {: RESULT=new ParseNode("empty"); :}
465 | class_body_declarations:cbd {: RESULT=cbd; :};
467 class_body_declarations ::=
468 class_body_declaration:cbd {:
469 ParseNode pn=new ParseNode("class_body_declaration_list");
473 | class_body_declarations:cbds class_body_declaration:cbd {:
479 class_body_declaration ::=
480 class_member_declaration:member {:
481 RESULT=(new ParseNode("member")).addChild(member).getRoot();
483 // | static_initializer
484 | constructor_declaration:constructor {:
485 RESULT=(new ParseNode("constructor")).addChild(constructor).getRoot();
488 RESULT=(new ParseNode("block")).addChild(block).getRoot();
491 class_member_declaration ::=
492 field_declaration:field {:
493 RESULT=(new ParseNode("field")).addChild(field).getRoot();
495 | method_declaration:method {:
496 RESULT=(new ParseNode("method")).addChild(method).getRoot();
498 /* repeat the prod for 'class_declaration' here: */
499 // | modifiers_opt CLASS IDENTIFIER super_opt class_body
500 // | interface_declaration
501 | SEMICOLON {: RESULT=new ParseNode("empty"); :}
504 // 19.8.2) Field Declarations
505 field_declaration ::=
506 modifiers_opt:mo type:type variable_declarators:var SEMICOLON {:
507 ParseNode pn=new ParseNode("field_declaration");
508 pn.addChild("modifier").addChild(mo);
509 pn.addChild("type").addChild(type);
510 pn.addChild("variables").addChild(var);
515 variable_declarators ::=
516 variable_declarator:vd {:
517 ParseNode pn=new ParseNode("variable_declarators_list");
521 | variable_declarators:vds COMMA variable_declarator:vd {:
526 variable_declarator ::=
527 variable_declarator_id:id {:
528 ParseNode pn=new ParseNode("variable_declarator");
532 | variable_declarator_id:id EQ variable_initializer:init {:
533 ParseNode pn=new ParseNode("variable_declarator");
535 pn.addChild("initializer").addChild(init);
539 variable_declarator_id ::=
541 RESULT=(new ParseNode("single")).addChild(id).getRoot();:}
542 | variable_declarator_id:id LBRACK RBRACK {:
543 RESULT=(new ParseNode("array")).addChild(id).getRoot();:}
545 variable_initializer ::=
546 expression:exp {: RESULT=exp; :}
547 // | array_initializer
550 // 19.8.3) Method Declarations
551 method_declaration ::=
552 method_header:header method_body:body {:
553 ParseNode pn=new ParseNode("method_declaration");
555 pn.addChild("body").addChild(body);
560 modifiers_opt:mo type:type method_declarator:decl //throws_opt
562 ParseNode pn=new ParseNode("method_header");
563 pn.addChild("modifiers").addChild(mo);
564 pn.addChild("returntype").addChild(type);
568 | modifiers_opt:mo VOID method_declarator:decl //throws_opt
570 ParseNode pn=new ParseNode("method_header");
571 pn.addChild("modifiers").addChild(mo);
576 method_declarator ::=
577 IDENTIFIER:id LPAREN formal_parameter_list_opt:params RPAREN {:
578 ParseNode pn=new ParseNode("method_declarator");
579 pn.addChild("name").addChild(id);
580 pn.addChild("parameters").addChild(params);
583 // | method_declarator LBRACK RBRACK // deprecated
584 // be careful; the above production also allows 'void foo() []'
586 formal_parameter_list_opt ::=
587 {: RESULT=new ParseNode("empty"); :}
588 | formal_parameter_list:fpl {:
592 formal_parameter_list ::=
593 formal_parameter:fp {:
594 ParseNode pn=new ParseNode("formal_parameter_list");
598 | formal_parameter_list:fpl COMMA formal_parameter:fp {:
604 type:type variable_declarator_id:name {:
605 ParseNode pn=new ParseNode("formal_parameter");
610 // | FINAL type variable_declarator_id
615 //throws ::= THROWS class_type_list
617 //class_type_list ::=
619 // | class_type_list COMMA class_type
621 method_body ::= block:block {:
624 | SEMICOLON {: RESULT=new ParseNode("empty"); :}
627 // 19.8.4) Static Initializers
628 //static_initializer ::=
632 // 19.8.5) Constructor Declarations
633 constructor_declaration ::=
634 modifiers_opt:mo constructor_declarator:cd
636 constructor_body:body {:
637 ParseNode pn=new ParseNode("constructor_declaration");
638 pn.addChild("modifiers").addChild(mo);
640 pn.addChild("body").addChild(body);
644 constructor_declarator ::=
645 simple_name:name LPAREN formal_parameter_list_opt:fplo RPAREN {:
646 ParseNode pn=new ParseNode("constructor_declarator");
648 pn.addChild("parameters").addChild(fplo);
653 // LBRACE explicit_constructor_invocation:eci block_statements:bs RBRACE |
654 // LBRACE explicit_constructor_invocation RBRACE |
655 LBRACE block_statements:block RBRACE {:
656 ParseNode pn=new ParseNode("constructor_body");
660 | LBRACE RBRACE {: RESULT=new ParseNode("empty"); :}
662 //explicit_constructor_invocation ::=
663 // THIS LPAREN argument_list_opt RPAREN SEMICOLON
664 // | SUPER LPAREN argument_list_opt RPAREN SEMICOLON
665 // | primary DOT THIS LPAREN argument_list_opt RPAREN SEMICOLON
666 // | primary DOT SUPER LPAREN argument_list_opt RPAREN SEMICOLON
671 // 19.9.1) Interface Declarations
672 //interface_declaration ::=
673 // modifiers_opt INTERFACE IDENTIFIER extends_interfaces_opt
676 //extends_interfaces_opt ::=
677 // | extends_interfaces
679 //extends_interfaces ::=
680 // EXTENDS interface_type
681 // | extends_interfaces COMMA interface_type
684 // LBRACE interface_member_declarations_opt RBRACE
686 //interface_member_declarations_opt ::=
687 // | interface_member_declarations
689 //interface_member_declarations ::=
690 // interface_member_declaration
691 // | interface_member_declarations interface_member_declaration
693 //interface_member_declaration ::=
694 // constant_declaration
695 // | abstract_method_declaration
696 // | class_declaration
697 // | interface_declaration
700 //constant_declaration ::=
702 // // need to semantically check that modifiers of field declaration
703 // // include only PUBLIC, STATIC, or FINAL. Other modifiers are
706 //abstract_method_declaration ::=
707 // method_header SEMICOLON
712 //array_initializer ::=
713 // LBRACE variable_initializers COMMA RBRACE
714 // | LBRACE variable_initializers RBRACE
715 // | LBRACE COMMA RBRACE
718 //variable_initializers ::=
719 // variable_initializer
720 // | variable_initializers COMMA variable_initializer
723 // 19.11) Blocks and Statements
724 block ::= LBRACE block_statements_opt:bso RBRACE {:
728 block_statements_opt ::=
729 {: RESULT=new ParseNode("empty"); :}
730 | block_statements:bs {:
735 block_statement:bs {:
736 ParseNode pn=new ParseNode("block_statement_list");
740 | block_statements:bss block_statement:bs {:
746 local_variable_declaration_statement:lvds {:
749 | statement:statement {:
752 // | class_declaration
753 // | interface_declaration
755 local_variable_declaration_statement ::=
756 local_variable_declaration:lvd SEMICOLON {:
760 local_variable_declaration ::=
761 type:type variable_declarators:var {:
762 ParseNode pn=new ParseNode("local_variable_declaration");
767 // | FINAL type variable_declarators
769 statement ::= statement_without_trailing_substatement:st {:
772 // | labeled_statement:st {: RESULT=st; :}
773 | if_then_statement:st {: RESULT=st; :}
774 | if_then_else_statement:st {: RESULT=st; :}
775 | while_statement:st {: RESULT=st; :}
776 | for_statement:st {: RESULT=st; :}
778 statement_no_short_if ::=
779 statement_without_trailing_substatement:st {: RESULT=st; :}
780 // | labeled_statement_no_short_if:st {: RESULT=st; :}
781 | if_then_else_statement_no_short_if:st {: RESULT=st; :}
782 | while_statement_no_short_if:st {: RESULT=st; :}
783 | for_statement_no_short_if:st {: RESULT=st; :}
785 statement_without_trailing_substatement ::=
786 block:st {: RESULT=st; :}
787 | empty_statement:st {: RESULT=st; :}
788 | expression_statement:st {: RESULT=st; :}
789 // | switch_statement
791 | break_statement:st {: RESULT=st; :}
792 | continue_statement:st {: RESULT=st; :}
793 | return_statement:st {: RESULT=st; :}
794 // | synchronized_statement
797 // | assert_statement
800 SEMICOLON {: RESULT=new ParseNode("nop"); :}
802 //labeled_statement ::=
803 // IDENTIFIER COLON statement
805 //labeled_statement_no_short_if ::=
806 // IDENTIFIER COLON statement_no_short_if
808 expression_statement ::=
809 statement_expression:se SEMICOLON {:
810 ParseNode pn=new ParseNode("expression");
814 statement_expression ::=
815 assignment:st {: RESULT=st; :}
816 | preincrement_expression:st {: RESULT=st; :}
817 | predecrement_expression:st {: RESULT=st; :}
818 | postincrement_expression:st {: RESULT=st; :}
819 | postdecrement_expression:st {: RESULT=st; :}
820 | method_invocation:st {: RESULT=st; :}
821 | class_instance_creation_expression:st {: RESULT=st; :}
823 if_then_statement ::=
824 IF LPAREN expression:exp RPAREN statement:st {:
825 ParseNode pn=new ParseNode("ifstatement");
826 pn.addChild("condition").addChild(exp);
827 pn.addChild("statement").addChild(st);
831 if_then_else_statement ::=
832 IF LPAREN expression:exp RPAREN statement_no_short_if:st
833 ELSE statement:else_st {:
834 ParseNode pn=new ParseNode("ifstatement");
835 pn.addChild("condition").addChild(exp);
836 pn.addChild("statement").addChild(st);
837 pn.addChild("else_statement").addChild(else_st);
841 if_then_else_statement_no_short_if ::=
842 IF LPAREN expression:exp RPAREN statement_no_short_if:st
843 ELSE statement_no_short_if:else_st {:
844 ParseNode pn=new ParseNode("ifstatement");
845 pn.addChild("condition").addChild(exp);
846 pn.addChild("statement").addChild(st);
847 pn.addChild("else_statement").addChild(else_st);
851 //switch_statement ::=
852 // SWITCH LPAREN expression RPAREN switch_block
855 // LBRACE switch_block_statement_groups switch_labels RBRACE
856 // | LBRACE switch_block_statement_groups RBRACE
857 // | LBRACE switch_labels RBRACE
860 //switch_block_statement_groups ::=
861 // switch_block_statement_group
862 // | switch_block_statement_groups switch_block_statement_group
864 //switch_block_statement_group ::=
865 // switch_labels block_statements
869 // | switch_labels switch_label
872 // CASE constant_expression COLON
877 WHILE LPAREN expression:exp RPAREN statement:st {:
878 ParseNode pn=new ParseNode("whilestatement");
879 pn.addChild("condition").addChild(exp);
880 pn.addChild("statement").addChild(st);
884 while_statement_no_short_if ::=
885 WHILE LPAREN expression:exp RPAREN statement_no_short_if:st {:
886 ParseNode pn=new ParseNode("whilestatement");
887 pn.addChild("condition").addChild(exp);
888 pn.addChild("statement").addChild(st);
893 DO statement:st WHILE LPAREN expression:exp RPAREN SEMICOLON {:
894 ParseNode pn=new ParseNode("dowhilestatement");
895 pn.addChild("condition").addChild(exp);
896 pn.addChild("statement").addChild(st);
901 FOR LPAREN for_init_opt:init SEMICOLON expression_opt:exp SEMICOLON
902 for_update_opt:update RPAREN statement:st {:
903 ParseNode pn=new ParseNode("forstatement");
904 pn.addChild("initializer").addChild(init);
905 pn.addChild("condition").addChild(exp);
906 pn.addChild("update").addChild(update);
907 pn.addChild("statement").addChild(st);
911 for_statement_no_short_if ::=
912 FOR LPAREN for_init_opt:init SEMICOLON expression_opt:exp SEMICOLON
913 for_update_opt:update RPAREN statement_no_short_if:st {:
914 ParseNode pn=new ParseNode("forstatement");
915 pn.addChild("initializer").addChild(init);
916 pn.addChild("condition").addChild(exp);
917 pn.addChild("update").addChild(update);
918 pn.addChild("statement").addChild(st);
923 {: RESULT=new ParseNode("empty"); :}
924 | for_init:init {: RESULT=init; :}
926 for_init ::= statement_expression_list:list {: RESULT=list; :}
927 | local_variable_declaration:decl {: RESULT=decl; :}
930 {: RESULT=new ParseNode("empty"); :}
931 | for_update:update {: RESULT=update; :}
933 for_update ::= statement_expression_list:list {: RESULT=list; :}
935 statement_expression_list ::=
936 statement_expression:expr {:
937 RESULT=(new ParseNode("statement_expression_list")).addChild(expr).getRoot();
939 | statement_expression_list:list COMMA statement_expression:expr {:
952 SEMICOLON {: RESULT=new ParseNode("break"); :}
955 continue_statement ::=
959 {: RESULT=new ParseNode("continue"); :}
962 RETURN expression_opt:exp SEMICOLON {:
963 RESULT=(new ParseNode("return")).addChild(exp).getRoot(); :}
965 //throw_statement ::=
966 // THROW expression SEMICOLON
968 //synchronized_statement ::=
969 // SYNCHRONIZED LPAREN expression RPAREN block
973 // | TRY block catches_opt finally
978 //catches ::= catch_clause
979 // | catches catch_clause
982 // CATCH LPAREN formal_parameter RPAREN block
984 //finally ::= FINALLY block
986 //assert_statement ::=
987 // ASSERT expression SEMICOLON
988 // | ASSERT expression COLON expression SEMICOLON
991 // 19.12) Expressions
992 primary ::= primary_no_new_array:st {:
994 // | array_creation_init:st {:
997 | array_creation_uninit:st {:
1001 primary_no_new_array ::=
1002 literal:lit {: RESULT=lit; :}
1003 | THIS {: RESULT=new ParseNode("this"); :}
1004 | LPAREN expression:exp RPAREN {: RESULT=exp; :}
1005 | class_instance_creation_expression:exp {: RESULT=exp; :}
1006 | field_access:exp {: RESULT=exp; :}
1007 | method_invocation:exp {: RESULT=exp; :}
1008 | array_access:exp {: RESULT=exp; :}
1009 // | primitive_type DOT CLASS
1011 // | array_type DOT CLASS
1015 class_instance_creation_expression ::=
1016 NEW class_or_interface_type:type LPAREN argument_list_opt:args RPAREN {:
1017 ParseNode pn=new ParseNode("createobject");
1022 // | NEW class_or_interface_type LPAREN argument_list_opt RPAREN class_body
1023 // | primary DOT NEW IDENTIFIER
1024 // LPAREN argument_list_opt RPAREN {:
1027 // | primary DOT NEW IDENTIFIER
1028 // LPAREN argument_list_opt RPAREN class_body
1029 // | name DOT NEW IDENTIFIER
1030 // LPAREN argument_list_opt RPAREN
1031 // | name DOT NEW IDENTIFIER
1032 // LPAREN argument_list_opt RPAREN class_body
1034 argument_list_opt ::=
1035 {: RESULT=new ParseNode("empty"); :}
1036 | argument_list:args {: RESULT=args; :}
1040 ParseNode pn=new ParseNode("argument_list");
1044 | argument_list:list COMMA expression:exp {:
1049 array_creation_uninit ::=
1050 NEW primitive_type:type dim_exprs:dimexpr dims_opt:dims {:
1051 ParseNode pn=new ParseNode("createarray");
1053 pn.addChild(dimexpr);
1054 pn.addChild("dims_opt").setLiteral(dims);
1057 | NEW class_or_interface_type:type dim_exprs:dimexpr dims_opt:dims {:
1058 ParseNode pn=new ParseNode("createarray");
1060 pn.addChild(dimexpr);
1061 pn.addChild("dims_opt").setLiteral(dims);
1065 //array_creation_init ::=
1066 // NEW primitive_type dims array_initializer
1067 // | NEW class_or_interface_type dims array_initializer
1069 dim_exprs ::= dim_expr:exp {:
1070 ParseNode pn=new ParseNode("dim_exprs");
1073 | dim_exprs:base dim_expr:exp {:
1078 dim_expr ::= LBRACK expression:exp RBRACK {: RESULT=exp; :}
1080 dims_opt ::= {: RESULT=null; :}
1081 | dims:dims {: RESULT = dims; :}
1084 dims ::= LBRACK RBRACK {: RESULT=new Integer(0); :}
1085 | dims:dims LBRACK RBRACK {: RESULT=new Integer(dims.intValue()+1); :}
1089 primary:base DOT IDENTIFIER:id {:
1090 ParseNode pn=new ParseNode("fieldaccess");
1091 pn.addChild("base").addChild(base);
1092 pn.addChild("field").addChild(id);
1095 // | SUPER DOT IDENTIFIER
1096 // | name DOT SUPER DOT IDENTIFIER
1098 method_invocation ::=
1099 name:name LPAREN argument_list_opt:args RPAREN {:
1100 ParseNode pn=new ParseNode("methodinvoke1");
1105 | primary:base DOT IDENTIFIER:name LPAREN argument_list_opt:args RPAREN {:
1106 ParseNode pn=new ParseNode("methodinvoke2");
1107 pn.addChild("base").addChild(base);
1108 pn.addChild("id").addChild(name);
1112 // | SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
1113 // | name DOT SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
1116 name:name LBRACK expression:exp RBRACK {:
1117 ParseNode pn=new ParseNode("arrayaccess");
1118 pn.addChild("base").addChild(name);
1119 pn.addChild("index").addChild(exp);
1122 | primary_no_new_array:base LBRACK expression:exp RBRACK {:
1123 ParseNode pn=new ParseNode("arrayaccess");
1124 pn.addChild("base").addChild(base);
1125 pn.addChild("index").addChild(exp);
1128 // | array_creation_init:init LBRACK expression:exp RBRACK {:
1129 // ParseNode pn=new ParseNode("arrayaccess");
1130 // pn.addChild("init").addChild(init);
1131 // pn.addChild("index").addChild(exp);
1135 postfix_expression ::=
1138 | name:exp {: RESULT=exp; :}
1139 | postincrement_expression:exp {: RESULT=exp; :}
1140 | postdecrement_expression:exp {: RESULT=exp; :}
1142 postincrement_expression ::=
1143 postfix_expression:exp PLUSPLUS
1144 {: RESULT=(new ParseNode("postinc")).addChild(exp).getRoot(); :}
1146 postdecrement_expression ::=
1147 postfix_expression:exp MINUSMINUS
1148 {: RESULT=(new ParseNode("postdec")).addChild(exp).getRoot(); :}
1150 unary_expression ::=
1151 preincrement_expression:exp {: RESULT=exp; :}
1152 | predecrement_expression:exp {: RESULT=exp; :}
1153 | PLUS unary_expression:exp
1154 {: RESULT=(new ParseNode("unaryplus")).addChild(exp).getRoot(); :}
1155 | MINUS unary_expression:exp
1156 {: RESULT=(new ParseNode("unaryminus")).addChild(exp).getRoot(); :}
1157 | unary_expression_not_plus_minus:exp {:
1160 preincrement_expression ::=
1161 PLUSPLUS unary_expression:exp
1162 {: RESULT=(new ParseNode("preinc")).addChild(exp).getRoot(); :}
1164 predecrement_expression ::=
1165 MINUSMINUS unary_expression:exp
1166 {: RESULT=(new ParseNode("predec")).addChild(exp).getRoot(); :}
1168 unary_expression_not_plus_minus ::=
1169 postfix_expression:exp {:
1171 // | COMP unary_expression
1172 | NOT unary_expression:exp
1173 {: RESULT=(new ParseNode("not")).addChild(exp).getRoot(); :}
1174 | cast_expression:exp {: RESULT=exp; :}
1177 LPAREN primitive_type:type
1179 RPAREN unary_expression:exp {:
1180 ParseNode pn=new ParseNode("cast1");
1181 pn.addChild("type").addChild(type);
1182 pn.addChild("exp").addChild(exp);
1185 | LPAREN expression:type RPAREN unary_expression_not_plus_minus:exp {:
1186 ParseNode pn=new ParseNode("cast2");
1187 pn.addChild("type").addChild(type);
1188 pn.addChild("exp").addChild(exp);
1192 // | LPAREN name dims RPAREN unary_expression_not_plus_minus
1194 multiplicative_expression ::=
1195 unary_expression:exp {:
1197 | multiplicative_expression:exp1 MULT unary_expression:exp2 {:
1198 ParseNode pn=new ParseNode("mult");
1203 | multiplicative_expression:exp1 DIV unary_expression:exp2 {:
1204 ParseNode pn=new ParseNode("div");
1209 | multiplicative_expression:exp1 MOD unary_expression:exp2 {:
1210 ParseNode pn=new ParseNode("mod");
1216 additive_expression ::=
1217 multiplicative_expression:exp {:
1219 | additive_expression:exp1 PLUS multiplicative_expression:exp2 {:
1220 ParseNode pn=new ParseNode("add");
1225 | additive_expression:exp1 MINUS multiplicative_expression:exp2 {:
1226 ParseNode pn=new ParseNode("sub");
1232 shift_expression ::=
1233 additive_expression:exp {:
1235 | shift_expression:exp1 LSHIFT additive_expression:exp2 {:
1236 ParseNode pn=new ParseNode("leftshift");
1241 | shift_expression:exp1 RSHIFT additive_expression:exp2 {:
1242 ParseNode pn=new ParseNode("rightshift");
1247 // | shift_expression URSHIFT additive_expression
1249 relational_expression ::=
1250 shift_expression:exp {:
1252 | relational_expression:exp1 LT shift_expression:exp2 {:
1253 ParseNode pn=new ParseNode("comp_lt");
1258 | relational_expression:exp1 GT shift_expression:exp2 {:
1259 ParseNode pn=new ParseNode("comp_gt");
1264 | relational_expression:exp1 LTEQ shift_expression:exp2 {:
1265 ParseNode pn=new ParseNode("comp_lte");
1270 | relational_expression:exp1 GTEQ shift_expression:exp2 {:
1271 ParseNode pn=new ParseNode("comp_gte");
1276 // | relational_expression INSTANCEOF reference_type
1279 equality_expression ::=
1280 relational_expression:exp {:
1282 | equality_expression:exp1 EQEQ relational_expression:exp2 {:
1283 ParseNode pn=new ParseNode("equal");
1288 | equality_expression:exp1 NOTEQ relational_expression:exp2 {:
1289 ParseNode pn=new ParseNode("not_equal");
1296 equality_expression:exp {:
1298 | and_expression:exp1 AND equality_expression:exp2 {:
1299 ParseNode pn=new ParseNode("bitwise_and");
1305 exclusive_or_expression ::=
1306 and_expression:expr {:
1309 | exclusive_or_expression:exp1 XOR and_expression:exp2 {:
1310 ParseNode pn=new ParseNode("bitwise_xor");
1316 inclusive_or_expression ::=
1317 exclusive_or_expression:exclor {:
1319 | inclusive_or_expression:exp1 OR exclusive_or_expression:exp2 {:
1320 ParseNode pn=new ParseNode("bitwise_or");
1326 conditional_and_expression ::=
1327 inclusive_or_expression:inclor {:
1329 | conditional_and_expression:exp1 ANDAND inclusive_or_expression:exp2 {:
1330 ParseNode pn=new ParseNode("logical_and");
1336 conditional_or_expression ::=
1337 conditional_and_expression:condand {:
1339 | conditional_or_expression:exp1 OROR conditional_and_expression:exp2 {:
1340 ParseNode pn=new ParseNode("logical_or");
1346 conditional_expression ::=
1347 conditional_or_expression:condor {:
1349 // | conditional_or_expression QUESTION expression
1350 // COLON conditional_expression
1352 assignment_expression ::=
1353 conditional_expression:expr {:
1355 assignment:assign {:
1358 // semantic check necessary here to ensure a valid left-hand side.
1359 // allowing a parenthesized variable here on the lhs was introduced in
1360 // JLS 2; thanks to Eric Blake for pointing this out.
1361 assignment ::= postfix_expression:lvalue assignment_operator:op assignment_expression:rvalue {:
1362 ParseNode pn=new ParseNode("assignment");
1363 pn.addChild("op").addChild(op);
1364 ParseNode pnargs=pn.addChild("args");
1365 pnargs.addChild(lvalue);
1366 pnargs.addChild(rvalue);
1370 assignment_operator ::=
1371 EQ {: RESULT=new ParseNode("eq"); :}
1372 | MULTEQ {: RESULT=new ParseNode("multeq"); :}
1373 | DIVEQ {: RESULT=new ParseNode("diveq"); :}
1374 | MODEQ {: RESULT=new ParseNode("modeq"); :}
1375 | PLUSEQ {: RESULT=new ParseNode("pluseq"); :}
1376 | MINUSEQ {: RESULT=new ParseNode("minuseq"); :}
1377 | LSHIFTEQ {: RESULT=new ParseNode("lshifteq"); :}
1378 | RSHIFTEQ {: RESULT=new ParseNode("rshifteq"); :}
1379 // | URSHIFTEQ {: RESULT=new ParseNode("urshifteq"); :}
1380 | ANDEQ {: RESULT=new ParseNode("andeq"); :}
1381 | XOREQ {: RESULT=new ParseNode("xoreq"); :}
1382 | OREQ {: RESULT=new ParseNode("oreq"); :}
1385 {: RESULT=new ParseNode("empty"); :}
1389 expression ::= assignment_expression:exp {:
1392 //constant_expression ::=