Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / cemcom / program.g
1 /*
2  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  */
5 /* $Id: program.g,v 3.21 1994/06/24 12:05:36 ceriel Exp $ */
6 /* PROGRAM PARSER */
7
8 /*      The presence of typedef declarations renders it impossible to
9         make a context-free grammar of C. Consequently we need
10         context-sensitive parsing techniques, the simplest one being
11         a subtle cooperation between the parser and the lexical scanner.
12         The lexical scanner has to know whether to return IDENTIFIER
13         or TYPE_IDENTIFIER for a given tag, and it obtains this information
14         from the definition list, as constructed by the parser.
15         The present grammar is essentially LL(2), and is processed by
16         a parser generator which accepts LL(1) with tie breaking rules
17         in C, of the form %if(cond) and %while(cond). To solve the LL(1)
18         ambiguities, the lexical scanner does a one symbol look-ahead.
19         This symbol, however, cannot always be correctly assessed, since
20         the present symbol may cause a change in the definition list
21         which causes the identification of the look-ahead symbol to be
22         invalidated.
23         The lexical scanner relies on the parser (or its routines) to
24         detect this situation and then update the look-ahead symbol.
25         An alternative approach would be to reassess the look-ahead symbol
26         in the lexical scanner when it is promoted to dot symbol. This
27         would be more beautiful but less correct, since then for a short
28         while there would be a discrepancy between the look-ahead symbol
29         and the definition list; I think it would nevertheless work in
30         correct programs.
31         A third solution would be to enter the identifier as soon as it
32         is found; its storage class is then known, although its full type
33         isn't. We would have to fill that in afterwards.
34
35         At block exit the situation is even worse. Upon reading the
36         closing brace, the names declared inside the function are cleared
37         from the name list. This action may expose a type identifier that
38         is the same as the identifier in the look-ahead symbol. This
39         situation certainly invalidates the third solution, and casts
40         doubts upon the second.
41 */
42
43 %lexical        LLlex;
44 %start          C_program, program;
45 %start          If_expr, control_if_expression;
46
47 {
48 #include        "lint.h"
49 #include        "nopp.h"
50 #include        "arith.h"
51 #include        "LLlex.h"
52 #include        "idf.h"
53 #include        "label.h"
54 #include        "type.h"
55 #include        "declar.h"
56 #include        "decspecs.h"
57 #include        "code.h"
58 #include        "expr.h"
59 #include        "def.h"
60 #ifdef  LINT
61 #include        "l_lint.h"
62 #endif  /* LINT */
63
64 #ifndef NOPP
65 extern arith ifval;
66 #endif /* NOPP */
67
68 extern error();
69 }
70
71 control_if_expression
72         {
73                 struct expr *exprX;
74         }
75 :
76         constant_expression(&exprX)
77                 {
78 #ifndef NOPP
79                         register struct expr *expr = exprX;
80                         if (expr->ex_flags & EX_SIZEOF)
81                                 expr_error(expr,
82                                         "sizeof not allowed in preprocessor");
83                         ifval = expr->VL_VALUE;
84                         free_expression(expr);
85 #endif /* NOPP */
86                 }
87 ;
88
89 /* 10 */
90 program:
91         [%persistent external_definition]*
92         {unstack_world();}
93 ;
94
95 /*      A C identifier definition is remarkable in that it formulates
96         the declaration in a way different from most other languages:
97         e.g., rather than defining x as a pointer-to-integer, it defines
98         *x as an integer and lets the compiler deduce that x is actually
99         pointer-to-integer.  This has profound consequences, both for the
100         structure of an identifier definition and for the compiler.
101         
102         A definition starts with a decl_specifiers, which contains things
103         like
104                 typedef int
105         which is implicitly repeated for every definition in the list, and
106         then for each identifier a declarator is given, of the form
107                 *a()
108         or so.  The decl_specifiers is kept in a struct decspecs, to be
109         used again and again, while the declarator is stored in a struct
110         declarator, only to be passed to declare_idf together with the
111         struct decspecs.
112 */
113
114 external_definition
115         {
116                 struct decspecs Ds;
117                 struct declarator Dc;
118         }
119 :
120         {
121                 Ds = null_decspecs;
122                 Dc = null_declarator;
123         }
124         ext_decl_specifiers(&Ds)
125         [
126                 declarator(&Dc)
127                 {
128                         declare_idf(&Ds, &Dc, level);
129 #ifdef  LINT
130                         lint_ext_def(Dc.dc_idf, Ds.ds_sc);
131 #endif  /* LINT */
132                 }
133                 [
134                         function(&Ds, &Dc)
135                 |
136                         non_function(&Ds, &Dc)
137                 ]
138         |
139                 ';'
140         ]
141         {remove_declarator(&Dc);}
142 |
143         asm_statement                   /* top level, would you believe */
144 ;
145
146 ext_decl_specifiers(struct decspecs *ds;) :
147 %if (DOT != IDENTIFIER || AHEAD == IDENTIFIER) /* the thin ice in  R.M. 11.1 */
148         decl_specifiers(ds)
149 |
150         empty
151         {do_decspecs(ds);}
152 ;
153
154 non_function(register struct decspecs *ds; register struct declarator *dc;)
155 :
156         {reject_params(dc);}
157         [
158                 initializer(dc->dc_idf, ds->ds_sc)
159         |
160                 { code_declaration(dc->dc_idf, (struct expr *) 0, level, ds->ds_sc); }
161         ]
162         {
163 #ifdef  LINT
164                 lint_non_function_decl(ds, dc);
165 #endif  /* LINT */
166         }
167         [
168                 ','
169                 init_declarator(ds)
170         ]*
171         ';'
172 ;
173
174 /* 10.1 */
175 function(struct decspecs *ds; struct declarator *dc;)
176         {
177                 arith fbytes;
178         }
179 :
180         {       register struct idf *idf = dc->dc_idf;
181 #ifdef  LINT
182                 lint_start_function();
183 #endif  /* LINT */
184                 init_idf(idf);
185                 stack_level();          /* L_FORMAL1 declarations */
186                 declare_params(dc);
187                 begin_proc(ds, idf);    /* sets global function info */
188                 stack_level();          /* L_FORMAL2 declarations */
189         }
190         declaration*
191         {
192                 declare_formals(&fbytes);
193 #ifdef  LINT
194                 lint_formals();
195 #endif  /* LINT */
196         }
197         compound_statement
198         {
199                 end_proc(fbytes);
200 #ifdef  LINT
201                 lint_implicit_return();
202 #endif  /* LINT */
203                 unstack_level();        /* L_FORMAL2 declarations */
204 #ifdef  LINT
205                 lint_end_formals();
206 #endif  /* LINT */
207                 unstack_level();        /* L_FORMAL1 declarations */
208 #ifdef  LINT
209                 lint_end_function();
210 #endif  /* LINT */
211         }
212 ;