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 argument_list_opt, argument_list;
200 //non terminal ParseNode array_creation_init;
201 non terminal ParseNode array_creation_uninit;
202 non terminal ParseNode dim_exprs, dim_expr;
203 non terminal Integer dims_opt, dims;
204 non terminal ParseNode field_access, method_invocation;
205 non terminal ParseNode array_access;
206 non terminal ParseNode postfix_expression;
207 non terminal ParseNode postincrement_expression, postdecrement_expression;
208 non terminal ParseNode unary_expression, unary_expression_not_plus_minus;
209 non terminal ParseNode preincrement_expression, predecrement_expression;
210 non terminal ParseNode cast_expression;
211 non terminal ParseNode multiplicative_expression, additive_expression;
212 non terminal ParseNode shift_expression, relational_expression, equality_expression;
213 non terminal ParseNode and_expression, exclusive_or_expression, inclusive_or_expression;
214 non terminal ParseNode conditional_and_expression, conditional_or_expression;
215 non terminal ParseNode conditional_expression;
216 non terminal ParseNode assignment_expression;
217 non terminal ParseNode assignment;
218 non terminal ParseNode assignment_operator;
219 non terminal ParseNode expression_opt, expression;
220 //non terminal ParseNode constant_expression;
221 //failure aware computation keywords
226 non terminal ParseNode flag_declaration;
227 non terminal ParseNode task_declaration;
228 non terminal ParseNode task_parameter_list;
229 non terminal ParseNode task_parameter;
230 non terminal ParseNode flag_expression;
231 non terminal ParseNode flag_andexpression;
232 non terminal ParseNode flag_notexpression;
233 non terminal ParseNode task_exitstatement;
234 non terminal ParseNode flag_effects_opt;
235 non terminal ParseNode flag_effects;
236 non terminal ParseNode flag_effect;
237 non terminal ParseNode flag_list;
238 non terminal ParseNode flag_change;
245 TASK IDENTIFIER:id LPAREN task_parameter_list:tpl RPAREN
249 ParseNode pn=new ParseNode("task_declaration");
250 pn.addChild("name").addChild(id);
253 pn.addChild("body").addChild(body);
257 task_parameter_list ::=
259 ParseNode pn=new ParseNode("task_parameter_list");
263 | task_parameter_list:fpl COMMA task_parameter:fp {:
270 type:type variable_declarator_id:name LBRACE flag_expression:exp RBRACE {:
271 ParseNode pn=new ParseNode("task_parameter");
280 flag_andexpression:exp {:
283 | flag_expression:exp1 OROR flag_andexpression:exp2 {:
284 ParseNode pn=new ParseNode("or");
291 flag_andexpression ::=
292 flag_notexpression:exp {: RESULT=exp; :}
293 | flag_notexpression:exp1 ANDAND flag_andexpression:exp2 {:
294 ParseNode pn=new ParseNode("and");
301 flag_notexpression ::=
302 NOT flag_notexpression:exp {:
303 ParseNode pn=new ParseNode("not");
307 | LPAREN flag_expression:exp RPAREN {:
311 ParseNode pn=new ParseNode("name");
317 task_exitstatement ::= TASKEXIT flag_effects_opt SEMICOLON{:
318 RESULT=new ParseNode("taskexit");
321 flag_effects_opt ::= LPAREN flag_effects:fe RPAREN {:RESULT=fe;:}
322 | {: RESULT = null; :}
325 flag_effects ::= flag_effect:fe {:
326 ParseNode pn=new ParseNode("flag_effects_list");
330 | flag_effects:fes COMMA flag_effect:fe {:
335 flag_effect ::= IDENTIFIER:id LBRACE flag_list:fl RBRACE {:
336 ParseNode pn=new ParseNode("flag_effect");
341 flag_list ::= flag_change:fc {:
342 ParseNode pn=new ParseNode("flag_list");
346 | flag_list:fl COMMA flag_change:fc {:
351 flag_change ::= IDENTIFIER:id {:
352 RESULT=new ParseNode("name").addChild(id).getRoot();
355 RESULT=new ParseNode("not").addChild("name").addChild(id).getRoot();
358 // 19.2) The Syntactic Grammar
359 goal ::= compilation_unit:cu
365 // 19.3) Lexical Structure.
368 literal ::= INTEGER_LITERAL:integer_lit
370 ParseNode pn=new ParseNode("literal");
371 pn.addChild("integer").setLiteral(integer_lit);
374 | FLOATING_POINT_LITERAL:float_lit
376 ParseNode pn=new ParseNode("literal");
377 pn.addChild("float").setLiteral(float_lit);
380 | BOOLEAN_LITERAL:boolean_lit
382 ParseNode pn=new ParseNode("literal");
383 pn.addChild("boolean").setLiteral(boolean_lit);
386 | CHARACTER_LITERAL:char_lit
388 ParseNode pn=new ParseNode("literal");
389 pn.addChild("char").setLiteral(char_lit);
392 | STRING_LITERAL:string_lit
394 ParseNode pn=new ParseNode("literal");
395 pn.addChild("string").setLiteral(string_lit);
400 RESULT=(new ParseNode("literal")).addChild("null").getRoot();
404 // 19.4) Types, Values, and Variables
405 type ::= primitive_type:type {: RESULT=type; :}
406 | reference_type:type {: RESULT=type; :}
410 numeric_type:type {: RESULT=type; :}
411 | BOOLEAN {: RESULT=(new ParseNode("type")).addChild("boolean").getRoot(); :}
413 numeric_type::= integral_type:type {: RESULT=type; :}
414 | floating_point_type:type {: RESULT=type; :}
417 BYTE {: RESULT=(new ParseNode("type")).addChild("byte").getRoot(); :}
418 | SHORT {: RESULT=(new ParseNode("type")).addChild("short").getRoot(); :}
419 | INT {: RESULT=(new ParseNode("type")).addChild("int").getRoot(); :}
420 | LONG {: RESULT=(new ParseNode("type")).addChild("long").getRoot(); :}
421 | CHAR {: RESULT=(new ParseNode("type")).addChild("char").getRoot(); :}
423 floating_point_type ::=
424 FLOAT {: RESULT=(new ParseNode("type")).addChild("float").getRoot(); :}
425 | DOUBLE {: RESULT=(new ParseNode("type")).addChild("double").getRoot(); :}
429 class_or_interface_type:type {: RESULT=type; :}
430 | array_type:type {: RESULT=type; :}
432 class_or_interface_type ::= name:name {:
433 RESULT=(new ParseNode("type")).addChild("class").addChild(name).getRoot();
436 class_type ::= class_or_interface_type:type {: RESULT=type; :};
437 //interface_type ::= class_or_interface_type;
439 array_type ::= primitive_type:prim dims:dims {:
440 ParseNode pn=(new ParseNode("type")).addChild("array");
441 pn.addChild("basetype").addChild(prim);
442 pn.addChild("dims").setLiteral(dims);
445 | name:name dims:dims {:
446 ParseNode pn=(new ParseNode("type")).addChild("array");
447 pn.addChild("basetype").addChild("type").addChild("class").addChild(name);
448 pn.addChild("dims").setLiteral(dims);
454 name ::= simple_name:name {: RESULT=name; :}
455 | qualified_name:name {: RESULT=name; :}
457 simple_name ::= IDENTIFIER:id {:
458 RESULT=(new ParseNode("name")).addChild("identifier").addChild(id).getRoot();
461 qualified_name ::= name:name DOT IDENTIFIER:id {:
462 ParseNode pn=new ParseNode("name");
463 pn.addChild("base").addChild(name);
464 pn.addChild("identifier").addChild(id);
471 // package_declaration_opt
472 // import_declarations_opt
473 type_declarations_opt:tdo {:
474 ParseNode pn=new ParseNode("compilation_unit");
479 //package_declaration_opt ::= package_declaration | ;
480 //import_declarations_opt ::= import_declarations | ;
481 type_declarations_opt ::= type_declarations:tds {:
484 {: RESULT=new ParseNode("empty"); :}
487 //import_declarations ::=
488 // import_declaration
489 // | import_declarations import_declaration
492 type_declarations ::=
493 type_declaration:td {:
494 ParseNode pn=new ParseNode("type_declaration_list");
498 | type_declarations:tds type_declaration:td {:
504 //package_declaration ::=
505 // PACKAGE name SEMICOLON
507 //import_declaration ::=
508 // single_type_import_declaration
509 // | type_import_on_demand_declaration
511 //single_type_import_declaration ::=
512 // IMPORT name SEMICOLON
514 //type_import_on_demand_declaration ::=
515 // IMPORT name DOT MULT SEMICOLON
523 | task_declaration:td
527 // | interface_declaration
528 | SEMICOLON {: RESULT=new ParseNode("empty"); :}
531 // 19.7) Productions used only in the LALR(1) grammar
533 {: RESULT=new ParseNode("empty"); :}
538 modifiers ::= modifier:mo {:
539 ParseNode pn=new ParseNode("modifier_list");
543 | modifiers:mos modifier:mo {:
549 PUBLIC {: RESULT=new ParseNode("public"); :}|
550 PROTECTED {: RESULT=new ParseNode("protected"); :}|
551 PRIVATE {: RESULT=new ParseNode("private"); :}|
552 STATIC {: RESULT=new ParseNode("static"); :} |
554 FINAL {: RESULT=new ParseNode("final"); :}|
555 NATIVE {: RESULT=new ParseNode("native"); :}
559 // STRICTFP // note that semantic analysis must check that the
560 // context of the modifier allows strictfp.
565 // 19.8.1) Class Declaration:
566 class_declaration ::=
567 modifiers_opt:mo CLASS IDENTIFIER:id super_opt:so //interfaces_opt
570 ParseNode pn=new ParseNode("class_declaration");
571 pn.addChild("modifiers").addChild(mo);
572 pn.addChild("name").addChild(id);
573 pn.addChild("super").addChild(so);
574 pn.addChild("classbody").addChild(body);
578 super ::= EXTENDS class_type:classtype {:
583 {: RESULT=new ParseNode("empty"); :}
589 //interfaces ::= IMPLEMENTS interface_type_list
594 //interface_type_list ::=
596 // | interface_type_list COMMA interface_type
599 class_body ::= LBRACE class_body_declarations_opt:cbdo RBRACE {: RESULT=cbdo; :}
602 class_body_declarations_opt ::=
603 {: RESULT=new ParseNode("empty"); :}
604 | class_body_declarations:cbd {: RESULT=cbd; :};
606 class_body_declarations ::=
607 class_body_declaration:cbd {:
608 ParseNode pn=new ParseNode("class_body_declaration_list");
612 | class_body_declarations:cbds class_body_declaration:cbd {:
618 class_body_declaration ::=
619 class_member_declaration:member {:
620 RESULT=(new ParseNode("member")).addChild(member).getRoot();
622 // | static_initializer
623 | constructor_declaration:constructor {:
624 RESULT=(new ParseNode("constructor")).addChild(constructor).getRoot();
627 RESULT=(new ParseNode("block")).addChild(block).getRoot();
630 class_member_declaration ::=
631 //failure aware computation
632 flag_declaration:flag {:
633 RESULT=(new ParseNode("flag")).addChild(flag).getRoot();
636 field_declaration:field {:
637 RESULT=(new ParseNode("field")).addChild(field).getRoot();
639 | method_declaration:method {:
640 RESULT=(new ParseNode("method")).addChild(method).getRoot();
642 /* repeat the prod for 'class_declaration' here: */
643 // | modifiers_opt CLASS IDENTIFIER super_opt class_body
644 // | interface_declaration
645 | SEMICOLON {: RESULT=new ParseNode("empty"); :}
648 //Failure aware computation
650 FLAG IDENTIFIER:id SEMICOLON {:
651 ParseNode pn=new ParseNode("flag_declaration");
652 pn.addChild("name").addChild(id);
657 // 19.8.2) Field Declarations
658 field_declaration ::=
659 modifiers_opt:mo type:type variable_declarators:var SEMICOLON {:
660 ParseNode pn=new ParseNode("field_declaration");
661 pn.addChild("modifier").addChild(mo);
662 pn.addChild("type").addChild(type);
663 pn.addChild("variables").addChild(var);
668 variable_declarators ::=
669 variable_declarator:vd {:
670 ParseNode pn=new ParseNode("variable_declarators_list");
674 | variable_declarators:vds COMMA variable_declarator:vd {:
679 variable_declarator ::=
680 variable_declarator_id:id {:
681 ParseNode pn=new ParseNode("variable_declarator");
685 | variable_declarator_id:id EQ variable_initializer:init {:
686 ParseNode pn=new ParseNode("variable_declarator");
688 pn.addChild("initializer").addChild(init);
692 variable_declarator_id ::=
694 RESULT=(new ParseNode("single")).addChild(id).getRoot();:}
695 | variable_declarator_id:id LBRACK RBRACK {:
696 RESULT=(new ParseNode("array")).addChild(id).getRoot();:}
698 variable_initializer ::=
699 expression:exp {: RESULT=exp; :}
700 // | array_initializer
703 // 19.8.3) Method Declarations
704 method_declaration ::=
705 method_header:header method_body:body {:
706 ParseNode pn=new ParseNode("method_declaration");
708 pn.addChild("body").addChild(body);
713 modifiers_opt:mo type:type method_declarator:decl //throws_opt
715 ParseNode pn=new ParseNode("method_header");
716 pn.addChild("modifiers").addChild(mo);
717 pn.addChild("returntype").addChild(type);
721 | modifiers_opt:mo VOID method_declarator:decl //throws_opt
723 ParseNode pn=new ParseNode("method_header");
724 pn.addChild("modifiers").addChild(mo);
729 method_declarator ::=
730 IDENTIFIER:id LPAREN formal_parameter_list_opt:params RPAREN {:
731 ParseNode pn=new ParseNode("method_declarator");
732 pn.addChild("name").addChild(id);
733 pn.addChild("parameters").addChild(params);
736 // | method_declarator LBRACK RBRACK // deprecated
737 // be careful; the above production also allows 'void foo() []'
739 formal_parameter_list_opt ::=
740 {: RESULT=new ParseNode("empty"); :}
741 | formal_parameter_list:fpl {:
745 formal_parameter_list ::=
746 formal_parameter:fp {:
747 ParseNode pn=new ParseNode("formal_parameter_list");
751 | formal_parameter_list:fpl COMMA formal_parameter:fp {:
757 type:type variable_declarator_id:name {:
758 ParseNode pn=new ParseNode("formal_parameter");
763 // | FINAL type variable_declarator_id
768 //throws ::= THROWS class_type_list
770 //class_type_list ::=
772 // | class_type_list COMMA class_type
774 method_body ::= block:block {:
777 | SEMICOLON {: RESULT=new ParseNode("empty"); :}
780 // 19.8.4) Static Initializers
781 //static_initializer ::=
785 // 19.8.5) Constructor Declarations
786 constructor_declaration ::=
787 modifiers_opt:mo constructor_declarator:cd
789 constructor_body:body {:
790 ParseNode pn=new ParseNode("constructor_declaration");
791 pn.addChild("modifiers").addChild(mo);
793 pn.addChild("body").addChild(body);
797 constructor_declarator ::=
798 simple_name:name LPAREN formal_parameter_list_opt:fplo RPAREN {:
799 ParseNode pn=new ParseNode("constructor_declarator");
801 pn.addChild("parameters").addChild(fplo);
806 // LBRACE explicit_constructor_invocation:eci block_statements:bs RBRACE |
807 // LBRACE explicit_constructor_invocation RBRACE |
808 LBRACE block_statements:block RBRACE {:
809 ParseNode pn=new ParseNode("constructor_body");
813 | LBRACE RBRACE {: RESULT=new ParseNode("empty"); :}
815 //explicit_constructor_invocation ::=
816 // THIS LPAREN argument_list_opt RPAREN SEMICOLON
817 // | SUPER LPAREN argument_list_opt RPAREN SEMICOLON
818 // | primary DOT THIS LPAREN argument_list_opt RPAREN SEMICOLON
819 // | primary DOT SUPER LPAREN argument_list_opt RPAREN SEMICOLON
824 // 19.9.1) Interface Declarations
825 //interface_declaration ::=
826 // modifiers_opt INTERFACE IDENTIFIER extends_interfaces_opt
829 //extends_interfaces_opt ::=
830 // | extends_interfaces
832 //extends_interfaces ::=
833 // EXTENDS interface_type
834 // | extends_interfaces COMMA interface_type
837 // LBRACE interface_member_declarations_opt RBRACE
839 //interface_member_declarations_opt ::=
840 // | interface_member_declarations
842 //interface_member_declarations ::=
843 // interface_member_declaration
844 // | interface_member_declarations interface_member_declaration
846 //interface_member_declaration ::=
847 // constant_declaration
848 // | abstract_method_declaration
849 // | class_declaration
850 // | interface_declaration
853 //constant_declaration ::=
855 // // need to semantically check that modifiers of field declaration
856 // // include only PUBLIC, STATIC, or FINAL. Other modifiers are
859 //abstract_method_declaration ::=
860 // method_header SEMICOLON
865 //array_initializer ::=
866 // LBRACE variable_initializers COMMA RBRACE
867 // | LBRACE variable_initializers RBRACE
868 // | LBRACE COMMA RBRACE
871 //variable_initializers ::=
872 // variable_initializer
873 // | variable_initializers COMMA variable_initializer
876 // 19.11) Blocks and Statements
877 block ::= LBRACE block_statements_opt:bso RBRACE {:
881 block_statements_opt ::=
882 {: RESULT=new ParseNode("empty"); :}
883 | block_statements:bs {:
888 block_statement:bs {:
889 ParseNode pn=new ParseNode("block_statement_list");
893 | block_statements:bss block_statement:bs {:
899 local_variable_declaration_statement:lvds {:
902 | statement:statement {:
905 // | class_declaration
906 // | interface_declaration
908 local_variable_declaration_statement ::=
909 local_variable_declaration:lvd SEMICOLON {:
913 local_variable_declaration ::=
914 type:type variable_declarators:var {:
915 ParseNode pn=new ParseNode("local_variable_declaration");
920 // | FINAL type variable_declarators
922 statement ::= statement_without_trailing_substatement:st {:
925 // | labeled_statement:st {: RESULT=st; :}
926 | if_then_statement:st {: RESULT=st; :}
927 | if_then_else_statement:st {: RESULT=st; :}
928 | while_statement:st {: RESULT=st; :}
929 | for_statement:st {: RESULT=st; :}
931 statement_no_short_if ::=
932 statement_without_trailing_substatement:st {: RESULT=st; :}
933 // | labeled_statement_no_short_if:st {: RESULT=st; :}
934 | if_then_else_statement_no_short_if:st {: RESULT=st; :}
935 | while_statement_no_short_if:st {: RESULT=st; :}
936 | for_statement_no_short_if:st {: RESULT=st; :}
938 statement_without_trailing_substatement ::=
939 block:st {: RESULT=st; :}
940 | empty_statement:st {: RESULT=st; :}
941 | expression_statement:st {: RESULT=st; :}
942 // | switch_statement
944 | break_statement:st {: RESULT=st; :}
945 | continue_statement:st {: RESULT=st; :}
946 | return_statement:st {: RESULT=st; :}
947 | task_exitstatement:st {: RESULT=st; :}
948 // | synchronized_statement
951 // | assert_statement
954 SEMICOLON {: RESULT=new ParseNode("nop"); :}
956 //labeled_statement ::=
957 // IDENTIFIER COLON statement
959 //labeled_statement_no_short_if ::=
960 // IDENTIFIER COLON statement_no_short_if
962 expression_statement ::=
963 statement_expression:se SEMICOLON {:
964 ParseNode pn=new ParseNode("expression");
968 statement_expression ::=
969 assignment:st {: RESULT=st; :}
970 | preincrement_expression:st {: RESULT=st; :}
971 | predecrement_expression:st {: RESULT=st; :}
972 | postincrement_expression:st {: RESULT=st; :}
973 | postdecrement_expression:st {: RESULT=st; :}
974 | method_invocation:st {: RESULT=st; :}
975 | class_instance_creation_expression:st {: RESULT=st; :}
977 if_then_statement ::=
978 IF LPAREN expression:exp RPAREN statement:st {:
979 ParseNode pn=new ParseNode("ifstatement");
980 pn.addChild("condition").addChild(exp);
981 pn.addChild("statement").addChild(st);
985 if_then_else_statement ::=
986 IF LPAREN expression:exp RPAREN statement_no_short_if:st
987 ELSE statement:else_st {:
988 ParseNode pn=new ParseNode("ifstatement");
989 pn.addChild("condition").addChild(exp);
990 pn.addChild("statement").addChild(st);
991 pn.addChild("else_statement").addChild(else_st);
995 if_then_else_statement_no_short_if ::=
996 IF LPAREN expression:exp RPAREN statement_no_short_if:st
997 ELSE statement_no_short_if:else_st {:
998 ParseNode pn=new ParseNode("ifstatement");
999 pn.addChild("condition").addChild(exp);
1000 pn.addChild("statement").addChild(st);
1001 pn.addChild("else_statement").addChild(else_st);
1005 //switch_statement ::=
1006 // SWITCH LPAREN expression RPAREN switch_block
1009 // LBRACE switch_block_statement_groups switch_labels RBRACE
1010 // | LBRACE switch_block_statement_groups RBRACE
1011 // | LBRACE switch_labels RBRACE
1014 //switch_block_statement_groups ::=
1015 // switch_block_statement_group
1016 // | switch_block_statement_groups switch_block_statement_group
1018 //switch_block_statement_group ::=
1019 // switch_labels block_statements
1023 // | switch_labels switch_label
1026 // CASE constant_expression COLON
1031 WHILE LPAREN expression:exp RPAREN statement:st {:
1032 ParseNode pn=new ParseNode("whilestatement");
1033 pn.addChild("condition").addChild(exp);
1034 pn.addChild("statement").addChild(st);
1038 while_statement_no_short_if ::=
1039 WHILE LPAREN expression:exp RPAREN statement_no_short_if:st {:
1040 ParseNode pn=new ParseNode("whilestatement");
1041 pn.addChild("condition").addChild(exp);
1042 pn.addChild("statement").addChild(st);
1047 DO statement:st WHILE LPAREN expression:exp RPAREN SEMICOLON {:
1048 ParseNode pn=new ParseNode("dowhilestatement");
1049 pn.addChild("condition").addChild(exp);
1050 pn.addChild("statement").addChild(st);
1055 FOR LPAREN for_init_opt:init SEMICOLON expression_opt:exp SEMICOLON
1056 for_update_opt:update RPAREN statement:st {:
1057 ParseNode pn=new ParseNode("forstatement");
1058 pn.addChild("initializer").addChild(init);
1059 pn.addChild("condition").addChild(exp);
1060 pn.addChild("update").addChild(update);
1061 pn.addChild("statement").addChild(st);
1065 for_statement_no_short_if ::=
1066 FOR LPAREN for_init_opt:init SEMICOLON expression_opt:exp SEMICOLON
1067 for_update_opt:update RPAREN statement_no_short_if:st {:
1068 ParseNode pn=new ParseNode("forstatement");
1069 pn.addChild("initializer").addChild(init);
1070 pn.addChild("condition").addChild(exp);
1071 pn.addChild("update").addChild(update);
1072 pn.addChild("statement").addChild(st);
1077 {: RESULT=new ParseNode("empty"); :}
1078 | for_init:init {: RESULT=init; :}
1080 for_init ::= statement_expression_list:list {: RESULT=list; :}
1081 | local_variable_declaration:decl {: RESULT=decl; :}
1084 {: RESULT=new ParseNode("empty"); :}
1085 | for_update:update {: RESULT=update; :}
1087 for_update ::= statement_expression_list:list {: RESULT=list; :}
1089 statement_expression_list ::=
1090 statement_expression:expr {:
1091 RESULT=(new ParseNode("statement_expression_list")).addChild(expr).getRoot();
1093 | statement_expression_list:list COMMA statement_expression:expr {:
1094 list.addChild(expr);
1099 //identifier_opt ::=
1106 SEMICOLON {: RESULT=new ParseNode("break"); :}
1109 continue_statement ::=
1113 {: RESULT=new ParseNode("continue"); :}
1115 return_statement ::=
1116 RETURN expression_opt:exp SEMICOLON {:
1117 RESULT=(new ParseNode("return")).addChild(exp).getRoot(); :}
1119 //throw_statement ::=
1120 // THROW expression SEMICOLON
1122 //synchronized_statement ::=
1123 // SYNCHRONIZED LPAREN expression RPAREN block
1126 // TRY block catches
1127 // | TRY block catches_opt finally
1132 //catches ::= catch_clause
1133 // | catches catch_clause
1136 // CATCH LPAREN formal_parameter RPAREN block
1138 //finally ::= FINALLY block
1140 //assert_statement ::=
1141 // ASSERT expression SEMICOLON
1142 // | ASSERT expression COLON expression SEMICOLON
1145 // 19.12) Expressions
1146 primary ::= primary_no_new_array:st {:
1148 // | array_creation_init:st {:
1151 | array_creation_uninit:st {:
1155 primary_no_new_array ::=
1156 literal:lit {: RESULT=lit; :}
1157 | THIS {: RESULT=new ParseNode("this"); :}
1158 | LPAREN expression:exp RPAREN {: RESULT=exp; :}
1159 | class_instance_creation_expression:exp {: RESULT=exp; :}
1160 | field_access:exp {: RESULT=exp; :}
1161 | method_invocation:exp {: RESULT=exp; :}
1162 | array_access:exp {: RESULT=exp; :}
1163 // | primitive_type DOT CLASS
1165 // | array_type DOT CLASS
1169 class_instance_creation_expression ::=
1170 NEW class_or_interface_type:type LPAREN argument_list_opt:args RPAREN {:
1171 ParseNode pn=new ParseNode("createobject");
1176 // | NEW class_or_interface_type LPAREN argument_list_opt RPAREN class_body
1177 // | primary DOT NEW IDENTIFIER
1178 // LPAREN argument_list_opt RPAREN {:
1181 // | primary DOT NEW IDENTIFIER
1182 // LPAREN argument_list_opt RPAREN class_body
1183 // | name DOT NEW IDENTIFIER
1184 // LPAREN argument_list_opt RPAREN
1185 // | name DOT NEW IDENTIFIER
1186 // LPAREN argument_list_opt RPAREN class_body
1188 argument_list_opt ::=
1189 {: RESULT=new ParseNode("empty"); :}
1190 | argument_list:args {: RESULT=args; :}
1194 ParseNode pn=new ParseNode("argument_list");
1198 | argument_list:list COMMA expression:exp {:
1203 array_creation_uninit ::=
1204 NEW primitive_type:type dim_exprs:dimexpr dims_opt:dims {:
1205 ParseNode pn=new ParseNode("createarray");
1207 pn.addChild(dimexpr);
1208 pn.addChild("dims_opt").setLiteral(dims);
1211 | NEW class_or_interface_type:type dim_exprs:dimexpr dims_opt:dims {:
1212 ParseNode pn=new ParseNode("createarray");
1214 pn.addChild(dimexpr);
1215 pn.addChild("dims_opt").setLiteral(dims);
1219 //array_creation_init ::=
1220 // NEW primitive_type dims array_initializer
1221 // | NEW class_or_interface_type dims array_initializer
1223 dim_exprs ::= dim_expr:exp {:
1224 ParseNode pn=new ParseNode("dim_exprs");
1227 | dim_exprs:base dim_expr:exp {:
1232 dim_expr ::= LBRACK expression:exp RBRACK {: RESULT=exp; :}
1234 dims_opt ::= {: RESULT=null; :}
1235 | dims:dims {: RESULT = dims; :}
1238 dims ::= LBRACK RBRACK {: RESULT=new Integer(0); :}
1239 | dims:dims LBRACK RBRACK {: RESULT=new Integer(dims.intValue()+1); :}
1243 primary:base DOT IDENTIFIER:id {:
1244 ParseNode pn=new ParseNode("fieldaccess");
1245 pn.addChild("base").addChild(base);
1246 pn.addChild("field").addChild(id);
1249 // | SUPER DOT IDENTIFIER
1250 // | name DOT SUPER DOT IDENTIFIER
1252 method_invocation ::=
1253 name:name LPAREN argument_list_opt:args RPAREN {:
1254 ParseNode pn=new ParseNode("methodinvoke1");
1259 | primary:base DOT IDENTIFIER:name LPAREN argument_list_opt:args RPAREN {:
1260 ParseNode pn=new ParseNode("methodinvoke2");
1261 pn.addChild("base").addChild(base);
1262 pn.addChild("id").addChild(name);
1266 // | SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
1267 // | name DOT SUPER DOT IDENTIFIER LPAREN argument_list_opt RPAREN
1270 name:name LBRACK expression:exp RBRACK {:
1271 ParseNode pn=new ParseNode("arrayaccess");
1272 pn.addChild("base").addChild(name);
1273 pn.addChild("index").addChild(exp);
1276 | primary_no_new_array:base LBRACK expression:exp RBRACK {:
1277 ParseNode pn=new ParseNode("arrayaccess");
1278 pn.addChild("base").addChild(base);
1279 pn.addChild("index").addChild(exp);
1282 // | array_creation_init:init LBRACK expression:exp RBRACK {:
1283 // ParseNode pn=new ParseNode("arrayaccess");
1284 // pn.addChild("init").addChild(init);
1285 // pn.addChild("index").addChild(exp);
1289 postfix_expression ::=
1292 | name:exp {: RESULT=exp; :}
1293 | postincrement_expression:exp {: RESULT=exp; :}
1294 | postdecrement_expression:exp {: RESULT=exp; :}
1296 postincrement_expression ::=
1297 postfix_expression:exp PLUSPLUS
1298 {: RESULT=(new ParseNode("postinc")).addChild(exp).getRoot(); :}
1300 postdecrement_expression ::=
1301 postfix_expression:exp MINUSMINUS
1302 {: RESULT=(new ParseNode("postdec")).addChild(exp).getRoot(); :}
1304 unary_expression ::=
1305 preincrement_expression:exp {: RESULT=exp; :}
1306 | predecrement_expression:exp {: RESULT=exp; :}
1307 | PLUS unary_expression:exp
1308 {: RESULT=(new ParseNode("unaryplus")).addChild(exp).getRoot(); :}
1309 | MINUS unary_expression:exp
1310 {: RESULT=(new ParseNode("unaryminus")).addChild(exp).getRoot(); :}
1311 | unary_expression_not_plus_minus:exp {:
1314 preincrement_expression ::=
1315 PLUSPLUS unary_expression:exp
1316 {: RESULT=(new ParseNode("preinc")).addChild(exp).getRoot(); :}
1318 predecrement_expression ::=
1319 MINUSMINUS unary_expression:exp
1320 {: RESULT=(new ParseNode("predec")).addChild(exp).getRoot(); :}
1322 unary_expression_not_plus_minus ::=
1323 postfix_expression:exp {:
1325 // | COMP unary_expression
1326 | NOT unary_expression:exp
1327 {: RESULT=(new ParseNode("not")).addChild(exp).getRoot(); :}
1328 | cast_expression:exp {: RESULT=exp; :}
1331 LPAREN primitive_type:type
1333 RPAREN unary_expression:exp {:
1334 ParseNode pn=new ParseNode("cast1");
1335 pn.addChild("type").addChild(type);
1336 pn.addChild("exp").addChild(exp);
1339 | LPAREN expression:type RPAREN unary_expression_not_plus_minus:exp {:
1340 ParseNode pn=new ParseNode("cast2");
1341 pn.addChild("type").addChild(type);
1342 pn.addChild("exp").addChild(exp);
1346 // | LPAREN name dims RPAREN unary_expression_not_plus_minus
1348 multiplicative_expression ::=
1349 unary_expression:exp {:
1351 | multiplicative_expression:exp1 MULT unary_expression:exp2 {:
1352 ParseNode pn=new ParseNode("mult");
1357 | multiplicative_expression:exp1 DIV unary_expression:exp2 {:
1358 ParseNode pn=new ParseNode("div");
1363 | multiplicative_expression:exp1 MOD unary_expression:exp2 {:
1364 ParseNode pn=new ParseNode("mod");
1370 additive_expression ::=
1371 multiplicative_expression:exp {:
1373 | additive_expression:exp1 PLUS multiplicative_expression:exp2 {:
1374 ParseNode pn=new ParseNode("add");
1379 | additive_expression:exp1 MINUS multiplicative_expression:exp2 {:
1380 ParseNode pn=new ParseNode("sub");
1386 shift_expression ::=
1387 additive_expression:exp {:
1389 | shift_expression:exp1 LSHIFT additive_expression:exp2 {:
1390 ParseNode pn=new ParseNode("leftshift");
1395 | shift_expression:exp1 RSHIFT additive_expression:exp2 {:
1396 ParseNode pn=new ParseNode("rightshift");
1401 // | shift_expression URSHIFT additive_expression
1403 relational_expression ::=
1404 shift_expression:exp {:
1406 | relational_expression:exp1 LT shift_expression:exp2 {:
1407 ParseNode pn=new ParseNode("comp_lt");
1412 | relational_expression:exp1 GT shift_expression:exp2 {:
1413 ParseNode pn=new ParseNode("comp_gt");
1418 | relational_expression:exp1 LTEQ shift_expression:exp2 {:
1419 ParseNode pn=new ParseNode("comp_lte");
1424 | relational_expression:exp1 GTEQ shift_expression:exp2 {:
1425 ParseNode pn=new ParseNode("comp_gte");
1430 // | relational_expression INSTANCEOF reference_type
1433 equality_expression ::=
1434 relational_expression:exp {:
1436 | equality_expression:exp1 EQEQ relational_expression:exp2 {:
1437 ParseNode pn=new ParseNode("equal");
1442 | equality_expression:exp1 NOTEQ relational_expression:exp2 {:
1443 ParseNode pn=new ParseNode("not_equal");
1450 equality_expression:exp {:
1452 | and_expression:exp1 AND equality_expression:exp2 {:
1453 ParseNode pn=new ParseNode("bitwise_and");
1459 exclusive_or_expression ::=
1460 and_expression:expr {:
1463 | exclusive_or_expression:exp1 XOR and_expression:exp2 {:
1464 ParseNode pn=new ParseNode("bitwise_xor");
1470 inclusive_or_expression ::=
1471 exclusive_or_expression:exclor {:
1473 | inclusive_or_expression:exp1 OR exclusive_or_expression:exp2 {:
1474 ParseNode pn=new ParseNode("bitwise_or");
1480 conditional_and_expression ::=
1481 inclusive_or_expression:inclor {:
1483 | conditional_and_expression:exp1 ANDAND inclusive_or_expression:exp2 {:
1484 ParseNode pn=new ParseNode("logical_and");
1490 conditional_or_expression ::=
1491 conditional_and_expression:condand {:
1493 | conditional_or_expression:exp1 OROR conditional_and_expression:exp2 {:
1494 ParseNode pn=new ParseNode("logical_or");
1500 conditional_expression ::=
1501 conditional_or_expression:condor {:
1503 // | conditional_or_expression QUESTION expression
1504 // COLON conditional_expression
1506 assignment_expression ::=
1507 conditional_expression:expr {:
1509 assignment:assign {:
1512 // semantic check necessary here to ensure a valid left-hand side.
1513 // allowing a parenthesized variable here on the lhs was introduced in
1514 // JLS 2; thanks to Eric Blake for pointing this out.
1515 assignment ::= postfix_expression:lvalue assignment_operator:op assignment_expression:rvalue {:
1516 ParseNode pn=new ParseNode("assignment");
1517 pn.addChild("op").addChild(op);
1518 ParseNode pnargs=pn.addChild("args");
1519 pnargs.addChild(lvalue);
1520 pnargs.addChild(rvalue);
1524 assignment_operator ::=
1525 EQ {: RESULT=new ParseNode("eq"); :}
1526 | MULTEQ {: RESULT=new ParseNode("multeq"); :}
1527 | DIVEQ {: RESULT=new ParseNode("diveq"); :}
1528 | MODEQ {: RESULT=new ParseNode("modeq"); :}
1529 | PLUSEQ {: RESULT=new ParseNode("pluseq"); :}
1530 | MINUSEQ {: RESULT=new ParseNode("minuseq"); :}
1531 | LSHIFTEQ {: RESULT=new ParseNode("lshifteq"); :}
1532 | RSHIFTEQ {: RESULT=new ParseNode("rshifteq"); :}
1533 // | URSHIFTEQ {: RESULT=new ParseNode("urshifteq"); :}
1534 | ANDEQ {: RESULT=new ParseNode("andeq"); :}
1535 | XOREQ {: RESULT=new ParseNode("xoreq"); :}
1536 | OREQ {: RESULT=new ParseNode("oreq"); :}
1539 {: RESULT=new ParseNode("empty"); :}
1543 expression ::= assignment_expression:exp {:
1546 //constant_expression ::=