1 /* Parser grammar for quick source code scan of Java(TM) language programs.
2 Copyright (C) 1998, 1999 Free Software Foundation, Inc.
3 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* This file parses Java source code. Action can be further completed
27 to achieve a desired behavior. This file isn't part of the Java
28 language gcc front end.
30 The grammar conforms to the Java grammar described in "The Java(TM)
31 Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
32 1996, ISBN 0-201-63451-1"
34 Some rules have been modified to support JDK1.1 inner classes
35 definitions and other extensions. */
46 extern char *input_filename;
47 extern FILE *finput, *out;
49 /* Obstack for the lexer. */
50 struct obstack temporary_obstack;
52 /* The current parser context. */
53 static struct parser_ctxt *ctxp;
55 /* Error and warning counts, current line number, because they're used
58 int java_warning_count;
61 /* Tweak default rules when necessary. */
63 #define USE_ABSORBER absorber = 0
65 /* Keep track of the current class name and package name. */
66 static const char *current_class;
67 static const char *package_name;
69 /* Keep track of whether things have be listed before. */
70 static int previous_output;
72 /* Record modifier uses */
73 static int modifier_value;
75 /* Keep track of number of bracket pairs after a variable declarator
77 static int bracket_count;
79 /* Record a method declaration */
80 struct method_declarator {
81 const char *method_name;
84 #define NEW_METHOD_DECLARATOR(D,N,A) \
87 (struct method_declarator *)xmalloc (sizeof (struct method_declarator)); \
88 (D)->method_name = (N); \
92 /* Two actions for this grammar */
93 static void report_class_declaration PROTO ((const char *));
94 static void report_main_declaration PROTO ((struct method_declarator *));
102 struct method_declarator *declarator;
103 int value; /* For modifiers */
112 /* Things defined here have to match the order of what's in the
113 binop_lookup table. */
115 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
116 %token LS_TK SRS_TK ZRS_TK
117 %token AND_TK XOR_TK OR_TK
118 %token BOOL_AND_TK BOOL_OR_TK
119 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
121 /* This maps to the same binop_lookup entry than the token above */
123 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
125 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
126 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
129 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
131 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
132 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
133 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
134 %token PAD_TK ABSTRACT_TK MODIFIER_TK
136 /* Keep those two in order, too */
137 %token DECR_TK INCR_TK
139 /* From now one, things can be in any order */
141 %token DEFAULT_TK IF_TK THROW_TK
142 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
143 %token THROWS_TK BREAK_TK IMPORT_TK
144 %token ELSE_TK INSTANCEOF_TK RETURN_TK
145 %token VOID_TK CATCH_TK INTERFACE_TK
146 %token CASE_TK EXTENDS_TK FINALLY_TK
147 %token SUPER_TK WHILE_TK CLASS_TK
148 %token SWITCH_TK CONST_TK TRY_TK
149 %token FOR_TK NEW_TK CONTINUE_TK
150 %token GOTO_TK PACKAGE_TK THIS_TK
152 %token BYTE_TK SHORT_TK INT_TK LONG_TK
153 %token CHAR_TK INTEGRAL_TK
155 %token FLOAT_TK DOUBLE_TK FP_TK
159 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
161 %token ASSIGN_ANY_TK ASSIGN_TK
162 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
164 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
165 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
167 %type <node> ID_TK identifier name simple_name qualified_name type
168 primitive_type reference_type array_type formal_parameter_list
169 formal_parameter class_or_interface_type class_type interface_type
170 %type <declarator> method_declarator
171 %type <value> MODIFIER_TK
174 /* 19.2 Production from 2.3: The Syntactic Grammar */
179 /* 19.3 Productions from 3: Lexical structure */
189 /* 19.4 Productions from 4: Types, Values and Variables */
198 /* use preset global here. FIXME */
199 $$ = xstrdup ("int");
203 /* use preset global here. FIXME */
204 $$ = xstrdup ("double");
208 /* use preset global here. FIXME */
209 $$ = xstrdup ("boolean");
214 class_or_interface_type
218 class_or_interface_type:
223 class_or_interface_type /* Default rule */
227 class_or_interface_type
231 primitive_type OSB_TK CSB_TK
234 $$ = concat ("[", $1, NULL);
236 | array_type OSB_TK CSB_TK
238 $$ = concat ("[", $1, NULL);
242 /* 19.5 Productions from 6: Names */
244 simple_name /* Default rule */
245 | qualified_name /* Default rule */
249 identifier /* Default rule */
253 name DOT_TK identifier
255 $$ = concat ($1, ".", $3, NULL);
263 /* 19.6: Production from 7: Packages */
265 | package_declaration
266 | import_declarations
268 | package_declaration import_declarations
269 | package_declaration type_declarations
270 | import_declarations type_declarations
271 | package_declaration import_declarations type_declarations
276 | import_declarations import_declaration
281 | type_declarations type_declaration
285 PACKAGE_TK name SC_TK
286 { package_name = $2; }
290 single_type_import_declaration
291 | type_import_on_demand_declaration
294 single_type_import_declaration:
298 type_import_on_demand_declaration:
299 IMPORT_TK name DOT_TK MULT_TK SC_TK
304 | interface_declaration
308 /* 19.7 Shortened from the original:
309 modifiers: modifier | modifiers modifier
310 modifier: any of public... */
320 | modifiers MODIFIER_TK
330 /* 19.8.1 Production from $8.1: Class Declaration */
332 modifiers CLASS_TK identifier super interfaces
334 report_class_declaration($3);
338 | CLASS_TK identifier super interfaces
339 { report_class_declaration($2); }
344 | EXTENDS_TK class_type
348 | IMPLEMENTS_TK interface_type_list
354 | interface_type_list C_TK interface_type
360 | OCB_TK class_body_declarations CCB_TK
363 class_body_declarations:
364 class_body_declaration
365 | class_body_declarations class_body_declaration
368 class_body_declaration:
369 class_member_declaration
371 | constructor_declaration
372 | block /* Added, JDK1.1, instance initializer */
375 class_member_declaration:
378 | class_declaration /* Added, JDK1.1 inner classes */
379 | interface_declaration /* Added, JDK1.1 inner classes */
382 /* 19.8.2 Productions from 8.3: Field Declarations */
384 type variable_declarators SC_TK
386 | modifiers type variable_declarators SC_TK
387 { modifier_value = 0; }
390 variable_declarators:
391 /* Should we use build_decl_list () instead ? FIXME */
392 variable_declarator /* Default rule */
393 | variable_declarators C_TK variable_declarator
397 variable_declarator_id
398 | variable_declarator_id ASSIGN_TK variable_initializer
401 variable_declarator_id:
403 { bracket_count = 0; USE_ABSORBER; }
404 | variable_declarator_id OSB_TK CSB_TK
408 variable_initializer:
413 /* 19.8.3 Productions from 8.4: Method Declarations */
415 method_header method_body
419 type method_declarator throws
421 | VOID_TK method_declarator throws
422 | modifiers type method_declarator throws
423 { modifier_value = 0; }
424 | modifiers VOID_TK method_declarator throws
426 report_main_declaration ($3);
432 identifier OP_TK CP_TK
434 struct method_declarator *d;
435 NEW_METHOD_DECLARATOR (d, $1, NULL);
438 | identifier OP_TK formal_parameter_list CP_TK
440 struct method_declarator *d;
441 NEW_METHOD_DECLARATOR (d, $1, $3);
444 | method_declarator OSB_TK CSB_TK
447 formal_parameter_list:
449 | formal_parameter_list C_TK formal_parameter
451 $$ = concat ($1, ",", $3, NULL);
456 type variable_declarator_id
462 char *n = xmalloc (bracket_count + 1 + strlen ($$));
463 for (i = 0; i < bracket_count; ++i)
465 strcpy (n + bracket_count, $$);
471 | modifiers type variable_declarator_id /* Added, JDK1.1 final locals */
476 char *n = xmalloc (bracket_count + 1 + strlen ($$));
477 for (i = 0; i < bracket_count; ++i)
479 strcpy (n + bracket_count, $$);
488 | THROWS_TK class_type_list
494 | class_type_list C_TK class_type
504 /* 19.8.4 Productions from 8.5: Static Initializers */
507 | static block SC_TK /* Shouldn't be here. FIXME */
510 static: /* Test lval.sub_token here */
515 /* 19.8.5 Productions from 8.6: Constructor Declarations */
516 /* NOTE FOR FURTHER WORK ON CONSTRUCTORS:
517 - If a forbidded modifier is found, the the error is either the use of
518 a forbidded modifier for a constructor OR bogus attempt to declare a
519 method without having specified the return type. FIXME */
520 constructor_declaration:
521 constructor_declarator throws constructor_body
522 | modifiers constructor_declarator throws constructor_body
523 { modifier_value = 0; }
524 /* extra SC_TK, FIXME */
525 | constructor_declarator throws constructor_body SC_TK
526 /* extra SC_TK, FIXME */
527 | modifiers constructor_declarator throws constructor_body SC_TK
528 { modifier_value = 0; }
529 /* I'm not happy with the SC_TK addition. It isn't in the grammer and should
530 probably be matched by and empty statement. But it doesn't work. FIXME */
533 constructor_declarator:
534 simple_name OP_TK CP_TK
536 | simple_name OP_TK formal_parameter_list CP_TK
542 | OCB_TK explicit_constructor_invocation CCB_TK
543 | OCB_TK block_statements CCB_TK
544 | OCB_TK explicit_constructor_invocation block_statements CCB_TK
547 /* Error recovery for that rule moved down expression_statement: rule. */
548 explicit_constructor_invocation:
549 this_or_super OP_TK CP_TK SC_TK
550 | this_or_super OP_TK argument_list CP_TK SC_TK
551 /* Added, JDK1.1 inner classes. Modified because the rule
552 'primary' couldn't work. */
553 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
555 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
559 this_or_super: /* Added, simplifies error diagnostics */
564 /* 19.9 Productions from 9: Interfaces */
565 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
566 interface_declaration:
567 INTERFACE_TK identifier interface_body
568 { report_class_declaration ($2); modifier_value = 0; }
569 | modifiers INTERFACE_TK identifier interface_body
570 { report_class_declaration ($3); modifier_value = 0; }
571 | INTERFACE_TK identifier extends_interfaces interface_body
572 { report_class_declaration ($2); modifier_value = 0; }
573 | modifiers INTERFACE_TK identifier extends_interfaces interface_body
574 { report_class_declaration ($3); modifier_value = 0; }
578 EXTENDS_TK interface_type
579 | extends_interfaces C_TK interface_type
584 | OCB_TK interface_member_declarations CCB_TK
587 interface_member_declarations:
588 interface_member_declaration
589 | interface_member_declarations interface_member_declaration
592 interface_member_declaration:
594 | abstract_method_declaration
595 | class_declaration /* Added, JDK1.1 inner classes */
596 | interface_declaration /* Added, JDK1.1 inner classes */
599 constant_declaration:
603 abstract_method_declaration:
607 /* 19.10 Productions from 10: Arrays */
610 | OCB_TK variable_initializers CCB_TK
612 | OCB_TK variable_initializers C_TK CCB_TK
615 variable_initializers:
617 | variable_initializers C_TK variable_initializer
620 /* 19.11 Production from 14: Blocks and Statements */
623 | OCB_TK block_statements CCB_TK
628 | block_statements block_statement
632 local_variable_declaration_statement
634 | class_declaration /* Added, JDK1.1 inner classes */
637 local_variable_declaration_statement:
638 local_variable_declaration SC_TK /* Can't catch missing ';' here */
641 local_variable_declaration:
642 type variable_declarators
644 | modifiers type variable_declarators /* Added, JDK1.1 final locals */
645 { modifier_value = 0; }
649 statement_without_trailing_substatement
652 | if_then_else_statement
658 statement_without_trailing_substatement
659 | labeled_statement_nsi
660 | if_then_else_statement_nsi
661 | while_statement_nsi
665 statement_without_trailing_substatement:
668 | expression_statement
674 | synchronized_statement
692 labeled_statement_nsi:
693 label_decl statement_nsi
696 /* We concentrate here a bunch of error handling rules that we couldn't write
697 earlier, because expression_statement catches a missing ';'. */
698 expression_statement:
699 statement_expression SC_TK
702 statement_expression:
704 | pre_increment_expression
705 | pre_decrement_expression
706 | post_increment_expression
707 | post_decrement_expression
709 | class_instance_creation_expression
713 IF_TK OP_TK expression CP_TK statement
716 if_then_else_statement:
717 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
720 if_then_else_statement_nsi:
721 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
725 SWITCH_TK OP_TK expression CP_TK switch_block
730 | OCB_TK switch_labels CCB_TK
731 | OCB_TK switch_block_statement_groups CCB_TK
732 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
735 switch_block_statement_groups:
736 switch_block_statement_group
737 | switch_block_statement_groups switch_block_statement_group
740 switch_block_statement_group:
741 switch_labels block_statements
747 | switch_labels switch_label
751 CASE_TK constant_expression REL_CL_TK
752 | DEFAULT_TK REL_CL_TK
756 WHILE_TK OP_TK expression CP_TK
760 while_expression statement
764 while_expression statement_nsi
772 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
776 for_begin SC_TK expression SC_TK for_update CP_TK statement
777 | for_begin SC_TK SC_TK for_update CP_TK statement
781 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
782 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
792 for_init: /* Can be empty */
793 | statement_expression_list
794 | local_variable_declaration
797 for_update: /* Can be empty */
798 | statement_expression_list
801 statement_expression_list:
803 | statement_expression_list C_TK statement_expression
808 | BREAK_TK identifier SC_TK
813 | CONTINUE_TK identifier SC_TK
818 | RETURN_TK expression SC_TK
822 THROW_TK expression SC_TK
825 synchronized_statement:
826 synchronized OP_TK expression CP_TK block
827 | synchronized OP_TK expression CP_TK error
830 synchronized: /* Test lval.sub_token here */
837 | TRY_TK block finally
838 | TRY_TK block catches finally
843 | catches catch_clause
847 CATCH_TK OP_TK formal_parameter CP_TK block
854 /* 19.12 Production from 15: Expressions */
857 | array_creation_expression
860 primary_no_new_array:
863 | OP_TK expression CP_TK
864 | class_instance_creation_expression
868 /* type DOT_TK CLASS_TK doens't work. So we split the rule
869 'type' into its components. Missing is something for array,
870 which will complete the reference_type part. FIXME */
871 | name DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
873 | primitive_type DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
875 | VOID_TK DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
876 /* Added, JDK1.1 inner classes. Documentation is wrong
877 refering to a 'ClassName' (class_name) rule that doesn't
878 exist. Used name instead. */
879 | name DOT_TK THIS_TK
883 class_instance_creation_expression:
884 NEW_TK class_type OP_TK argument_list CP_TK
885 | NEW_TK class_type OP_TK CP_TK
886 /* Added, JDK1.1 inner classes but modified to use
887 'class_type' instead of 'TypeName' (type_name) mentionned
888 in the documentation but doesn't exist. */
889 | NEW_TK class_type OP_TK argument_list CP_TK class_body
890 | NEW_TK class_type OP_TK CP_TK class_body
891 /* Added, JDK1.1 inner classes, modified to use name or
892 primary instead of primary solely which couldn't work in
894 | something_dot_new identifier OP_TK CP_TK
895 | something_dot_new identifier OP_TK CP_TK class_body
896 | something_dot_new identifier OP_TK argument_list CP_TK
897 | something_dot_new identifier OP_TK argument_list CP_TK class_body
900 something_dot_new: /* Added, not part of the specs. */
903 | primary DOT_TK NEW_TK
908 | argument_list C_TK expression
909 | argument_list C_TK error
912 array_creation_expression:
913 NEW_TK primitive_type dim_exprs
914 | NEW_TK class_or_interface_type dim_exprs
915 | NEW_TK primitive_type dim_exprs dims
916 | NEW_TK class_or_interface_type dim_exprs dims
917 /* Added, JDK1.1 anonymous array. Initial documentation rule
919 | NEW_TK class_or_interface_type dims array_initializer
920 | NEW_TK primitive_type dims array_initializer
929 OSB_TK expression CSB_TK
938 primary DOT_TK identifier
939 | SUPER_TK DOT_TK identifier
945 | name OP_TK argument_list CP_TK
947 | primary DOT_TK identifier OP_TK CP_TK
948 | primary DOT_TK identifier OP_TK argument_list CP_TK
949 | SUPER_TK DOT_TK identifier OP_TK CP_TK
950 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
954 name OSB_TK expression CSB_TK
956 | primary_no_new_array OSB_TK expression CSB_TK
963 | post_increment_expression
964 | post_decrement_expression
967 post_increment_expression:
968 postfix_expression INCR_TK
971 post_decrement_expression:
972 postfix_expression DECR_TK
976 pre_increment_expression
977 | pre_decrement_expression
978 | PLUS_TK unary_expression
979 | MINUS_TK unary_expression
980 | unary_expression_not_plus_minus
983 pre_increment_expression:
984 INCR_TK unary_expression
987 pre_decrement_expression:
988 DECR_TK unary_expression
991 unary_expression_not_plus_minus:
993 | NOT_TK unary_expression
994 | NEG_TK unary_expression
998 cast_expression: /* Error handling here is potentially weak */
999 OP_TK primitive_type dims CP_TK unary_expression
1000 | OP_TK primitive_type CP_TK unary_expression
1001 | OP_TK expression CP_TK unary_expression_not_plus_minus
1002 | OP_TK name dims CP_TK unary_expression_not_plus_minus
1005 multiplicative_expression:
1007 | multiplicative_expression MULT_TK unary_expression
1008 | multiplicative_expression DIV_TK unary_expression
1009 | multiplicative_expression REM_TK unary_expression
1012 additive_expression:
1013 multiplicative_expression
1014 | additive_expression PLUS_TK multiplicative_expression
1015 | additive_expression MINUS_TK multiplicative_expression
1020 | shift_expression LS_TK additive_expression
1021 | shift_expression SRS_TK additive_expression
1022 | shift_expression ZRS_TK additive_expression
1025 relational_expression:
1027 | relational_expression LT_TK shift_expression
1028 | relational_expression GT_TK shift_expression
1029 | relational_expression LTE_TK shift_expression
1030 | relational_expression GTE_TK shift_expression
1031 | relational_expression INSTANCEOF_TK reference_type
1034 equality_expression:
1035 relational_expression
1036 | equality_expression EQ_TK relational_expression
1037 | equality_expression NEQ_TK relational_expression
1042 | and_expression AND_TK equality_expression
1045 exclusive_or_expression:
1047 | exclusive_or_expression XOR_TK and_expression
1050 inclusive_or_expression:
1051 exclusive_or_expression
1052 | inclusive_or_expression OR_TK exclusive_or_expression
1055 conditional_and_expression:
1056 inclusive_or_expression
1057 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
1060 conditional_or_expression:
1061 conditional_and_expression
1062 | conditional_or_expression BOOL_OR_TK conditional_and_expression
1065 conditional_expression: /* Error handling here is weak */
1066 conditional_or_expression
1067 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
1070 assignment_expression:
1071 conditional_expression
1076 left_hand_side assignment_operator assignment_expression
1086 assignment_operator:
1092 assignment_expression
1095 constant_expression:
1101 /* Create a new parser context */
1104 java_push_parser_context ()
1106 struct parser_ctxt *new =
1107 (struct parser_ctxt *) xcalloc (1, sizeof (struct parser_ctxt));
1113 /* Actions defined here */
1116 report_class_declaration (name)
1119 extern int flag_dump_class, flag_list_filename;
1121 if (flag_dump_class)
1123 if (!previous_output)
1125 if (flag_list_filename)
1126 fprintf (out, "%s: ", input_filename);
1127 previous_output = 1;
1131 fprintf (out, "%s.%s ", package_name, name);
1133 fprintf (out, "%s ", name);
1136 current_class = name;
1140 report_main_declaration (declarator)
1141 struct method_declarator *declarator;
1143 extern int flag_find_main;
1146 && modifier_value == 2
1147 && !strcmp (declarator->method_name, "main")
1149 && declarator->args [0] == '['
1150 && (! strcmp (declarator->args+1, "String")
1151 || ! strcmp (declarator->args + 1, "java.lang.String"))
1154 if (!previous_output)
1157 fprintf (out, "%s.%s ", package_name, current_class);
1159 fprintf (out, current_class);
1160 previous_output = 1;
1165 /* Reset global status used by the report functions. */
1167 void reset_report ()
1169 previous_output = 0;
1170 current_class = package_name = NULL;
1175 const char *msg ATTRIBUTE_UNUSED;