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
107 // 19.2) The Syntactic Grammar
108 non terminal ParseNode goal;
109 // 19.3) Lexical Structure
110 non terminal ParseNode literal;
111 // 19.4) Types, Values, and Variables
112 non terminal ParseNode type, primitive_type, numeric_type;
113 non terminal ParseNode integral_type, floating_point_type;
114 non terminal ParseNode reference_type;
115 non terminal ParseNode class_or_interface_type;
116 non terminal ParseNode class_type;
117 //non terminal ParseNode interface_type;
118 non terminal ParseNode array_type;
120 non terminal ParseNode name, simple_name, qualified_name;
122 non terminal ParseNode compilation_unit;
123 //non terminal ParseNode package_declaration_opt, package_declaration;
124 //non terminal ParseNode import_declarations_opt, import_declarations;
125 non terminal ParseNode type_declarations_opt, type_declarations;
126 //non terminal ParseNode import_declaration;
127 //non terminal ParseNode single_type_import_declaration;
128 //non terminal ParseNode type_import_on_demand_declaration;
129 non terminal ParseNode type_declaration;
130 // 19.7) Productions used only in the LALR(1) grammar
131 non terminal ParseNode modifiers_opt, modifiers, modifier;
132 // 19.8.1) Class Declaration
133 non terminal ParseNode class_declaration, super, super_opt;
134 //non terminal interfaces, interfaces_opt, interface_type_list;
135 non terminal ParseNode class_body;
136 non terminal ParseNode class_body_declarations, class_body_declarations_opt;
137 non terminal ParseNode class_body_declaration, class_member_declaration;
138 // 19.8.2) Field Declarations
139 non terminal ParseNode field_declaration, variable_declarators, variable_declarator;
140 non terminal ParseNode variable_declarator_id;
141 non terminal ParseNode variable_initializer;
142 // 19.8.3) Method Declarations
143 non terminal ParseNode method_declaration, method_header, method_declarator;
144 non terminal ParseNode formal_parameter_list_opt, formal_parameter_list;
145 non terminal ParseNode formal_parameter;
146 //non terminal ParseNode throws_opt;
147 //non terminal ParseNode throws;
148 //non terminal ParseNode class_type_list;
149 non terminal ParseNode method_body;
150 // 19.8.4) Static Initializers
151 //non terminal ParseNode static_initializer;
152 // 19.8.5) Constructor Declarations
153 non terminal ParseNode constructor_declaration, constructor_declarator;
154 non terminal ParseNode constructor_body;
155 //non terminal ParseNode explicit_constructor_invocation;
156 // 19.9.1) Interface Declarations
157 //non terminal ParseNode interface_declaration;
158 //non terminal ParseNode extends_interfaces_opt, extends_interfaces;
159 //non terminal ParseNode interface_body;
160 //non terminal ParseNode interface_member_declarations_opt, interface_member_declarations;
161 //non terminal ParseNode interface_member_declaration, constant_declaration;
162 //non terminal ParseNode abstract_method_declaration;
164 //non terminal ParseNode array_initializer;
165 //non terminal ParseNode variable_initializers;
166 // 19.11) Blocks and Statements
167 non terminal ParseNode block;
168 non terminal ParseNode block_statements_opt, block_statements, block_statement;
169 non terminal ParseNode local_variable_declaration_statement, local_variable_declaration;
170 non terminal ParseNode statement, statement_no_short_if;
171 non terminal ParseNode statement_without_trailing_substatement;
172 non terminal ParseNode empty_statement;
173 //non terminal ParseNode labeled_statement, labeled_statement_no_short_if;
174 non terminal ParseNode expression_statement, statement_expression;
175 non terminal ParseNode if_then_statement;
176 non terminal ParseNode if_then_else_statement, if_then_else_statement_no_short_if;
177 //non terminal ParseNode switch_statement, switch_block;
178 //non terminal ParseNode switch_block_statement_groups;
179 //non terminal ParseNode switch_block_statement_group;
180 //non terminal ParseNode switch_labels, switch_label;
181 non terminal ParseNode while_statement, while_statement_no_short_if;
182 non terminal ParseNode do_statement;
183 non terminal ParseNode for_statement, for_statement_no_short_if;
184 non terminal ParseNode for_init_opt, for_init;
185 non terminal ParseNode for_update_opt, for_update;
186 non terminal ParseNode statement_expression_list;
187 //non terminal ParseNode identifier_opt;
188 non terminal ParseNode break_statement, continue_statement;
189 non terminal ParseNode return_statement;
190 //non terminal ParseNode throw_statement;
191 //non terminal ParseNode synchronized_statement, try_statement;
192 //non terminal ParseNode catches_opt;
193 //non terminal ParseNode catches, catch_clause;
194 //non terminal ParseNode finally;
195 //non terminal ParseNode assert_statement;
196 // 19.12) Expressions
197 non terminal ParseNode primary, primary_no_new_array;
198 non terminal ParseNode class_instance_creation_expression;
199 non terminal ParseNode cons_argument_list_opt, cons_argument_list;
200 non terminal ParseNode argument_list_opt, argument_list;
201 //non terminal ParseNode array_creation_init;
202 non terminal ParseNode array_creation_uninit;
203 non terminal ParseNode dim_exprs, dim_expr;
204 non terminal Integer dims_opt, dims;
205 non terminal ParseNode field_access, method_invocation;
206 non terminal ParseNode array_access;
207 non terminal ParseNode postfix_expression;
208 non terminal ParseNode postincrement_expression, postdecrement_expression;
209 non terminal ParseNode unary_expression, unary_expression_not_plus_minus;
210 non terminal ParseNode preincrement_expression, predecrement_expression;
211 non terminal ParseNode cast_expression;
212 non terminal ParseNode multiplicative_expression, additive_expression;
213 non terminal ParseNode shift_expression, relational_expression, equality_expression;
214 non terminal ParseNode and_expression, exclusive_or_expression, inclusive_or_expression;
215 non terminal ParseNode conditional_and_expression, conditional_or_expression;
216 non terminal ParseNode conditional_expression;
217 non terminal ParseNode assignment_expression;
218 non terminal ParseNode assignment;
219 non terminal ParseNode assignment_operator;
220 non terminal ParseNode expression_opt, expression;
221 //non terminal ParseNode constant_expression;
222 //failure aware computation keywords
228 non terminal ParseNode flag_declaration;
229 non terminal ParseNode task_declaration;
230 non terminal ParseNode task_parameter_list;
231 non terminal ParseNode task_parameter;
232 non terminal ParseNode flag_expression;
233 non terminal ParseNode flag_andexpression;
234 non terminal ParseNode flag_notexpression;
235 non terminal ParseNode task_exitstatement;
236 non terminal ParseNode flag_effects_opt;
237 non terminal ParseNode flag_effects;
238 non terminal ParseNode flag_effect;
239 non terminal ParseNode flag_list;
240 non terminal ParseNode flag_list_opt;
241 non terminal ParseNode flag_change;
243 non terminal ParseNode cons_checks_opt;
244 non terminal ParseNode cons_checks;
245 non terminal ParseNode cons_check;
252 TASK IDENTIFIER:id LPAREN task_parameter_list:tpl RPAREN
256 ParseNode pn=new ParseNode("task_declaration");
257 pn.addChild("name").addChild(id);
260 pn.addChild("body").addChild(body);
264 task_parameter_list ::=
266 ParseNode pn=new ParseNode("task_parameter_list");
270 | task_parameter_list:fpl COMMA task_parameter:fp {:
277 type:type variable_declarator_id:name LBRACE flag_expression:exp RBRACE {:
278 ParseNode pn=new ParseNode("task_parameter");
281 pn.addChild("flag").addChild(exp);
287 flag_andexpression:exp {:
290 | flag_expression:exp1 OROR flag_andexpression:exp2 {:
291 ParseNode pn=new ParseNode("or");
298 flag_andexpression ::=
299 flag_notexpression:exp {: RESULT=exp; :}
300 | flag_notexpression:exp1 ANDAND flag_andexpression:exp2 {:
301 ParseNode pn=new ParseNode("and");
308 flag_notexpression ::=
309 NOT flag_notexpression:exp {:
310 ParseNode pn=new ParseNode("not");
314 | LPAREN flag_expression:exp RPAREN {:
318 ParseNode pn=new ParseNode("name");
324 task_exitstatement ::= TASKEXIT flag_effects_opt:opt cons_checks_opt:cco SEMICOLON {:
325 RESULT=(new ParseNode("taskexit")).addChild(opt).getRoot().addChild(cco).getRoot();
328 cons_checks_opt ::= ASSERT LPAREN cons_checks:cc RPAREN {: RESULT=cc; :}
329 | {: RESULT = new ParseNode("empty"); :}
332 cons_checks ::= cons_check:cc {:
333 ParseNode pn=new ParseNode("cons_checks");
337 | cons_checks:ccs COMMA cons_check:cc {:
342 cons_check ::= IDENTIFIER:name LPAREN cons_argument_list_opt:args RPAREN {:
343 ParseNode pn=new ParseNode("cons_check");
344 pn.addChild("name").addChild("identifier").addChild(name);
349 flag_effects_opt ::= LPAREN flag_effects:fe RPAREN {:RESULT=fe;:}
350 | {: RESULT = new ParseNode("empty"); :}
353 flag_effects ::= flag_effect:fe {:
354 ParseNode pn=new ParseNode("flag_effects_list");
358 | flag_effects:fes COMMA flag_effect:fe {:
363 flag_effect ::= IDENTIFIER:id LBRACE flag_list:fl RBRACE {:
364 ParseNode pn=new ParseNode("flag_effect");
365 pn.addChild("name").addChild(id);
370 flag_list_opt ::= LBRACE flag_list:fl RBRACE {:RESULT=fl;:}
371 | {: RESULT = new ParseNode("empty"); :}
374 flag_list ::= flag_change:fc {:
375 ParseNode pn=new ParseNode("flag_list");
379 | flag_list:fl COMMA flag_change:fc {:
384 flag_change ::= IDENTIFIER:id {:
385 RESULT=new ParseNode("name").addChild(id).getRoot();
388 RESULT=new ParseNode("not").addChild("name").addChild(id).getRoot();
391 // 19.2) The Syntactic Grammar
392 goal ::= compilation_unit:cu
398 // 19.3) Lexical Structure.
401 literal ::= INTEGER_LITERAL:integer_lit
403 ParseNode pn=new ParseNode("literal");
404 pn.addChild("integer").setLiteral(integer_lit);
407 | FLOATING_POINT_LITERAL:float_lit
409 ParseNode pn=new ParseNode("literal");
410 pn.addChild("float").setLiteral(float_lit);
413 | BOOLEAN_LITERAL:boolean_lit
415 ParseNode pn=new ParseNode("literal");
416 pn.addChild("boolean").setLiteral(boolean_lit);
419 | CHARACTER_LITERAL:char_lit
421 ParseNode pn=new ParseNode("literal");
422 pn.addChild("char").setLiteral(char_lit);
425 | STRING_LITERAL:string_lit
427 ParseNode pn=new ParseNode("literal");
428 pn.addChild("string").setLiteral(string_lit);
433 RESULT=(new ParseNode("literal")).addChild("null").getRoot();
437 // 19.4) Types, Values, and Variables
438 type ::= primitive_type:type {: RESULT=type; :}
439 | reference_type:type {: RESULT=type; :}
443 numeric_type:type {: RESULT=type; :}
444 | BOOLEAN {: RESULT=(new ParseNode("type")).addChild("boolean").getRoot(); :}
446 numeric_type::= integral_type:type {: RESULT=type; :}
447 | floating_point_type:type {: RESULT=type; :}
450 BYTE {: RESULT=(new ParseNode("type")).addChild("byte").getRoot(); :}
451 | SHORT {: RESULT=(new ParseNode("type")).addChild("short").getRoot(); :}
452 | INT {: RESULT=(new ParseNode("type")).addChild("int").getRoot(); :}
453 | LONG {: RESULT=(new ParseNode("type")).addChild("long").getRoot(); :}
454 | CHAR {: RESULT=(new ParseNode("type")).addChild("char").getRoot(); :}
456 floating_point_type ::=
457 FLOAT {: RESULT=(new ParseNode("type")).addChild("float").getRoot(); :}
458 | DOUBLE {: RESULT=(new ParseNode("type")).addChild("double").getRoot(); :}
462 class_or_interface_type:type {: RESULT=type; :}
463 | array_type:type {: RESULT=type; :}
465 class_or_interface_type ::= name:name {:
466 RESULT=(new ParseNode("type")).addChild("class").addChild(name).getRoot();
469 class_type ::= class_or_interface_type:type {: RESULT=type; :};
470 //interface_type ::= class_or_interface_type;
472 array_type ::= primitive_type:prim dims:dims {:
473 ParseNode pn=(new ParseNode("type")).addChild("array");
474 pn.addChild("basetype").addChild(prim);
475 pn.addChild("dims").setLiteral(dims);
478 | name:name dims:dims {:
479 ParseNode pn=(new ParseNode("type")).addChild("array");
480 pn.addChild("basetype").addChild("type").addChild("class").addChild(name);
481 pn.addChild("dims").setLiteral(dims);
487 name ::= simple_name:name {: RESULT=name; :}
488 | qualified_name:name {: RESULT=name; :}
490 simple_name ::= IDENTIFIER:id {:
491 RESULT=(new ParseNode("name")).addChild("identifier").addChild(id).getRoot();
494 qualified_name ::= name:name DOT IDENTIFIER:id {:
495 ParseNode pn=new ParseNode("name");
496 pn.addChild("base").addChild(name);
497 pn.addChild("identifier").addChild(id);
504 // package_declaration_opt
505 // import_declarations_opt
506 type_declarations_opt:tdo {:
507 ParseNode pn=new ParseNode("compilation_unit");
512 //package_declaration_opt ::= package_declaration | ;
513 //import_declarations_opt ::= import_declarations | ;
514 type_declarations_opt ::= type_declarations:tds {:
517 {: RESULT=new ParseNode("empty"); :}
520 //import_declarations ::=
521 // import_declaration
522 // | import_declarations import_declaration
525 type_declarations ::=
526 type_declaration:td {:
527 ParseNode pn=new ParseNode("type_declaration_list");
531 | type_declarations:tds type_declaration:td {:
537 //package_declaration ::=
538 // PACKAGE name SEMICOLON
540 //import_declaration ::=
541 // single_type_import_declaration
542 // | type_import_on_demand_declaration
544 //single_type_import_declaration ::=
545 // IMPORT name SEMICOLON
547 //type_import_on_demand_declaration ::=
548 // IMPORT name DOT MULT SEMICOLON
556 | task_declaration:td
560 // | interface_declaration
561 | SEMICOLON {: RESULT=new ParseNode("empty"); :}
564 // 19.7) Productions used only in the LALR(1) grammar
566 {: RESULT=new ParseNode("empty"); :}
571 modifiers ::= modifier:mo {:
572 ParseNode pn=new ParseNode("modifier_list");
576 | modifiers:mos modifier:mo {:
582 PUBLIC {: RESULT=new ParseNode("public"); :}|
583 PROTECTED {: RESULT=new ParseNode("protected"); :}|
584 PRIVATE {: RESULT=new ParseNode("private"); :}|
585 STATIC {: RESULT=new ParseNode("static"); :} |
587 FINAL {: RESULT=new ParseNode("final"); :}|
588 NATIVE {: RESULT=new ParseNode("native"); :}
592 // STRICTFP // note that semantic analysis must check that the
593 // context of the modifier allows strictfp.
598 // 19.8.1) Class Declaration:
599 class_declaration ::=
600 modifiers_opt:mo CLASS IDENTIFIER:id super_opt:so //interfaces_opt
603 ParseNode pn=new ParseNode("class_declaration");
604 pn.addChild("modifiers").addChild(mo);
605 pn.addChild("name").addChild(id);
606 pn.addChild("super").addChild(so);
607 pn.addChild("classbody").addChild(body);
611 super ::= EXTENDS class_type:classtype {:
616 {: RESULT=new ParseNode("empty"); :}
622 //interfaces ::= IMPLEMENTS interface_type_list
627 //interface_type_list ::=
629 // | interface_type_list COMMA interface_type
632 class_body ::= LBRACE class_body_declarations_opt:cbdo RBRACE {: RESULT=cbdo; :}
635 class_body_declarations_opt ::=
636 {: RESULT=new ParseNode("empty"); :}
637 | class_body_declarations:cbd {: RESULT=cbd; :};
639 class_body_declarations ::=
640 class_body_declaration:cbd {:
641 ParseNode pn=new ParseNode("class_body_declaration_list");
645 | class_body_declarations:cbds class_body_declaration:cbd {:
651 class_body_declaration ::=
652 class_member_declaration:member {:
653 RESULT=(new ParseNode("member")).addChild(member).getRoot();
655 // | static_initializer
656 | constructor_declaration:constructor {:
657 RESULT=(new ParseNode("constructor")).addChild(constructor).getRoot();
660 RESULT=(new ParseNode("block")).addChild(block).getRoot();
663 class_member_declaration ::=
664 //failure aware computation
665 flag_declaration:flag {:
666 RESULT=(new ParseNode("flag")).addChild(flag).getRoot();
669 field_declaration:field {:
670 RESULT=(new ParseNode("field")).addChild(field).getRoot();
672 | method_declaration:method {:
673 RESULT=(new ParseNode("method")).addChild(method).getRoot();
675 /* repeat the prod for 'class_declaration' here: */
676 // | modifiers_opt CLASS IDENTIFIER super_opt class_body
677 // | interface_declaration
678 | SEMICOLON {: RESULT=new ParseNode("empty"); :}
681 //Failure aware computation
683 FLAG IDENTIFIER:id SEMICOLON {:
684 ParseNode pn=new ParseNode("flag_declaration");
685 pn.addChild("name").addChild(id);
688 EXTERNAL FLAG IDENTIFIER:id SEMICOLON {:
689 ParseNode pn=new ParseNode("flag_declaration");
690 pn.addChild("name").addChild(id);
691 pn.addChild("external");
696 // 19.8.2) Field Declarations
697 field_declaration ::=
698 modifiers_opt:mo type:type variable_declarators:var SEMICOLON {:
699 ParseNode pn=new ParseNode("field_declaration");
700 pn.addChild("modifier").addChild(mo);
701 pn.addChild("type").addChild(type);
702 pn.addChild("variables").addChild(var);
707 variable_declarators ::=
708 variable_declarator:vd {:
709 ParseNode pn=new ParseNode("variable_declarators_list");
713 | variable_declarators:vds COMMA variable_declarator:vd {:
718 variable_declarator ::=
719 variable_declarator_id:id {:
720 ParseNode pn=new ParseNode("variable_declarator");
724 | variable_declarator_id:id EQ variable_initializer:init {:
725 ParseNode pn=new ParseNode("variable_declarator");
727 pn.addChild("initializer").addChild(init);
731 variable_declarator_id ::=
733 RESULT=(new ParseNode("single")).addChild(id).getRoot();:}
734 | variable_declarator_id:id LBRACK RBRACK {:
735 RESULT=(new ParseNode("array")).addChild(id).getRoot();:}
737 variable_initializer ::=
738 expression:exp {: RESULT=exp; :}
739 // | array_initializer
742 // 19.8.3) Method Declarations
743 method_declaration ::=
744 method_header:header method_body:body {:
745 ParseNode pn=new ParseNode("method_declaration");
747 pn.addChild("body").addChild(body);
752 modifiers_opt:mo type:type method_declarator:decl //throws_opt
754 ParseNode pn=new ParseNode("method_header");
755 pn.addChild("modifiers").addChild(mo);
756 pn.addChild("returntype").addChild(type);
760 | modifiers_opt:mo VOID method_declarator:decl //throws_opt
762 ParseNode pn=new ParseNode("method_header");
763 pn.addChild("modifiers").addChild(mo);
768 method_declarator ::=
769 IDENTIFIER:id LPAREN formal_parameter_list_opt:params RPAREN {:
770 ParseNode pn=new ParseNode("method_declarator");
771 pn.addChild("name").addChild(id);
772 pn.addChild("parameters").addChild(params);
775 // | method_declarator LBRACK RBRACK // deprecated
776 // be careful; the above production also allows 'void foo() []'
778 formal_parameter_list_opt ::=
779 {: RESULT=new ParseNode("empty"); :}
780 | formal_parameter_list:fpl {:
784 formal_parameter_list ::=
785 formal_parameter:fp {:
786 ParseNode pn=new ParseNode("formal_parameter_list");
790 | formal_parameter_list:fpl COMMA formal_parameter:fp {:
796 type:type variable_declarator_id:name {:
797 ParseNode pn=new ParseNode("formal_parameter");
802 // | FINAL type variable_declarator_id
807 //throws ::= THROWS class_type_list
809 //class_type_list ::=
811 // | class_type_list COMMA class_type
813 method_body ::= block:block {:
816 | SEMICOLON {: RESULT=new ParseNode("empty"); :}
819 // 19.8.4) Static Initializers
820 //static_initializer ::=
824 // 19.8.5) Constructor Declarations
825 constructor_declaration ::=
826 modifiers_opt:mo constructor_declarator:cd
828 constructor_body:body {:
829 ParseNode pn=new ParseNode("constructor_declaration");
830 pn.addChild("modifiers").addChild(mo);
832 pn.addChild("body").addChild(body);
836 constructor_declarator ::=
837 simple_name:name LPAREN formal_parameter_list_opt:fplo RPAREN {:
838 ParseNode pn=new ParseNode("constructor_declarator");
840 pn.addChild("parameters").addChild(fplo);
845 // LBRACE explicit_constructor_invocation:eci block_statements:bs RBRACE |
846 // LBRACE explicit_constructor_invocation RBRACE |
847 LBRACE block_statements:block RBRACE {:
848 ParseNode pn=new ParseNode("constructor_body");
852 | LBRACE RBRACE {: RESULT=new ParseNode("empty"); :}
854 //explicit_constructor_invocation ::=
855 // THIS LPAREN argument_list_opt RPAREN SEMICOLON
856 // | SUPER LPAREN argument_list_opt RPAREN SEMICOLON
857 // | primary DOT THIS LPAREN argument_list_opt RPAREN SEMICOLON
858 // | primary DOT SUPER LPAREN argument_list_opt RPAREN SEMICOLON
863 // 19.9.1) Interface Declarations
864 //interface_declaration ::=
865 // modifiers_opt INTERFACE IDENTIFIER extends_interfaces_opt
868 //extends_interfaces_opt ::=
869 // | extends_interfaces
871 //extends_interfaces ::=
872 // EXTENDS interface_type
873 // | extends_interfaces COMMA interface_type
876 // LBRACE interface_member_declarations_opt RBRACE
878 //interface_member_declarations_opt ::=
879 // | interface_member_declarations
881 //interface_member_declarations ::=
882 // interface_member_declaration
883 // | interface_member_declarations interface_member_declaration
885 //interface_member_declaration ::=
886 // constant_declaration
887 // | abstract_method_declaration
888 // | class_declaration
889 // | interface_declaration
892 //constant_declaration ::=
894 // // need to semantically check that modifiers of field declaration
895 // // include only PUBLIC, STATIC, or FINAL. Other modifiers are
898 //abstract_method_declaration ::=
899 // method_header SEMICOLON
904 //array_initializer ::=
905 // LBRACE variable_initializers COMMA RBRACE
906 // | LBRACE variable_initializers RBRACE
907 // | LBRACE COMMA RBRACE
910 //variable_initializers ::=
911 // variable_initializer
912 // | variable_initializers COMMA variable_initializer
915 // 19.11) Blocks and Statements
916 block ::= LBRACE block_statements_opt:bso RBRACE {:
920 block_statements_opt ::=
921 {: RESULT=new ParseNode("empty"); :}
922 | block_statements:bs {:
927 block_statement:bs {:
928 ParseNode pn=new ParseNode("block_statement_list");
932 | block_statements:bss block_statement:bs {:
938 local_variable_declaration_statement:lvds {:
941 | statement:statement {:
944 // | class_declaration
945 // | interface_declaration
947 local_variable_declaration_statement ::=
948 local_variable_declaration:lvd SEMICOLON {:
952 local_variable_declaration ::=
953 type:type variable_declarators:var {:
954 ParseNode pn=new ParseNode("local_variable_declaration");
959 // | FINAL type variable_declarators
961 statement ::= statement_without_trailing_substatement:st {:
964 // | labeled_statement:st {: RESULT=st; :}
965 | if_then_statement:st {: RESULT=st; :}
966 | if_then_else_statement:st {: RESULT=st; :}
967 | while_statement:st {: RESULT=st; :}
968 | for_statement:st {: RESULT=st; :}
970 statement_no_short_if ::=
971 statement_without_trailing_substatement:st {: RESULT=st; :}
972 // | labeled_statement_no_short_if:st {: RESULT=st; :}
973 | if_then_else_statement_no_short_if:st {: RESULT=st; :}
974 | while_statement_no_short_if:st {: RESULT=st; :}
975 | for_statement_no_short_if:st {: RESULT=st; :}
977 statement_without_trailing_substatement ::=
978 block:st {: RESULT=st; :}
979 | empty_statement:st {: RESULT=st; :}
980 | expression_statement:st {: RESULT=st; :}
981 // | switch_statement
982 | do_statement:dos {:RESULT=dos; :}
983 | break_statement:st {: RESULT=st; :}
984 | continue_statement:st {: RESULT=st; :}
985 | return_statement:st {: RESULT=st; :}
986 | task_exitstatement:st {: RESULT=st; :}
987 // | synchronized_statement
990 // | assert_statement
993 SEMICOLON {: RESULT=new ParseNode("nop"); :}
995 //labeled_statement ::=
996 // IDENTIFIER COLON statement
998 //labeled_statement_no_short_if ::=
999 // IDENTIFIER COLON statement_no_short_if
1001 expression_statement ::=
1002 statement_expression:se SEMICOLON {:
1003 ParseNode pn=new ParseNode("expression");
1007 statement_expression ::=
1008 assignment:st {: RESULT=st; :}
1009 | preincrement_expression:st {: RESULT=st; :}
1010 | predecrement_expression:st {: RESULT=st; :}
1011 | postincrement_expression:st {: RESULT=st; :}
1012 | postdecrement_expression:st {: RESULT=st; :}
1013 | method_invocation:st {: RESULT=st; :}
1014 | class_instance_creation_expression:st {: RESULT=st; :}
1016 if_then_statement ::=
1017 IF LPAREN expression:exp RPAREN statement:st {:
1018 ParseNode pn=new ParseNode("ifstatement");
1019 pn.addChild("condition").addChild(exp);
1020 pn.addChild("statement").addChild(st);
1024 if_then_else_statement ::=
1025 IF LPAREN expression:exp RPAREN statement_no_short_if:st
1026 ELSE statement:else_st {:
1027 ParseNode pn=new ParseNode("ifstatement");
1028 pn.addChild("condition").addChild(exp);
1029 pn.addChild("statement").addChild(st);
1030 pn.addChild("else_statement").addChild(else_st);
1034 if_then_else_statement_no_short_if ::=
1035 IF LPAREN expression:exp RPAREN statement_no_short_if:st
1036 ELSE statement_no_short_if:else_st {:
1037 ParseNode pn=new ParseNode("ifstatement");
1038 pn.addChild("condition").addChild(exp);
1039 pn.addChild("statement").addChild(st);
1040 pn.addChild("else_statement").addChild(else_st);
1044 //switch_statement ::=
1045 // SWITCH LPAREN expression RPAREN switch_block
1048 // LBRACE switch_block_statement_groups switch_labels RBRACE
1049 // | LBRACE switch_block_statement_groups RBRACE
1050 // | LBRACE switch_labels RBRACE
1053 //switch_block_statement_groups ::=
1054 // switch_block_statement_group
1055 // | switch_block_statement_groups switch_block_statement_group
1057 //switch_block_statement_group ::=
1058 // switch_labels block_statements
1062 // | switch_labels switch_label
1065 // CASE constant_expression COLON
1070 WHILE LPAREN expression:exp RPAREN statement:st {:
1071 ParseNode pn=new ParseNode("whilestatement");
1072 pn.addChild("condition").addChild(exp);
1073 pn.addChild("statement").addChild(st);
1077 while_statement_no_short_if ::=
1078 WHILE LPAREN expression:exp RPAREN statement_no_short_if:st {:
1079 ParseNode pn=new ParseNode("whilestatement");
1080 pn.addChild("condition").addChild(exp);
1081 pn.addChild("statement").addChild(st);
1086 DO statement:st WHILE LPAREN expression:exp RPAREN SEMICOLON {:
1087 ParseNode pn=new ParseNode("dowhilestatement");
1088 pn.addChild("condition").addChild(exp);
1089 pn.addChild("statement").addChild(st);
1094 FOR LPAREN for_init_opt:init SEMICOLON expression_opt:exp SEMICOLON
1095 for_update_opt:update RPAREN statement:st {:
1096 ParseNode pn=new ParseNode("forstatement");
1097 pn.addChild("initializer").addChild(init);
1098 pn.addChild("condition").addChild(exp);
1099 pn.addChild("update").addChild(update);
1100 pn.addChild("statement").addChild(st);
1104 for_statement_no_short_if ::=
1105 FOR LPAREN for_init_opt:init SEMICOLON expression_opt:exp SEMICOLON
1106 for_update_opt:update RPAREN statement_no_short_if:st {:
1107 ParseNode pn=new ParseNode("forstatement");
1108 pn.addChild("initializer").addChild(init);
1109 pn.addChild("condition").addChild(exp);
1110 pn.addChild("update").addChild(update);
1111 pn.addChild("statement").addChild(st);
1116 {: RESULT=new ParseNode("empty"); :}
1117 | for_init:init {: RESULT=init; :}
1119 for_init ::= statement_expression_list:list {: RESULT=list; :}
1120 | local_variable_declaration:decl {: RESULT=decl; :}
1123 {: RESULT=new ParseNode("empty"); :}
1124 | for_update:update {: RESULT=update; :}
1126 for_update ::= statement_expression_list:list {: RESULT=list; :}
1128 statement_expression_list ::=
1129 statement_expression:expr {:
1130 RESULT=(new ParseNode("statement_expression_list")).addChild(expr).getRoot();
1132 | statement_expression_list:list COMMA statement_expression:expr {:
1133 list.addChild(expr);
1138 //identifier_opt ::=
1145 SEMICOLON {: RESULT=new ParseNode("break"); :}
1148 continue_statement ::=
1152 {: RESULT=new ParseNode("continue"); :}
1154 return_statement ::=
1155 RETURN expression_opt:exp SEMICOLON {:
1156 RESULT=(new ParseNode("return")).addChild(exp).getRoot(); :}
1158 //throw_statement ::=
1159 // THROW expression SEMICOLON
1161 //synchronized_statement ::=
1162 // SYNCHRONIZED LPAREN expression RPAREN block
1165 // TRY block catches
1166 // | TRY block catches_opt finally
1171 //catches ::= catch_clause
1172 // | catches catch_clause
1175 // CATCH LPAREN formal_parameter RPAREN block
1177 //finally ::= FINALLY block
1179 //assert_statement ::=
1180 // ASSERT expression SEMICOLON
1181 // | ASSERT expression COLON expression SEMICOLON
1184 // 19.12) Expressions
1185 primary ::= primary_no_new_array:st {:
1187 // | array_creation_init:st {:
1190 | array_creation_uninit:st {:
1194 primary_no_new_array ::=
1195 literal:lit {: RESULT=lit; :}
1196 | THIS {: RESULT=new ParseNode("this"); :}
1197 | LPAREN expression:exp RPAREN {: RESULT=exp; :}
1198 | class_instance_creation_expression:exp {: RESULT=exp; :}
1199 | field_access:exp {: RESULT=exp; :}
1200 | method_invocation:exp {: RESULT=exp; :}
1201 | array_access:exp {: RESULT=exp; :}
1202 // | primitive_type DOT CLASS
1204 // | array_type DOT CLASS
1208 class_instance_creation_expression ::=
1209 NEW class_or_interface_type:type LPAREN argument_list_opt:args RPAREN flag_list_opt:feo {:
1210 ParseNode pn=new ParseNode("createobject");
1216 // | NEW class_or_interface_type LPAREN argument_list_opt RPAREN class_body
1217 // | primary DOT NEW IDENTIFIER
1218 // LPAREN argument_list_opt RPAREN {:
1221 // | primary DOT NEW IDENTIFIER
1222 // LPAREN argument_list_opt RPAREN class_body
1223 // | name DOT NEW IDENTIFIER
1224 // LPAREN argument_list_opt RPAREN
1225 // | name DOT NEW IDENTIFIER
1226 // LPAREN argument_list_opt RPAREN class_body
1228 cons_argument_list_opt ::=
1229 {: RESULT=new ParseNode("empty"); :}
1230 | cons_argument_list:args {: RESULT=args; :}
1233 cons_argument_list ::=
1234 IDENTIFIER:id COLON expression:exp {:
1235 ParseNode pn=new ParseNode("cons_argument_list");
1236 ParseNode pnarg=pn.addChild("binding");
1237 pnarg.addChild("var").addChild(id);
1238 pnarg.addChild("exp").addChild(exp);
1241 | argument_list:list COMMA IDENTIFIER:id COLON expression:exp {:
1242 ParseNode pnarg=new ParseNode("binding");
1243 pnarg.addChild("var").addChild(id);
1244 pnarg.addChild("exp").addChild(exp);
1245 list.addChild(pnarg);
1250 argument_list_opt ::=
1251 {: RESULT=new ParseNode("empty"); :}
1252 | argument_list:args {: RESULT=args; :}
1257 ParseNode pn=new ParseNode("argument_list");
1261 | argument_list:list COMMA expression:exp {:
1266 array_creation_uninit ::=
1267 NEW primitive_type:type dim_exprs:dimexpr dims_opt:dims {:
1268 ParseNode pn=new ParseNode("createarray");
1270 pn.addChild(dimexpr);
1271 pn.addChild("dims_opt").setLiteral(dims);
1274 | NEW class_or_interface_type:type dim_exprs:dimexpr dims_opt:dims {:
1275 ParseNode pn=new ParseNode("createarray");
1277 pn.addChild(dimexpr);
1278 pn.addChild("dims_opt").setLiteral(dims);
1282 //array_creation_init ::=
1283 // NEW primitive_type dims array_initializer
1284 // | NEW class_or_interface_type dims array_initializer
1286 dim_exprs ::= dim_expr:exp {:
1287 ParseNode pn=new ParseNode("dim_exprs");
1290 | dim_exprs:base dim_expr:exp {:
1295 dim_expr ::= LBRACK expression:exp RBRACK {: RESULT=exp; :}
1297 dims_opt ::= {: RESULT=new Integer(0); :}
1298 | dims:dims {: RESULT = dims; :}
1301 dims ::= LBRACK RBRACK {: RESULT=new Integer(1); :}
1302 | dims:dims LBRACK RBRACK {: RESULT=new Integer(dims.intValue()+1); :}
1306 primary:base DOT IDENTIFIER:id {:
1307 ParseNode pn=new ParseNode("fieldaccess");
1308 pn.addChild("base").addChild(base);
1309 pn.addChild("field").addChild(id);
1312 // | SUPER DOT IDENTIFIER
1313 // | name DOT SUPER DOT IDENTIFIER
1315 method_invocation ::=
1316 name:name LPAREN argument_list_opt:args RPAREN {:
1317 ParseNode pn=new ParseNode("methodinvoke1");
1322 | primary:base DOT IDENTIFIER:name LPAREN argument_list_opt:args RPAREN {:
1323 ParseNode pn=new ParseNode("methodinvoke2");
1324 pn.addChild("base").addChild(base);
1325 pn.addChild("id").addChild(name);
1329 // | SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
1330 // | name DOT SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
1333 name:name LBRACK expression:exp RBRACK {:
1334 ParseNode pn=new ParseNode("arrayaccess");
1335 pn.addChild("base").addChild(name);
1336 pn.addChild("index").addChild(exp);
1339 | primary_no_new_array:base LBRACK expression:exp RBRACK {:
1340 ParseNode pn=new ParseNode("arrayaccess");
1341 pn.addChild("base").addChild(base);
1342 pn.addChild("index").addChild(exp);
1345 // | array_creation_init:init LBRACK expression:exp RBRACK {:
1346 // ParseNode pn=new ParseNode("arrayaccess");
1347 // pn.addChild("init").addChild(init);
1348 // pn.addChild("index").addChild(exp);
1352 postfix_expression ::=
1355 | name:exp {: RESULT=exp; :}
1356 | postincrement_expression:exp {: RESULT=exp; :}
1357 | postdecrement_expression:exp {: RESULT=exp; :}
1359 postincrement_expression ::=
1360 postfix_expression:exp PLUSPLUS
1361 {: RESULT=(new ParseNode("postinc")).addChild(exp).getRoot(); :}
1363 postdecrement_expression ::=
1364 postfix_expression:exp MINUSMINUS
1365 {: RESULT=(new ParseNode("postdec")).addChild(exp).getRoot(); :}
1367 unary_expression ::=
1368 preincrement_expression:exp {: RESULT=exp; :}
1369 | predecrement_expression:exp {: RESULT=exp; :}
1370 | PLUS unary_expression:exp
1371 {: RESULT=(new ParseNode("unaryplus")).addChild(exp).getRoot(); :}
1372 | MINUS unary_expression:exp
1373 {: RESULT=(new ParseNode("unaryminus")).addChild(exp).getRoot(); :}
1374 | unary_expression_not_plus_minus:exp {:
1377 preincrement_expression ::=
1378 PLUSPLUS unary_expression:exp
1379 {: RESULT=(new ParseNode("preinc")).addChild(exp).getRoot(); :}
1381 predecrement_expression ::=
1382 MINUSMINUS unary_expression:exp
1383 {: RESULT=(new ParseNode("predec")).addChild(exp).getRoot(); :}
1385 unary_expression_not_plus_minus ::=
1386 postfix_expression:exp {:
1388 // | COMP unary_expression
1389 | NOT unary_expression:exp
1390 {: RESULT=(new ParseNode("not")).addChild(exp).getRoot(); :}
1391 | cast_expression:exp {: RESULT=exp; :}
1394 LPAREN primitive_type:type
1396 RPAREN unary_expression:exp {:
1397 ParseNode pn=new ParseNode("cast1");
1398 pn.addChild("type").addChild(type);
1399 pn.addChild("exp").addChild(exp);
1402 | LPAREN expression:type RPAREN unary_expression_not_plus_minus:exp {:
1403 ParseNode pn=new ParseNode("cast2");
1404 pn.addChild("type").addChild(type);
1405 pn.addChild("exp").addChild(exp);
1409 // | LPAREN name dims RPAREN unary_expression_not_plus_minus
1411 multiplicative_expression ::=
1412 unary_expression:exp {:
1414 | multiplicative_expression:exp1 MULT unary_expression:exp2 {:
1415 ParseNode pn=new ParseNode("mult");
1420 | multiplicative_expression:exp1 DIV unary_expression:exp2 {:
1421 ParseNode pn=new ParseNode("div");
1426 | multiplicative_expression:exp1 MOD unary_expression:exp2 {:
1427 ParseNode pn=new ParseNode("mod");
1433 additive_expression ::=
1434 multiplicative_expression:exp {:
1436 | additive_expression:exp1 PLUS multiplicative_expression:exp2 {:
1437 ParseNode pn=new ParseNode("add");
1442 | additive_expression:exp1 MINUS multiplicative_expression:exp2 {:
1443 ParseNode pn=new ParseNode("sub");
1449 shift_expression ::=
1450 additive_expression:exp {:
1452 | shift_expression:exp1 LSHIFT additive_expression:exp2 {:
1453 ParseNode pn=new ParseNode("leftshift");
1458 | shift_expression:exp1 RSHIFT additive_expression:exp2 {:
1459 ParseNode pn=new ParseNode("rightshift");
1464 // | shift_expression URSHIFT additive_expression
1466 relational_expression ::=
1467 shift_expression:exp {:
1469 | relational_expression:exp1 LT shift_expression:exp2 {:
1470 ParseNode pn=new ParseNode("comp_lt");
1475 | relational_expression:exp1 GT shift_expression:exp2 {:
1476 ParseNode pn=new ParseNode("comp_gt");
1481 | relational_expression:exp1 LTEQ shift_expression:exp2 {:
1482 ParseNode pn=new ParseNode("comp_lte");
1487 | relational_expression:exp1 GTEQ shift_expression:exp2 {:
1488 ParseNode pn=new ParseNode("comp_gte");
1493 // | relational_expression INSTANCEOF reference_type
1496 equality_expression ::=
1497 relational_expression:exp {:
1499 | equality_expression:exp1 EQEQ relational_expression:exp2 {:
1500 ParseNode pn=new ParseNode("equal");
1505 | equality_expression:exp1 NOTEQ relational_expression:exp2 {:
1506 ParseNode pn=new ParseNode("not_equal");
1513 equality_expression:exp {:
1515 | and_expression:exp1 AND equality_expression:exp2 {:
1516 ParseNode pn=new ParseNode("bitwise_and");
1522 exclusive_or_expression ::=
1523 and_expression:expr {:
1526 | exclusive_or_expression:exp1 XOR and_expression:exp2 {:
1527 ParseNode pn=new ParseNode("bitwise_xor");
1533 inclusive_or_expression ::=
1534 exclusive_or_expression:exclor {:
1536 | inclusive_or_expression:exp1 OR exclusive_or_expression:exp2 {:
1537 ParseNode pn=new ParseNode("bitwise_or");
1543 conditional_and_expression ::=
1544 inclusive_or_expression:inclor {:
1546 | conditional_and_expression:exp1 ANDAND inclusive_or_expression:exp2 {:
1547 ParseNode pn=new ParseNode("logical_and");
1553 conditional_or_expression ::=
1554 conditional_and_expression:condand {:
1556 | conditional_or_expression:exp1 OROR conditional_and_expression:exp2 {:
1557 ParseNode pn=new ParseNode("logical_or");
1563 conditional_expression ::=
1564 conditional_or_expression:condor {:
1566 // | conditional_or_expression QUESTION expression
1567 // COLON conditional_expression
1569 assignment_expression ::=
1570 conditional_expression:expr {:
1572 assignment:assign {:
1575 // semantic check necessary here to ensure a valid left-hand side.
1576 // allowing a parenthesized variable here on the lhs was introduced in
1577 // JLS 2; thanks to Eric Blake for pointing this out.
1578 assignment ::= postfix_expression:lvalue assignment_operator:op assignment_expression:rvalue {:
1579 ParseNode pn=new ParseNode("assignment");
1580 pn.addChild("op").addChild(op);
1581 ParseNode pnargs=pn.addChild("args");
1582 pnargs.addChild(lvalue);
1583 pnargs.addChild(rvalue);
1587 assignment_operator ::=
1588 EQ {: RESULT=new ParseNode("eq"); :}
1589 | MULTEQ {: RESULT=new ParseNode("multeq"); :}
1590 | DIVEQ {: RESULT=new ParseNode("diveq"); :}
1591 | MODEQ {: RESULT=new ParseNode("modeq"); :}
1592 | PLUSEQ {: RESULT=new ParseNode("pluseq"); :}
1593 | MINUSEQ {: RESULT=new ParseNode("minuseq"); :}
1594 | LSHIFTEQ {: RESULT=new ParseNode("lshifteq"); :}
1595 | RSHIFTEQ {: RESULT=new ParseNode("rshifteq"); :}
1596 // | URSHIFTEQ {: RESULT=new ParseNode("urshifteq"); :}
1597 | ANDEQ {: RESULT=new ParseNode("andeq"); :}
1598 | XOREQ {: RESULT=new ParseNode("xoreq"); :}
1599 | OREQ {: RESULT=new ParseNode("oreq"); :}
1602 {: RESULT=new ParseNode("empty"); :}
1606 expression ::= assignment_expression:exp {:
1609 //constant_expression ::=