OSDN Git Service

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