1234949Sbapt/* $Id: grammar.y,v 1.5 2012/01/15 20:00:59 tom Exp $
2234949Sbapt *
3234949Sbapt * yacc grammar for C function prototype generator
4234949Sbapt * This was derived from the grammar in Appendix A of
5234949Sbapt * "The C Programming Language" by Kernighan and Ritchie.
6234949Sbapt */
7234949Sbapt%expect 1
8234949Sbapt%{
9234949Sbapt#ifdef YYBISON
10234949Sbapt#include <stdlib.h>
11234949Sbapt#define YYSTYPE_IS_DECLARED
12234949Sbapt#define yyerror yaccError
13234949Sbapt#endif
14234949Sbapt
15234949Sbapt#if defined(YYBISON) || !defined(YYBYACC)
16234949Sbaptstatic void yyerror(const char *s);
17234949Sbapt#endif
18234949Sbapt%}
19234949Sbapt
20234949Sbapt%token <text> '(' '*' '&'
21234949Sbapt	/* identifiers that are not reserved words */
22234949Sbapt	T_IDENTIFIER T_TYPEDEF_NAME T_DEFINE_NAME
23234949Sbapt
24234949Sbapt	/* storage class */
25234949Sbapt	T_AUTO T_EXTERN T_REGISTER T_STATIC T_TYPEDEF
26234949Sbapt	/* This keyword included for compatibility with C++. */
27234949Sbapt	T_INLINE
28234949Sbapt	/* This keyword included for compatibility with GCC */
29234949Sbapt	T_EXTENSION
30234949Sbapt
31234949Sbapt	/* type specifiers */
32234949Sbapt	T_CHAR T_DOUBLE T_FLOAT T_INT T_VOID
33234949Sbapt	T_LONG T_SHORT T_SIGNED T_UNSIGNED
34234949Sbapt	T_ENUM T_STRUCT T_UNION
35234949Sbapt	/* C9X new types */
36234949Sbapt	T_Bool T_Complex T_Imaginary
37234949Sbapt
38234949Sbapt	/* type qualifiers */
39234949Sbapt	T_TYPE_QUALIFIER
40234949Sbapt
41234949Sbapt	/* paired square brackets and everything between them: [ ... ] */
42234949Sbapt	T_BRACKETS
43234949Sbapt
44234949Sbapt%token
45234949Sbapt	/* left brace */
46234949Sbapt	T_LBRACE
47234949Sbapt	/* all input to the matching right brace */
48234949Sbapt	T_MATCHRBRACE
49234949Sbapt
50234949Sbapt	/* three periods */
51234949Sbapt	T_ELLIPSIS
52234949Sbapt
53234949Sbapt	/* constant expression or paired braces following an equal sign */
54234949Sbapt	T_INITIALIZER
55234949Sbapt
56234949Sbapt	/* string literal */
57234949Sbapt	T_STRING_LITERAL
58234949Sbapt
59234949Sbapt	/* asm */
60234949Sbapt	T_ASM
61234949Sbapt	/* ( "string literal" ) following asm keyword */
62234949Sbapt	T_ASMARG
63234949Sbapt
64234949Sbapt	/* va_dcl from <varargs.h> */
65234949Sbapt	T_VA_DCL
66234949Sbapt
67234949Sbapt%type <decl_spec> decl_specifiers decl_specifier
68234949Sbapt%type <decl_spec> storage_class type_specifier type_qualifier
69234949Sbapt%type <decl_spec> struct_or_union_specifier enum_specifier
70234949Sbapt%type <decl_list> init_declarator_list
71234949Sbapt%type <declarator> init_declarator declarator direct_declarator
72234949Sbapt%type <declarator> abs_declarator direct_abs_declarator
73234949Sbapt%type <param_list> parameter_type_list parameter_list
74234949Sbapt%type <parameter> parameter_declaration
75234949Sbapt%type <param_list> opt_identifier_list identifier_list
76234949Sbapt%type <text> struct_or_union pointer opt_type_qualifiers type_qualifier_list
77234949Sbapt	any_id identifier_or_ref
78234949Sbapt%type <text> enumeration
79234949Sbapt
80234949Sbapt%{
81234949Sbapt#include <stdio.h>
82234949Sbapt#include <ctype.h>
83234949Sbapt#include <string.h>
84234949Sbapt
85234949Sbapt#define OPT_LINTLIBRARY 1
86234949Sbapt
87234949Sbapt#ifndef TRUE
88234949Sbapt#define	TRUE	(1)
89234949Sbapt#endif
90234949Sbapt
91234949Sbapt#ifndef FALSE
92234949Sbapt#define	FALSE	(0)
93234949Sbapt#endif
94234949Sbapt
95234949Sbapt/* #include "cproto.h" */
96234949Sbapt#define MAX_TEXT_SIZE 1024
97234949Sbapt
98234949Sbapt/* Prototype styles */
99234949Sbapt#if OPT_LINTLIBRARY
100234949Sbapt#define PROTO_ANSI_LLIB		-2	/* form ANSI lint-library source */
101234949Sbapt#define PROTO_LINTLIBRARY	-1	/* form lint-library source */
102234949Sbapt#endif
103234949Sbapt#define PROTO_NONE		0	/* do not output any prototypes */
104234949Sbapt#define PROTO_TRADITIONAL	1	/* comment out parameters */
105234949Sbapt#define PROTO_ABSTRACT		2	/* comment out parameter names */
106234949Sbapt#define PROTO_ANSI		3	/* ANSI C prototype */
107234949Sbapt
108234949Sbapttypedef int PrototypeStyle;
109234949Sbapt
110234949Sbapttypedef char boolean;
111234949Sbapt
112234949Sbaptextern boolean types_out;
113234949Sbaptextern PrototypeStyle proto_style;
114234949Sbapt
115234949Sbapt#define ansiLintLibrary() (proto_style == PROTO_ANSI_LLIB)
116234949Sbapt#define knrLintLibrary()  (proto_style == PROTO_LINTLIBRARY)
117234949Sbapt#define lintLibrary()     (knrLintLibrary() || ansiLintLibrary())
118234949Sbapt
119234949Sbapt#if OPT_LINTLIBRARY
120234949Sbapt#define FUNC_UNKNOWN		-1	/* unspecified */
121234949Sbapt#else
122234949Sbapt#define FUNC_UNKNOWN		0	/* unspecified (same as FUNC_NONE) */
123234949Sbapt#endif
124234949Sbapt#define FUNC_NONE		0	/* not a function definition */
125234949Sbapt#define FUNC_TRADITIONAL	1	/* traditional style */
126234949Sbapt#define FUNC_ANSI		2	/* ANSI style */
127234949Sbapt#define FUNC_BOTH		3	/* both styles */
128234949Sbapt
129234949Sbapttypedef int FuncDefStyle;
130234949Sbapt
131234949Sbapt/* Source file text */
132234949Sbapttypedef struct text {
133234949Sbapt    char text[MAX_TEXT_SIZE];	/* source text */
134234949Sbapt    long begin; 		/* offset in temporary file */
135234949Sbapt} Text;
136234949Sbapt
137234949Sbapt/* Declaration specifier flags */
138234949Sbapt#define DS_NONE 	0	/* default */
139234949Sbapt#define DS_EXTERN	1	/* contains "extern" specifier */
140234949Sbapt#define DS_STATIC	2	/* contains "static" specifier */
141234949Sbapt#define DS_CHAR 	4	/* contains "char" type specifier */
142234949Sbapt#define DS_SHORT	8	/* contains "short" type specifier */
143234949Sbapt#define DS_FLOAT	16	/* contains "float" type specifier */
144234949Sbapt#define DS_INLINE	32	/* contains "inline" specifier */
145234949Sbapt#define DS_JUNK 	64	/* we're not interested in this declaration */
146234949Sbapt
147234949Sbapt/* This structure stores information about a declaration specifier. */
148234949Sbapttypedef struct decl_spec {
149234949Sbapt    unsigned short flags;	/* flags defined above */
150234949Sbapt    char *text; 		/* source text */
151234949Sbapt    long begin; 		/* offset in temporary file */
152234949Sbapt} DeclSpec;
153234949Sbapt
154234949Sbapt/* This is a list of function parameters. */
155234949Sbapttypedef struct _ParameterList {
156234949Sbapt    struct parameter *first;	/* pointer to first parameter in list */
157234949Sbapt    struct parameter *last;	/* pointer to last parameter in list */
158234949Sbapt    long begin_comment; 	/* begin offset of comment */
159234949Sbapt    long end_comment;		/* end offset of comment */
160234949Sbapt    char *comment;		/* comment at start of parameter list */
161234949Sbapt} ParameterList;
162234949Sbapt
163234949Sbapt/* This structure stores information about a declarator. */
164234949Sbapttypedef struct _Declarator {
165234949Sbapt    char *name; 			/* name of variable or function */
166234949Sbapt    char *text; 			/* source text */
167234949Sbapt    long begin; 			/* offset in temporary file */
168234949Sbapt    long begin_comment; 		/* begin offset of comment */
169234949Sbapt    long end_comment;			/* end offset of comment */
170234949Sbapt    FuncDefStyle func_def;		/* style of function definition */
171234949Sbapt    ParameterList params;		/* function parameters */
172234949Sbapt    boolean pointer;			/* TRUE if it declares a pointer */
173234949Sbapt    struct _Declarator *head;		/* head function declarator */
174234949Sbapt    struct _Declarator *func_stack;	/* stack of function declarators */
175234949Sbapt    struct _Declarator *next;		/* next declarator in list */
176234949Sbapt} Declarator;
177234949Sbapt
178234949Sbapt/* This structure stores information about a function parameter. */
179234949Sbapttypedef struct parameter {
180234949Sbapt    struct parameter *next;	/* next parameter in list */
181234949Sbapt    DeclSpec decl_spec;
182234949Sbapt    Declarator *declarator;
183234949Sbapt    char *comment;		/* comment following the parameter */
184234949Sbapt} Parameter;
185234949Sbapt
186234949Sbapt/* This is a list of declarators. */
187234949Sbapttypedef struct declarator_list {
188234949Sbapt    Declarator *first;		/* pointer to first declarator in list */
189234949Sbapt    Declarator *last;		/* pointer to last declarator in list */
190234949Sbapt} DeclaratorList;
191234949Sbapt
192234949Sbapt/* #include "symbol.h" */
193234949Sbapttypedef struct symbol {
194234949Sbapt    struct symbol *next;	/* next symbol in list */
195234949Sbapt    char *name; 		/* name of symbol */
196234949Sbapt    char *value;		/* value of symbol (for defines) */
197234949Sbapt    short flags;		/* symbol attributes */
198234949Sbapt} Symbol;
199234949Sbapt
200234949Sbapt/* parser stack entry type */
201234949Sbapttypedef union {
202234949Sbapt    Text text;
203234949Sbapt    DeclSpec decl_spec;
204234949Sbapt    Parameter *parameter;
205234949Sbapt    ParameterList param_list;
206234949Sbapt    Declarator *declarator;
207234949Sbapt    DeclaratorList decl_list;
208234949Sbapt} YYSTYPE;
209234949Sbapt
210234949Sbapt/* The hash table length should be a prime number. */
211234949Sbapt#define SYM_MAX_HASH 251
212234949Sbapt
213234949Sbapttypedef struct symbol_table {
214234949Sbapt    Symbol *bucket[SYM_MAX_HASH];	/* hash buckets */
215234949Sbapt} SymbolTable;
216234949Sbapt
217234949Sbaptextern SymbolTable *new_symbol_table	/* Create symbol table */
218234949Sbapt	(void);
219234949Sbaptextern void free_symbol_table		/* Destroy symbol table */
220234949Sbapt	(SymbolTable *s);
221234949Sbaptextern Symbol *find_symbol		/* Lookup symbol name */
222234949Sbapt	(SymbolTable *s, const char *n);
223234949Sbaptextern Symbol *new_symbol		/* Define new symbol */
224234949Sbapt	(SymbolTable *s, const char *n, const char *v, int f);
225234949Sbapt
226234949Sbapt/* #include "semantic.h" */
227234949Sbaptextern void new_decl_spec (DeclSpec *, const char *, long, int);
228234949Sbaptextern void free_decl_spec (DeclSpec *);
229234949Sbaptextern void join_decl_specs (DeclSpec *, DeclSpec *, DeclSpec *);
230234949Sbaptextern void check_untagged (DeclSpec *);
231234949Sbaptextern Declarator *new_declarator (const char *, const char *, long);
232234949Sbaptextern void free_declarator (Declarator *);
233234949Sbaptextern void new_decl_list (DeclaratorList *, Declarator *);
234234949Sbaptextern void free_decl_list (DeclaratorList *);
235234949Sbaptextern void add_decl_list (DeclaratorList *, DeclaratorList *, Declarator *);
236234949Sbaptextern Parameter *new_parameter (DeclSpec *, Declarator *);
237234949Sbaptextern void free_parameter (Parameter *);
238234949Sbaptextern void new_param_list (ParameterList *, Parameter *);
239234949Sbaptextern void free_param_list (ParameterList *);
240234949Sbaptextern void add_param_list (ParameterList *, ParameterList *, Parameter *);
241234949Sbaptextern void new_ident_list (ParameterList *);
242234949Sbaptextern void add_ident_list (ParameterList *, ParameterList *, const char *);
243234949Sbaptextern void set_param_types (ParameterList *, DeclSpec *, DeclaratorList *);
244234949Sbaptextern void gen_declarations (DeclSpec *, DeclaratorList *);
245234949Sbaptextern void gen_prototype (DeclSpec *, Declarator *);
246234949Sbaptextern void gen_func_declarator (Declarator *);
247234949Sbaptextern void gen_func_definition (DeclSpec *, Declarator *);
248234949Sbapt
249234949Sbaptextern void init_parser     (void);
250234949Sbaptextern void process_file    (FILE *infile, char *name);
251234949Sbaptextern char *cur_text       (void);
252234949Sbaptextern char *cur_file_name  (void);
253234949Sbaptextern char *implied_typedef (void);
254234949Sbaptextern void include_file    (char *name, int convert);
255234949Sbaptextern char *supply_parm    (int count);
256234949Sbaptextern char *xstrdup        (const char *);
257234949Sbaptextern int already_declared (char *name);
258234949Sbaptextern int is_actual_func   (Declarator *d);
259234949Sbaptextern int lint_ellipsis    (Parameter *p);
260234949Sbaptextern int want_typedef     (void);
261234949Sbaptextern void begin_tracking  (void);
262234949Sbaptextern void begin_typedef   (void);
263234949Sbaptextern void copy_typedef    (char *s);
264234949Sbaptextern void ellipsis_varargs (Declarator *d);
265234949Sbaptextern void end_typedef     (void);
266234949Sbaptextern void flush_varargs   (void);
267234949Sbaptextern void fmt_library     (int code);
268234949Sbaptextern void imply_typedef   (const char *s);
269234949Sbaptextern void indent          (FILE *outf);
270234949Sbaptextern void put_blankline   (FILE *outf);
271234949Sbaptextern void put_body        (FILE *outf, DeclSpec *decl_spec, Declarator *declarator);
272234949Sbaptextern void put_char        (FILE *outf, int c);
273234949Sbaptextern void put_error       (void);
274234949Sbaptextern void put_newline     (FILE *outf);
275234949Sbaptextern void put_padded      (FILE *outf, const char *s);
276234949Sbaptextern void put_string      (FILE *outf, const char *s);
277234949Sbaptextern void track_in        (void);
278234949Sbapt
279234949Sbaptextern boolean file_comments;
280234949Sbaptextern FuncDefStyle func_style;
281234949Sbaptextern char base_file[];
282234949Sbapt
283234949Sbaptextern	int	yylex (void);
284234949Sbapt
285234949Sbapt/* declaration specifier attributes for the typedef statement currently being
286234949Sbapt * scanned
287234949Sbapt */
288234949Sbaptstatic int cur_decl_spec_flags;
289234949Sbapt
290234949Sbapt/* pointer to parameter list for the current function definition */
291234949Sbaptstatic ParameterList *func_params;
292234949Sbapt
293234949Sbapt/* A parser semantic action sets this pointer to the current declarator in
294234949Sbapt * a function parameter declaration in order to catch any comments following
295234949Sbapt * the parameter declaration on the same line.  If the lexer scans a comment
296234949Sbapt * and <cur_declarator> is not NULL, then the comment is attached to the
297234949Sbapt * declarator.  To ignore subsequent comments, the lexer sets this to NULL
298234949Sbapt * after scanning a comment or end of line.
299234949Sbapt */
300234949Sbaptstatic Declarator *cur_declarator;
301234949Sbapt
302234949Sbapt/* temporary string buffer */
303234949Sbaptstatic char buf[MAX_TEXT_SIZE];
304234949Sbapt
305234949Sbapt/* table of typedef names */
306234949Sbaptstatic SymbolTable *typedef_names;
307234949Sbapt
308234949Sbapt/* table of define names */
309234949Sbaptstatic SymbolTable *define_names;
310234949Sbapt
311234949Sbapt/* table of type qualifiers */
312234949Sbaptstatic SymbolTable *type_qualifiers;
313234949Sbapt
314234949Sbapt/* information about the current input file */
315234949Sbapttypedef struct {
316234949Sbapt    char *base_name;		/* base input file name */
317234949Sbapt    char *file_name;		/* current file name */
318234949Sbapt    FILE *file; 		/* input file */
319234949Sbapt    unsigned line_num;		/* current line number in input file */
320234949Sbapt    FILE *tmp_file;		/* temporary file */
321234949Sbapt    long begin_comment; 	/* tmp file offset after last written ) or ; */
322234949Sbapt    long end_comment;		/* tmp file offset after last comment */
323234949Sbapt    boolean convert;		/* if TRUE, convert function definitions */
324234949Sbapt    boolean changed;		/* TRUE if conversion done in this file */
325234949Sbapt} IncludeStack;
326234949Sbapt
327234949Sbaptstatic IncludeStack *cur_file;	/* current input file */
328234949Sbapt
329234949Sbapt/* #include "yyerror.c" */
330234949Sbapt
331234949Sbaptstatic int haveAnsiParam (void);
332234949Sbapt
333234949Sbapt
334234949Sbapt/* Flags to enable us to find if a procedure returns a value.
335234949Sbapt */
336234949Sbaptstatic int return_val;	/* nonzero on BRACES iff return-expression found */
337234949Sbapt
338234949Sbaptstatic const char *
339234949Sbaptdft_decl_spec (void)
340234949Sbapt{
341234949Sbapt    return (lintLibrary() && !return_val) ? "void" : "int";
342234949Sbapt}
343234949Sbapt
344234949Sbaptstatic int
345234949SbapthaveAnsiParam (void)
346234949Sbapt{
347234949Sbapt    Parameter *p;
348234949Sbapt    if (func_params != 0) {
349234949Sbapt	for (p = func_params->first; p != 0; p = p->next) {
350234949Sbapt	    if (p->declarator->func_def == FUNC_ANSI) {
351234949Sbapt		return TRUE;
352234949Sbapt	    }
353234949Sbapt	}
354234949Sbapt    }
355234949Sbapt    return FALSE;
356234949Sbapt}
357234949Sbapt%}
358234949Sbapt%%
359234949Sbapt
360234949Sbaptprogram
361234949Sbapt	: /* empty */
362234949Sbapt	| translation_unit
363234949Sbapt	;
364234949Sbapt
365234949Sbapttranslation_unit
366234949Sbapt	: external_declaration
367234949Sbapt	| translation_unit external_declaration
368234949Sbapt	;
369234949Sbapt
370234949Sbaptexternal_declaration
371234949Sbapt	: declaration
372234949Sbapt	| function_definition
373234949Sbapt	| ';'
374234949Sbapt	| linkage_specification
375234949Sbapt	| T_ASM T_ASMARG ';'
376234949Sbapt	| error T_MATCHRBRACE
377234949Sbapt	{
378234949Sbapt	    yyerrok;
379234949Sbapt	}
380234949Sbapt	| error ';'
381234949Sbapt	{
382234949Sbapt	    yyerrok;
383234949Sbapt	}
384234949Sbapt	;
385234949Sbapt
386234949Sbaptbraces
387234949Sbapt	: T_LBRACE T_MATCHRBRACE
388234949Sbapt	;
389234949Sbapt
390234949Sbaptlinkage_specification
391234949Sbapt	: T_EXTERN T_STRING_LITERAL braces
392234949Sbapt	{
393234949Sbapt	    /* Provide an empty action here so bison will not complain about
394234949Sbapt	     * incompatible types in the default action it normally would
395234949Sbapt	     * have generated.
396234949Sbapt	     */
397234949Sbapt	}
398234949Sbapt	| T_EXTERN T_STRING_LITERAL declaration
399234949Sbapt	{
400234949Sbapt	    /* empty */
401234949Sbapt	}
402234949Sbapt	;
403234949Sbapt
404234949Sbaptdeclaration
405234949Sbapt	: decl_specifiers ';'
406234949Sbapt	{
407234949Sbapt#if OPT_LINTLIBRARY
408234949Sbapt	    if (types_out && want_typedef()) {
409234949Sbapt		gen_declarations(&$1, (DeclaratorList *)0);
410234949Sbapt		flush_varargs();
411234949Sbapt	    }
412234949Sbapt#endif
413234949Sbapt	    free_decl_spec(&$1);
414234949Sbapt	    end_typedef();
415234949Sbapt	}
416234949Sbapt	| decl_specifiers init_declarator_list ';'
417234949Sbapt	{
418234949Sbapt	    if (func_params != NULL) {
419234949Sbapt		set_param_types(func_params, &$1, &$2);
420234949Sbapt	    } else {
421234949Sbapt		gen_declarations(&$1, &$2);
422234949Sbapt#if OPT_LINTLIBRARY
423234949Sbapt		flush_varargs();
424234949Sbapt#endif
425234949Sbapt		free_decl_list(&$2);
426234949Sbapt	    }
427234949Sbapt	    free_decl_spec(&$1);
428234949Sbapt	    end_typedef();
429234949Sbapt	}
430234949Sbapt	| any_typedef decl_specifiers
431234949Sbapt	{
432234949Sbapt	    cur_decl_spec_flags = $2.flags;
433234949Sbapt	    free_decl_spec(&$2);
434234949Sbapt	}
435234949Sbapt	  opt_declarator_list ';'
436234949Sbapt	{
437234949Sbapt	    end_typedef();
438234949Sbapt	}
439234949Sbapt	;
440234949Sbapt
441234949Sbaptany_typedef
442234949Sbapt	: T_EXTENSION T_TYPEDEF
443234949Sbapt	{
444234949Sbapt	    begin_typedef();
445234949Sbapt	}
446234949Sbapt	| T_TYPEDEF
447234949Sbapt	{
448234949Sbapt	    begin_typedef();
449234949Sbapt	}
450234949Sbapt	;
451234949Sbapt
452234949Sbaptopt_declarator_list
453234949Sbapt	: /* empty */
454234949Sbapt	| declarator_list
455234949Sbapt	;
456234949Sbapt
457234949Sbaptdeclarator_list
458234949Sbapt	: declarator
459234949Sbapt	{
460234949Sbapt	    int flags = cur_decl_spec_flags;
461234949Sbapt
462234949Sbapt	    /* If the typedef is a pointer type, then reset the short type
463234949Sbapt	     * flags so it does not get promoted.
464234949Sbapt	     */
465234949Sbapt	    if (strcmp($1->text, $1->name) != 0)
466234949Sbapt		flags &= ~(DS_CHAR | DS_SHORT | DS_FLOAT);
467234949Sbapt	    new_symbol(typedef_names, $1->name, NULL, flags);
468234949Sbapt	    free_declarator($1);
469234949Sbapt	}
470234949Sbapt	| declarator_list ',' declarator
471234949Sbapt	{
472234949Sbapt	    int flags = cur_decl_spec_flags;
473234949Sbapt
474234949Sbapt	    if (strcmp($3->text, $3->name) != 0)
475234949Sbapt		flags &= ~(DS_CHAR | DS_SHORT | DS_FLOAT);
476234949Sbapt	    new_symbol(typedef_names, $3->name, NULL, flags);
477234949Sbapt	    free_declarator($3);
478234949Sbapt	}
479234949Sbapt	;
480234949Sbapt
481234949Sbaptfunction_definition
482234949Sbapt	: decl_specifiers declarator
483234949Sbapt	{
484234949Sbapt	    check_untagged(&$1);
485234949Sbapt	    if ($2->func_def == FUNC_NONE) {
486234949Sbapt		yyerror("syntax error");
487234949Sbapt		YYERROR;
488234949Sbapt	    }
489234949Sbapt	    func_params = &($2->head->params);
490234949Sbapt	    func_params->begin_comment = cur_file->begin_comment;
491234949Sbapt	    func_params->end_comment = cur_file->end_comment;
492234949Sbapt	}
493234949Sbapt	  opt_declaration_list T_LBRACE
494234949Sbapt	{
495234949Sbapt	    /* If we're converting to K&R and we've got a nominally K&R
496234949Sbapt	     * function which has a parameter which is ANSI (i.e., a prototyped
497234949Sbapt	     * function pointer), then we must override the deciphered value of
498234949Sbapt	     * 'func_def' so that the parameter will be converted.
499234949Sbapt	     */
500234949Sbapt	    if (func_style == FUNC_TRADITIONAL
501234949Sbapt	     && haveAnsiParam()
502234949Sbapt	     && $2->head->func_def == func_style) {
503234949Sbapt		$2->head->func_def = FUNC_BOTH;
504234949Sbapt	    }
505234949Sbapt
506234949Sbapt	    func_params = NULL;
507234949Sbapt
508234949Sbapt	    if (cur_file->convert)
509234949Sbapt		gen_func_definition(&$1, $2);
510234949Sbapt	    gen_prototype(&$1, $2);
511234949Sbapt#if OPT_LINTLIBRARY
512234949Sbapt	    flush_varargs();
513234949Sbapt#endif
514234949Sbapt	    free_decl_spec(&$1);
515234949Sbapt	    free_declarator($2);
516234949Sbapt	}
517234949Sbapt	  T_MATCHRBRACE
518234949Sbapt	| declarator
519234949Sbapt	{
520234949Sbapt	    if ($1->func_def == FUNC_NONE) {
521234949Sbapt		yyerror("syntax error");
522234949Sbapt		YYERROR;
523234949Sbapt	    }
524234949Sbapt	    func_params = &($1->head->params);
525234949Sbapt	    func_params->begin_comment = cur_file->begin_comment;
526234949Sbapt	    func_params->end_comment = cur_file->end_comment;
527234949Sbapt	}
528234949Sbapt	  opt_declaration_list T_LBRACE T_MATCHRBRACE
529234949Sbapt	{
530234949Sbapt	    DeclSpec decl_spec;
531234949Sbapt
532234949Sbapt	    func_params = NULL;
533234949Sbapt
534234949Sbapt	    new_decl_spec(&decl_spec, dft_decl_spec(), $1->begin, DS_NONE);
535234949Sbapt	    if (cur_file->convert)
536234949Sbapt		gen_func_definition(&decl_spec, $1);
537234949Sbapt	    gen_prototype(&decl_spec, $1);
538234949Sbapt#if OPT_LINTLIBRARY
539234949Sbapt	    flush_varargs();
540234949Sbapt#endif
541234949Sbapt	    free_decl_spec(&decl_spec);
542234949Sbapt	    free_declarator($1);
543234949Sbapt	}
544234949Sbapt	;
545234949Sbapt
546234949Sbaptopt_declaration_list
547234949Sbapt	: /* empty */
548234949Sbapt	| T_VA_DCL
549234949Sbapt	| declaration_list
550234949Sbapt	;
551234949Sbapt
552234949Sbaptdeclaration_list
553234949Sbapt	: declaration
554234949Sbapt	| declaration_list declaration
555234949Sbapt	;
556234949Sbapt
557234949Sbaptdecl_specifiers
558234949Sbapt	: decl_specifier
559234949Sbapt	| decl_specifiers decl_specifier
560234949Sbapt	{
561234949Sbapt	    join_decl_specs(&$$, &$1, &$2);
562234949Sbapt	    free($1.text);
563234949Sbapt	    free($2.text);
564234949Sbapt	}
565234949Sbapt	;
566234949Sbapt
567234949Sbaptdecl_specifier
568234949Sbapt	: storage_class
569234949Sbapt	| type_specifier
570234949Sbapt	| type_qualifier
571234949Sbapt	;
572234949Sbapt
573234949Sbaptstorage_class
574234949Sbapt	: T_AUTO
575234949Sbapt	{
576234949Sbapt	    new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
577234949Sbapt	}
578234949Sbapt	| T_EXTERN
579234949Sbapt	{
580234949Sbapt	    new_decl_spec(&$$, $1.text, $1.begin, DS_EXTERN);
581234949Sbapt	}
582234949Sbapt	| T_REGISTER
583234949Sbapt	{
584234949Sbapt	    new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
585234949Sbapt	}
586234949Sbapt	| T_STATIC
587234949Sbapt	{
588234949Sbapt	    new_decl_spec(&$$, $1.text, $1.begin, DS_STATIC);
589234949Sbapt	}
590234949Sbapt	| T_INLINE
591234949Sbapt	{
592234949Sbapt	    new_decl_spec(&$$, $1.text, $1.begin, DS_INLINE);
593234949Sbapt	}
594234949Sbapt	| T_EXTENSION
595234949Sbapt	{
596234949Sbapt	    new_decl_spec(&$$, $1.text, $1.begin, DS_JUNK);
597234949Sbapt	}
598234949Sbapt	;
599234949Sbapt
600234949Sbapttype_specifier
601234949Sbapt	: T_CHAR
602234949Sbapt	{
603234949Sbapt	    new_decl_spec(&$$, $1.text, $1.begin, DS_CHAR);
604234949Sbapt	}
605234949Sbapt	| T_DOUBLE
606234949Sbapt	{
607234949Sbapt	    new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
608234949Sbapt	}
609234949Sbapt	| T_FLOAT
610234949Sbapt	{
611234949Sbapt	    new_decl_spec(&$$, $1.text, $1.begin, DS_FLOAT);
612234949Sbapt	}
613234949Sbapt	| T_INT
614234949Sbapt	{
615234949Sbapt	    new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
616234949Sbapt	}
617234949Sbapt	| T_LONG
618234949Sbapt	{
619234949Sbapt	    new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
620234949Sbapt	}
621234949Sbapt	| T_SHORT
622234949Sbapt	{
623234949Sbapt	    new_decl_spec(&$$, $1.text, $1.begin, DS_SHORT);
624234949Sbapt	}
625234949Sbapt	| T_SIGNED
626234949Sbapt	{
627234949Sbapt	    new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
628234949Sbapt	}
629234949Sbapt	| T_UNSIGNED
630234949Sbapt	{
631234949Sbapt	    new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
632234949Sbapt	}
633234949Sbapt	| T_VOID
634234949Sbapt	{
635234949Sbapt	    new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
636234949Sbapt	}
637234949Sbapt	| T_Bool
638234949Sbapt	{
639234949Sbapt	    new_decl_spec(&$$, $1.text, $1.begin, DS_CHAR);
640234949Sbapt	}
641234949Sbapt	| T_Complex
642234949Sbapt	{
643234949Sbapt	    new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
644234949Sbapt	}
645234949Sbapt	| T_Imaginary
646234949Sbapt	{
647234949Sbapt	    new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
648234949Sbapt	}
649234949Sbapt	| T_TYPEDEF_NAME
650234949Sbapt	{
651234949Sbapt	    Symbol *s;
652234949Sbapt	    s = find_symbol(typedef_names, $1.text);
653234949Sbapt	    if (s != NULL)
654234949Sbapt		new_decl_spec(&$$, $1.text, $1.begin, s->flags);
655234949Sbapt	}
656234949Sbapt	| struct_or_union_specifier
657234949Sbapt	| enum_specifier
658234949Sbapt	;
659234949Sbapt
660234949Sbapttype_qualifier
661234949Sbapt	: T_TYPE_QUALIFIER
662234949Sbapt	{
663234949Sbapt	    new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
664234949Sbapt	}
665234949Sbapt	| T_DEFINE_NAME
666234949Sbapt	{
667234949Sbapt	    /* This rule allows the <pointer> nonterminal to scan #define
668234949Sbapt	     * names as if they were type modifiers.
669234949Sbapt	     */
670234949Sbapt	    Symbol *s;
671234949Sbapt	    s = find_symbol(define_names, $1.text);
672234949Sbapt	    if (s != NULL)
673234949Sbapt		new_decl_spec(&$$, $1.text, $1.begin, s->flags);
674234949Sbapt	}
675234949Sbapt	;
676234949Sbapt
677234949Sbaptstruct_or_union_specifier
678234949Sbapt	: struct_or_union any_id braces
679234949Sbapt	{
680234949Sbapt	    char *s;
681234949Sbapt	    if ((s = implied_typedef()) == 0)
682234949Sbapt	        (void)sprintf(s = buf, "%s %s", $1.text, $2.text);
683234949Sbapt	    new_decl_spec(&$$, s, $1.begin, DS_NONE);
684234949Sbapt	}
685234949Sbapt	| struct_or_union braces
686234949Sbapt	{
687234949Sbapt	    char *s;
688234949Sbapt	    if ((s = implied_typedef()) == 0)
689234949Sbapt		(void)sprintf(s = buf, "%s {}", $1.text);
690234949Sbapt	    new_decl_spec(&$$, s, $1.begin, DS_NONE);
691234949Sbapt	}
692234949Sbapt	| struct_or_union any_id
693234949Sbapt	{
694234949Sbapt	    (void)sprintf(buf, "%s %s", $1.text, $2.text);
695234949Sbapt	    new_decl_spec(&$$, buf, $1.begin, DS_NONE);
696234949Sbapt	}
697234949Sbapt	;
698234949Sbapt
699234949Sbaptstruct_or_union
700234949Sbapt	: T_STRUCT
701234949Sbapt	{
702234949Sbapt	    imply_typedef($$.text);
703234949Sbapt	}
704234949Sbapt	| T_UNION
705234949Sbapt	{
706234949Sbapt	    imply_typedef($$.text);
707234949Sbapt	}
708234949Sbapt	;
709234949Sbapt
710234949Sbaptinit_declarator_list
711234949Sbapt	: init_declarator
712234949Sbapt	{
713234949Sbapt	    new_decl_list(&$$, $1);
714234949Sbapt	}
715234949Sbapt	| init_declarator_list ',' init_declarator
716234949Sbapt	{
717234949Sbapt	    add_decl_list(&$$, &$1, $3);
718234949Sbapt	}
719234949Sbapt	;
720234949Sbapt
721234949Sbaptinit_declarator
722234949Sbapt	: declarator
723234949Sbapt	{
724234949Sbapt	    if ($1->func_def != FUNC_NONE && func_params == NULL &&
725234949Sbapt		func_style == FUNC_TRADITIONAL && cur_file->convert) {
726234949Sbapt		gen_func_declarator($1);
727234949Sbapt		fputs(cur_text(), cur_file->tmp_file);
728234949Sbapt	    }
729234949Sbapt	    cur_declarator = $$;
730234949Sbapt	}
731234949Sbapt	| declarator '='
732234949Sbapt	{
733234949Sbapt	    if ($1->func_def != FUNC_NONE && func_params == NULL &&
734234949Sbapt		func_style == FUNC_TRADITIONAL && cur_file->convert) {
735234949Sbapt		gen_func_declarator($1);
736234949Sbapt		fputs(" =", cur_file->tmp_file);
737234949Sbapt	    }
738234949Sbapt	}
739234949Sbapt	  T_INITIALIZER
740234949Sbapt	;
741234949Sbapt
742234949Sbaptenum_specifier
743234949Sbapt	: enumeration any_id braces
744234949Sbapt	{
745234949Sbapt	    char *s;
746234949Sbapt	    if ((s = implied_typedef()) == 0)
747234949Sbapt		(void)sprintf(s = buf, "enum %s", $2.text);
748234949Sbapt	    new_decl_spec(&$$, s, $1.begin, DS_NONE);
749234949Sbapt	}
750234949Sbapt	| enumeration braces
751234949Sbapt	{
752234949Sbapt	    char *s;
753234949Sbapt	    if ((s = implied_typedef()) == 0)
754234949Sbapt		(void)sprintf(s = buf, "%s {}", $1.text);
755234949Sbapt	    new_decl_spec(&$$, s, $1.begin, DS_NONE);
756234949Sbapt	}
757234949Sbapt	| enumeration any_id
758234949Sbapt	{
759234949Sbapt	    (void)sprintf(buf, "enum %s", $2.text);
760234949Sbapt	    new_decl_spec(&$$, buf, $1.begin, DS_NONE);
761234949Sbapt	}
762234949Sbapt	;
763234949Sbapt
764234949Sbaptenumeration
765234949Sbapt	: T_ENUM
766234949Sbapt	{
767234949Sbapt	    imply_typedef("enum");
768234949Sbapt	    $$ = $1;
769234949Sbapt	}
770234949Sbapt	;
771234949Sbapt
772234949Sbaptany_id
773234949Sbapt	: T_IDENTIFIER
774234949Sbapt	| T_TYPEDEF_NAME
775234949Sbapt	;
776234949Sbapt
777234949Sbaptdeclarator
778234949Sbapt	: pointer direct_declarator
779234949Sbapt	{
780234949Sbapt	    $$ = $2;
781234949Sbapt	    (void)sprintf(buf, "%s%s", $1.text, $$->text);
782234949Sbapt	    free($$->text);
783234949Sbapt	    $$->text = xstrdup(buf);
784234949Sbapt	    $$->begin = $1.begin;
785234949Sbapt	    $$->pointer = TRUE;
786234949Sbapt	}
787234949Sbapt	| direct_declarator
788234949Sbapt	;
789234949Sbapt
790234949Sbaptdirect_declarator
791234949Sbapt	: identifier_or_ref
792234949Sbapt	{
793234949Sbapt	    $$ = new_declarator($1.text, $1.text, $1.begin);
794234949Sbapt	}
795234949Sbapt	| '(' declarator ')'
796234949Sbapt	{
797234949Sbapt	    $$ = $2;
798234949Sbapt	    (void)sprintf(buf, "(%s)", $$->text);
799234949Sbapt	    free($$->text);
800234949Sbapt	    $$->text = xstrdup(buf);
801234949Sbapt	    $$->begin = $1.begin;
802234949Sbapt	}
803234949Sbapt	| direct_declarator T_BRACKETS
804234949Sbapt	{
805234949Sbapt	    $$ = $1;
806234949Sbapt	    (void)sprintf(buf, "%s%s", $$->text, $2.text);
807234949Sbapt	    free($$->text);
808234949Sbapt	    $$->text = xstrdup(buf);
809234949Sbapt	}
810234949Sbapt	| direct_declarator '(' parameter_type_list ')'
811234949Sbapt	{
812234949Sbapt	    $$ = new_declarator("%s()", $1->name, $1->begin);
813234949Sbapt	    $$->params = $3;
814234949Sbapt	    $$->func_stack = $1;
815234949Sbapt	    $$->head = ($1->func_stack == NULL) ? $$ : $1->head;
816234949Sbapt	    $$->func_def = FUNC_ANSI;
817234949Sbapt	}
818234949Sbapt	| direct_declarator '(' opt_identifier_list ')'
819234949Sbapt	{
820234949Sbapt	    $$ = new_declarator("%s()", $1->name, $1->begin);
821234949Sbapt	    $$->params = $3;
822234949Sbapt	    $$->func_stack = $1;
823234949Sbapt	    $$->head = ($1->func_stack == NULL) ? $$ : $1->head;
824234949Sbapt	    $$->func_def = FUNC_TRADITIONAL;
825234949Sbapt	}
826234949Sbapt	;
827234949Sbapt
828234949Sbaptpointer
829234949Sbapt	: '*' opt_type_qualifiers
830234949Sbapt	{
831234949Sbapt	    (void)sprintf($$.text, "*%s", $2.text);
832234949Sbapt	    $$.begin = $1.begin;
833234949Sbapt	}
834234949Sbapt	| '*' opt_type_qualifiers pointer
835234949Sbapt	{
836234949Sbapt	    (void)sprintf($$.text, "*%s%s", $2.text, $3.text);
837234949Sbapt	    $$.begin = $1.begin;
838234949Sbapt	}
839234949Sbapt	;
840234949Sbapt
841234949Sbaptopt_type_qualifiers
842234949Sbapt	: /* empty */
843234949Sbapt	{
844234949Sbapt	    strcpy($$.text, "");
845234949Sbapt	    $$.begin = 0L;
846234949Sbapt	}
847234949Sbapt	| type_qualifier_list
848234949Sbapt	;
849234949Sbapt
850234949Sbapttype_qualifier_list
851234949Sbapt	: type_qualifier
852234949Sbapt	{
853234949Sbapt	    (void)sprintf($$.text, "%s ", $1.text);
854234949Sbapt	    $$.begin = $1.begin;
855234949Sbapt	    free($1.text);
856234949Sbapt	}
857234949Sbapt	| type_qualifier_list type_qualifier
858234949Sbapt	{
859234949Sbapt	    (void)sprintf($$.text, "%s%s ", $1.text, $2.text);
860234949Sbapt	    $$.begin = $1.begin;
861234949Sbapt	    free($2.text);
862234949Sbapt	}
863234949Sbapt	;
864234949Sbapt
865234949Sbaptparameter_type_list
866234949Sbapt	: parameter_list
867234949Sbapt	| parameter_list ',' T_ELLIPSIS
868234949Sbapt	{
869234949Sbapt	    add_ident_list(&$$, &$1, "...");
870234949Sbapt	}
871234949Sbapt	;
872234949Sbapt
873234949Sbaptparameter_list
874234949Sbapt	: parameter_declaration
875234949Sbapt	{
876234949Sbapt	    new_param_list(&$$, $1);
877234949Sbapt	}
878234949Sbapt	| parameter_list ',' parameter_declaration
879234949Sbapt	{
880234949Sbapt	    add_param_list(&$$, &$1, $3);
881234949Sbapt	}
882234949Sbapt	;
883234949Sbapt
884234949Sbaptparameter_declaration
885234949Sbapt	: decl_specifiers declarator
886234949Sbapt	{
887234949Sbapt	    check_untagged(&$1);
888234949Sbapt	    $$ = new_parameter(&$1, $2);
889234949Sbapt	}
890234949Sbapt	| decl_specifiers abs_declarator
891234949Sbapt	{
892234949Sbapt	    check_untagged(&$1);
893234949Sbapt	    $$ = new_parameter(&$1, $2);
894234949Sbapt	}
895234949Sbapt	| decl_specifiers
896234949Sbapt	{
897234949Sbapt	    check_untagged(&$1);
898234949Sbapt	    $$ = new_parameter(&$1, (Declarator *)0);
899234949Sbapt	}
900234949Sbapt	;
901234949Sbapt
902234949Sbaptopt_identifier_list
903234949Sbapt	: /* empty */
904234949Sbapt	{
905234949Sbapt	    new_ident_list(&$$);
906234949Sbapt	}
907234949Sbapt	| identifier_list
908234949Sbapt	;
909234949Sbapt
910234949Sbaptidentifier_list
911234949Sbapt	: any_id
912234949Sbapt	{
913234949Sbapt	    new_ident_list(&$$);
914234949Sbapt	    add_ident_list(&$$, &$$, $1.text);
915234949Sbapt	}
916234949Sbapt	| identifier_list ',' any_id
917234949Sbapt	{
918234949Sbapt	    add_ident_list(&$$, &$1, $3.text);
919234949Sbapt	}
920234949Sbapt	;
921234949Sbapt
922234949Sbaptidentifier_or_ref
923234949Sbapt	: any_id
924234949Sbapt	{
925234949Sbapt	    $$ = $1;
926234949Sbapt	}
927234949Sbapt	| '&' any_id
928234949Sbapt	{
929234949Sbapt#if OPT_LINTLIBRARY
930234949Sbapt	    if (lintLibrary()) { /* Lint doesn't grok C++ ref variables */
931234949Sbapt		$$ = $2;
932234949Sbapt	    } else
933234949Sbapt#endif
934234949Sbapt		(void)sprintf($$.text, "&%s", $2.text);
935234949Sbapt	    $$.begin = $1.begin;
936234949Sbapt	}
937234949Sbapt	;
938234949Sbapt
939234949Sbaptabs_declarator
940234949Sbapt	: pointer
941234949Sbapt	{
942234949Sbapt	    $$ = new_declarator($1.text, "", $1.begin);
943234949Sbapt	}
944234949Sbapt	| pointer direct_abs_declarator
945234949Sbapt	{
946234949Sbapt	    $$ = $2;
947234949Sbapt	    (void)sprintf(buf, "%s%s", $1.text, $$->text);
948234949Sbapt	    free($$->text);
949234949Sbapt	    $$->text = xstrdup(buf);
950234949Sbapt	    $$->begin = $1.begin;
951234949Sbapt	}
952234949Sbapt	| direct_abs_declarator
953234949Sbapt	;
954234949Sbapt
955234949Sbaptdirect_abs_declarator
956234949Sbapt	: '(' abs_declarator ')'
957234949Sbapt	{
958234949Sbapt	    $$ = $2;
959234949Sbapt	    (void)sprintf(buf, "(%s)", $$->text);
960234949Sbapt	    free($$->text);
961234949Sbapt	    $$->text = xstrdup(buf);
962234949Sbapt	    $$->begin = $1.begin;
963234949Sbapt	}
964234949Sbapt	| direct_abs_declarator T_BRACKETS
965234949Sbapt	{
966234949Sbapt	    $$ = $1;
967234949Sbapt	    (void)sprintf(buf, "%s%s", $$->text, $2.text);
968234949Sbapt	    free($$->text);
969234949Sbapt	    $$->text = xstrdup(buf);
970234949Sbapt	}
971234949Sbapt	| T_BRACKETS
972234949Sbapt	{
973234949Sbapt	    $$ = new_declarator($1.text, "", $1.begin);
974234949Sbapt	}
975234949Sbapt	| direct_abs_declarator '(' parameter_type_list ')'
976234949Sbapt	{
977234949Sbapt	    $$ = new_declarator("%s()", "", $1->begin);
978234949Sbapt	    $$->params = $3;
979234949Sbapt	    $$->func_stack = $1;
980234949Sbapt	    $$->head = ($1->func_stack == NULL) ? $$ : $1->head;
981234949Sbapt	    $$->func_def = FUNC_ANSI;
982234949Sbapt	}
983234949Sbapt	| direct_abs_declarator '(' ')'
984234949Sbapt	{
985234949Sbapt	    $$ = new_declarator("%s()", "", $1->begin);
986234949Sbapt	    $$->func_stack = $1;
987234949Sbapt	    $$->head = ($1->func_stack == NULL) ? $$ : $1->head;
988234949Sbapt	    $$->func_def = FUNC_ANSI;
989234949Sbapt	}
990234949Sbapt	| '(' parameter_type_list ')'
991234949Sbapt	{
992234949Sbapt	    Declarator *d;
993234949Sbapt
994234949Sbapt	    d = new_declarator("", "", $1.begin);
995234949Sbapt	    $$ = new_declarator("%s()", "", $1.begin);
996234949Sbapt	    $$->params = $2;
997234949Sbapt	    $$->func_stack = d;
998234949Sbapt	    $$->head = $$;
999234949Sbapt	    $$->func_def = FUNC_ANSI;
1000234949Sbapt	}
1001234949Sbapt	| '(' ')'
1002234949Sbapt	{
1003234949Sbapt	    Declarator *d;
1004234949Sbapt
1005234949Sbapt	    d = new_declarator("", "", $1.begin);
1006234949Sbapt	    $$ = new_declarator("%s()", "", $1.begin);
1007234949Sbapt	    $$->func_stack = d;
1008234949Sbapt	    $$->head = $$;
1009234949Sbapt	    $$->func_def = FUNC_ANSI;
1010234949Sbapt	}
1011234949Sbapt	;
1012234949Sbapt
1013234949Sbapt%%
1014234949Sbapt
1015234949Sbapt/* lex.yy.c */
1016234949Sbapt#define BEGIN yy_start = 1 + 2 *
1017234949Sbapt
1018234949Sbapt#define CPP1 1
1019234949Sbapt#define INIT1 2
1020234949Sbapt#define INIT2 3
1021234949Sbapt#define CURLY 4
1022234949Sbapt#define LEXYACC 5
1023234949Sbapt#define ASM 6
1024234949Sbapt#define CPP_INLINE 7
1025234949Sbapt
1026234949Sbaptextern char *yytext;
1027234949Sbaptextern FILE *yyin, *yyout;
1028234949Sbapt
1029234949Sbaptstatic int curly;			/* number of curly brace nesting levels */
1030234949Sbaptstatic int ly_count;			/* number of occurances of %% */
1031234949Sbaptstatic int inc_depth;			/* include nesting level */
1032234949Sbaptstatic SymbolTable *included_files;	/* files already included */
1033234949Sbaptstatic int yy_start = 0;		/* start state number */
1034234949Sbapt
1035234949Sbapt#define grammar_error(s) yaccError(s)
1036234949Sbapt
1037234949Sbaptstatic void
1038234949SbaptyaccError (const char *msg)
1039234949Sbapt{
1040234949Sbapt    func_params = NULL;
1041234949Sbapt    put_error();		/* tell what line we're on, and what file */
1042234949Sbapt    fprintf(stderr, "%s at token '%s'\n", msg, yytext);
1043234949Sbapt}
1044234949Sbapt
1045234949Sbapt/* Initialize the table of type qualifier keywords recognized by the lexical
1046234949Sbapt * analyzer.
1047234949Sbapt */
1048234949Sbaptvoid
1049234949Sbaptinit_parser (void)
1050234949Sbapt{
1051234949Sbapt    static const char *keywords[] = {
1052234949Sbapt	"const",
1053234949Sbapt	"restrict",
1054234949Sbapt	"volatile",
1055234949Sbapt	"interrupt",
1056234949Sbapt#ifdef vms
1057234949Sbapt	"noshare",
1058234949Sbapt	"readonly",
1059234949Sbapt#endif
1060234949Sbapt#if defined(MSDOS) || defined(OS2)
1061234949Sbapt	"__cdecl",
1062234949Sbapt	"__export",
1063234949Sbapt	"__far",
1064234949Sbapt	"__fastcall",
1065234949Sbapt	"__fortran",
1066234949Sbapt	"__huge",
1067234949Sbapt	"__inline",
1068234949Sbapt	"__interrupt",
1069234949Sbapt	"__loadds",
1070234949Sbapt	"__near",
1071234949Sbapt	"__pascal",
1072234949Sbapt	"__saveregs",
1073234949Sbapt	"__segment",
1074234949Sbapt	"__stdcall",
1075234949Sbapt	"__syscall",
1076234949Sbapt	"_cdecl",
1077234949Sbapt	"_cs",
1078234949Sbapt	"_ds",
1079234949Sbapt	"_es",
1080234949Sbapt	"_export",
1081234949Sbapt	"_far",
1082234949Sbapt	"_fastcall",
1083234949Sbapt	"_fortran",
1084234949Sbapt	"_huge",
1085234949Sbapt	"_interrupt",
1086234949Sbapt	"_loadds",
1087234949Sbapt	"_near",
1088234949Sbapt	"_pascal",
1089234949Sbapt	"_saveregs",
1090234949Sbapt	"_seg",
1091234949Sbapt	"_segment",
1092234949Sbapt	"_ss",
1093234949Sbapt	"cdecl",
1094234949Sbapt	"far",
1095234949Sbapt	"huge",
1096234949Sbapt	"near",
1097234949Sbapt	"pascal",
1098234949Sbapt#ifdef OS2
1099234949Sbapt	"__far16",
1100234949Sbapt#endif
1101234949Sbapt#endif
1102234949Sbapt#ifdef __GNUC__
1103234949Sbapt	/* gcc aliases */
1104234949Sbapt	"__builtin_va_arg",
1105234949Sbapt	"__builtin_va_list",
1106234949Sbapt	"__const",
1107234949Sbapt	"__const__",
1108234949Sbapt	"__inline",
1109234949Sbapt	"__inline__",
1110234949Sbapt	"__restrict",
1111234949Sbapt	"__restrict__",
1112234949Sbapt	"__volatile",
1113234949Sbapt	"__volatile__",
1114234949Sbapt#endif
1115234949Sbapt    };
1116234949Sbapt    unsigned i;
1117234949Sbapt
1118234949Sbapt    /* Initialize type qualifier table. */
1119234949Sbapt    type_qualifiers = new_symbol_table();
1120234949Sbapt    for (i = 0; i < sizeof(keywords)/sizeof(keywords[0]); ++i) {
1121234949Sbapt	new_symbol(type_qualifiers, keywords[i], NULL, DS_NONE);
1122234949Sbapt    }
1123234949Sbapt}
1124234949Sbapt
1125234949Sbapt/* Process the C source file.  Write function prototypes to the standard
1126234949Sbapt * output.  Convert function definitions and write the converted source
1127234949Sbapt * code to a temporary file.
1128234949Sbapt */
1129234949Sbaptvoid
1130234949Sbaptprocess_file (FILE *infile, char *name)
1131234949Sbapt{
1132234949Sbapt    char *s;
1133234949Sbapt
1134234949Sbapt    if (strlen(name) > 2) {
1135234949Sbapt	s = name + strlen(name) - 2;
1136234949Sbapt	if (*s == '.') {
1137234949Sbapt	    ++s;
1138234949Sbapt	    if (*s == 'l' || *s == 'y')
1139234949Sbapt		BEGIN LEXYACC;
1140234949Sbapt#if defined(MSDOS) || defined(OS2)
1141234949Sbapt	    if (*s == 'L' || *s == 'Y')
1142234949Sbapt		BEGIN LEXYACC;
1143234949Sbapt#endif
1144234949Sbapt	}
1145234949Sbapt    }
1146234949Sbapt
1147234949Sbapt    included_files = new_symbol_table();
1148234949Sbapt    typedef_names = new_symbol_table();
1149234949Sbapt    define_names = new_symbol_table();
1150234949Sbapt    inc_depth = -1;
1151234949Sbapt    curly = 0;
1152234949Sbapt    ly_count = 0;
1153234949Sbapt    func_params = NULL;
1154234949Sbapt    yyin = infile;
1155234949Sbapt    include_file(strcpy(base_file, name), func_style != FUNC_NONE);
1156234949Sbapt    if (file_comments) {
1157234949Sbapt#if OPT_LINTLIBRARY
1158234949Sbapt    	if (lintLibrary()) {
1159234949Sbapt	    put_blankline(stdout);
1160234949Sbapt	    begin_tracking();
1161234949Sbapt	}
1162234949Sbapt#endif
1163234949Sbapt	put_string(stdout, "/* ");
1164234949Sbapt	put_string(stdout, cur_file_name());
1165234949Sbapt	put_string(stdout, " */\n");
1166234949Sbapt    }
1167234949Sbapt    yyparse();
1168234949Sbapt    free_symbol_table(define_names);
1169234949Sbapt    free_symbol_table(typedef_names);
1170234949Sbapt    free_symbol_table(included_files);
1171234949Sbapt}
1172234949Sbapt
1173234949Sbapt#ifdef NO_LEAKS
1174234949Sbaptvoid
1175234949Sbaptfree_parser(void)
1176234949Sbapt{
1177234949Sbapt    free_symbol_table (type_qualifiers);
1178234949Sbapt#ifdef FLEX_SCANNER
1179234949Sbapt    if (yy_current_buffer != 0)
1180234949Sbapt	yy_delete_buffer(yy_current_buffer);
1181234949Sbapt#endif
1182234949Sbapt}
1183234949Sbapt#endif
1184