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
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");
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
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 //interfaces_opt
418 ParseNode pn=new ParseNode("class_declaration");
419 pn.addChild("modifiers").addChild(mo);
420 pn.addChild("name").addChild(id);
421 pn.addChild("super").addChild(so);
422 pn.addChild("classbody").addChild(body);
426 super ::= EXTENDS class_type:classtype {:
431 {: RESULT=new ParseNode("empty"); :}
437 //interfaces ::= IMPLEMENTS interface_type_list
442 //interface_type_list ::=
444 // | interface_type_list COMMA interface_type
447 class_body ::= LBRACE class_body_declarations_opt:cbdo RBRACE {: RESULT=cbdo; :}
450 class_body_declarations_opt ::=
451 {: RESULT=new ParseNode("empty"); :}
452 | class_body_declarations:cbd {: RESULT=cbd; :};
454 class_body_declarations ::=
455 class_body_declaration:cbd {:
456 ParseNode pn=new ParseNode("class_body_declaration_list");
460 | class_body_declarations:cbds class_body_declaration:cbd {:
466 class_body_declaration ::=
467 class_member_declaration:member {:
468 RESULT=(new ParseNode("member")).addChild(member).getRoot();
470 // | static_initializer
471 | constructor_declaration:constructor {:
472 RESULT=(new ParseNode("constructor")).addChild(constructor).getRoot();
475 RESULT=(new ParseNode("block")).addChild(block).getRoot();
478 class_member_declaration ::=
479 field_declaration:field {:
480 RESULT=(new ParseNode("field")).addChild(field).getRoot();
482 | method_declaration:method {:
483 RESULT=(new ParseNode("method")).addChild(method).getRoot();
485 /* repeat the prod for 'class_declaration' here: */
486 // | modifiers_opt CLASS IDENTIFIER super_opt class_body
487 // | interface_declaration
488 | SEMICOLON {: RESULT=new ParseNode("empty"); :}
491 // 19.8.2) Field Declarations
492 field_declaration ::=
493 modifiers_opt:mo type:type variable_declarators:var SEMICOLON {:
494 ParseNode pn=new ParseNode("field_declaration");
495 pn.addChild("modifier").addChild(mo);
496 pn.addChild("type").addChild(type);
497 pn.addChild("variables").addChild(var);
502 variable_declarators ::=
503 variable_declarator:vd {:
504 ParseNode pn=new ParseNode("variable_declarators_list");
508 | variable_declarators:vds COMMA variable_declarator:vd {:
513 variable_declarator ::=
514 variable_declarator_id:id {:
515 ParseNode pn=new ParseNode("variable_declarator");
519 | variable_declarator_id:id EQ variable_initializer:init {:
520 ParseNode pn=new ParseNode("variable_declarator");
522 pn.addChild("initializer").addChild(init);
526 variable_declarator_id ::=
528 RESULT=(new ParseNode("single")).addChild(id).getRoot();:}
529 // | variable_declarator_id:id LBRACK RBRACK {:
530 // RESULT=(new ParseNode("array")).addChild(id).getRoot();:}
532 variable_initializer ::=
533 expression:exp {: RESULT=exp; :}
534 // | array_initializer
537 // 19.8.3) Method Declarations
538 method_declaration ::=
539 method_header:header method_body:body {:
540 ParseNode pn=new ParseNode("method_declaration");
542 pn.addChild("body").addChild(body);
547 modifiers_opt:mo type:type method_declarator:decl //throws_opt
549 ParseNode pn=new ParseNode("method_header");
550 pn.addChild("modifiers").addChild(mo);
551 pn.addChild("returntype").addChild(type);
555 | modifiers_opt:mo VOID method_declarator:decl //throws_opt
557 ParseNode pn=new ParseNode("method_header");
558 pn.addChild("modifiers").addChild(mo);
563 method_declarator ::=
564 IDENTIFIER:id LPAREN formal_parameter_list_opt:params RPAREN {:
565 ParseNode pn=new ParseNode("method_declarator");
566 pn.addChild("name").addChild(id);
567 pn.addChild("parameters").addChild(params);
570 // | method_declarator LBRACK RBRACK // deprecated
571 // be careful; the above production also allows 'void foo() []'
573 formal_parameter_list_opt ::=
574 {: RESULT=new ParseNode("empty"); :}
575 | formal_parameter_list:fpl {:
579 formal_parameter_list ::=
580 formal_parameter:fp {:
581 ParseNode pn=new ParseNode("formal_parameter_list");
585 | formal_parameter_list:fpl COMMA formal_parameter:fp {:
591 type:type variable_declarator_id:name {:
592 ParseNode pn=new ParseNode("formal_parameter");
597 // | FINAL type variable_declarator_id
602 //throws ::= THROWS class_type_list
604 //class_type_list ::=
606 // | class_type_list COMMA class_type
608 method_body ::= block:block {:
611 | SEMICOLON {: RESULT=new ParseNode("empty"); :}
614 // 19.8.4) Static Initializers
615 //static_initializer ::=
619 // 19.8.5) Constructor Declarations
620 constructor_declaration ::=
621 modifiers_opt:mo constructor_declarator:cd
623 constructor_body:body {:
624 ParseNode pn=new ParseNode("constructor_declaration");
625 pn.addChild("modifier").addChild(mo);
626 pn.addChild("declarator").addChild(cd);
627 pn.addChild("body").addChild(body);
631 constructor_declarator ::=
632 simple_name:name LPAREN formal_parameter_list_opt:fplo RPAREN {:
633 ParseNode pn=new ParseNode("constructor_declarator");
634 pn.addChild("name").addChild(name);
635 pn.addChild("parameters").addChild(fplo);
640 // LBRACE explicit_constructor_invocation:eci block_statements:bs RBRACE |
641 // LBRACE explicit_constructor_invocation RBRACE |
642 LBRACE block_statements:block RBRACE {:
643 ParseNode pn=new ParseNode("constructor_body");
644 pn.addChild("block").addChild(block);
647 | LBRACE RBRACE {: RESULT=new ParseNode("empty"); :}
649 //explicit_constructor_invocation ::=
650 // THIS LPAREN argument_list_opt RPAREN SEMICOLON
651 // | SUPER LPAREN argument_list_opt RPAREN SEMICOLON
652 // | primary DOT THIS LPAREN argument_list_opt RPAREN SEMICOLON
653 // | primary DOT SUPER LPAREN argument_list_opt RPAREN SEMICOLON
658 // 19.9.1) Interface Declarations
659 //interface_declaration ::=
660 // modifiers_opt INTERFACE IDENTIFIER extends_interfaces_opt
663 //extends_interfaces_opt ::=
664 // | extends_interfaces
666 //extends_interfaces ::=
667 // EXTENDS interface_type
668 // | extends_interfaces COMMA interface_type
671 // LBRACE interface_member_declarations_opt RBRACE
673 //interface_member_declarations_opt ::=
674 // | interface_member_declarations
676 //interface_member_declarations ::=
677 // interface_member_declaration
678 // | interface_member_declarations interface_member_declaration
680 //interface_member_declaration ::=
681 // constant_declaration
682 // | abstract_method_declaration
683 // | class_declaration
684 // | interface_declaration
687 //constant_declaration ::=
689 // // need to semantically check that modifiers of field declaration
690 // // include only PUBLIC, STATIC, or FINAL. Other modifiers are
693 //abstract_method_declaration ::=
694 // method_header SEMICOLON
699 //array_initializer ::=
700 // LBRACE variable_initializers COMMA RBRACE
701 // | LBRACE variable_initializers RBRACE
702 // | LBRACE COMMA RBRACE
705 //variable_initializers ::=
706 // variable_initializer
707 // | variable_initializers COMMA variable_initializer
710 // 19.11) Blocks and Statements
711 block ::= LBRACE block_statements_opt:bso RBRACE {:
712 ParseNode pn=new ParseNode("block");
713 pn.addChild("block_statements_opt").addChild(bso);
717 block_statements_opt ::=
718 {: RESULT=new ParseNode("empty"); :}
719 | block_statements:bs {:
720 ParseNode pn=new ParseNode("block_statements_opt");
721 pn.addChild("block_statements").addChild(bs);
726 block_statement:bs {:
727 ParseNode pn=new ParseNode("block_statement_list");
731 | block_statements:bss block_statement:bs {:
737 local_variable_declaration_statement:lvds {:
738 ParseNode pn=new ParseNode("block_statement");
739 pn.addChild("local_variable_declaration").addChild(lvds);
742 | statement:statement {:
743 ParseNode pn=new ParseNode("statement");
744 pn.addChild("statement").addChild(statement);
747 // | class_declaration
748 // | interface_declaration
750 local_variable_declaration_statement ::=
751 local_variable_declaration:lvd SEMICOLON {:
755 local_variable_declaration ::=
756 type:type variable_declarators:var_decls {:
757 ParseNode pn=new ParseNode("local_variable_declaration");
758 pn.addChild("type").addChild(type);
759 pn.addChild("variables").addChild(var_decls);
762 // | FINAL type variable_declarators
764 statement ::= statement_without_trailing_substatement:st {:
767 // | labeled_statement:st {: RESULT=st; :}
768 | if_then_statement:st {: RESULT=st; :}
769 | if_then_else_statement:st {: RESULT=st; :}
770 | while_statement:st {: RESULT=st; :}
771 | for_statement:st {: RESULT=st; :}
773 statement_no_short_if ::=
774 statement_without_trailing_substatement:st {: RESULT=st; :}
775 // | labeled_statement_no_short_if:st {: RESULT=st; :}
776 | if_then_else_statement_no_short_if:st {: RESULT=st; :}
777 | while_statement_no_short_if:st {: RESULT=st; :}
778 | for_statement_no_short_if:st {: RESULT=st; :}
780 statement_without_trailing_substatement ::=
781 block:st {: RESULT=st; :}
782 | empty_statement:st {: RESULT=st; :}
783 | expression_statement:st {: RESULT=st; :}
784 // | switch_statement
786 | break_statement:st {: RESULT=st; :}
787 | continue_statement:st {: RESULT=st; :}
788 | return_statement:st {: RESULT=st; :}
789 // | synchronized_statement
792 // | assert_statement
795 SEMICOLON {: RESULT=(new ParseNode("statement")).addChild("nop").getRoot(); :}
797 //labeled_statement ::=
798 // IDENTIFIER COLON statement
800 //labeled_statement_no_short_if ::=
801 // IDENTIFIER COLON statement_no_short_if
803 expression_statement ::=
804 statement_expression:se SEMICOLON {: RESULT=se; :}
806 statement_expression ::=
807 assignment:st {: RESULT=st; :}
808 | preincrement_expression:st {: RESULT=st; :}
809 | predecrement_expression:st {: RESULT=st; :}
810 | postincrement_expression:st {: RESULT=st; :}
811 | postdecrement_expression:st {: RESULT=st; :}
812 | method_invocation:st {: RESULT=st; :}
813 | class_instance_creation_expression:st {: RESULT=st; :}
815 if_then_statement ::=
816 IF LPAREN expression:exp RPAREN statement:st {:
817 ParseNode pn=new ParseNode("ifstatement");
818 pn.addChild("condition").addChild(exp);
819 pn.addChild("statement").addChild(st);
823 if_then_else_statement ::=
824 IF LPAREN expression:exp RPAREN statement_no_short_if:st
825 ELSE statement:else_st {:
826 ParseNode pn=new ParseNode("ifstatement");
827 pn.addChild("condition").addChild(exp);
828 pn.addChild("statement").addChild(st);
829 pn.addChild("else_statement").addChild(else_st);
833 if_then_else_statement_no_short_if ::=
834 IF LPAREN expression:exp RPAREN statement_no_short_if:st
835 ELSE statement_no_short_if:else_st {:
836 ParseNode pn=new ParseNode("ifstatement");
837 pn.addChild("condition").addChild(exp);
838 pn.addChild("statement").addChild(st);
839 pn.addChild("else_statement").addChild(else_st);
843 //switch_statement ::=
844 // SWITCH LPAREN expression RPAREN switch_block
847 // LBRACE switch_block_statement_groups switch_labels RBRACE
848 // | LBRACE switch_block_statement_groups RBRACE
849 // | LBRACE switch_labels RBRACE
852 //switch_block_statement_groups ::=
853 // switch_block_statement_group
854 // | switch_block_statement_groups switch_block_statement_group
856 //switch_block_statement_group ::=
857 // switch_labels block_statements
861 // | switch_labels switch_label
864 // CASE constant_expression COLON
869 WHILE LPAREN expression:exp RPAREN statement:st {:
870 ParseNode pn=new ParseNode("whilestatement");
871 pn.addChild("condition").addChild(exp);
872 pn.addChild("statement").addChild(st);
876 while_statement_no_short_if ::=
877 WHILE LPAREN expression:exp RPAREN statement_no_short_if:st {:
878 ParseNode pn=new ParseNode("whilestatement");
879 pn.addChild("condition").addChild(exp);
880 pn.addChild("statement").addChild(st);
885 // DO statement WHILE LPAREN expression RPAREN SEMICOLON
888 FOR LPAREN for_init_opt:init SEMICOLON expression_opt:exp SEMICOLON
889 for_update_opt:update RPAREN statement:st {:
890 ParseNode pn=new ParseNode("forstatement");
891 pn.addChild("initializer").addChild(init);
892 pn.addChild("condition").addChild(exp);
893 pn.addChild("update").addChild(update);
894 pn.addChild("statement").addChild(st);
898 for_statement_no_short_if ::=
899 FOR LPAREN for_init_opt:init SEMICOLON expression_opt:exp SEMICOLON
900 for_update_opt:update RPAREN statement_no_short_if:st {:
901 ParseNode pn=new ParseNode("forstatement");
902 pn.addChild("initializer").addChild(init);
903 pn.addChild("condition").addChild(exp);
904 pn.addChild("update").addChild(update);
905 pn.addChild("statement").addChild(st);
910 {: RESULT=new ParseNode("empty"); :}
911 | for_init:init {: RESULT=init; :}
913 for_init ::= statement_expression_list:list {: RESULT=list; :}
914 | local_variable_declaration:decl {: RESULT=decl; :}
917 {: RESULT=new ParseNode("empty"); :}
918 | for_update:update {: RESULT=update; :}
920 for_update ::= statement_expression_list:list {: RESULT=list; :}
922 statement_expression_list ::=
923 statement_expression:expr {:
924 RESULT=(new ParseNode("statement_expression_list")).addChild(expr).getRoot();
926 | statement_expression_list:list COMMA statement_expression:expr {:
939 SEMICOLON {: RESULT=new ParseNode("break"); :}
942 continue_statement ::=
946 {: RESULT=new ParseNode("continue"); :}
949 RETURN expression_opt:exp SEMICOLON {:
950 RESULT=(new ParseNode("return")).addChild("retvalue").addChild(exp).getRoot(); :}
952 //throw_statement ::=
953 // THROW expression SEMICOLON
955 //synchronized_statement ::=
956 // SYNCHRONIZED LPAREN expression RPAREN block
960 // | TRY block catches_opt finally
965 //catches ::= catch_clause
966 // | catches catch_clause
969 // CATCH LPAREN formal_parameter RPAREN block
971 //finally ::= FINALLY block
973 //assert_statement ::=
974 // ASSERT expression SEMICOLON
975 // | ASSERT expression COLON expression SEMICOLON
978 // 19.12) Expressions
979 primary ::= primary_no_new_array:st {:
981 // | array_creation_init
982 // | array_creation_uninit
984 primary_no_new_array ::=
985 literal:lit {: RESULT=lit; :}
986 | THIS {: RESULT=new ParseNode("this"); :}
987 | LPAREN expression:exp RPAREN {: RESULT=exp; :}
988 | class_instance_creation_expression:exp {: RESULT=exp; :}
989 | field_access:exp {: RESULT=exp; :}
990 | method_invocation:exp {: RESULT=exp; :}
992 // | primitive_type DOT CLASS
994 // | array_type DOT CLASS
998 class_instance_creation_expression ::=
999 NEW class_or_interface_type:type LPAREN argument_list_opt:args RPAREN {:
1000 ParseNode pn=new ParseNode("createobject");
1001 pn.addChild("type").addChild(type);
1002 pn.addChild("args").addChild(args);
1005 // | NEW class_or_interface_type LPAREN argument_list_opt RPAREN class_body
1006 // | primary DOT NEW IDENTIFIER
1007 // LPAREN argument_list_opt RPAREN {:
1010 // | primary DOT NEW IDENTIFIER
1011 // LPAREN argument_list_opt RPAREN class_body
1012 // | name DOT NEW IDENTIFIER
1013 // LPAREN argument_list_opt RPAREN
1014 // | name DOT NEW IDENTIFIER
1015 // LPAREN argument_list_opt RPAREN class_body
1017 argument_list_opt ::=
1018 {: RESULT=new ParseNode("empty"); :}
1019 | argument_list:args {: RESULT=args; :}
1023 ParseNode pn=new ParseNode("argument_list");
1027 | argument_list:list COMMA expression:exp {:
1032 //array_creation_uninit ::=
1033 // NEW primitive_type dim_exprs dims_opt
1034 // | NEW class_or_interface_type dim_exprs dims_opt
1036 //array_creation_init ::=
1037 // NEW primitive_type dims array_initializer
1038 // | NEW class_or_interface_type dims array_initializer
1040 //dim_exprs ::= dim_expr
1041 // | dim_exprs dim_expr
1043 //dim_expr ::= LBRACK expression RBRACK
1048 //dims ::= LBRACK RBRACK
1049 // | dims LBRACK RBRACK
1052 primary:base DOT IDENTIFIER:id {:
1053 ParseNode pn=new ParseNode("fieldaccess");
1054 pn.addChild("base").addChild(base);
1055 pn.addChild("field").addChild(id);
1058 // | SUPER DOT IDENTIFIER
1059 // | name DOT SUPER DOT IDENTIFIER
1061 method_invocation ::=
1062 name:name LPAREN argument_list_opt:args RPAREN {:
1063 ParseNode pn=new ParseNode("methodinvoke");
1064 pn.addChild("name").addChild(name);
1065 pn.addChild("args").addChild(args);
1068 | primary:base DOT IDENTIFIER:name LPAREN argument_list_opt:args RPAREN {:
1069 ParseNode pn=new ParseNode("methodinvoke");
1070 pn.addChild("base").addChild(base);
1071 pn.addChild("name").addChild(name);
1072 pn.addChild("args").addChild(args);
1075 // | SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
1076 // | name DOT SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
1079 // name LBRACK expression RBRACK
1080 // | primary_no_new_array LBRACK expression RBRACK
1081 // | array_creation_init LBRACK expression RBRACK
1083 postfix_expression ::=
1086 | name:exp {: RESULT=exp; :}
1087 | postincrement_expression:exp {: RESULT=exp; :}
1088 | postdecrement_expression:exp {: RESULT=exp; :}
1090 postincrement_expression ::=
1091 postfix_expression:exp PLUSPLUS
1092 {: RESULT=(new ParseNode("postinc")).addChild(exp).getRoot(); :}
1094 postdecrement_expression ::=
1095 postfix_expression:exp MINUSMINUS
1096 {: RESULT=(new ParseNode("postdec")).addChild(exp).getRoot(); :}
1098 unary_expression ::=
1099 preincrement_expression:exp {: RESULT=exp; :}
1100 | predecrement_expression:exp {: RESULT=exp; :}
1101 | PLUS unary_expression:exp
1102 {: RESULT=(new ParseNode("unaryplus")).addChild(exp).getRoot(); :}
1103 | MINUS unary_expression:exp
1104 {: RESULT=(new ParseNode("unaryminus")).addChild(exp).getRoot(); :}
1105 | unary_expression_not_plus_minus:exp {:
1108 preincrement_expression ::=
1109 PLUSPLUS unary_expression:exp
1110 {: RESULT=(new ParseNode("preinc")).addChild(exp).getRoot(); :}
1112 predecrement_expression ::=
1113 MINUSMINUS unary_expression:exp
1114 {: RESULT=(new ParseNode("predec")).addChild(exp).getRoot(); :}
1116 unary_expression_not_plus_minus ::=
1117 postfix_expression:exp {:
1119 // | COMP unary_expression
1120 | NOT unary_expression:exp
1121 {: RESULT=(new ParseNode("not")).addChild(exp).getRoot(); :}
1122 | cast_expression:exp {: RESULT=exp; :}
1125 LPAREN primitive_type:type
1127 RPAREN unary_expression:exp {:
1128 ParseNode pn=new ParseNode("cast");
1129 pn.addChild("type").addChild(type);
1130 pn.addChild("exp").addChild(exp);
1133 | LPAREN expression:type RPAREN unary_expression_not_plus_minus:exp {:
1134 ParseNode pn=new ParseNode("cast");
1135 pn.addChild("type").addChild(type);
1136 pn.addChild("exp").addChild(exp);
1140 // | LPAREN name dims RPAREN unary_expression_not_plus_minus
1142 multiplicative_expression ::=
1143 unary_expression:exp {:
1145 | multiplicative_expression:exp1 MULT unary_expression:exp2 {:
1146 ParseNode pn=new ParseNode("mult");
1151 | multiplicative_expression:exp1 DIV unary_expression:exp2 {:
1152 ParseNode pn=new ParseNode("div");
1157 | multiplicative_expression:exp1 MOD unary_expression:exp2 {:
1158 ParseNode pn=new ParseNode("mod");
1164 additive_expression ::=
1165 multiplicative_expression:exp {:
1167 | additive_expression:exp1 PLUS multiplicative_expression:exp2 {:
1168 ParseNode pn=new ParseNode("add");
1173 | additive_expression:exp1 MINUS multiplicative_expression:exp2 {:
1174 ParseNode pn=new ParseNode("sub");
1180 shift_expression ::=
1181 additive_expression:exp {:
1183 | shift_expression:exp1 LSHIFT additive_expression:exp2 {:
1184 ParseNode pn=new ParseNode("leftshift");
1189 | shift_expression:exp1 RSHIFT additive_expression:exp2 {:
1190 ParseNode pn=new ParseNode("rightshift");
1195 // | shift_expression URSHIFT additive_expression
1197 relational_expression ::=
1198 shift_expression:exp {:
1200 | relational_expression:exp1 LT shift_expression:exp2 {:
1201 ParseNode pn=new ParseNode("comp_lt");
1206 | relational_expression:exp1 GT shift_expression:exp2 {:
1207 ParseNode pn=new ParseNode("comp_gt");
1212 | relational_expression:exp1 LTEQ shift_expression:exp2 {:
1213 ParseNode pn=new ParseNode("comp_lte");
1218 | relational_expression:exp1 GTEQ shift_expression:exp2 {:
1219 ParseNode pn=new ParseNode("comp_gte");
1224 // | relational_expression INSTANCEOF reference_type
1227 equality_expression ::=
1228 relational_expression:exp {:
1230 | equality_expression:exp1 EQEQ relational_expression:exp2 {:
1231 ParseNode pn=new ParseNode("equal");
1236 | equality_expression:exp1 NOTEQ relational_expression:exp2 {:
1237 ParseNode pn=new ParseNode("not_equal");
1244 equality_expression:exp {:
1246 | and_expression:exp1 AND equality_expression:exp2 {:
1247 ParseNode pn=new ParseNode("bitwise_and");
1253 exclusive_or_expression ::=
1254 and_expression:expr {:
1257 | exclusive_or_expression:exp1 XOR and_expression:exp2 {:
1258 ParseNode pn=new ParseNode("bitwise_xor");
1264 inclusive_or_expression ::=
1265 exclusive_or_expression:exclor {:
1267 | inclusive_or_expression:exp1 OR exclusive_or_expression:exp2 {:
1268 ParseNode pn=new ParseNode("bitwise_or");
1274 conditional_and_expression ::=
1275 inclusive_or_expression:inclor {:
1277 | conditional_and_expression:exp1 ANDAND inclusive_or_expression:exp2 {:
1278 ParseNode pn=new ParseNode("logical_and");
1284 conditional_or_expression ::=
1285 conditional_and_expression:condand {:
1287 | conditional_or_expression:exp1 OROR conditional_and_expression:exp2 {:
1288 ParseNode pn=new ParseNode("logical_or");
1294 conditional_expression ::=
1295 conditional_or_expression:condor {:
1297 // | conditional_or_expression QUESTION expression
1298 // COLON conditional_expression
1300 assignment_expression ::=
1301 conditional_expression:expr {:
1303 assignment:assign {:
1306 // semantic check necessary here to ensure a valid left-hand side.
1307 // allowing a parenthesized variable here on the lhs was introduced in
1308 // JLS 2; thanks to Eric Blake for pointing this out.
1309 assignment ::= postfix_expression:lvalue assignment_operator:op assignment_expression:rvalue {:
1310 ParseNode pn=new ParseNode("assignment");
1311 pn.addChild("op").addChild(op);
1312 ParseNode pnargs=pn.addChild("args");
1313 pnargs.addChild(lvalue);
1314 pnargs.addChild(rvalue);
1318 assignment_operator ::=
1319 EQ {: RESULT=(new ParseNode("assignment_operator")).addChild("eq"); :}
1320 | MULTEQ {: RESULT=(new ParseNode("assignment_operator")).addChild("multeq"); :}
1321 | DIVEQ {: RESULT=(new ParseNode("assignment_operator")).addChild("diveq"); :}
1322 | MODEQ {: RESULT=(new ParseNode("assignment_operator")).addChild("modeq"); :}
1323 | PLUSEQ {: RESULT=(new ParseNode("assignment_operator")).addChild("pluseq"); :}
1324 | MINUSEQ {: RESULT=(new ParseNode("assignment_operator")).addChild("minuseq"); :}
1325 | LSHIFTEQ {: RESULT=(new ParseNode("assignment_operator")).addChild("lshifteq"); :}
1326 | RSHIFTEQ {: RESULT=(new ParseNode("assignment_operator")).addChild("rshifteq"); :}
1327 // | URSHIFTEQ {: RESULT=(new ParseNode("assignment_operator")).addChild("urshifteq"); :}
1328 | ANDEQ {: RESULT=(new ParseNode("assignment_operator")).addChild("andeq"); :}
1329 | XOREQ {: RESULT=(new ParseNode("assignment_operator")).addChild("xoreq"); :}
1330 | OREQ {: RESULT=(new ParseNode("assignment_operator")).addChild("oreq"); :}
1333 {: RESULT=new ParseNode("empty"); :}
1337 expression ::= assignment_expression:exp {:
1340 //constant_expression ::=