1 /* $Id: grammar.y,v 1.5 2012/01/15 20:00:59 tom Exp $
3 * yacc grammar for C function prototype generator
4 * This was derived from the grammar in Appendix A of
5 * "The C Programming Language" by Kernighan and Ritchie.
11 #define YYSTYPE_IS_DECLARED
12 #define yyerror yaccError
15 #if defined(YYBISON) || !defined(YYBYACC)
16 static void yyerror(const char *s);
20 %token <text> '(' '*' '&'
21 /* identifiers that are not reserved words */
22 T_IDENTIFIER T_TYPEDEF_NAME T_DEFINE_NAME
25 T_AUTO T_EXTERN T_REGISTER T_STATIC T_TYPEDEF
26 /* This keyword included for compatibility with C++. */
28 /* This keyword included for compatibility with GCC */
32 T_CHAR T_DOUBLE T_FLOAT T_INT T_VOID
33 T_LONG T_SHORT T_SIGNED T_UNSIGNED
34 T_ENUM T_STRUCT T_UNION
36 T_Bool T_Complex T_Imaginary
41 /* paired square brackets and everything between them: [ ... ] */
47 /* all input to the matching right brace */
53 /* constant expression or paired braces following an equal sign */
61 /* ( "string literal" ) following asm keyword */
64 /* va_dcl from <varargs.h> */
67 %type <decl_spec> decl_specifiers decl_specifier
68 %type <decl_spec> storage_class type_specifier type_qualifier
69 %type <decl_spec> struct_or_union_specifier enum_specifier
70 %type <decl_list> init_declarator_list
71 %type <declarator> init_declarator declarator direct_declarator
72 %type <declarator> abs_declarator direct_abs_declarator
73 %type <param_list> parameter_type_list parameter_list
74 %type <parameter> parameter_declaration
75 %type <param_list> opt_identifier_list identifier_list
76 %type <text> struct_or_union pointer opt_type_qualifiers type_qualifier_list
77 any_id identifier_or_ref
78 %type <text> enumeration
85 #define OPT_LINTLIBRARY 1
95 /* #include "cproto.h" */
96 #define MAX_TEXT_SIZE 1024
98 /* Prototype styles */
100 #define PROTO_ANSI_LLIB -2 /* form ANSI lint-library source */
101 #define PROTO_LINTLIBRARY -1 /* form lint-library source */
103 #define PROTO_NONE 0 /* do not output any prototypes */
104 #define PROTO_TRADITIONAL 1 /* comment out parameters */
105 #define PROTO_ABSTRACT 2 /* comment out parameter names */
106 #define PROTO_ANSI 3 /* ANSI C prototype */
108 typedef int PrototypeStyle;
110 typedef char boolean;
112 extern boolean types_out;
113 extern PrototypeStyle proto_style;
115 #define ansiLintLibrary() (proto_style == PROTO_ANSI_LLIB)
116 #define knrLintLibrary() (proto_style == PROTO_LINTLIBRARY)
117 #define lintLibrary() (knrLintLibrary() || ansiLintLibrary())
120 #define FUNC_UNKNOWN -1 /* unspecified */
122 #define FUNC_UNKNOWN 0 /* unspecified (same as FUNC_NONE) */
124 #define FUNC_NONE 0 /* not a function definition */
125 #define FUNC_TRADITIONAL 1 /* traditional style */
126 #define FUNC_ANSI 2 /* ANSI style */
127 #define FUNC_BOTH 3 /* both styles */
129 typedef int FuncDefStyle;
131 /* Source file text */
132 typedef struct text {
133 char text[MAX_TEXT_SIZE]; /* source text */
134 long begin; /* offset in temporary file */
137 /* Declaration specifier flags */
138 #define DS_NONE 0 /* default */
139 #define DS_EXTERN 1 /* contains "extern" specifier */
140 #define DS_STATIC 2 /* contains "static" specifier */
141 #define DS_CHAR 4 /* contains "char" type specifier */
142 #define DS_SHORT 8 /* contains "short" type specifier */
143 #define DS_FLOAT 16 /* contains "float" type specifier */
144 #define DS_INLINE 32 /* contains "inline" specifier */
145 #define DS_JUNK 64 /* we're not interested in this declaration */
147 /* This structure stores information about a declaration specifier. */
148 typedef struct decl_spec {
149 unsigned short flags; /* flags defined above */
150 char *text; /* source text */
151 long begin; /* offset in temporary file */
154 /* This is a list of function parameters. */
155 typedef struct _ParameterList {
156 struct parameter *first; /* pointer to first parameter in list */
157 struct parameter *last; /* pointer to last parameter in list */
158 long begin_comment; /* begin offset of comment */
159 long end_comment; /* end offset of comment */
160 char *comment; /* comment at start of parameter list */
163 /* This structure stores information about a declarator. */
164 typedef struct _Declarator {
165 char *name; /* name of variable or function */
166 char *text; /* source text */
167 long begin; /* offset in temporary file */
168 long begin_comment; /* begin offset of comment */
169 long end_comment; /* end offset of comment */
170 FuncDefStyle func_def; /* style of function definition */
171 ParameterList params; /* function parameters */
172 boolean pointer; /* TRUE if it declares a pointer */
173 struct _Declarator *head; /* head function declarator */
174 struct _Declarator *func_stack; /* stack of function declarators */
175 struct _Declarator *next; /* next declarator in list */
178 /* This structure stores information about a function parameter. */
179 typedef struct parameter {
180 struct parameter *next; /* next parameter in list */
182 Declarator *declarator;
183 char *comment; /* comment following the parameter */
186 /* This is a list of declarators. */
187 typedef struct declarator_list {
188 Declarator *first; /* pointer to first declarator in list */
189 Declarator *last; /* pointer to last declarator in list */
192 /* #include "symbol.h" */
193 typedef struct symbol {
194 struct symbol *next; /* next symbol in list */
195 char *name; /* name of symbol */
196 char *value; /* value of symbol (for defines) */
197 short flags; /* symbol attributes */
200 /* parser stack entry type */
204 Parameter *parameter;
205 ParameterList param_list;
206 Declarator *declarator;
207 DeclaratorList decl_list;
210 /* The hash table length should be a prime number. */
211 #define SYM_MAX_HASH 251
213 typedef struct symbol_table {
214 Symbol *bucket[SYM_MAX_HASH]; /* hash buckets */
217 extern SymbolTable *new_symbol_table /* Create symbol table */
219 extern void free_symbol_table /* Destroy symbol table */
221 extern Symbol *find_symbol /* Lookup symbol name */
222 (SymbolTable *s, const char *n);
223 extern Symbol *new_symbol /* Define new symbol */
224 (SymbolTable *s, const char *n, const char *v, int f);
226 /* #include "semantic.h" */
227 extern void new_decl_spec (DeclSpec *, const char *, long, int);
228 extern void free_decl_spec (DeclSpec *);
229 extern void join_decl_specs (DeclSpec *, DeclSpec *, DeclSpec *);
230 extern void check_untagged (DeclSpec *);
231 extern Declarator *new_declarator (const char *, const char *, long);
232 extern void free_declarator (Declarator *);
233 extern void new_decl_list (DeclaratorList *, Declarator *);
234 extern void free_decl_list (DeclaratorList *);
235 extern void add_decl_list (DeclaratorList *, DeclaratorList *, Declarator *);
236 extern Parameter *new_parameter (DeclSpec *, Declarator *);
237 extern void free_parameter (Parameter *);
238 extern void new_param_list (ParameterList *, Parameter *);
239 extern void free_param_list (ParameterList *);
240 extern void add_param_list (ParameterList *, ParameterList *, Parameter *);
241 extern void new_ident_list (ParameterList *);
242 extern void add_ident_list (ParameterList *, ParameterList *, const char *);
243 extern void set_param_types (ParameterList *, DeclSpec *, DeclaratorList *);
244 extern void gen_declarations (DeclSpec *, DeclaratorList *);
245 extern void gen_prototype (DeclSpec *, Declarator *);
246 extern void gen_func_declarator (Declarator *);
247 extern void gen_func_definition (DeclSpec *, Declarator *);
249 extern void init_parser (void);
250 extern void process_file (FILE *infile, char *name);
251 extern char *cur_text (void);
252 extern char *cur_file_name (void);
253 extern char *implied_typedef (void);
254 extern void include_file (char *name, int convert);
255 extern char *supply_parm (int count);
256 extern char *xstrdup (const char *);
257 extern int already_declared (char *name);
258 extern int is_actual_func (Declarator *d);
259 extern int lint_ellipsis (Parameter *p);
260 extern int want_typedef (void);
261 extern void begin_tracking (void);
262 extern void begin_typedef (void);
263 extern void copy_typedef (char *s);
264 extern void ellipsis_varargs (Declarator *d);
265 extern void end_typedef (void);
266 extern void flush_varargs (void);
267 extern void fmt_library (int code);
268 extern void imply_typedef (const char *s);
269 extern void indent (FILE *outf);
270 extern void put_blankline (FILE *outf);
271 extern void put_body (FILE *outf, DeclSpec *decl_spec, Declarator *declarator);
272 extern void put_char (FILE *outf, int c);
273 extern void put_error (void);
274 extern void put_newline (FILE *outf);
275 extern void put_padded (FILE *outf, const char *s);
276 extern void put_string (FILE *outf, const char *s);
277 extern void track_in (void);
279 extern boolean file_comments;
280 extern FuncDefStyle func_style;
281 extern char base_file[];
283 extern int yylex (void);
285 /* declaration specifier attributes for the typedef statement currently being
288 static int cur_decl_spec_flags;
290 /* pointer to parameter list for the current function definition */
291 static ParameterList *func_params;
293 /* A parser semantic action sets this pointer to the current declarator in
294 * a function parameter declaration in order to catch any comments following
295 * the parameter declaration on the same line. If the lexer scans a comment
296 * and <cur_declarator> is not NULL, then the comment is attached to the
297 * declarator. To ignore subsequent comments, the lexer sets this to NULL
298 * after scanning a comment or end of line.
300 static Declarator *cur_declarator;
302 /* temporary string buffer */
303 static char buf[MAX_TEXT_SIZE];
305 /* table of typedef names */
306 static SymbolTable *typedef_names;
308 /* table of define names */
309 static SymbolTable *define_names;
311 /* table of type qualifiers */
312 static SymbolTable *type_qualifiers;
314 /* information about the current input file */
316 char *base_name; /* base input file name */
317 char *file_name; /* current file name */
318 FILE *file; /* input file */
319 unsigned line_num; /* current line number in input file */
320 FILE *tmp_file; /* temporary file */
321 long begin_comment; /* tmp file offset after last written ) or ; */
322 long end_comment; /* tmp file offset after last comment */
323 boolean convert; /* if TRUE, convert function definitions */
324 boolean changed; /* TRUE if conversion done in this file */
327 static IncludeStack *cur_file; /* current input file */
329 /* #include "yyerror.c" */
331 static int haveAnsiParam (void);
334 /* Flags to enable us to find if a procedure returns a value.
336 static int return_val; /* nonzero on BRACES iff return-expression found */
341 return (lintLibrary() && !return_val) ? "void" : "int";
348 if (func_params != 0) {
349 for (p = func_params->first; p != 0; p = p->next) {
350 if (p->declarator->func_def == FUNC_ANSI) {
366 : external_declaration
367 | translation_unit external_declaration
372 | function_definition
374 | linkage_specification
376 | error T_MATCHRBRACE
387 : T_LBRACE T_MATCHRBRACE
390 linkage_specification
391 : T_EXTERN T_STRING_LITERAL braces
393 /* Provide an empty action here so bison will not complain about
394 * incompatible types in the default action it normally would
398 | T_EXTERN T_STRING_LITERAL declaration
405 : decl_specifiers ';'
408 if (types_out && want_typedef()) {
409 gen_declarations(&$1, (DeclaratorList *)0);
416 | decl_specifiers init_declarator_list ';'
418 if (func_params != NULL) {
419 set_param_types(func_params, &$1, &$2);
421 gen_declarations(&$1, &$2);
430 | any_typedef decl_specifiers
432 cur_decl_spec_flags = $2.flags;
435 opt_declarator_list ';'
442 : T_EXTENSION T_TYPEDEF
460 int flags = cur_decl_spec_flags;
462 /* If the typedef is a pointer type, then reset the short type
463 * flags so it does not get promoted.
465 if (strcmp($1->text, $1->name) != 0)
466 flags &= ~(DS_CHAR | DS_SHORT | DS_FLOAT);
467 new_symbol(typedef_names, $1->name, NULL, flags);
470 | declarator_list ',' declarator
472 int flags = cur_decl_spec_flags;
474 if (strcmp($3->text, $3->name) != 0)
475 flags &= ~(DS_CHAR | DS_SHORT | DS_FLOAT);
476 new_symbol(typedef_names, $3->name, NULL, flags);
482 : decl_specifiers declarator
485 if ($2->func_def == FUNC_NONE) {
486 yyerror("syntax error");
489 func_params = &($2->head->params);
490 func_params->begin_comment = cur_file->begin_comment;
491 func_params->end_comment = cur_file->end_comment;
493 opt_declaration_list T_LBRACE
495 /* If we're converting to K&R and we've got a nominally K&R
496 * function which has a parameter which is ANSI (i.e., a prototyped
497 * function pointer), then we must override the deciphered value of
498 * 'func_def' so that the parameter will be converted.
500 if (func_style == FUNC_TRADITIONAL
502 && $2->head->func_def == func_style) {
503 $2->head->func_def = FUNC_BOTH;
508 if (cur_file->convert)
509 gen_func_definition(&$1, $2);
510 gen_prototype(&$1, $2);
520 if ($1->func_def == FUNC_NONE) {
521 yyerror("syntax error");
524 func_params = &($1->head->params);
525 func_params->begin_comment = cur_file->begin_comment;
526 func_params->end_comment = cur_file->end_comment;
528 opt_declaration_list T_LBRACE T_MATCHRBRACE
534 new_decl_spec(&decl_spec, dft_decl_spec(), $1->begin, DS_NONE);
535 if (cur_file->convert)
536 gen_func_definition(&decl_spec, $1);
537 gen_prototype(&decl_spec, $1);
541 free_decl_spec(&decl_spec);
554 | declaration_list declaration
559 | decl_specifiers decl_specifier
561 join_decl_specs(&$$, &$1, &$2);
576 new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
580 new_decl_spec(&$$, $1.text, $1.begin, DS_EXTERN);
584 new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
588 new_decl_spec(&$$, $1.text, $1.begin, DS_STATIC);
592 new_decl_spec(&$$, $1.text, $1.begin, DS_INLINE);
596 new_decl_spec(&$$, $1.text, $1.begin, DS_JUNK);
603 new_decl_spec(&$$, $1.text, $1.begin, DS_CHAR);
607 new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
611 new_decl_spec(&$$, $1.text, $1.begin, DS_FLOAT);
615 new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
619 new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
623 new_decl_spec(&$$, $1.text, $1.begin, DS_SHORT);
627 new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
631 new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
635 new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
639 new_decl_spec(&$$, $1.text, $1.begin, DS_CHAR);
643 new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
647 new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
652 s = find_symbol(typedef_names, $1.text);
654 new_decl_spec(&$$, $1.text, $1.begin, s->flags);
656 | struct_or_union_specifier
663 new_decl_spec(&$$, $1.text, $1.begin, DS_NONE);
667 /* This rule allows the <pointer> nonterminal to scan #define
668 * names as if they were type modifiers.
671 s = find_symbol(define_names, $1.text);
673 new_decl_spec(&$$, $1.text, $1.begin, s->flags);
677 struct_or_union_specifier
678 : struct_or_union any_id braces
681 if ((s = implied_typedef()) == 0)
682 (void)sprintf(s = buf, "%s %s", $1.text, $2.text);
683 new_decl_spec(&$$, s, $1.begin, DS_NONE);
685 | struct_or_union braces
688 if ((s = implied_typedef()) == 0)
689 (void)sprintf(s = buf, "%s {}", $1.text);
690 new_decl_spec(&$$, s, $1.begin, DS_NONE);
692 | struct_or_union any_id
694 (void)sprintf(buf, "%s %s", $1.text, $2.text);
695 new_decl_spec(&$$, buf, $1.begin, DS_NONE);
702 imply_typedef($$.text);
706 imply_typedef($$.text);
713 new_decl_list(&$$, $1);
715 | init_declarator_list ',' init_declarator
717 add_decl_list(&$$, &$1, $3);
724 if ($1->func_def != FUNC_NONE && func_params == NULL &&
725 func_style == FUNC_TRADITIONAL && cur_file->convert) {
726 gen_func_declarator($1);
727 fputs(cur_text(), cur_file->tmp_file);
733 if ($1->func_def != FUNC_NONE && func_params == NULL &&
734 func_style == FUNC_TRADITIONAL && cur_file->convert) {
735 gen_func_declarator($1);
736 fputs(" =", cur_file->tmp_file);
743 : enumeration any_id braces
746 if ((s = implied_typedef()) == 0)
747 (void)sprintf(s = buf, "enum %s", $2.text);
748 new_decl_spec(&$$, s, $1.begin, DS_NONE);
753 if ((s = implied_typedef()) == 0)
754 (void)sprintf(s = buf, "%s {}", $1.text);
755 new_decl_spec(&$$, s, $1.begin, DS_NONE);
759 (void)sprintf(buf, "enum %s", $2.text);
760 new_decl_spec(&$$, buf, $1.begin, DS_NONE);
767 imply_typedef("enum");
778 : pointer direct_declarator
781 (void)sprintf(buf, "%s%s", $1.text, $$->text);
783 $$->text = xstrdup(buf);
784 $$->begin = $1.begin;
793 $$ = new_declarator($1.text, $1.text, $1.begin);
798 (void)sprintf(buf, "(%s)", $$->text);
800 $$->text = xstrdup(buf);
801 $$->begin = $1.begin;
803 | direct_declarator T_BRACKETS
806 (void)sprintf(buf, "%s%s", $$->text, $2.text);
808 $$->text = xstrdup(buf);
810 | direct_declarator '(' parameter_type_list ')'
812 $$ = new_declarator("%s()", $1->name, $1->begin);
815 $$->head = ($1->func_stack == NULL) ? $$ : $1->head;
816 $$->func_def = FUNC_ANSI;
818 | direct_declarator '(' opt_identifier_list ')'
820 $$ = new_declarator("%s()", $1->name, $1->begin);
823 $$->head = ($1->func_stack == NULL) ? $$ : $1->head;
824 $$->func_def = FUNC_TRADITIONAL;
829 : '*' opt_type_qualifiers
831 (void)sprintf($$.text, "*%s", $2.text);
834 | '*' opt_type_qualifiers pointer
836 (void)sprintf($$.text, "*%s%s", $2.text, $3.text);
847 | type_qualifier_list
853 (void)sprintf($$.text, "%s ", $1.text);
857 | type_qualifier_list type_qualifier
859 (void)sprintf($$.text, "%s%s ", $1.text, $2.text);
867 | parameter_list ',' T_ELLIPSIS
869 add_ident_list(&$$, &$1, "...");
874 : parameter_declaration
876 new_param_list(&$$, $1);
878 | parameter_list ',' parameter_declaration
880 add_param_list(&$$, &$1, $3);
884 parameter_declaration
885 : decl_specifiers declarator
888 $$ = new_parameter(&$1, $2);
890 | decl_specifiers abs_declarator
893 $$ = new_parameter(&$1, $2);
898 $$ = new_parameter(&$1, (Declarator *)0);
914 add_ident_list(&$$, &$$, $1.text);
916 | identifier_list ',' any_id
918 add_ident_list(&$$, &$1, $3.text);
930 if (lintLibrary()) { /* Lint doesn't grok C++ ref variables */
934 (void)sprintf($$.text, "&%s", $2.text);
942 $$ = new_declarator($1.text, "", $1.begin);
944 | pointer direct_abs_declarator
947 (void)sprintf(buf, "%s%s", $1.text, $$->text);
949 $$->text = xstrdup(buf);
950 $$->begin = $1.begin;
952 | direct_abs_declarator
955 direct_abs_declarator
956 : '(' abs_declarator ')'
959 (void)sprintf(buf, "(%s)", $$->text);
961 $$->text = xstrdup(buf);
962 $$->begin = $1.begin;
964 | direct_abs_declarator T_BRACKETS
967 (void)sprintf(buf, "%s%s", $$->text, $2.text);
969 $$->text = xstrdup(buf);
973 $$ = new_declarator($1.text, "", $1.begin);
975 | direct_abs_declarator '(' parameter_type_list ')'
977 $$ = new_declarator("%s()", "", $1->begin);
980 $$->head = ($1->func_stack == NULL) ? $$ : $1->head;
981 $$->func_def = FUNC_ANSI;
983 | direct_abs_declarator '(' ')'
985 $$ = new_declarator("%s()", "", $1->begin);
987 $$->head = ($1->func_stack == NULL) ? $$ : $1->head;
988 $$->func_def = FUNC_ANSI;
990 | '(' parameter_type_list ')'
994 d = new_declarator("", "", $1.begin);
995 $$ = new_declarator("%s()", "", $1.begin);
999 $$->func_def = FUNC_ANSI;
1005 d = new_declarator("", "", $1.begin);
1006 $$ = new_declarator("%s()", "", $1.begin);
1009 $$->func_def = FUNC_ANSI;
1016 #define BEGIN yy_start = 1 + 2 *
1024 #define CPP_INLINE 7
1026 extern char *yytext;
1027 extern FILE *yyin, *yyout;
1029 static int curly; /* number of curly brace nesting levels */
1030 static int ly_count; /* number of occurances of %% */
1031 static int inc_depth; /* include nesting level */
1032 static SymbolTable *included_files; /* files already included */
1033 static int yy_start = 0; /* start state number */
1035 #define grammar_error(s) yaccError(s)
1038 yaccError (const char *msg)
1041 put_error(); /* tell what line we're on, and what file */
1042 fprintf(stderr, "%s at token '%s'\n", msg, yytext);
1045 /* Initialize the table of type qualifier keywords recognized by the lexical
1051 static const char *keywords[] = {
1060 #if defined(MSDOS) || defined(OS2)
1105 "__builtin_va_list",
1118 /* Initialize type qualifier table. */
1119 type_qualifiers = new_symbol_table();
1120 for (i = 0; i < sizeof(keywords)/sizeof(keywords[0]); ++i) {
1121 new_symbol(type_qualifiers, keywords[i], NULL, DS_NONE);
1125 /* Process the C source file. Write function prototypes to the standard
1126 * output. Convert function definitions and write the converted source
1127 * code to a temporary file.
1130 process_file (FILE *infile, char *name)
1134 if (strlen(name) > 2) {
1135 s = name + strlen(name) - 2;
1138 if (*s == 'l' || *s == 'y')
1140 #if defined(MSDOS) || defined(OS2)
1141 if (*s == 'L' || *s == 'Y')
1147 included_files = new_symbol_table();
1148 typedef_names = new_symbol_table();
1149 define_names = new_symbol_table();
1155 include_file(strcpy(base_file, name), func_style != FUNC_NONE);
1156 if (file_comments) {
1158 if (lintLibrary()) {
1159 put_blankline(stdout);
1163 put_string(stdout, "/* ");
1164 put_string(stdout, cur_file_name());
1165 put_string(stdout, " */\n");
1168 free_symbol_table(define_names);
1169 free_symbol_table(typedef_names);
1170 free_symbol_table(included_files);
1177 free_symbol_table (type_qualifiers);
1179 if (yy_current_buffer != 0)
1180 yy_delete_buffer(yy_current_buffer);