OSDN Git Service

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