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