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".
5 /* $Id: program.g,v 3.21 1994/06/24 12:05:36 ceriel Exp $ */
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
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
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.
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.
44 %start C_program, program;
45 %start If_expr, control_if_expression;
76 constant_expression(&exprX)
79 register struct expr *expr = exprX;
80 if (expr->ex_flags & EX_SIZEOF)
82 "sizeof not allowed in preprocessor");
83 ifval = expr->VL_VALUE;
84 free_expression(expr);
91 [%persistent external_definition]*
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.
102 A definition starts with a decl_specifiers, which contains things
105 which is implicitly repeated for every definition in the list, and
106 then for each identifier a declarator is given, of the form
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
117 struct declarator Dc;
122 Dc = null_declarator;
124 ext_decl_specifiers(&Ds)
128 declare_idf(&Ds, &Dc, level);
130 lint_ext_def(Dc.dc_idf, Ds.ds_sc);
136 non_function(&Ds, &Dc)
141 {remove_declarator(&Dc);}
143 asm_statement /* top level, would you believe */
146 ext_decl_specifiers(struct decspecs *ds;) :
147 %if (DOT != IDENTIFIER || AHEAD == IDENTIFIER) /* the thin ice in R.M. 11.1 */
154 non_function(register struct decspecs *ds; register struct declarator *dc;)
158 initializer(dc->dc_idf, ds->ds_sc)
160 { code_declaration(dc->dc_idf, (struct expr *) 0, level, ds->ds_sc); }
164 lint_non_function_decl(ds, dc);
175 function(struct decspecs *ds; struct declarator *dc;)
180 { register struct idf *idf = dc->dc_idf;
182 lint_start_function();
185 stack_level(); /* L_FORMAL1 declarations */
187 begin_proc(ds, idf); /* sets global function info */
188 stack_level(); /* L_FORMAL2 declarations */
192 declare_formals(&fbytes);
201 lint_implicit_return();
203 unstack_level(); /* L_FORMAL2 declarations */
207 unstack_level(); /* L_FORMAL1 declarations */