OSDN Git Service

dee5dc2ddf423a3839b053c603d638cf59f5831d
[pf3gnuchains/gcc-fork.git] / gcc / java / parse-scan.y
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)
4
5 This file is part of GNU CC.
6
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)
10 any later version.
11
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.
16
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.
21
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.  */
25
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.
29
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"
33
34 Some rules have been modified to support JDK1.1 inner classes
35 definitions and other extensions.  */
36
37 %{
38 #define JC1_LITE
39
40 #include "config.h"
41 #include "system.h"
42
43 #include "obstack.h"
44 #include "toplev.h"
45
46 extern char *input_filename;
47 extern FILE *finput, *out;
48
49 /* Obstack for the lexer.  */
50 struct obstack temporary_obstack;
51
52 /* The current parser context.  */
53 static struct parser_ctxt *ctxp;
54
55 /* Error and warning counts, current line number, because they're used
56    elsewhere  */
57 int java_error_count;
58 int java_warning_count;
59 int lineno;
60
61 /* Tweak default rules when necessary.  */
62 static int absorber;
63 #define USE_ABSORBER absorber = 0
64
65 /* Keep track of the current class name and package name.  */
66 static const char *current_class;
67 static const char *package_name;
68
69 /* Keep track of whether things have be listed before.  */
70 static int previous_output;
71
72 /* Record modifier uses  */
73 static int modifier_value;
74
75 /* Keep track of number of bracket pairs after a variable declarator
76    id.  */
77 static int bracket_count; 
78
79 /* Record a method declaration  */
80 struct method_declarator {
81   const char *method_name;
82   const char *args;
83 };
84 #define NEW_METHOD_DECLARATOR(D,N,A)                                         \
85 {                                                                            \
86   (D) =                                                                      \
87     (struct method_declarator *)xmalloc (sizeof (struct method_declarator)); \
88   (D)->method_name = (N);                                                    \
89   (D)->args = (A);                                                           \
90 }
91
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 *));
95
96 #include "lex.h"
97 #include "parse.h"
98 %}
99
100 %union {
101   char *node;
102   struct method_declarator *declarator;
103   int value;                    /* For modifiers */
104 }
105
106 %{
107 #include "lex.c"
108 %}
109
110 %pure_parser
111
112 /* Things defined here have to match the order of what's in the
113    binop_lookup table.  */
114
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
120
121 /* This maps to the same binop_lookup entry than the token above */
122
123 %token   PLUS_ASSIGN_TK  MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
124 %token   REM_ASSIGN_TK   
125 %token   LS_ASSIGN_TK    SRS_ASSIGN_TK   ZRS_ASSIGN_TK
126 %token   AND_ASSIGN_TK   XOR_ASSIGN_TK   OR_ASSIGN_TK
127
128
129 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
130
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
135
136 /* Keep those two in order, too */
137 %token   DECR_TK INCR_TK
138
139 /* From now one, things can be in any order */
140
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
151
152 %token   BYTE_TK         SHORT_TK           INT_TK            LONG_TK
153 %token   CHAR_TK         INTEGRAL_TK
154
155 %token   FLOAT_TK        DOUBLE_TK          FP_TK
156
157 %token   ID_TK
158
159 %token   REL_QM_TK         REL_CL_TK NOT_TK  NEG_TK
160
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
163
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
166
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
172
173 %%
174 /* 19.2 Production from 2.3: The Syntactic Grammar  */
175 goal:
176         compilation_unit
177 ;
178
179 /* 19.3 Productions from 3: Lexical structure  */
180 literal:
181         INT_LIT_TK
182 |       FP_LIT_TK
183 |       BOOL_LIT_TK
184 |       CHAR_LIT_TK
185 |       STRING_LIT_TK
186 |       NULL_TK
187 ;
188
189 /* 19.4 Productions from 4: Types, Values and Variables  */
190 type:
191         primitive_type
192 |       reference_type
193 ;
194
195 primitive_type:
196         INTEGRAL_TK
197                 {
198                   /* use preset global here. FIXME */
199                   $$ = xstrdup ("int");
200                 }
201 |       FP_TK
202                 {
203                   /* use preset global here. FIXME */
204                   $$ = xstrdup ("double");
205                 }
206 |       BOOLEAN_TK
207                 {
208                   /* use preset global here. FIXME */
209                   $$ = xstrdup ("boolean");
210                 }
211 ;
212
213 reference_type:
214         class_or_interface_type
215 |       array_type
216 ;
217
218 class_or_interface_type:
219         name
220 ;
221
222 class_type:
223         class_or_interface_type /* Default rule */
224 ;
225
226 interface_type:
227          class_or_interface_type
228 ;
229
230 array_type:
231         primitive_type OSB_TK CSB_TK
232 |       name OSB_TK CSB_TK
233                 {
234                   $$ = concat ("[", $1, NULL);
235                 }
236 |       array_type OSB_TK CSB_TK
237                 {       
238                   $$ = concat ("[", $1, NULL);
239                 }
240 ;
241
242 /* 19.5 Productions from 6: Names  */
243 name:
244         simple_name             /* Default rule */
245 |       qualified_name          /* Default rule */
246 ;
247
248 simple_name:
249         identifier              /* Default rule */
250 ;
251
252 qualified_name:
253         name DOT_TK identifier
254                 { 
255                   $$ = concat ($1, ".", $3, NULL);
256                 }
257 ;
258
259 identifier:
260         ID_TK
261 ;
262
263 /* 19.6: Production from 7: Packages  */
264 compilation_unit:
265 |       package_declaration
266 |       import_declarations
267 |       type_declarations
268 |       package_declaration import_declarations
269 |       package_declaration type_declarations
270 |       import_declarations type_declarations
271 |       package_declaration import_declarations type_declarations
272 ;
273
274 import_declarations:
275         import_declaration
276 |       import_declarations import_declaration
277 ;
278
279 type_declarations:
280         type_declaration
281 |       type_declarations type_declaration
282 ;
283
284 package_declaration:
285         PACKAGE_TK name SC_TK
286                 { package_name = $2; }
287 ;
288
289 import_declaration:
290         single_type_import_declaration
291 |       type_import_on_demand_declaration
292 ;
293
294 single_type_import_declaration:
295         IMPORT_TK name SC_TK
296 ;
297
298 type_import_on_demand_declaration:
299         IMPORT_TK name DOT_TK MULT_TK SC_TK
300 ;
301
302 type_declaration:
303         class_declaration
304 |       interface_declaration
305 |       SC_TK
306 ;
307
308 /* 19.7 Shortened from the original:
309    modifiers: modifier | modifiers modifier
310    modifier: any of public...  */
311 modifiers:
312         MODIFIER_TK
313                 { 
314                   if ($1 == PUBLIC_TK)
315                     modifier_value++;
316                   if ($1 == STATIC_TK)
317                     modifier_value++;
318                   USE_ABSORBER;
319                 }       
320 |       modifiers MODIFIER_TK
321                 { 
322                   if ($2 == PUBLIC_TK)
323                     modifier_value++;
324                   if ($2 == STATIC_TK)
325                     modifier_value++;
326                   USE_ABSORBER;
327                 }       
328 ;
329
330 /* 19.8.1 Production from $8.1: Class Declaration */
331 class_declaration:
332         modifiers CLASS_TK identifier super interfaces 
333                 { 
334                   report_class_declaration($3);
335                   modifier_value = 0;
336                 }
337         class_body
338 |       CLASS_TK identifier super interfaces 
339                 { report_class_declaration($2); }
340         class_body
341 ;
342
343 super:
344 |       EXTENDS_TK class_type
345 ;
346
347 interfaces:
348 |       IMPLEMENTS_TK interface_type_list
349 ;
350
351 interface_type_list:
352         interface_type
353                 { USE_ABSORBER; }
354 |       interface_type_list C_TK interface_type
355                 { USE_ABSORBER; }
356 ;
357
358 class_body:
359         OCB_TK CCB_TK
360 |       OCB_TK class_body_declarations CCB_TK
361 ;
362
363 class_body_declarations:
364         class_body_declaration
365 |       class_body_declarations class_body_declaration
366 ;
367
368 class_body_declaration:
369         class_member_declaration
370 |       static_initializer
371 |       constructor_declaration
372 |       block                   /* Added, JDK1.1, instance initializer */
373 ;
374
375 class_member_declaration:
376         field_declaration
377 |       method_declaration
378 |       class_declaration       /* Added, JDK1.1 inner classes */
379 |       interface_declaration   /* Added, JDK1.1 inner classes */
380 ;
381
382 /* 19.8.2 Productions from 8.3: Field Declarations  */
383 field_declaration:
384         type variable_declarators SC_TK
385                 { USE_ABSORBER; }
386 |       modifiers type variable_declarators SC_TK
387                 { modifier_value = 0; }
388 ;
389
390 variable_declarators:
391         /* Should we use build_decl_list () instead ? FIXME */
392         variable_declarator     /* Default rule */
393 |       variable_declarators C_TK variable_declarator
394 ;
395
396 variable_declarator:
397         variable_declarator_id
398 |       variable_declarator_id ASSIGN_TK variable_initializer
399 ;
400
401 variable_declarator_id:
402         identifier
403                 { bracket_count = 0; USE_ABSORBER; }
404 |       variable_declarator_id OSB_TK CSB_TK
405                 { ++bracket_count; }
406 ;
407
408 variable_initializer:
409         expression
410 |       array_initializer
411 ;
412
413 /* 19.8.3 Productions from 8.4: Method Declarations  */
414 method_declaration:
415         method_header method_body
416 ;
417
418 method_header:  
419         type method_declarator throws
420                 { USE_ABSORBER; }
421 |       VOID_TK method_declarator throws
422 |       modifiers type method_declarator throws
423                 { modifier_value = 0; }
424 |       modifiers VOID_TK method_declarator throws
425                 { 
426                   report_main_declaration ($3);
427                   modifier_value = 0;
428                 }
429 ;
430
431 method_declarator:
432         identifier OP_TK CP_TK
433                 { 
434                   struct method_declarator *d;
435                   NEW_METHOD_DECLARATOR (d, $1, NULL);
436                   $$ = d;
437                 }
438 |       identifier OP_TK formal_parameter_list CP_TK
439                 { 
440                   struct method_declarator *d;
441                   NEW_METHOD_DECLARATOR (d, $1, $3);
442                   $$ = d;
443                 }
444 |       method_declarator OSB_TK CSB_TK
445 ;
446
447 formal_parameter_list:
448         formal_parameter
449 |       formal_parameter_list C_TK formal_parameter
450                 {
451                   $$ = concat ($1, ",", $3, NULL);
452                 }
453 ;
454
455 formal_parameter:
456         type variable_declarator_id
457                 { 
458                   USE_ABSORBER;
459                   if (bracket_count)
460                     {
461                       int i;
462                       char *n = xmalloc (bracket_count + 1 + strlen ($$));
463                       for (i = 0; i < bracket_count; ++i)
464                         n[i] = '[';
465                       strcpy (n + bracket_count, $$);
466                       $$ = n;
467                     }
468                   else
469                     $$ = $1;
470                 }
471 |       modifiers type variable_declarator_id /* Added, JDK1.1 final locals */
472                 {
473                   if (bracket_count)
474                     {
475                       int i;
476                       char *n = xmalloc (bracket_count + 1 + strlen ($$));
477                       for (i = 0; i < bracket_count; ++i)
478                         n[i] = '[';
479                       strcpy (n + bracket_count, $$);
480                       $$ = n;
481                     }
482                   else
483                     $$ = $2;
484                 }
485 ;
486
487 throws:
488 |       THROWS_TK class_type_list
489 ;
490
491 class_type_list:
492         class_type
493                 { USE_ABSORBER; }
494 |       class_type_list C_TK class_type
495                 { USE_ABSORBER; }
496 ;
497
498 method_body:
499         block
500 |       block SC_TK
501 |       SC_TK
502 ;
503
504 /* 19.8.4 Productions from 8.5: Static Initializers  */
505 static_initializer:
506         static block
507 |       static block SC_TK      /* Shouldn't be here. FIXME */
508 ;
509
510 static:                         /* Test lval.sub_token here */
511         MODIFIER_TK
512                 { USE_ABSORBER; }
513 ;
514
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 */
531 ;
532
533 constructor_declarator:
534         simple_name OP_TK CP_TK
535                 { USE_ABSORBER; }
536 |       simple_name OP_TK formal_parameter_list CP_TK
537                 { USE_ABSORBER; }
538 ;
539
540 constructor_body:
541         OCB_TK CCB_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
545 ;
546
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
554                 { USE_ABSORBER; }
555 |       name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
556                 { USE_ABSORBER; }
557 ;
558
559 this_or_super:                  /* Added, simplifies error diagnostics */
560         THIS_TK
561 |       SUPER_TK
562 ;
563
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; }
575 ;
576
577 extends_interfaces:
578         EXTENDS_TK interface_type
579 |       extends_interfaces C_TK interface_type
580 ;
581
582 interface_body:
583         OCB_TK CCB_TK
584 |       OCB_TK interface_member_declarations CCB_TK
585 ;
586
587 interface_member_declarations:
588         interface_member_declaration
589 |       interface_member_declarations interface_member_declaration
590 ;
591
592 interface_member_declaration:
593         constant_declaration
594 |       abstract_method_declaration
595 |       class_declaration       /* Added, JDK1.1 inner classes */
596 |       interface_declaration   /* Added, JDK1.1 inner classes */
597 ;
598
599 constant_declaration:
600         field_declaration
601 ;
602
603 abstract_method_declaration:
604         method_header SC_TK
605 ;
606
607 /* 19.10 Productions from 10: Arrays  */
608 array_initializer:
609         OCB_TK CCB_TK
610 |       OCB_TK variable_initializers CCB_TK
611 |       OCB_TK C_TK CCB_TK
612 |       OCB_TK variable_initializers C_TK CCB_TK
613 ;
614
615 variable_initializers:
616         variable_initializer
617 |       variable_initializers C_TK variable_initializer
618 ;
619
620 /* 19.11 Production from 14: Blocks and Statements  */
621 block:
622         OCB_TK CCB_TK
623 |       OCB_TK block_statements CCB_TK
624 ;
625
626 block_statements:
627         block_statement
628 |       block_statements block_statement
629 ;
630
631 block_statement:
632         local_variable_declaration_statement
633 |       statement
634 |       class_declaration       /* Added, JDK1.1 inner classes */
635 ;
636
637 local_variable_declaration_statement:
638         local_variable_declaration SC_TK /* Can't catch missing ';' here */
639 ;
640
641 local_variable_declaration:
642         type variable_declarators
643                 { USE_ABSORBER; }
644 |       modifiers type variable_declarators /* Added, JDK1.1 final locals */
645                 { modifier_value = 0; }
646 ;
647
648 statement:
649         statement_without_trailing_substatement
650 |       labeled_statement
651 |       if_then_statement
652 |       if_then_else_statement
653 |       while_statement
654 |       for_statement
655 ;
656
657 statement_nsi:
658         statement_without_trailing_substatement
659 |       labeled_statement_nsi
660 |       if_then_else_statement_nsi
661 |       while_statement_nsi
662 |       for_statement_nsi
663 ;
664
665 statement_without_trailing_substatement:
666         block
667 |       empty_statement
668 |       expression_statement
669 |       switch_statement
670 |       do_statement
671 |       break_statement
672 |       continue_statement
673 |       return_statement
674 |       synchronized_statement
675 |       throw_statement
676 |       try_statement
677 ;
678
679 empty_statement:
680         SC_TK
681 ;
682
683 label_decl:
684         identifier REL_CL_TK
685                 { USE_ABSORBER; }
686 ;
687
688 labeled_statement:
689         label_decl statement
690 ;
691
692 labeled_statement_nsi:
693         label_decl statement_nsi
694 ;
695
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
700 ;
701
702 statement_expression: 
703         assignment
704 |       pre_increment_expression
705 |       pre_decrement_expression
706 |       post_increment_expression
707 |       post_decrement_expression
708 |       method_invocation
709 |       class_instance_creation_expression
710 ;
711
712 if_then_statement:
713         IF_TK OP_TK expression CP_TK statement
714 ;
715
716 if_then_else_statement:
717         IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
718 ;
719
720 if_then_else_statement_nsi:
721         IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
722 ;
723
724 switch_statement:
725         SWITCH_TK OP_TK expression CP_TK switch_block
726 ;
727
728 switch_block:
729         OCB_TK CCB_TK
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
733 ;
734
735 switch_block_statement_groups: 
736         switch_block_statement_group
737 |       switch_block_statement_groups switch_block_statement_group
738 ;
739
740 switch_block_statement_group:
741         switch_labels block_statements
742 ;
743
744
745 switch_labels:
746         switch_label
747 |       switch_labels switch_label
748 ;
749
750 switch_label:
751         CASE_TK constant_expression REL_CL_TK
752 |       DEFAULT_TK REL_CL_TK
753 ;
754
755 while_expression:
756         WHILE_TK OP_TK expression CP_TK
757 ;
758
759 while_statement:
760         while_expression statement
761 ;
762
763 while_statement_nsi:
764         while_expression statement_nsi
765 ;
766
767 do_statement_begin:
768         DO_TK
769 ;
770
771 do_statement: 
772         do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
773 ;
774
775 for_statement:
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
778 ;
779
780 for_statement_nsi:
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
783 ;
784
785 for_header:
786         FOR_TK OP_TK
787 ;
788
789 for_begin:
790         for_header for_init
791 ;
792 for_init:                       /* Can be empty */
793 |       statement_expression_list
794 |       local_variable_declaration
795 ;
796
797 for_update:                     /* Can be empty */
798 |       statement_expression_list
799 ;
800
801 statement_expression_list:
802         statement_expression
803 |       statement_expression_list C_TK statement_expression
804 ;
805
806 break_statement:
807         BREAK_TK SC_TK
808 |       BREAK_TK identifier SC_TK
809 ;
810
811 continue_statement:
812         CONTINUE_TK SC_TK
813 |       CONTINUE_TK identifier SC_TK
814 ;
815
816 return_statement:
817         RETURN_TK SC_TK
818 |       RETURN_TK expression SC_TK
819 ;
820
821 throw_statement:
822         THROW_TK expression SC_TK
823 ;
824
825 synchronized_statement:
826         synchronized OP_TK expression CP_TK block
827 |       synchronized OP_TK expression CP_TK error
828 ;
829
830 synchronized:                   /* Test lval.sub_token here */
831         MODIFIER_TK
832                 { USE_ABSORBER; }
833 ;
834
835 try_statement:
836         TRY_TK block catches
837 |       TRY_TK block finally
838 |       TRY_TK block catches finally
839 ;
840
841 catches:
842         catch_clause
843 |       catches catch_clause
844 ;
845
846 catch_clause:
847         CATCH_TK OP_TK formal_parameter CP_TK block
848 ;
849
850 finally:
851         FINALLY_TK block
852 ;
853
854 /* 19.12 Production from 15: Expressions  */
855 primary:
856         primary_no_new_array
857 |       array_creation_expression
858 ;
859
860 primary_no_new_array:
861         literal
862 |       THIS_TK
863 |       OP_TK expression CP_TK
864 |       class_instance_creation_expression
865 |       field_access
866 |       method_invocation
867 |       array_access
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 */
872                 { USE_ABSORBER; }
873 |       primitive_type DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
874                 { USE_ABSORBER; }
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
880                 { USE_ABSORBER; }
881 ;
882
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
893            all situations.  */
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
898 ;
899
900 something_dot_new:              /* Added, not part of the specs. */
901         name DOT_TK NEW_TK
902                 { USE_ABSORBER; }
903 |       primary DOT_TK NEW_TK
904 ;
905
906 argument_list:
907         expression
908 |       argument_list C_TK expression
909 |       argument_list C_TK error
910 ;
911
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
918            modified */
919 |       NEW_TK class_or_interface_type dims array_initializer
920 |       NEW_TK primitive_type dims array_initializer
921 ;
922
923 dim_exprs:
924         dim_expr
925 |       dim_exprs dim_expr
926 ;
927
928 dim_expr:
929         OSB_TK expression CSB_TK
930 ;
931
932 dims:                           
933         OSB_TK CSB_TK
934 |       dims OSB_TK CSB_TK
935 ;
936
937 field_access:
938         primary DOT_TK identifier
939 |       SUPER_TK DOT_TK identifier
940 ;
941
942 method_invocation:
943         name OP_TK CP_TK
944                 { USE_ABSORBER; }
945 |       name OP_TK argument_list CP_TK
946                 { USE_ABSORBER; }
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
951 ;
952
953 array_access:
954         name OSB_TK expression CSB_TK
955                 { USE_ABSORBER; }
956 |       primary_no_new_array OSB_TK expression CSB_TK
957 ;
958
959 postfix_expression:
960         primary
961 |       name
962                 { USE_ABSORBER; }
963 |       post_increment_expression
964 |       post_decrement_expression
965 ;
966
967 post_increment_expression:
968         postfix_expression INCR_TK
969 ;
970
971 post_decrement_expression:
972         postfix_expression DECR_TK
973 ;
974
975 unary_expression:
976         pre_increment_expression
977 |       pre_decrement_expression
978 |       PLUS_TK unary_expression
979 |       MINUS_TK unary_expression
980 |       unary_expression_not_plus_minus
981 ;
982
983 pre_increment_expression:
984         INCR_TK unary_expression
985 ;
986
987 pre_decrement_expression:
988         DECR_TK unary_expression
989 ;
990
991 unary_expression_not_plus_minus:
992         postfix_expression
993 |       NOT_TK unary_expression
994 |       NEG_TK unary_expression
995 |       cast_expression
996 ;
997
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
1003 ;
1004
1005 multiplicative_expression:
1006         unary_expression
1007 |       multiplicative_expression MULT_TK unary_expression
1008 |       multiplicative_expression DIV_TK unary_expression
1009 |       multiplicative_expression REM_TK unary_expression
1010 ;
1011
1012 additive_expression:
1013         multiplicative_expression
1014 |       additive_expression PLUS_TK multiplicative_expression
1015 |       additive_expression MINUS_TK multiplicative_expression
1016 ;
1017
1018 shift_expression:
1019         additive_expression
1020 |       shift_expression LS_TK additive_expression
1021 |       shift_expression SRS_TK additive_expression
1022 |       shift_expression ZRS_TK additive_expression
1023 ;
1024
1025 relational_expression:
1026         shift_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
1032 ;
1033
1034 equality_expression:
1035         relational_expression
1036 |       equality_expression EQ_TK relational_expression
1037 |       equality_expression NEQ_TK relational_expression
1038 ;
1039
1040 and_expression:
1041         equality_expression
1042 |       and_expression AND_TK equality_expression
1043 ;
1044
1045 exclusive_or_expression:
1046         and_expression
1047 |       exclusive_or_expression XOR_TK and_expression
1048 ;
1049
1050 inclusive_or_expression:
1051         exclusive_or_expression
1052 |       inclusive_or_expression OR_TK exclusive_or_expression
1053 ;
1054
1055 conditional_and_expression:
1056         inclusive_or_expression
1057 |       conditional_and_expression BOOL_AND_TK inclusive_or_expression
1058 ;
1059
1060 conditional_or_expression:
1061         conditional_and_expression
1062 |       conditional_or_expression BOOL_OR_TK conditional_and_expression
1063 ;
1064
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
1068 ;
1069
1070 assignment_expression:
1071         conditional_expression
1072 |       assignment
1073 ;
1074
1075 assignment:
1076         left_hand_side assignment_operator assignment_expression
1077 ;
1078
1079 left_hand_side:
1080         name
1081                 { USE_ABSORBER; }
1082 |       field_access
1083 |       array_access
1084 ;
1085
1086 assignment_operator:
1087         ASSIGN_ANY_TK
1088 |       ASSIGN_TK
1089 ;
1090
1091 expression:
1092         assignment_expression
1093 ;
1094
1095 constant_expression:
1096         expression
1097 ;
1098
1099 %%
1100 \f
1101 /* Create a new parser context */
1102
1103 void
1104 java_push_parser_context ()
1105 {
1106   struct parser_ctxt *new = 
1107     (struct parser_ctxt *) xcalloc (1, sizeof (struct parser_ctxt));
1108
1109   new->next = ctxp;
1110   ctxp = new;
1111 }  
1112
1113 /* Actions defined here */
1114
1115 static void
1116 report_class_declaration (name)
1117      const char * name;
1118 {
1119   extern int flag_dump_class, flag_list_filename;
1120
1121   if (flag_dump_class)
1122     {
1123       if (!previous_output)
1124         {
1125           if (flag_list_filename)
1126             fprintf (out, "%s: ", input_filename);
1127           previous_output = 1;
1128         }
1129         
1130       if (package_name)
1131         fprintf (out, "%s.%s ", package_name, name);
1132       else
1133         fprintf (out, "%s ", name);
1134     }
1135       
1136   current_class = name;
1137 }
1138
1139 static void
1140 report_main_declaration (declarator)
1141      struct method_declarator *declarator;
1142 {
1143   extern int flag_find_main;
1144
1145   if (flag_find_main
1146       && modifier_value == 2
1147       && !strcmp (declarator->method_name, "main") 
1148       && declarator->args 
1149       && declarator->args [0] == '[' 
1150       && (! strcmp (declarator->args+1, "String")
1151           || ! strcmp (declarator->args + 1, "java.lang.String"))
1152       && current_class)
1153     {
1154       if (!previous_output)
1155         {
1156           if (package_name)
1157             fprintf (out, "%s.%s ", package_name, current_class);
1158           else
1159             fprintf (out, current_class);
1160           previous_output = 1;
1161         }
1162     }
1163 }
1164
1165 /* Reset global status used by the report functions.  */
1166
1167 void reset_report ()
1168 {
1169   previous_output = 0;
1170   current_class = package_name = NULL;
1171 }
1172
1173 void
1174 yyerror (msg)
1175      const char *msg ATTRIBUTE_UNUSED;
1176 {
1177 }