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, array_creation_uninit;
200 //non terminal ParseNode dim_exprs, dim_expr, dims_opt, dims;
201 non terminal ParseNode field_access, method_invocation;
202 //non terminal ParseNode array_access;
203 non terminal ParseNode postfix_expression;
204 non terminal ParseNode postincrement_expression, postdecrement_expression;
205 non terminal ParseNode unary_expression, unary_expression_not_plus_minus;
206 non terminal ParseNode preincrement_expression, predecrement_expression;
207 non terminal ParseNode cast_expression;
208 non terminal ParseNode multiplicative_expression, additive_expression;
209 non terminal ParseNode shift_expression, relational_expression, equality_expression;
210 non terminal ParseNode and_expression, exclusive_or_expression, inclusive_or_expression;
211 non terminal ParseNode conditional_and_expression, conditional_or_expression;
212 non terminal ParseNode conditional_expression;
213 non terminal ParseNode assignment_expression;
214 non terminal ParseNode assignment;
215 non terminal ParseNode assignment_operator;
216 non terminal ParseNode expression_opt, expression;
217 //non terminal ParseNode constant_expression;
221 // 19.2) The Syntactic Grammar
222 goal ::= compilation_unit:cu
224 RESULT = (new ParseNode("goal")).addChild(cu).getRoot();
228 // 19.3) Lexical Structure.
231 literal ::= INTEGER_LITERAL:integer_lit
233 ParseNode pn=new ParseNode("literal");
234 pn.addChild("integer").setLiteral(integer_lit);
237 | FLOATING_POINT_LITERAL:float_lit
239 ParseNode pn=new ParseNode("literal");
240 pn.addChild("float").setLiteral(float_lit);
243 | BOOLEAN_LITERAL:boolean_lit
245 ParseNode pn=new ParseNode("literal");
246 pn.addChild("boolean").setLiteral(boolean_lit);
249 | CHARACTER_LITERAL:char_lit
251 ParseNode pn=new ParseNode("literal");
252 pn.addChild("char").setLiteral(char_lit);
255 | STRING_LITERAL:string_lit
257 ParseNode pn=new ParseNode("literal");
258 pn.addChild("string").setLiteral(string_lit);
263 RESULT=(new ParseNode("literal")).addChild("null").getRoot();
267 // 19.4) Types, Values, and Variables
268 type ::= primitive_type:type {: RESULT=type; :}
269 | reference_type:type {: RESULT=type; :}
272 numeric_type:type {: RESULT=type; :}
273 | BOOLEAN {: RESULT=(new ParseNode("type")).addChild("boolean").getRoot(); :}
275 numeric_type::= integral_type:type {: RESULT=type; :}
276 | floating_point_type:type {: RESULT=type; :}
279 BYTE {: RESULT=(new ParseNode("type")).addChild("byte").getRoot(); :}
280 | SHORT {: RESULT=(new ParseNode("type")).addChild("short").getRoot(); :}
281 | INT {: RESULT=(new ParseNode("type")).addChild("int").getRoot(); :}
282 | LONG {: RESULT=(new ParseNode("type")).addChild("long").getRoot(); :}
283 | CHAR {: RESULT=(new ParseNode("type")).addChild("char").getRoot(); :}
285 floating_point_type ::=
286 FLOAT {: RESULT=(new ParseNode("type")).addChild("float").getRoot(); :}
287 | DOUBLE {: RESULT=(new ParseNode("type")).addChild("double").getRoot(); :}
291 class_or_interface_type:type {: RESULT=type; :}
292 // | array_type:type {: RESULT=type; :}
294 class_or_interface_type ::= name:name {:
295 RESULT=(new ParseNode("type")).addChild("class").addChild(name).getRoot();
298 class_type ::= class_or_interface_type:type {: RESULT=type; :};
299 //interface_type ::= class_or_interface_type;
301 //array_type ::= primitive_type dims
306 name ::= simple_name:name {: RESULT=name; :}
307 | qualified_name:name {: RESULT=name; :}
309 simple_name ::= IDENTIFIER:id {:
310 RESULT=(new ParseNode("name")).addChild("identifier").addChild(id).getRoot();
313 qualified_name ::= name:name DOT IDENTIFIER:id {:
314 ParseNode pn=new ParseNode("name");
315 pn.addChild("base").addChild(name);
316 pn.addChild("identifier").addChild(id);
323 // package_declaration_opt
324 // import_declarations_opt
325 type_declarations_opt:tdo {:
326 ParseNode pn=new ParseNode("compilation_unit");
327 pn.addChild("types").addChild(tdo);
331 //package_declaration_opt ::= package_declaration | ;
332 //import_declarations_opt ::= import_declarations | ;
333 type_declarations_opt ::= type_declarations:tds {:
336 {: RESULT=new ParseNode("empty"); :}
339 //import_declarations ::=
340 // import_declaration
341 // | import_declarations import_declaration
344 type_declarations ::=
345 type_declaration:td {:
346 ParseNode pn=new ParseNode("type_declaration_list");
350 | type_declarations:tds type_declaration:td {:
356 //package_declaration ::=
357 // PACKAGE name SEMICOLON
359 //import_declaration ::=
360 // single_type_import_declaration
361 // | type_import_on_demand_declaration
363 //single_type_import_declaration ::=
364 // IMPORT name SEMICOLON
366 //type_import_on_demand_declaration ::=
367 // IMPORT name DOT MULT SEMICOLON
373 RESULT=(new ParseNode("type_declaration")).addChild("class").addChild(cd);
375 // | interface_declaration
376 | SEMICOLON {: RESULT=new ParseNode("empty"); :}
379 // 19.7) Productions used only in the LALR(1) grammar
381 {: RESULT=new ParseNode("empty"); :}
386 modifiers ::= modifier:mo {:
387 ParseNode pn=new ParseNode("modifier_list");
391 | modifiers:mos modifier:mo {:
397 PUBLIC {: RESULT=new ParseNode("public"); :}|
398 PROTECTED {: RESULT=new ParseNode("protected"); :}|
399 PRIVATE {: RESULT=new ParseNode("private"); :}|
400 STATIC {: RESULT=new ParseNode("static"); :} |
402 FINAL {: RESULT=new ParseNode("final"); :}|
403 NATIVE {: RESULT=new ParseNode("native"); :}
407 // STRICTFP // note that semantic analysis must check that the
408 // context of the modifier allows strictfp.
413 // 19.8.1) Class Declaration:
414 class_declaration ::=
415 modifiers_opt:mo CLASS IDENTIFIER:id super_opt:so {:
416 ParseNode pn=new ParseNode("class_declaration");
417 pn.addChild("modifiers").addChild(mo);
418 pn.addChild("name").addChild(id);
419 pn.addChild("super").addChild(so);
425 ParseNode pn=new ParseNode("class_declaration");
426 pn.addChild("modifiers").addChild(mo);
427 pn.addChild("name").addChild(id);
428 pn.addChild("super").addChild(so);
429 pn.addChild("body").addChild(body);
433 super ::= EXTENDS class_type:classtype {:
434 ParseNode pn=new ParseNode("super");
435 pn.addChild(classtype);
440 {: RESULT=new ParseNode("empty"); :}
446 //interfaces ::= IMPLEMENTS interface_type_list
451 //interface_type_list ::=
453 // | interface_type_list COMMA interface_type
456 class_body ::= LBRACE class_body_declarations_opt:cbdo RBRACE {: RESULT=cbdo; :}
459 class_body_declarations_opt ::=
460 {: RESULT=new ParseNode("empty"); :}
461 | class_body_declarations:cbd {: RESULT=cbd; :};
463 class_body_declarations ::=
464 class_body_declaration:cbd {:
465 ParseNode pn=new ParseNode("class_body_declaration_list");
469 | class_body_declarations:cbds class_body_declaration:cbd {:
475 class_body_declaration ::=
476 class_member_declaration:member {:
477 RESULT=(new ParseNode("class_body_declaration")).addChild("member").addChild(member).getRoot();
479 // | static_initializer
480 | constructor_declaration:constructor {:
481 RESULT=(new ParseNode("class_body_declaration")).addChild("constructor").addChild(constructor).getRoot();
484 RESULT=(new ParseNode("class_body_declaration")).addChild("block").addChild(block).getRoot();
487 class_member_declaration ::=
488 field_declaration:field {:
489 RESULT=(new ParseNode("class_member_declaration")).addChild("field").addChild(field).getRoot();
491 | method_declaration:method {:
492 RESULT=(new ParseNode("class_member_declaration")).addChild("method").addChild(method).getRoot();
494 /* repeat the prod for 'class_declaration' here: */
495 // | modifiers_opt CLASS IDENTIFIER super_opt class_body
496 // | interface_declaration
497 | SEMICOLON {: RESULT=new ParseNode("empty"); :}
500 // 19.8.2) Field Declarations
501 field_declaration ::=
502 modifiers_opt:mo type:type variable_declarators:var SEMICOLON {:
503 ParseNode pn=new ParseNode("field_declaration");
504 pn.addChild("modifier").addChild(mo);
505 pn.addChild("type").addChild(type);
506 pn.addChild("variables").addChild(var);
511 variable_declarators ::=
512 variable_declarator:vd {:
513 ParseNode pn=new ParseNode("variable_declarators_list");
517 | variable_declarators:vds COMMA variable_declarator:vd {:
522 variable_declarator ::=
523 variable_declarator_id:id {:
524 ParseNode pn=new ParseNode("variable_declarator");
525 pn.addChild("identifier").addChild(id);
528 | variable_declarator_id:id EQ variable_initializer:init {:
529 ParseNode pn=new ParseNode("variable_declarator");
530 pn.addChild("identifier").addChild(id);
531 pn.addChild("initializer").addChild(init);
535 variable_declarator_id ::=
537 RESULT=(new ParseNode("variable_declarator_id")).addChild("name").addChild(id);:}
538 // | variable_declarator_id:id LBRACK RBRACK {:
539 // RESULT=(new ParseNode("variable_declarator_id")).addChild("array").addChild(id);:}
541 variable_initializer ::=
542 expression:exp {: RESULT=exp; :}
543 // | array_initializer
546 // 19.8.3) Method Declarations
547 method_declaration ::=
548 method_header:header method_body:body {:
549 ParseNode pn=new ParseNode("method_declaration");
550 pn.addChild("header").addChild(header);
551 pn.addChild("body").addChild(body);
556 modifiers_opt:mo type:type method_declarator:decl //throws_opt
558 ParseNode pn=new ParseNode("method_header");
559 pn.addChild("modifiers").addChild(mo);
560 pn.addChild("returntype").addChild(type);
561 pn.addChild("declarator").addChild(decl);
564 | modifiers_opt:mo VOID method_declarator:decl //throws_opt
566 ParseNode pn=new ParseNode("method_header");
567 pn.addChild("modifiers").addChild(mo);
568 pn.addChild("declarator").addChild(decl);
572 method_declarator ::=
573 IDENTIFIER:id LPAREN formal_parameter_list_opt:params RPAREN {:
574 ParseNode pn=new ParseNode("method_declarator");
575 pn.addChild("name").addChild(id);
576 pn.addChild("parameters").addChild(params);
579 // | method_declarator LBRACK RBRACK // deprecated
580 // be careful; the above production also allows 'void foo() []'
582 formal_parameter_list_opt ::=
583 {: RESULT=new ParseNode("empty"); :}
584 | formal_parameter_list:fpl {:
588 formal_parameter_list ::=
589 formal_parameter:fp {:
590 ParseNode pn=new ParseNode("formal_parameter_list");
594 | formal_parameter_list:fpl COMMA formal_parameter:fp {:
600 type:type variable_declarator_id:name {:
601 ParseNode pn=new ParseNode("formal_parameter");
602 pn.addChild("type").addChild(type);
603 pn.addChild("name").addChild(name);
606 // | FINAL type variable_declarator_id
611 //throws ::= THROWS class_type_list
613 //class_type_list ::=
615 // | class_type_list COMMA class_type
617 method_body ::= block:block {:
620 | SEMICOLON {: RESULT=new ParseNode("empty"); :}
623 // 19.8.4) Static Initializers
624 //static_initializer ::=
628 // 19.8.5) Constructor Declarations
629 constructor_declaration ::=
630 modifiers_opt:mo constructor_declarator:cd
632 constructor_body:body {:
633 ParseNode pn=new ParseNode("constructor_declaration");
634 pn.addChild("modifier").addChild(mo);
635 pn.addChild("declarator").addChild(cd);
636 pn.addChild("body").addChild(body);
640 constructor_declarator ::=
641 simple_name:name LPAREN formal_parameter_list_opt:fplo RPAREN {:
642 ParseNode pn=new ParseNode("constructor_declarator");
643 pn.addChild("name").addChild(name);
644 pn.addChild("parameters").addChild(fplo);
649 // LBRACE explicit_constructor_invocation:eci block_statements:bs RBRACE |
650 // LBRACE explicit_constructor_invocation RBRACE |
651 LBRACE block_statements:block RBRACE {:
652 ParseNode pn=new ParseNode("constructor_body");
653 pn.addChild("block").addChild(block);
656 | LBRACE RBRACE {: RESULT=new ParseNode("empty"); :}
658 //explicit_constructor_invocation ::=
659 // THIS LPAREN argument_list_opt RPAREN SEMICOLON
660 // | SUPER LPAREN argument_list_opt RPAREN SEMICOLON
661 // | primary DOT THIS LPAREN argument_list_opt RPAREN SEMICOLON
662 // | primary DOT SUPER LPAREN argument_list_opt RPAREN SEMICOLON
667 // 19.9.1) Interface Declarations
668 //interface_declaration ::=
669 // modifiers_opt INTERFACE IDENTIFIER extends_interfaces_opt
672 //extends_interfaces_opt ::=
673 // | extends_interfaces
675 //extends_interfaces ::=
676 // EXTENDS interface_type
677 // | extends_interfaces COMMA interface_type
680 // LBRACE interface_member_declarations_opt RBRACE
682 //interface_member_declarations_opt ::=
683 // | interface_member_declarations
685 //interface_member_declarations ::=
686 // interface_member_declaration
687 // | interface_member_declarations interface_member_declaration
689 //interface_member_declaration ::=
690 // constant_declaration
691 // | abstract_method_declaration
692 // | class_declaration
693 // | interface_declaration
696 //constant_declaration ::=
698 // // need to semantically check that modifiers of field declaration
699 // // include only PUBLIC, STATIC, or FINAL. Other modifiers are
702 //abstract_method_declaration ::=
703 // method_header SEMICOLON
708 //array_initializer ::=
709 // LBRACE variable_initializers COMMA RBRACE
710 // | LBRACE variable_initializers RBRACE
711 // | LBRACE COMMA RBRACE
714 //variable_initializers ::=
715 // variable_initializer
716 // | variable_initializers COMMA variable_initializer
719 // 19.11) Blocks and Statements
720 block ::= LBRACE block_statements_opt:bso RBRACE {:
721 ParseNode pn=new ParseNode("block");
722 pn.addChild("block_statements_opt").addChild(bso);
726 block_statements_opt ::=
727 {: RESULT=new ParseNode("empty"); :}
728 | block_statements:bs {:
729 ParseNode pn=new ParseNode("block_statements_opt");
730 pn.addChild("block_statements").addChild(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 {:
747 ParseNode pn=new ParseNode("block_statement");
748 pn.addChild("local_variable_declaration").addChild(lvds);
751 | statement:statement {:
752 ParseNode pn=new ParseNode("statement");
753 pn.addChild("statement").addChild(statement);
756 // | class_declaration
757 // | interface_declaration
759 local_variable_declaration_statement ::=
760 local_variable_declaration:lvd SEMICOLON {:
764 local_variable_declaration ::=
765 type:type variable_declarators:var_decls {:
766 ParseNode pn=new ParseNode("local_variable_declaration");
767 pn.addChild("type").addChild(type);
768 pn.addChild("variables").addChild(var_decls);
771 // | FINAL type variable_declarators
773 statement ::= statement_without_trailing_substatement:st {:
776 // | labeled_statement:st {: RESULT=st; :}
777 | if_then_statement:st {: RESULT=st; :}
778 | if_then_else_statement:st {: RESULT=st; :}
779 | while_statement:st {: RESULT=st; :}
780 | for_statement:st {: RESULT=st; :}
782 statement_no_short_if ::=
783 statement_without_trailing_substatement:st {: RESULT=st; :}
784 // | labeled_statement_no_short_if:st {: RESULT=st; :}
785 | if_then_else_statement_no_short_if:st {: RESULT=st; :}
786 | while_statement_no_short_if:st {: RESULT=st; :}
787 | for_statement_no_short_if:st {: RESULT=st; :}
789 statement_without_trailing_substatement ::=
790 block:st {: RESULT=st; :}
791 | empty_statement:st {: RESULT=st; :}
792 | expression_statement:st {: RESULT=st; :}
793 // | switch_statement
795 | break_statement:st {: RESULT=st; :}
796 | continue_statement:st {: RESULT=st; :}
797 | return_statement:st {: RESULT=st; :}
798 // | synchronized_statement
801 // | assert_statement
804 SEMICOLON {: RESULT=(new ParseNode("statement")).addChild("nop").getRoot(); :}
806 //labeled_statement ::=
807 // IDENTIFIER COLON statement
809 //labeled_statement_no_short_if ::=
810 // IDENTIFIER COLON statement_no_short_if
812 expression_statement ::=
813 statement_expression:se SEMICOLON {: RESULT=se; :}
815 statement_expression ::=
816 assignment:st {: RESULT=st; :}
817 | preincrement_expression:st {: RESULT=st; :}
818 | predecrement_expression:st {: RESULT=st; :}
819 | postincrement_expression:st {: RESULT=st; :}
820 | postdecrement_expression:st {: RESULT=st; :}
821 | method_invocation:st {: RESULT=st; :}
822 | class_instance_creation_expression:st {: RESULT=st; :}
824 if_then_statement ::=
825 IF LPAREN expression:exp RPAREN statement:st {:
826 ParseNode pn=new ParseNode("ifstatement");
827 pn.addChild("condition").addChild(exp);
828 pn.addChild("statement").addChild(st);
832 if_then_else_statement ::=
833 IF LPAREN expression:exp RPAREN statement_no_short_if:st
834 ELSE statement:else_st {:
835 ParseNode pn=new ParseNode("ifstatement");
836 pn.addChild("condition").addChild(exp);
837 pn.addChild("statement").addChild(st);
838 pn.addChild("else_statement").addChild(else_st);
842 if_then_else_statement_no_short_if ::=
843 IF LPAREN expression:exp RPAREN statement_no_short_if:st
844 ELSE statement_no_short_if:else_st {:
845 ParseNode pn=new ParseNode("ifstatement");
846 pn.addChild("condition").addChild(exp);
847 pn.addChild("statement").addChild(st);
848 pn.addChild("else_statement").addChild(else_st);
852 //switch_statement ::=
853 // SWITCH LPAREN expression RPAREN switch_block
856 // LBRACE switch_block_statement_groups switch_labels RBRACE
857 // | LBRACE switch_block_statement_groups RBRACE
858 // | LBRACE switch_labels RBRACE
861 //switch_block_statement_groups ::=
862 // switch_block_statement_group
863 // | switch_block_statement_groups switch_block_statement_group
865 //switch_block_statement_group ::=
866 // switch_labels block_statements
870 // | switch_labels switch_label
873 // CASE constant_expression COLON
878 WHILE LPAREN expression:exp RPAREN statement:st {:
879 ParseNode pn=new ParseNode("whilestatement");
880 pn.addChild("condition").addChild(exp);
881 pn.addChild("statement").addChild(st);
885 while_statement_no_short_if ::=
886 WHILE LPAREN expression:exp RPAREN statement_no_short_if:st {:
887 ParseNode pn=new ParseNode("whilestatement");
888 pn.addChild("condition").addChild(exp);
889 pn.addChild("statement").addChild(st);
894 // DO statement WHILE LPAREN expression RPAREN SEMICOLON
897 FOR LPAREN for_init_opt:init SEMICOLON expression_opt:exp SEMICOLON
898 for_update_opt:update RPAREN statement:st {:
899 ParseNode pn=new ParseNode("forstatement");
900 pn.addChild("initializer").addChild(init);
901 pn.addChild("condition").addChild(exp);
902 pn.addChild("update").addChild(update);
903 pn.addChild("statement").addChild(st);
907 for_statement_no_short_if ::=
908 FOR LPAREN for_init_opt:init SEMICOLON expression_opt:exp SEMICOLON
909 for_update_opt:update RPAREN statement_no_short_if:st {:
910 ParseNode pn=new ParseNode("forstatement");
911 pn.addChild("initializer").addChild(init);
912 pn.addChild("condition").addChild(exp);
913 pn.addChild("update").addChild(update);
914 pn.addChild("statement").addChild(st);
919 {: RESULT=new ParseNode("empty"); :}
920 | for_init:init {: RESULT=init; :}
922 for_init ::= statement_expression_list:list {: RESULT=list; :}
923 | local_variable_declaration:decl {: RESULT=decl; :}
926 {: RESULT=new ParseNode("empty"); :}
927 | for_update:update {: RESULT=update; :}
929 for_update ::= statement_expression_list:list {: RESULT=list; :}
931 statement_expression_list ::=
932 statement_expression:expr {:
933 RESULT=(new ParseNode("statement_expression_list")).addChild(expr).getRoot();
935 | statement_expression_list:list COMMA statement_expression:expr {:
948 SEMICOLON {: RESULT=new ParseNode("break"); :}
951 continue_statement ::=
955 {: RESULT=new ParseNode("continue"); :}
958 RETURN expression_opt:exp SEMICOLON {:
959 RESULT=(new ParseNode("return")).addChild("retvalue").addChild(exp).getRoot(); :}
961 //throw_statement ::=
962 // THROW expression SEMICOLON
964 //synchronized_statement ::=
965 // SYNCHRONIZED LPAREN expression RPAREN block
969 // | TRY block catches_opt finally
974 //catches ::= catch_clause
975 // | catches catch_clause
978 // CATCH LPAREN formal_parameter RPAREN block
980 //finally ::= FINALLY block
982 //assert_statement ::=
983 // ASSERT expression SEMICOLON
984 // | ASSERT expression COLON expression SEMICOLON
987 // 19.12) Expressions
988 primary ::= primary_no_new_array:st {:
990 // | array_creation_init
991 // | array_creation_uninit
993 primary_no_new_array ::=
994 literal:lit {: RESULT=lit; :}
995 | THIS {: RESULT=new ParseNode("this"); :}
996 | LPAREN expression:exp RPAREN {: RESULT=exp; :}
997 | class_instance_creation_expression:exp {: RESULT=exp; :}
998 | field_access:exp {: RESULT=exp; :}
999 | method_invocation:exp {: RESULT=exp; :}
1001 // | primitive_type DOT CLASS
1003 // | array_type DOT CLASS
1007 class_instance_creation_expression ::=
1008 NEW class_or_interface_type:type LPAREN argument_list_opt:args RPAREN {:
1009 ParseNode pn=new ParseNode("createobject");
1010 pn.addChild("type").addChild(type);
1011 pn.addChild("args").addChild(args);
1014 // | NEW class_or_interface_type LPAREN argument_list_opt RPAREN class_body
1015 // | primary DOT NEW IDENTIFIER
1016 // LPAREN argument_list_opt RPAREN {:
1019 // | primary DOT NEW IDENTIFIER
1020 // LPAREN argument_list_opt RPAREN class_body
1021 // | name DOT NEW IDENTIFIER
1022 // LPAREN argument_list_opt RPAREN
1023 // | name DOT NEW IDENTIFIER
1024 // LPAREN argument_list_opt RPAREN class_body
1026 argument_list_opt ::=
1027 {: RESULT=new ParseNode("empty"); :}
1028 | argument_list:args {: RESULT=args; :}
1032 ParseNode pn=new ParseNode("argument_list");
1036 | argument_list:list COMMA expression:exp {:
1041 //array_creation_uninit ::=
1042 // NEW primitive_type dim_exprs dims_opt
1043 // | NEW class_or_interface_type dim_exprs dims_opt
1045 //array_creation_init ::=
1046 // NEW primitive_type dims array_initializer
1047 // | NEW class_or_interface_type dims array_initializer
1049 //dim_exprs ::= dim_expr
1050 // | dim_exprs dim_expr
1052 //dim_expr ::= LBRACK expression RBRACK
1057 //dims ::= LBRACK RBRACK
1058 // | dims LBRACK RBRACK
1061 primary:base DOT IDENTIFIER:id {:
1062 ParseNode pn=new ParseNode("fieldaccess");
1063 pn.addChild("base").addChild(base);
1064 pn.addChild("field").addChild(id);
1067 // | SUPER DOT IDENTIFIER
1068 // | name DOT SUPER DOT IDENTIFIER
1070 method_invocation ::=
1071 name:name LPAREN argument_list_opt:args RPAREN {:
1072 ParseNode pn=new ParseNode("methodinvoke");
1073 pn.addChild("name").addChild(name);
1074 pn.addChild("args").addChild(args);
1077 | primary:base DOT IDENTIFIER:name LPAREN argument_list_opt:args RPAREN {:
1078 ParseNode pn=new ParseNode("methodinvoke");
1079 pn.addChild("base").addChild(base);
1080 pn.addChild("name").addChild(name);
1081 pn.addChild("args").addChild(args);
1084 // | SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
1085 // | name DOT SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
1088 // name LBRACK expression RBRACK
1089 // | primary_no_new_array LBRACK expression RBRACK
1090 // | array_creation_init LBRACK expression RBRACK
1092 postfix_expression ::=
1095 | name:exp {: RESULT=exp; :}
1096 | postincrement_expression:exp {: RESULT=exp; :}
1097 | postdecrement_expression:exp {: RESULT=exp; :}
1099 postincrement_expression ::=
1100 postfix_expression:exp PLUSPLUS
1101 {: RESULT=(new ParseNode("postinc")).addChild(exp).getRoot(); :}
1103 postdecrement_expression ::=
1104 postfix_expression:exp MINUSMINUS
1105 {: RESULT=(new ParseNode("postdec")).addChild(exp).getRoot(); :}
1107 unary_expression ::=
1108 preincrement_expression:exp {: RESULT=exp; :}
1109 | predecrement_expression:exp {: RESULT=exp; :}
1110 | PLUS unary_expression:exp
1111 {: RESULT=(new ParseNode("unaryplus")).addChild(exp).getRoot(); :}
1112 | MINUS unary_expression:exp
1113 {: RESULT=(new ParseNode("unaryminus")).addChild(exp).getRoot(); :}
1114 | unary_expression_not_plus_minus:exp {:
1117 preincrement_expression ::=
1118 PLUSPLUS unary_expression:exp
1119 {: RESULT=(new ParseNode("preinc")).addChild(exp).getRoot(); :}
1121 predecrement_expression ::=
1122 MINUSMINUS unary_expression:exp
1123 {: RESULT=(new ParseNode("predec")).addChild(exp).getRoot(); :}
1125 unary_expression_not_plus_minus ::=
1126 postfix_expression:exp {:
1128 // | COMP unary_expression
1129 | NOT unary_expression:exp
1130 {: RESULT=(new ParseNode("not")).addChild(exp).getRoot(); :}
1131 | cast_expression:exp {: RESULT=exp; :}
1134 LPAREN primitive_type:type
1136 RPAREN unary_expression:exp {:
1137 ParseNode pn=new ParseNode("cast");
1138 pn.addChild("type").addChild(type);
1139 pn.addChild("exp").addChild(exp);
1142 | LPAREN expression:type RPAREN unary_expression_not_plus_minus:exp {:
1143 ParseNode pn=new ParseNode("cast");
1144 pn.addChild("type").addChild(type);
1145 pn.addChild("exp").addChild(exp);
1149 // | LPAREN name dims RPAREN unary_expression_not_plus_minus
1151 multiplicative_expression ::=
1152 unary_expression:exp {:
1154 | multiplicative_expression:exp1 MULT unary_expression:exp2 {:
1155 ParseNode pn=new ParseNode("mult");
1160 | multiplicative_expression:exp1 DIV unary_expression:exp2 {:
1161 ParseNode pn=new ParseNode("div");
1166 | multiplicative_expression:exp1 MOD unary_expression:exp2 {:
1167 ParseNode pn=new ParseNode("mod");
1173 additive_expression ::=
1174 multiplicative_expression:exp {:
1176 | additive_expression:exp1 PLUS multiplicative_expression:exp2 {:
1177 ParseNode pn=new ParseNode("add");
1182 | additive_expression:exp1 MINUS multiplicative_expression:exp2 {:
1183 ParseNode pn=new ParseNode("sub");
1189 shift_expression ::=
1190 additive_expression:exp {:
1192 | shift_expression:exp1 LSHIFT additive_expression:exp2 {:
1193 ParseNode pn=new ParseNode("leftshift");
1198 | shift_expression:exp1 RSHIFT additive_expression:exp2 {:
1199 ParseNode pn=new ParseNode("rightshift");
1204 // | shift_expression URSHIFT additive_expression
1206 relational_expression ::=
1207 shift_expression:exp {:
1209 | relational_expression:exp1 LT shift_expression:exp2 {:
1210 ParseNode pn=new ParseNode("comp_lt");
1215 | relational_expression:exp1 GT shift_expression:exp2 {:
1216 ParseNode pn=new ParseNode("comp_gt");
1221 | relational_expression:exp1 LTEQ shift_expression:exp2 {:
1222 ParseNode pn=new ParseNode("comp_lte");
1227 | relational_expression:exp1 GTEQ shift_expression:exp2 {:
1228 ParseNode pn=new ParseNode("comp_gte");
1233 // | relational_expression INSTANCEOF reference_type
1236 equality_expression ::=
1237 relational_expression:exp {:
1239 | equality_expression:exp1 EQEQ relational_expression:exp2 {:
1240 ParseNode pn=new ParseNode("equal");
1245 | equality_expression:exp1 NOTEQ relational_expression:exp2 {:
1246 ParseNode pn=new ParseNode("not_equal");
1253 equality_expression:exp {:
1255 | and_expression:exp1 AND equality_expression:exp2 {:
1256 ParseNode pn=new ParseNode("bitwise_and");
1262 exclusive_or_expression ::=
1263 and_expression:expr {:
1266 | exclusive_or_expression:exp1 XOR and_expression:exp2 {:
1267 ParseNode pn=new ParseNode("bitwise_xor");
1273 inclusive_or_expression ::=
1274 exclusive_or_expression:exclor {:
1276 | inclusive_or_expression:exp1 OR exclusive_or_expression:exp2 {:
1277 ParseNode pn=new ParseNode("bitwise_or");
1283 conditional_and_expression ::=
1284 inclusive_or_expression:inclor {:
1286 | conditional_and_expression:exp1 ANDAND inclusive_or_expression:exp2 {:
1287 ParseNode pn=new ParseNode("logical_and");
1293 conditional_or_expression ::=
1294 conditional_and_expression:condand {:
1296 | conditional_or_expression:exp1 OROR conditional_and_expression:exp2 {:
1297 ParseNode pn=new ParseNode("logical_or");
1303 conditional_expression ::=
1304 conditional_or_expression:condor {:
1306 // | conditional_or_expression QUESTION expression
1307 // COLON conditional_expression
1309 assignment_expression ::=
1310 conditional_expression:expr {:
1312 assignment:assign {:
1315 // semantic check necessary here to ensure a valid left-hand side.
1316 // allowing a parenthesized variable here on the lhs was introduced in
1317 // JLS 2; thanks to Eric Blake for pointing this out.
1318 assignment ::= postfix_expression:lvalue assignment_operator:op assignment_expression:rvalue {:
1319 ParseNode pn=new ParseNode("assignment");
1320 pn.addChild("op").addChild(op);
1321 ParseNode pnargs=pn.addChild("args");
1322 pnargs.addChild(lvalue);
1323 pnargs.addChild(rvalue);
1327 assignment_operator ::=
1328 EQ {: RESULT=(new ParseNode("assignment_operator")).addChild("eq"); :}
1329 | MULTEQ {: RESULT=(new ParseNode("assignment_operator")).addChild("multeq"); :}
1330 | DIVEQ {: RESULT=(new ParseNode("assignment_operator")).addChild("diveq"); :}
1331 | MODEQ {: RESULT=(new ParseNode("assignment_operator")).addChild("modeq"); :}
1332 | PLUSEQ {: RESULT=(new ParseNode("assignment_operator")).addChild("pluseq"); :}
1333 | MINUSEQ {: RESULT=(new ParseNode("assignment_operator")).addChild("minuseq"); :}
1334 | LSHIFTEQ {: RESULT=(new ParseNode("assignment_operator")).addChild("lshifteq"); :}
1335 | RSHIFTEQ {: RESULT=(new ParseNode("assignment_operator")).addChild("rshifteq"); :}
1336 // | URSHIFTEQ {: RESULT=(new ParseNode("assignment_operator")).addChild("urshifteq"); :}
1337 | ANDEQ {: RESULT=(new ParseNode("assignment_operator")).addChild("andeq"); :}
1338 | XOREQ {: RESULT=(new ParseNode("assignment_operator")).addChild("xoreq"); :}
1339 | OREQ {: RESULT=(new ParseNode("assignment_operator")).addChild("oreq"); :}
1342 {: RESULT=new ParseNode("empty"); :}
1346 expression ::= assignment_expression:exp {:
1349 //constant_expression ::=