grammar.y revision 1.1.1.3 1 1.1.1.3 christos /* $Id: grammar.y,v 1.1.1.3 2011/09/10 21:19:11 christos Exp $
2 1.1 christos *
3 1.1 christos * yacc grammar for C function prototype generator
4 1.1 christos * This was derived from the grammar in Appendix A of
5 1.1 christos * "The C Programming Language" by Kernighan and Ritchie.
6 1.1 christos */
7 1.1.1.2 christos %expect 1
8 1.1 christos %token <text> '(' '*' '&'
9 1.1 christos /* identifiers that are not reserved words */
10 1.1 christos T_IDENTIFIER T_TYPEDEF_NAME T_DEFINE_NAME
11 1.1 christos
12 1.1 christos /* storage class */
13 1.1 christos T_AUTO T_EXTERN T_REGISTER T_STATIC T_TYPEDEF
14 1.1 christos /* This keyword included for compatibility with C++. */
15 1.1 christos T_INLINE
16 1.1 christos /* This keyword included for compatibility with GCC */
17 1.1 christos T_EXTENSION
18 1.1 christos
19 1.1 christos /* type specifiers */
20 1.1 christos T_CHAR T_DOUBLE T_FLOAT T_INT T_VOID
21 1.1 christos T_LONG T_SHORT T_SIGNED T_UNSIGNED
22 1.1 christos T_ENUM T_STRUCT T_UNION
23 1.1 christos /* C9X new types */
24 1.1 christos T_Bool T_Complex T_Imaginary
25 1.1 christos
26 1.1 christos /* type qualifiers */
27 1.1 christos T_TYPE_QUALIFIER
28 1.1 christos
29 1.1 christos /* paired square brackets and everything between them: [ ... ] */
30 1.1 christos T_BRACKETS
31 1.1 christos
32 1.1 christos %token
33 1.1 christos /* left brace */
34 1.1 christos T_LBRACE
35 1.1 christos /* all input to the matching right brace */
36 1.1 christos T_MATCHRBRACE
37 1.1 christos
38 1.1 christos /* three periods */
39 1.1 christos T_ELLIPSIS
40 1.1 christos
41 1.1 christos /* constant expression or paired braces following an equal sign */
42 1.1 christos T_INITIALIZER
43 1.1 christos
44 1.1 christos /* string literal */
45 1.1 christos T_STRING_LITERAL
46 1.1 christos
47 1.1 christos /* asm */
48 1.1 christos T_ASM
49 1.1 christos /* ( "string literal" ) following asm keyword */
50 1.1 christos T_ASMARG
51 1.1 christos
52 1.1 christos /* va_dcl from <varargs.h> */
53 1.1 christos T_VA_DCL
54 1.1 christos
55 1.1 christos %type <decl_spec> decl_specifiers decl_specifier
56 1.1 christos %type <decl_spec> storage_class type_specifier type_qualifier
57 1.1 christos %type <decl_spec> struct_or_union_specifier enum_specifier
58 1.1 christos %type <decl_list> init_declarator_list
59 1.1 christos %type <declarator> init_declarator declarator direct_declarator
60 1.1 christos %type <declarator> abs_declarator direct_abs_declarator
61 1.1 christos %type <param_list> parameter_type_list parameter_list
62 1.1 christos %type <parameter> parameter_declaration
63 1.1 christos %type <param_list> opt_identifier_list identifier_list
64 1.1 christos %type <text> struct_or_union pointer opt_type_qualifiers type_qualifier_list
65 1.1 christos any_id identifier_or_ref
66 1.1 christos %type <text> enumeration
67 1.1 christos
68 1.1 christos %{
69 1.1 christos #include <stdio.h>
70 1.1 christos #include <ctype.h>
71 1.1.1.2 christos #include <string.h>
72 1.1.1.2 christos
73 1.1.1.2 christos #define OPT_LINTLIBRARY 1
74 1.1.1.2 christos
75 1.1.1.2 christos #ifndef TRUE
76 1.1.1.2 christos #define TRUE (1)
77 1.1.1.2 christos #endif
78 1.1.1.2 christos
79 1.1.1.2 christos #ifndef FALSE
80 1.1.1.2 christos #define FALSE (0)
81 1.1.1.2 christos #endif
82 1.1.1.2 christos
83 1.1.1.2 christos /* #include "cproto.h" */
84 1.1.1.2 christos #define MAX_TEXT_SIZE 1024
85 1.1.1.2 christos
86 1.1.1.2 christos /* Prototype styles */
87 1.1.1.2 christos #if OPT_LINTLIBRARY
88 1.1.1.2 christos #define PROTO_ANSI_LLIB -2 /* form ANSI lint-library source */
89 1.1.1.2 christos #define PROTO_LINTLIBRARY -1 /* form lint-library source */
90 1.1.1.2 christos #endif
91 1.1.1.2 christos #define PROTO_NONE 0 /* do not output any prototypes */
92 1.1.1.2 christos #define PROTO_TRADITIONAL 1 /* comment out parameters */
93 1.1.1.2 christos #define PROTO_ABSTRACT 2 /* comment out parameter names */
94 1.1.1.2 christos #define PROTO_ANSI 3 /* ANSI C prototype */
95 1.1.1.2 christos
96 1.1.1.2 christos typedef int PrototypeStyle;
97 1.1.1.2 christos
98 1.1.1.2 christos typedef char boolean;
99 1.1.1.2 christos
100 1.1.1.2 christos extern boolean types_out;
101 1.1.1.2 christos extern PrototypeStyle proto_style;
102 1.1.1.2 christos
103 1.1.1.2 christos #define ansiLintLibrary() (proto_style == PROTO_ANSI_LLIB)
104 1.1.1.2 christos #define knrLintLibrary() (proto_style == PROTO_LINTLIBRARY)
105 1.1.1.2 christos #define lintLibrary() (knrLintLibrary() || ansiLintLibrary())
106 1.1.1.2 christos
107 1.1.1.2 christos #if OPT_LINTLIBRARY
108 1.1.1.2 christos #define FUNC_UNKNOWN -1 /* unspecified */
109 1.1.1.2 christos #else
110 1.1.1.2 christos #define FUNC_UNKNOWN 0 /* unspecified (same as FUNC_NONE) */
111 1.1.1.2 christos #endif
112 1.1.1.2 christos #define FUNC_NONE 0 /* not a function definition */
113 1.1.1.2 christos #define FUNC_TRADITIONAL 1 /* traditional style */
114 1.1.1.2 christos #define FUNC_ANSI 2 /* ANSI style */
115 1.1.1.2 christos #define FUNC_BOTH 3 /* both styles */
116 1.1.1.2 christos
117 1.1.1.2 christos typedef int FuncDefStyle;
118 1.1.1.2 christos
119 1.1.1.2 christos /* Source file text */
120 1.1.1.2 christos typedef struct text {
121 1.1.1.2 christos char text[MAX_TEXT_SIZE]; /* source text */
122 1.1.1.2 christos long begin; /* offset in temporary file */
123 1.1.1.2 christos } Text;
124 1.1.1.2 christos
125 1.1.1.2 christos /* Declaration specifier flags */
126 1.1.1.2 christos #define DS_NONE 0 /* default */
127 1.1.1.2 christos #define DS_EXTERN 1 /* contains "extern" specifier */
128 1.1.1.2 christos #define DS_STATIC 2 /* contains "static" specifier */
129 1.1.1.2 christos #define DS_CHAR 4 /* contains "char" type specifier */
130 1.1.1.2 christos #define DS_SHORT 8 /* contains "short" type specifier */
131 1.1.1.2 christos #define DS_FLOAT 16 /* contains "float" type specifier */
132 1.1.1.2 christos #define DS_INLINE 32 /* contains "inline" specifier */
133 1.1.1.2 christos #define DS_JUNK 64 /* we're not interested in this declaration */
134 1.1.1.2 christos
135 1.1.1.2 christos /* This structure stores information about a declaration specifier. */
136 1.1.1.2 christos typedef struct decl_spec {
137 1.1.1.2 christos unsigned short flags; /* flags defined above */
138 1.1.1.2 christos char *text; /* source text */
139 1.1.1.2 christos long begin; /* offset in temporary file */
140 1.1.1.2 christos } DeclSpec;
141 1.1.1.2 christos
142 1.1.1.2 christos /* This is a list of function parameters. */
143 1.1.1.2 christos typedef struct _ParameterList {
144 1.1.1.2 christos struct parameter *first; /* pointer to first parameter in list */
145 1.1.1.2 christos struct parameter *last; /* pointer to last parameter in list */
146 1.1.1.2 christos long begin_comment; /* begin offset of comment */
147 1.1.1.2 christos long end_comment; /* end offset of comment */
148 1.1.1.2 christos char *comment; /* comment at start of parameter list */
149 1.1.1.2 christos } ParameterList;
150 1.1.1.2 christos
151 1.1.1.2 christos /* This structure stores information about a declarator. */
152 1.1.1.2 christos typedef struct _Declarator {
153 1.1.1.2 christos char *name; /* name of variable or function */
154 1.1.1.2 christos char *text; /* source text */
155 1.1.1.2 christos long begin; /* offset in temporary file */
156 1.1.1.2 christos long begin_comment; /* begin offset of comment */
157 1.1.1.2 christos long end_comment; /* end offset of comment */
158 1.1.1.2 christos FuncDefStyle func_def; /* style of function definition */
159 1.1.1.2 christos ParameterList params; /* function parameters */
160 1.1.1.2 christos boolean pointer; /* TRUE if it declares a pointer */
161 1.1.1.2 christos struct _Declarator *head; /* head function declarator */
162 1.1.1.2 christos struct _Declarator *func_stack; /* stack of function declarators */
163 1.1.1.2 christos struct _Declarator *next; /* next declarator in list */
164 1.1.1.2 christos } Declarator;
165 1.1.1.2 christos
166 1.1.1.2 christos /* This structure stores information about a function parameter. */
167 1.1.1.2 christos typedef struct parameter {
168 1.1.1.2 christos struct parameter *next; /* next parameter in list */
169 1.1.1.2 christos DeclSpec decl_spec;
170 1.1.1.2 christos Declarator *declarator;
171 1.1.1.2 christos char *comment; /* comment following the parameter */
172 1.1.1.2 christos } Parameter;
173 1.1.1.2 christos
174 1.1.1.2 christos /* This is a list of declarators. */
175 1.1.1.2 christos typedef struct declarator_list {
176 1.1.1.2 christos Declarator *first; /* pointer to first declarator in list */
177 1.1.1.2 christos Declarator *last; /* pointer to last declarator in list */
178 1.1.1.2 christos } DeclaratorList;
179 1.1.1.2 christos
180 1.1.1.2 christos /* #include "symbol.h" */
181 1.1.1.2 christos typedef struct symbol {
182 1.1.1.2 christos struct symbol *next; /* next symbol in list */
183 1.1.1.2 christos char *name; /* name of symbol */
184 1.1.1.2 christos char *value; /* value of symbol (for defines) */
185 1.1.1.2 christos short flags; /* symbol attributes */
186 1.1.1.2 christos } Symbol;
187 1.1.1.2 christos
188 1.1.1.2 christos /* parser stack entry type */
189 1.1.1.2 christos typedef union {
190 1.1.1.2 christos Text text;
191 1.1.1.2 christos DeclSpec decl_spec;
192 1.1.1.2 christos Parameter *parameter;
193 1.1.1.2 christos ParameterList param_list;
194 1.1.1.2 christos Declarator *declarator;
195 1.1.1.2 christos DeclaratorList decl_list;
196 1.1.1.2 christos } YYSTYPE;
197 1.1.1.2 christos
198 1.1.1.2 christos /* The hash table length should be a prime number. */
199 1.1.1.2 christos #define SYM_MAX_HASH 251
200 1.1.1.2 christos
201 1.1.1.2 christos typedef struct symbol_table {
202 1.1.1.2 christos Symbol *bucket[SYM_MAX_HASH]; /* hash buckets */
203 1.1.1.2 christos } SymbolTable;
204 1.1.1.2 christos
205 1.1.1.2 christos extern SymbolTable *new_symbol_table /* Create symbol table */
206 1.1.1.2 christos (void);
207 1.1.1.2 christos extern void free_symbol_table /* Destroy symbol table */
208 1.1.1.2 christos (SymbolTable *s);
209 1.1.1.2 christos extern Symbol *find_symbol /* Lookup symbol name */
210 1.1.1.2 christos (SymbolTable *s, const char *n);
211 1.1.1.2 christos extern Symbol *new_symbol /* Define new symbol */
212 1.1.1.2 christos (SymbolTable *s, const char *n, const char *v, int f);
213 1.1.1.2 christos
214 1.1.1.2 christos /* #include "semantic.h" */
215 1.1.1.2 christos extern void new_decl_spec (DeclSpec *, const char *, long, int);
216 1.1.1.2 christos extern void free_decl_spec (DeclSpec *);
217 1.1.1.2 christos extern void join_decl_specs (DeclSpec *, DeclSpec *, DeclSpec *);
218 1.1.1.2 christos extern void check_untagged (DeclSpec *);
219 1.1.1.2 christos extern Declarator *new_declarator (const char *, const char *, long);
220 1.1.1.2 christos extern void free_declarator (Declarator *);
221 1.1.1.2 christos extern void new_decl_list (DeclaratorList *, Declarator *);
222 1.1.1.2 christos extern void free_decl_list (DeclaratorList *);
223 1.1.1.2 christos extern void add_decl_list (DeclaratorList *, DeclaratorList *, Declarator *);
224 1.1.1.2 christos extern Parameter *new_parameter (DeclSpec *, Declarator *);
225 1.1.1.2 christos extern void free_parameter (Parameter *);
226 1.1.1.2 christos extern void new_param_list (ParameterList *, Parameter *);
227 1.1.1.2 christos extern void free_param_list (ParameterList *);
228 1.1.1.2 christos extern void add_param_list (ParameterList *, ParameterList *, Parameter *);
229 1.1.1.2 christos extern void new_ident_list (ParameterList *);
230 1.1.1.2 christos extern void add_ident_list (ParameterList *, ParameterList *, const char *);
231 1.1.1.2 christos extern void set_param_types (ParameterList *, DeclSpec *, DeclaratorList *);
232 1.1.1.2 christos extern void gen_declarations (DeclSpec *, DeclaratorList *);
233 1.1.1.2 christos extern void gen_prototype (DeclSpec *, Declarator *);
234 1.1.1.2 christos extern void gen_func_declarator (Declarator *);
235 1.1.1.2 christos extern void gen_func_definition (DeclSpec *, Declarator *);
236 1.1.1.2 christos
237 1.1.1.2 christos extern void init_parser (void);
238 1.1.1.2 christos extern void process_file (FILE *infile, char *name);
239 1.1.1.2 christos extern char *cur_text (void);
240 1.1.1.2 christos extern char *cur_file_name (void);
241 1.1.1.2 christos extern char *implied_typedef (void);
242 1.1.1.2 christos extern void include_file (char *name, int convert);
243 1.1.1.2 christos extern char *supply_parm (int count);
244 1.1.1.2 christos extern char *xstrdup (const char *);
245 1.1.1.2 christos extern int already_declared (char *name);
246 1.1.1.2 christos extern int is_actual_func (Declarator *d);
247 1.1.1.2 christos extern int lint_ellipsis (Parameter *p);
248 1.1.1.2 christos extern int want_typedef (void);
249 1.1.1.2 christos extern void begin_tracking (void);
250 1.1.1.2 christos extern void begin_typedef (void);
251 1.1.1.2 christos extern void copy_typedef (char *s);
252 1.1.1.2 christos extern void ellipsis_varargs (Declarator *d);
253 1.1.1.2 christos extern void end_typedef (void);
254 1.1.1.2 christos extern void flush_varargs (void);
255 1.1.1.2 christos extern void fmt_library (int code);
256 1.1.1.2 christos extern void imply_typedef (const char *s);
257 1.1.1.2 christos extern void indent (FILE *outf);
258 1.1.1.2 christos extern void put_blankline (FILE *outf);
259 1.1.1.2 christos extern void put_body (FILE *outf, DeclSpec *decl_spec, Declarator *declarator);
260 1.1.1.2 christos extern void put_char (FILE *outf, int c);
261 1.1.1.2 christos extern void put_error (void);
262 1.1.1.2 christos extern void put_newline (FILE *outf);
263 1.1.1.2 christos extern void put_padded (FILE *outf, const char *s);
264 1.1.1.2 christos extern void put_string (FILE *outf, const char *s);
265 1.1.1.2 christos extern void track_in (void);
266 1.1.1.2 christos
267 1.1.1.2 christos extern boolean file_comments;
268 1.1.1.2 christos extern FuncDefStyle func_style;
269 1.1.1.2 christos extern char base_file[];
270 1.1 christos
271 1.1 christos #define YYMAXDEPTH 150
272 1.1 christos
273 1.1 christos extern int yylex (void);
274 1.1 christos
275 1.1 christos /* declaration specifier attributes for the typedef statement currently being
276 1.1 christos * scanned
277 1.1 christos */
278 1.1 christos static int cur_decl_spec_flags;
279 1.1 christos
280 1.1 christos /* pointer to parameter list for the current function definition */
281 1.1 christos static ParameterList *func_params;
282 1.1 christos
283 1.1 christos /* A parser semantic action sets this pointer to the current declarator in
284 1.1 christos * a function parameter declaration in order to catch any comments following
285 1.1 christos * the parameter declaration on the same line. If the lexer scans a comment
286 1.1 christos * and <cur_declarator> is not NULL, then the comment is attached to the
287 1.1 christos * declarator. To ignore subsequent comments, the lexer sets this to NULL
288 1.1 christos * after scanning a comment or end of line.
289 1.1 christos */
290 1.1 christos static Declarator *cur_declarator;
291 1.1 christos
292 1.1 christos /* temporary string buffer */
293 1.1 christos static char buf[MAX_TEXT_SIZE];
294 1.1 christos
295 1.1 christos /* table of typedef names */
296 1.1 christos static SymbolTable *typedef_names;
297 1.1 christos
298 1.1 christos /* table of define names */
299 1.1 christos static SymbolTable *define_names;
300 1.1 christos
301 1.1 christos /* table of type qualifiers */
302 1.1 christos static SymbolTable *type_qualifiers;
303 1.1 christos
304 1.1 christos /* information about the current input file */
305 1.1 christos typedef struct {
306 1.1 christos char *base_name; /* base input file name */
307 1.1 christos char *file_name; /* current file name */
308 1.1 christos FILE *file; /* input file */
309 1.1 christos unsigned line_num; /* current line number in input file */
310 1.1 christos FILE *tmp_file; /* temporary file */
311 1.1 christos long begin_comment; /* tmp file offset after last written ) or ; */
312 1.1 christos long end_comment; /* tmp file offset after last comment */
313 1.1 christos boolean convert; /* if TRUE, convert function definitions */
314 1.1 christos boolean changed; /* TRUE if conversion done in this file */
315 1.1 christos } IncludeStack;
316 1.1 christos
317 1.1 christos static IncludeStack *cur_file; /* current input file */
318 1.1 christos
319 1.1.1.2 christos /* #include "yyerror.c" */
320 1.1 christos
321 1.1 christos static int haveAnsiParam (void);
322 1.1 christos
323 1.1 christos
324 1.1 christos /* Flags to enable us to find if a procedure returns a value.
325 1.1 christos */
326 1.1.1.2 christos static int return_val; /* nonzero on BRACES iff return-expression found */
327 1.1 christos
328 1.1.1.2 christos static const char *
329 1.1 christos dft_decl_spec (void)
330 1.1 christos {
331 1.1 christos return (lintLibrary() && !return_val) ? "void" : "int";
332 1.1 christos }
333 1.1 christos
334 1.1 christos static int
335 1.1 christos haveAnsiParam (void)
336 1.1 christos {
337 1.1 christos Parameter *p;
338 1.1 christos if (func_params != 0) {
339 1.1 christos for (p = func_params->first; p != 0; p = p->next) {
340 1.1 christos if (p->declarator->func_def == FUNC_ANSI) {
341 1.1 christos return TRUE;
342 1.1 christos }
343 1.1 christos }
344 1.1 christos }
345 1.1 christos return FALSE;
346 1.1 christos }
347 1.1 christos %}
348 1.1 christos %%
349 1.1 christos
350 1.1 christos program
351 1.1 christos : /* empty */
352 1.1 christos | translation_unit
353 1.1 christos ;
354 1.1 christos
355 1.1 christos translation_unit
356 1.1 christos : external_declaration
357 1.1 christos | translation_unit external_declaration
358 1.1 christos ;
359 1.1 christos
360 1.1 christos external_declaration
361 1.1 christos : declaration
362 1.1 christos | function_definition
363 1.1 christos | ';'
364 1.1 christos | linkage_specification
365 1.1 christos | T_ASM T_ASMARG ';'
366 1.1 christos | error T_MATCHRBRACE
367 1.1 christos {
368 1.1 christos yyerrok;
369 1.1 christos }
370 1.1 christos | error ';'
371 1.1 christos {
372 1.1 christos yyerrok;
373 1.1 christos }
374 1.1 christos ;
375 1.1 christos
376 1.1 christos braces
377 1.1 christos : T_LBRACE T_MATCHRBRACE
378 1.1 christos ;
379 1.1 christos
380 1.1 christos linkage_specification
381 1.1 christos : T_EXTERN T_STRING_LITERAL braces
382 1.1 christos {
383 1.1 christos /* Provide an empty action here so bison will not complain about
384 1.1 christos * incompatible types in the default action it normally would
385 1.1 christos * have generated.
386 1.1 christos */
387 1.1 christos }
388 1.1 christos | T_EXTERN T_STRING_LITERAL declaration
389 1.1 christos {
390 1.1 christos /* empty */
391 1.1 christos }
392 1.1 christos ;
393 1.1 christos
394 1.1 christos declaration
395 1.1 christos : decl_specifiers ';'
396 1.1 christos {
397 1.1 christos #if OPT_LINTLIBRARY
398 1.1 christos if (types_out && want_typedef()) {
399 1.1 christos gen_declarations(&$1, (DeclaratorList *)0);
400 1.1 christos flush_varargs();
401 1.1 christos }
402 1.1 christos #endif
403 1.1 christos free_decl_spec(&$1);
404 1.1 christos end_typedef();
405 1.1 christos }
406 1.1 christos | decl_specifiers init_declarator_list ';'
407 1.1 christos {
408 1.1 christos if (func_params != NULL) {
409 1.1 christos set_param_types(func_params, &$1, &$2);
410 1.1 christos } else {
411 1.1 christos gen_declarations(&$1, &$2);
412 1.1 christos #if OPT_LINTLIBRARY
413 1.1 christos flush_varargs();
414 1.1 christos #endif
415 1.1 christos free_decl_list(&$2);
416 1.1 christos }
417 1.1 christos free_decl_spec(&$1);
418 1.1 christos end_typedef();
419 1.1 christos }
420 1.1 christos | any_typedef decl_specifiers
421 1.1 christos {
422 1.1 christos cur_decl_spec_flags = $2.flags;
423 1.1 christos free_decl_spec(&$2);
424 1.1 christos }
425 1.1 christos opt_declarator_list ';'
426 1.1 christos {
427 1.1 christos end_typedef();
428 1.1 christos }
429 1.1 christos ;
430 1.1 christos
431 1.1 christos any_typedef
432 1.1 christos : T_EXTENSION T_TYPEDEF
433 1.1 christos {
434 1.1 christos begin_typedef();
435 1.1 christos }
436 1.1 christos | T_TYPEDEF
437 1.1 christos {
438 1.1 christos begin_typedef();
439 1.1 christos }
440 1.1 christos ;
441 1.1 christos
442 1.1 christos opt_declarator_list
443 1.1 christos : /* empty */
444 1.1 christos | declarator_list
445 1.1 christos ;
446 1.1 christos
447 1.1 christos declarator_list
448 1.1 christos : declarator
449 1.1 christos {
450 1.1 christos int flags = cur_decl_spec_flags;
451 1.1 christos
452 1.1 christos /* If the typedef is a pointer type, then reset the short type
453 1.1 christos * flags so it does not get promoted.
454 1.1 christos */
455 1.1 christos if (strcmp($1->text, $1->name) != 0)
456 1.1 christos flags &= ~(DS_CHAR | DS_SHORT | DS_FLOAT);
457 1.1 christos new_symbol(typedef_names, $1->name, NULL, flags);
458 1.1 christos free_declarator($1);
459 1.1 christos }
460 1.1 christos | declarator_list ',' declarator
461 1.1 christos {
462 1.1 christos int flags = cur_decl_spec_flags;
463 1.1 christos
464 1.1 christos if (strcmp($3->text, $3->name) != 0)
465 1.1 christos flags &= ~(DS_CHAR | DS_SHORT | DS_FLOAT);
466 1.1 christos new_symbol(typedef_names, $3->name, NULL, flags);
467 1.1 christos free_declarator($3);
468 1.1 christos }
469 1.1 christos ;
470 1.1 christos
471 1.1 christos function_definition
472 1.1 christos : decl_specifiers declarator
473 1.1 christos {
474 1.1 christos check_untagged(&$1);
475 1.1 christos if ($2->func_def == FUNC_NONE) {
476 1.1 christos yyerror("syntax error");
477 1.1 christos YYERROR;
478 1.1 christos }
479 1.1 christos func_params = &($2->head->params);
480 1.1 christos func_params->begin_comment = cur_file->begin_comment;
481 1.1 christos func_params->end_comment = cur_file->end_comment;
482 1.1 christos }
483 1.1 christos opt_declaration_list T_LBRACE
484 1.1 christos {
485 1.1 christos /* If we're converting to K&R and we've got a nominally K&R
486 1.1 christos * function which has a parameter which is ANSI (i.e., a prototyped
487 1.1 christos * function pointer), then we must override the deciphered value of
488 1.1 christos * 'func_def' so that the parameter will be converted.
489 1.1 christos */
490 1.1 christos if (func_style == FUNC_TRADITIONAL
491 1.1 christos && haveAnsiParam()
492 1.1 christos && $2->head->func_def == func_style) {
493 1.1 christos $2->head->func_def = FUNC_BOTH;
494 1.1 christos }
495 1.1 christos
496 1.1 christos func_params = NULL;
497 1.1 christos
498 1.1 christos if (cur_file->convert)
499 1.1 christos gen_func_definition(&$1, $2);
500 1.1 christos gen_prototype(&$1, $2);
501 1.1 christos #if OPT_LINTLIBRARY
502 1.1 christos flush_varargs();
503 1.1 christos #endif
504 1.1 christos free_decl_spec(&$1);
505 1.1 christos free_declarator($2);
506 1.1 christos }
507 1.1 christos T_MATCHRBRACE
508 1.1 christos | declarator
509 1.1 christos {
510 1.1 christos if ($1->func_def == FUNC_NONE) {
511 1.1 christos yyerror("syntax error");
512 1.1 christos YYERROR;
513 1.1 christos }
514 1.1 christos func_params = &($1->head->params);
515 1.1 christos func_params->begin_comment = cur_file->begin_comment;
516 1.1 christos func_params->end_comment = cur_file->end_comment;
517 1.1 christos }
518 1.1 christos opt_declaration_list T_LBRACE T_MATCHRBRACE
519 1.1 christos {
520 1.1 christos DeclSpec decl_spec;
521 1.1 christos
522 1.1 christos func_params = NULL;
523 1.1 christos
524 1.1 christos new_decl_spec(&decl_spec, dft_decl_spec(), $1->begin, DS_NONE);
525 1.1 christos if (cur_file->convert)
526 1.1 christos gen_func_definition(&decl_spec, $1);
527 1.1 christos gen_prototype(&decl_spec, $1);
528 1.1 christos #if OPT_LINTLIBRARY
529 1.1 christos flush_varargs();
530 1.1 christos #endif
531 1.1 christos free_decl_spec(&decl_spec);
532 1.1 christos free_declarator($1);
533 1.1 christos }
534 1.1 christos ;
535 1.1 christos
536 1.1 christos opt_declaration_list
537 1.1 christos : /* empty */
538 1.1 christos | T_VA_DCL
539 1.1 christos | declaration_list
540 1.1 christos ;
541 1.1 christos
542 1.1 christos declaration_list
543 1.1 christos : declaration
544 1.1 christos | declaration_list declaration
545 1.1 christos ;
546 1.1 christos
547 1.1 christos decl_specifiers
548 1.1 christos : decl_specifier
549 1.1 christos | decl_specifiers decl_specifier
550 1.1 christos {
551 1.1 christos join_decl_specs(&$$, &$1, &$2);
552 1.1 christos free($1.text);
553 1.1 christos free($2.text);
554 1.1 christos }
555 1.1 christos ;
556 1.1 christos
557 1.1 christos decl_specifier
558 1.1 christos : storage_class
559 1.1 christos | type_specifier
560 1.1 christos | type_qualifier
561 1.1 christos ;
562 1.1 christos
563 1.1 christos storage_class
564 1.1 christos : T_AUTO
565 1.1 christos {
566 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
567 1.1 christos }
568 1.1 christos | T_EXTERN
569 1.1 christos {
570 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, DS_EXTERN);
571 1.1 christos }
572 1.1 christos | T_REGISTER
573 1.1 christos {
574 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
575 1.1 christos }
576 1.1 christos | T_STATIC
577 1.1 christos {
578 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, DS_STATIC);
579 1.1 christos }
580 1.1 christos | T_INLINE
581 1.1 christos {
582 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, DS_INLINE);
583 1.1 christos }
584 1.1 christos | T_EXTENSION
585 1.1 christos {
586 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, DS_JUNK);
587 1.1 christos }
588 1.1 christos ;
589 1.1 christos
590 1.1 christos type_specifier
591 1.1 christos : T_CHAR
592 1.1 christos {
593 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, DS_CHAR);
594 1.1 christos }
595 1.1 christos | T_DOUBLE
596 1.1 christos {
597 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
598 1.1 christos }
599 1.1 christos | T_FLOAT
600 1.1 christos {
601 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, DS_FLOAT);
602 1.1 christos }
603 1.1 christos | T_INT
604 1.1 christos {
605 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
606 1.1 christos }
607 1.1 christos | T_LONG
608 1.1 christos {
609 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
610 1.1 christos }
611 1.1 christos | T_SHORT
612 1.1 christos {
613 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, DS_SHORT);
614 1.1 christos }
615 1.1 christos | T_SIGNED
616 1.1 christos {
617 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
618 1.1 christos }
619 1.1 christos | T_UNSIGNED
620 1.1 christos {
621 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
622 1.1 christos }
623 1.1 christos | T_VOID
624 1.1 christos {
625 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
626 1.1 christos }
627 1.1 christos | T_Bool
628 1.1 christos {
629 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, DS_CHAR);
630 1.1 christos }
631 1.1 christos | T_Complex
632 1.1 christos {
633 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
634 1.1 christos }
635 1.1 christos | T_Imaginary
636 1.1 christos {
637 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
638 1.1 christos }
639 1.1 christos | T_TYPEDEF_NAME
640 1.1 christos {
641 1.1 christos Symbol *s;
642 1.1 christos s = find_symbol(typedef_names, $1.text);
643 1.1 christos if (s != NULL)
644 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, s->flags);
645 1.1 christos }
646 1.1 christos | struct_or_union_specifier
647 1.1 christos | enum_specifier
648 1.1 christos ;
649 1.1 christos
650 1.1 christos type_qualifier
651 1.1 christos : T_TYPE_QUALIFIER
652 1.1 christos {
653 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
654 1.1 christos }
655 1.1 christos | T_DEFINE_NAME
656 1.1 christos {
657 1.1 christos /* This rule allows the <pointer> nonterminal to scan #define
658 1.1 christos * names as if they were type modifiers.
659 1.1 christos */
660 1.1 christos Symbol *s;
661 1.1 christos s = find_symbol(define_names, $1.text);
662 1.1 christos if (s != NULL)
663 1.1 christos new_decl_spec(&$$, $1.text, $1.begin, s->flags);
664 1.1 christos }
665 1.1 christos ;
666 1.1 christos
667 1.1 christos struct_or_union_specifier
668 1.1 christos : struct_or_union any_id braces
669 1.1 christos {
670 1.1 christos char *s;
671 1.1 christos if ((s = implied_typedef()) == 0)
672 1.1 christos (void)sprintf(s = buf, "%s %s", $1.text, $2.text);
673 1.1 christos new_decl_spec(&$$, s, $1.begin, DS_NONE);
674 1.1 christos }
675 1.1 christos | struct_or_union braces
676 1.1 christos {
677 1.1 christos char *s;
678 1.1 christos if ((s = implied_typedef()) == 0)
679 1.1 christos (void)sprintf(s = buf, "%s {}", $1.text);
680 1.1 christos new_decl_spec(&$$, s, $1.begin, DS_NONE);
681 1.1 christos }
682 1.1 christos | struct_or_union any_id
683 1.1 christos {
684 1.1 christos (void)sprintf(buf, "%s %s", $1.text, $2.text);
685 1.1 christos new_decl_spec(&$$, buf, $1.begin, DS_NONE);
686 1.1 christos }
687 1.1 christos ;
688 1.1 christos
689 1.1 christos struct_or_union
690 1.1 christos : T_STRUCT
691 1.1 christos {
692 1.1 christos imply_typedef($$.text);
693 1.1 christos }
694 1.1 christos | T_UNION
695 1.1 christos {
696 1.1 christos imply_typedef($$.text);
697 1.1 christos }
698 1.1 christos ;
699 1.1 christos
700 1.1 christos init_declarator_list
701 1.1 christos : init_declarator
702 1.1 christos {
703 1.1 christos new_decl_list(&$$, $1);
704 1.1 christos }
705 1.1 christos | init_declarator_list ',' init_declarator
706 1.1 christos {
707 1.1 christos add_decl_list(&$$, &$1, $3);
708 1.1 christos }
709 1.1 christos ;
710 1.1 christos
711 1.1 christos init_declarator
712 1.1 christos : declarator
713 1.1 christos {
714 1.1 christos if ($1->func_def != FUNC_NONE && func_params == NULL &&
715 1.1 christos func_style == FUNC_TRADITIONAL && cur_file->convert) {
716 1.1 christos gen_func_declarator($1);
717 1.1 christos fputs(cur_text(), cur_file->tmp_file);
718 1.1 christos }
719 1.1 christos cur_declarator = $$;
720 1.1 christos }
721 1.1 christos | declarator '='
722 1.1 christos {
723 1.1 christos if ($1->func_def != FUNC_NONE && func_params == NULL &&
724 1.1 christos func_style == FUNC_TRADITIONAL && cur_file->convert) {
725 1.1 christos gen_func_declarator($1);
726 1.1 christos fputs(" =", cur_file->tmp_file);
727 1.1 christos }
728 1.1 christos }
729 1.1 christos T_INITIALIZER
730 1.1 christos ;
731 1.1 christos
732 1.1 christos enum_specifier
733 1.1 christos : enumeration any_id braces
734 1.1 christos {
735 1.1 christos char *s;
736 1.1 christos if ((s = implied_typedef()) == 0)
737 1.1 christos (void)sprintf(s = buf, "enum %s", $2.text);
738 1.1 christos new_decl_spec(&$$, s, $1.begin, DS_NONE);
739 1.1 christos }
740 1.1 christos | enumeration braces
741 1.1 christos {
742 1.1 christos char *s;
743 1.1 christos if ((s = implied_typedef()) == 0)
744 1.1 christos (void)sprintf(s = buf, "%s {}", $1.text);
745 1.1 christos new_decl_spec(&$$, s, $1.begin, DS_NONE);
746 1.1 christos }
747 1.1 christos | enumeration any_id
748 1.1 christos {
749 1.1 christos (void)sprintf(buf, "enum %s", $2.text);
750 1.1 christos new_decl_spec(&$$, buf, $1.begin, DS_NONE);
751 1.1 christos }
752 1.1 christos ;
753 1.1 christos
754 1.1 christos enumeration
755 1.1 christos : T_ENUM
756 1.1 christos {
757 1.1 christos imply_typedef("enum");
758 1.1 christos $$ = $1;
759 1.1 christos }
760 1.1 christos ;
761 1.1 christos
762 1.1 christos any_id
763 1.1 christos : T_IDENTIFIER
764 1.1 christos | T_TYPEDEF_NAME
765 1.1 christos ;
766 1.1 christos
767 1.1 christos declarator
768 1.1 christos : pointer direct_declarator
769 1.1 christos {
770 1.1 christos $$ = $2;
771 1.1 christos (void)sprintf(buf, "%s%s", $1.text, $$->text);
772 1.1 christos free($$->text);
773 1.1 christos $$->text = xstrdup(buf);
774 1.1 christos $$->begin = $1.begin;
775 1.1 christos $$->pointer = TRUE;
776 1.1 christos }
777 1.1 christos | direct_declarator
778 1.1 christos ;
779 1.1 christos
780 1.1 christos direct_declarator
781 1.1 christos : identifier_or_ref
782 1.1 christos {
783 1.1 christos $$ = new_declarator($1.text, $1.text, $1.begin);
784 1.1 christos }
785 1.1 christos | '(' declarator ')'
786 1.1 christos {
787 1.1 christos $$ = $2;
788 1.1 christos (void)sprintf(buf, "(%s)", $$->text);
789 1.1 christos free($$->text);
790 1.1 christos $$->text = xstrdup(buf);
791 1.1 christos $$->begin = $1.begin;
792 1.1 christos }
793 1.1 christos | direct_declarator T_BRACKETS
794 1.1 christos {
795 1.1 christos $$ = $1;
796 1.1 christos (void)sprintf(buf, "%s%s", $$->text, $2.text);
797 1.1 christos free($$->text);
798 1.1 christos $$->text = xstrdup(buf);
799 1.1 christos }
800 1.1 christos | direct_declarator '(' parameter_type_list ')'
801 1.1 christos {
802 1.1 christos $$ = new_declarator("%s()", $1->name, $1->begin);
803 1.1 christos $$->params = $3;
804 1.1 christos $$->func_stack = $1;
805 1.1 christos $$->head = ($1->func_stack == NULL) ? $$ : $1->head;
806 1.1 christos $$->func_def = FUNC_ANSI;
807 1.1 christos }
808 1.1 christos | direct_declarator '(' opt_identifier_list ')'
809 1.1 christos {
810 1.1 christos $$ = new_declarator("%s()", $1->name, $1->begin);
811 1.1 christos $$->params = $3;
812 1.1 christos $$->func_stack = $1;
813 1.1 christos $$->head = ($1->func_stack == NULL) ? $$ : $1->head;
814 1.1 christos $$->func_def = FUNC_TRADITIONAL;
815 1.1 christos }
816 1.1 christos ;
817 1.1 christos
818 1.1 christos pointer
819 1.1 christos : '*' opt_type_qualifiers
820 1.1 christos {
821 1.1 christos (void)sprintf($$.text, "*%s", $2.text);
822 1.1 christos $$.begin = $1.begin;
823 1.1 christos }
824 1.1 christos | '*' opt_type_qualifiers pointer
825 1.1 christos {
826 1.1 christos (void)sprintf($$.text, "*%s%s", $2.text, $3.text);
827 1.1 christos $$.begin = $1.begin;
828 1.1 christos }
829 1.1 christos ;
830 1.1 christos
831 1.1 christos opt_type_qualifiers
832 1.1 christos : /* empty */
833 1.1 christos {
834 1.1 christos strcpy($$.text, "");
835 1.1 christos $$.begin = 0L;
836 1.1 christos }
837 1.1 christos | type_qualifier_list
838 1.1 christos ;
839 1.1 christos
840 1.1 christos type_qualifier_list
841 1.1 christos : type_qualifier
842 1.1 christos {
843 1.1 christos (void)sprintf($$.text, "%s ", $1.text);
844 1.1 christos $$.begin = $1.begin;
845 1.1 christos free($1.text);
846 1.1 christos }
847 1.1 christos | type_qualifier_list type_qualifier
848 1.1 christos {
849 1.1 christos (void)sprintf($$.text, "%s%s ", $1.text, $2.text);
850 1.1 christos $$.begin = $1.begin;
851 1.1 christos free($2.text);
852 1.1 christos }
853 1.1 christos ;
854 1.1 christos
855 1.1 christos parameter_type_list
856 1.1 christos : parameter_list
857 1.1 christos | parameter_list ',' T_ELLIPSIS
858 1.1 christos {
859 1.1 christos add_ident_list(&$$, &$1, "...");
860 1.1 christos }
861 1.1 christos ;
862 1.1 christos
863 1.1 christos parameter_list
864 1.1 christos : parameter_declaration
865 1.1 christos {
866 1.1 christos new_param_list(&$$, $1);
867 1.1 christos }
868 1.1 christos | parameter_list ',' parameter_declaration
869 1.1 christos {
870 1.1 christos add_param_list(&$$, &$1, $3);
871 1.1 christos }
872 1.1 christos ;
873 1.1 christos
874 1.1 christos parameter_declaration
875 1.1 christos : decl_specifiers declarator
876 1.1 christos {
877 1.1 christos check_untagged(&$1);
878 1.1 christos $$ = new_parameter(&$1, $2);
879 1.1 christos }
880 1.1 christos | decl_specifiers abs_declarator
881 1.1 christos {
882 1.1 christos check_untagged(&$1);
883 1.1 christos $$ = new_parameter(&$1, $2);
884 1.1 christos }
885 1.1 christos | decl_specifiers
886 1.1 christos {
887 1.1 christos check_untagged(&$1);
888 1.1 christos $$ = new_parameter(&$1, (Declarator *)0);
889 1.1 christos }
890 1.1 christos ;
891 1.1 christos
892 1.1 christos opt_identifier_list
893 1.1 christos : /* empty */
894 1.1 christos {
895 1.1 christos new_ident_list(&$$);
896 1.1 christos }
897 1.1 christos | identifier_list
898 1.1 christos ;
899 1.1 christos
900 1.1 christos identifier_list
901 1.1 christos : any_id
902 1.1 christos {
903 1.1 christos new_ident_list(&$$);
904 1.1 christos add_ident_list(&$$, &$$, $1.text);
905 1.1 christos }
906 1.1 christos | identifier_list ',' any_id
907 1.1 christos {
908 1.1 christos add_ident_list(&$$, &$1, $3.text);
909 1.1 christos }
910 1.1 christos ;
911 1.1 christos
912 1.1 christos identifier_or_ref
913 1.1 christos : any_id
914 1.1 christos {
915 1.1 christos $$ = $1;
916 1.1 christos }
917 1.1 christos | '&' any_id
918 1.1 christos {
919 1.1 christos #if OPT_LINTLIBRARY
920 1.1 christos if (lintLibrary()) { /* Lint doesn't grok C++ ref variables */
921 1.1 christos $$ = $2;
922 1.1 christos } else
923 1.1 christos #endif
924 1.1 christos (void)sprintf($$.text, "&%s", $2.text);
925 1.1 christos $$.begin = $1.begin;
926 1.1 christos }
927 1.1 christos ;
928 1.1 christos
929 1.1 christos abs_declarator
930 1.1 christos : pointer
931 1.1 christos {
932 1.1 christos $$ = new_declarator($1.text, "", $1.begin);
933 1.1 christos }
934 1.1 christos | pointer direct_abs_declarator
935 1.1 christos {
936 1.1 christos $$ = $2;
937 1.1 christos (void)sprintf(buf, "%s%s", $1.text, $$->text);
938 1.1 christos free($$->text);
939 1.1 christos $$->text = xstrdup(buf);
940 1.1 christos $$->begin = $1.begin;
941 1.1 christos }
942 1.1 christos | direct_abs_declarator
943 1.1 christos ;
944 1.1 christos
945 1.1 christos direct_abs_declarator
946 1.1 christos : '(' abs_declarator ')'
947 1.1 christos {
948 1.1 christos $$ = $2;
949 1.1 christos (void)sprintf(buf, "(%s)", $$->text);
950 1.1 christos free($$->text);
951 1.1 christos $$->text = xstrdup(buf);
952 1.1 christos $$->begin = $1.begin;
953 1.1 christos }
954 1.1 christos | direct_abs_declarator T_BRACKETS
955 1.1 christos {
956 1.1 christos $$ = $1;
957 1.1 christos (void)sprintf(buf, "%s%s", $$->text, $2.text);
958 1.1 christos free($$->text);
959 1.1 christos $$->text = xstrdup(buf);
960 1.1 christos }
961 1.1 christos | T_BRACKETS
962 1.1 christos {
963 1.1 christos $$ = new_declarator($1.text, "", $1.begin);
964 1.1 christos }
965 1.1 christos | direct_abs_declarator '(' parameter_type_list ')'
966 1.1 christos {
967 1.1 christos $$ = new_declarator("%s()", "", $1->begin);
968 1.1 christos $$->params = $3;
969 1.1 christos $$->func_stack = $1;
970 1.1 christos $$->head = ($1->func_stack == NULL) ? $$ : $1->head;
971 1.1 christos $$->func_def = FUNC_ANSI;
972 1.1 christos }
973 1.1 christos | direct_abs_declarator '(' ')'
974 1.1 christos {
975 1.1 christos $$ = new_declarator("%s()", "", $1->begin);
976 1.1 christos $$->func_stack = $1;
977 1.1 christos $$->head = ($1->func_stack == NULL) ? $$ : $1->head;
978 1.1 christos $$->func_def = FUNC_ANSI;
979 1.1 christos }
980 1.1 christos | '(' parameter_type_list ')'
981 1.1 christos {
982 1.1 christos Declarator *d;
983 1.1 christos
984 1.1 christos d = new_declarator("", "", $1.begin);
985 1.1 christos $$ = new_declarator("%s()", "", $1.begin);
986 1.1 christos $$->params = $2;
987 1.1 christos $$->func_stack = d;
988 1.1 christos $$->head = $$;
989 1.1 christos $$->func_def = FUNC_ANSI;
990 1.1 christos }
991 1.1 christos | '(' ')'
992 1.1 christos {
993 1.1 christos Declarator *d;
994 1.1 christos
995 1.1 christos d = new_declarator("", "", $1.begin);
996 1.1 christos $$ = new_declarator("%s()", "", $1.begin);
997 1.1 christos $$->func_stack = d;
998 1.1 christos $$->head = $$;
999 1.1 christos $$->func_def = FUNC_ANSI;
1000 1.1 christos }
1001 1.1 christos ;
1002 1.1 christos
1003 1.1 christos %%
1004 1.1 christos
1005 1.1.1.2 christos /* lex.yy.c */
1006 1.1.1.2 christos #define BEGIN yy_start = 1 + 2 *
1007 1.1.1.2 christos
1008 1.1.1.2 christos #define CPP1 1
1009 1.1.1.2 christos #define INIT1 2
1010 1.1.1.2 christos #define INIT2 3
1011 1.1.1.2 christos #define CURLY 4
1012 1.1.1.2 christos #define LEXYACC 5
1013 1.1.1.2 christos #define ASM 6
1014 1.1.1.2 christos #define CPP_INLINE 7
1015 1.1.1.2 christos
1016 1.1.1.2 christos extern char *yytext;
1017 1.1.1.2 christos extern FILE *yyin, *yyout;
1018 1.1.1.2 christos
1019 1.1.1.2 christos static int curly; /* number of curly brace nesting levels */
1020 1.1.1.2 christos static int ly_count; /* number of occurances of %% */
1021 1.1.1.2 christos static int inc_depth; /* include nesting level */
1022 1.1.1.2 christos static SymbolTable *included_files; /* files already included */
1023 1.1.1.2 christos static int yy_start = 0; /* start state number */
1024 1.1.1.2 christos
1025 1.1.1.2 christos #define grammar_error(s) yaccError(s)
1026 1.1 christos
1027 1.1 christos static void
1028 1.1.1.2 christos yaccError (const char *msg)
1029 1.1 christos {
1030 1.1 christos func_params = NULL;
1031 1.1 christos put_error(); /* tell what line we're on, and what file */
1032 1.1 christos fprintf(stderr, "%s at token '%s'\n", msg, yytext);
1033 1.1 christos }
1034 1.1 christos
1035 1.1 christos /* Initialize the table of type qualifier keywords recognized by the lexical
1036 1.1 christos * analyzer.
1037 1.1 christos */
1038 1.1 christos void
1039 1.1 christos init_parser (void)
1040 1.1 christos {
1041 1.1.1.2 christos static const char *keywords[] = {
1042 1.1 christos "const",
1043 1.1 christos "restrict",
1044 1.1 christos "volatile",
1045 1.1 christos "interrupt",
1046 1.1 christos #ifdef vms
1047 1.1 christos "noshare",
1048 1.1 christos "readonly",
1049 1.1 christos #endif
1050 1.1 christos #if defined(MSDOS) || defined(OS2)
1051 1.1 christos "__cdecl",
1052 1.1 christos "__export",
1053 1.1 christos "__far",
1054 1.1 christos "__fastcall",
1055 1.1 christos "__fortran",
1056 1.1 christos "__huge",
1057 1.1 christos "__inline",
1058 1.1 christos "__interrupt",
1059 1.1 christos "__loadds",
1060 1.1 christos "__near",
1061 1.1 christos "__pascal",
1062 1.1 christos "__saveregs",
1063 1.1 christos "__segment",
1064 1.1 christos "__stdcall",
1065 1.1 christos "__syscall",
1066 1.1 christos "_cdecl",
1067 1.1 christos "_cs",
1068 1.1 christos "_ds",
1069 1.1 christos "_es",
1070 1.1 christos "_export",
1071 1.1 christos "_far",
1072 1.1 christos "_fastcall",
1073 1.1 christos "_fortran",
1074 1.1 christos "_huge",
1075 1.1 christos "_interrupt",
1076 1.1 christos "_loadds",
1077 1.1 christos "_near",
1078 1.1 christos "_pascal",
1079 1.1 christos "_saveregs",
1080 1.1 christos "_seg",
1081 1.1 christos "_segment",
1082 1.1 christos "_ss",
1083 1.1 christos "cdecl",
1084 1.1 christos "far",
1085 1.1 christos "huge",
1086 1.1 christos "near",
1087 1.1 christos "pascal",
1088 1.1 christos #ifdef OS2
1089 1.1 christos "__far16",
1090 1.1 christos #endif
1091 1.1 christos #endif
1092 1.1 christos #ifdef __GNUC__
1093 1.1 christos /* gcc aliases */
1094 1.1 christos "__builtin_va_arg",
1095 1.1 christos "__builtin_va_list",
1096 1.1 christos "__const",
1097 1.1 christos "__const__",
1098 1.1 christos "__inline",
1099 1.1 christos "__inline__",
1100 1.1 christos "__restrict",
1101 1.1 christos "__restrict__",
1102 1.1 christos "__volatile",
1103 1.1 christos "__volatile__",
1104 1.1 christos #endif
1105 1.1 christos };
1106 1.1 christos unsigned i;
1107 1.1 christos
1108 1.1 christos /* Initialize type qualifier table. */
1109 1.1 christos type_qualifiers = new_symbol_table();
1110 1.1 christos for (i = 0; i < sizeof(keywords)/sizeof(keywords[0]); ++i) {
1111 1.1 christos new_symbol(type_qualifiers, keywords[i], NULL, DS_NONE);
1112 1.1 christos }
1113 1.1 christos }
1114 1.1 christos
1115 1.1 christos /* Process the C source file. Write function prototypes to the standard
1116 1.1 christos * output. Convert function definitions and write the converted source
1117 1.1 christos * code to a temporary file.
1118 1.1 christos */
1119 1.1 christos void
1120 1.1 christos process_file (FILE *infile, char *name)
1121 1.1 christos {
1122 1.1 christos char *s;
1123 1.1 christos
1124 1.1 christos if (strlen(name) > 2) {
1125 1.1 christos s = name + strlen(name) - 2;
1126 1.1 christos if (*s == '.') {
1127 1.1 christos ++s;
1128 1.1 christos if (*s == 'l' || *s == 'y')
1129 1.1 christos BEGIN LEXYACC;
1130 1.1 christos #if defined(MSDOS) || defined(OS2)
1131 1.1 christos if (*s == 'L' || *s == 'Y')
1132 1.1 christos BEGIN LEXYACC;
1133 1.1 christos #endif
1134 1.1 christos }
1135 1.1 christos }
1136 1.1 christos
1137 1.1 christos included_files = new_symbol_table();
1138 1.1 christos typedef_names = new_symbol_table();
1139 1.1 christos define_names = new_symbol_table();
1140 1.1 christos inc_depth = -1;
1141 1.1 christos curly = 0;
1142 1.1 christos ly_count = 0;
1143 1.1 christos func_params = NULL;
1144 1.1 christos yyin = infile;
1145 1.1 christos include_file(strcpy(base_file, name), func_style != FUNC_NONE);
1146 1.1 christos if (file_comments) {
1147 1.1 christos #if OPT_LINTLIBRARY
1148 1.1 christos if (lintLibrary()) {
1149 1.1 christos put_blankline(stdout);
1150 1.1 christos begin_tracking();
1151 1.1 christos }
1152 1.1 christos #endif
1153 1.1 christos put_string(stdout, "/* ");
1154 1.1 christos put_string(stdout, cur_file_name());
1155 1.1 christos put_string(stdout, " */\n");
1156 1.1 christos }
1157 1.1 christos yyparse();
1158 1.1 christos free_symbol_table(define_names);
1159 1.1 christos free_symbol_table(typedef_names);
1160 1.1 christos free_symbol_table(included_files);
1161 1.1 christos }
1162 1.1 christos
1163 1.1 christos #ifdef NO_LEAKS
1164 1.1 christos void
1165 1.1 christos free_parser(void)
1166 1.1 christos {
1167 1.1 christos free_symbol_table (type_qualifiers);
1168 1.1 christos #ifdef FLEX_SCANNER
1169 1.1 christos if (yy_current_buffer != 0)
1170 1.1 christos yy_delete_buffer(yy_current_buffer);
1171 1.1 christos #endif
1172 1.1 christos }
1173 1.1 christos #endif
1174