3 * Parser for C declarations.
4 * Copyright © 2011-2012, 2021, 2023 Nick Bowler
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 %name-prefix "cdecl__yy"
22 %parse-param {void *scanner}
23 %parse-param {struct cdecl **out}
24 %lex-param {yyscan_t scanner}
36 #include "cdecl-internal.h"
39 #define FAIL(msg) do { \
40 yyerror(&yylloc, NULL, NULL, msg); \
44 #define ALLOC(ptr, size) do { \
45 (ptr) = malloc(size); \
47 cdecl__errmsg(CDECL__ENOMEM); \
52 #define ALLOC_STRUCT(ptr, type, ...) do { \
53 ALLOC(ptr, sizeof (type)); \
54 *(ptr) = (type) { __VA_ARGS__ }; \
58 * With the postprocessing performed by fix-yytname.awk, all the symbol
59 * name strings can be used directly in error messages and there is no
60 * need for any string processing.
62 #define yytnamerr(a, b) cdecl__strlcpy(a, b, (a) ? INT_MAX : 0)
70 void cdecl__free(struct cdecl *);
71 int cdecl__yyparse(void *scanner, struct cdecl **out);
72 const char *cdecl__token_name(unsigned token);
79 struct cdecl_declspec *declspec;
80 struct cdecl_declarator *declarator;
82 struct parse_item *item;
86 static void yyerror(YYLTYPE *, yyscan_t, struct cdecl **, const char *);
87 static void free_decl(struct cdecl *);
89 static void free_declspec(struct cdecl_declspec *x)
91 struct cdecl_declspec *p;
99 static void free_declarator(struct cdecl_declarator *x)
101 struct cdecl_declarator *p;
107 case CDECL_DECL_NULL:
109 case CDECL_DECL_IDENT:
110 case CDECL_DECL_ARRAY:
112 case CDECL_DECL_POINTER:
113 free_declspec(x->u.pointer.qualifiers);
115 case CDECL_DECL_FUNCTION:
116 free_decl(x->u.function.parameters);
127 static void free_decl(struct cdecl *x)
134 /* The specifiers may be shared by an entire chain. */
135 if (!p || p->specifiers != x->specifiers)
136 free_declspec(x->specifiers);
138 free_declarator(x->declarators);
144 void cdecl__free(struct cdecl *decl)
150 * Join two declaration specifier lists into a single list, with "a" being the
151 * head of the new list.
153 * The list "a" is assumed to be nonempty.
155 static void join_specs(struct cdecl_declspec *a, struct cdecl_declspec *b)
163 * Alter an abstract declarator (type name) to declare an identifier instead,
164 * used by the English parser rules to reduce "identifier as type" sequences.
166 static struct cdecl *insert_identifier(struct cdecl *decl, struct parse_item *ident)
168 struct cdecl_declarator *d, **p = &decl->declarators;
170 while ((d = *p)->child)
173 *p = &ident->u.declarator;
177 static struct cdecl_declarator *nulldecl(void)
179 static const struct cdecl_declarator nulldecl = {0};
180 return (void *)&nulldecl;
182 #define NULLDECL (nulldecl())
186 %destructor { free($$); } <item>
187 %destructor { free_declspec($$); } <declspec>
188 %destructor { free_declarator($$); } <declarator>
189 %destructor { free_decl($$); } <decl>
194 %token <item> T_IDENT "identifier"
195 %token <uintval> T_UINT "integer constant"
197 %token T_SEMICOLON ";"
198 %token T_ASTERISK "*"
201 %token T_LBRACKET "["
202 %token T_RBRACKET "]"
204 %token T_ELLIPSIS "..."
206 %token <spectype> T_TYPEDEF "typedef"
207 %token <spectype> T_EXTERN "extern"
208 %token <spectype> T_STATIC "static"
209 %token <spectype> T_AUTO "auto"
210 %token <spectype> T_REGISTER "register"
212 %token <spectype> T_INLINE "inline"
214 %token <spectype> T_RESTRICT "restrict"
215 %token <spectype> T_VOLATILE "volatile"
216 %token <spectype> T_CONST "const"
218 %token <spectype> T_VOID "void"
219 %token <spectype> T_CHAR "char"
220 %token <spectype> T_SHORT "short"
221 %token <spectype> T_INT "int"
222 %token <spectype> T_LONG "long"
223 %token <spectype> T_FLOAT "float"
224 %token <spectype> T_DOUBLE "double"
225 %token <spectype> T_SIGNED "signed"
226 %token <spectype> T_UNSIGNED "unsigned"
227 %token <spectype> T_BOOL "_Bool"
228 %token <spectype> T_COMPLEX "_Complex"
229 %token <spectype> T_IMAGINARY "_Imaginary"
231 %token <spectype> T_STRUCT "struct"
232 %token <spectype> T_UNION "union"
233 %token <spectype> T_ENUM "enum"
239 %token T_DECLARE "declare"
240 %token T_POINTER "pointer"
241 %token T_FUNCTION "function"
242 %token T_RETURNING "returning"
243 %token T_ARRAY "array"
247 %token T_VLA "variable-length"
249 %type <item> vla_ident
250 %type <uintval> array_length
251 %type <boolval> varargs
252 %type <spectype> declspec_simple qualifier_simple
253 %type <spectype> typespec_simple typespec_tagged
254 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
255 %type <declspec> qualifier qualifiers
256 %type <declspec> declspecs declspecs_noid
257 %type <declarator> direct_declarator declarator pointer array parens postfix
258 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
259 %type <decl> cdecl declaration declarators declarator_wrap parameter
261 %type <item> english_vla
262 %type <declspec> storage_func_specs post_specs
263 %type <declspec> type_qual_spec type_qual_specs typedef_name_qual
264 %type <declarator> english_declarator english_array english_function
265 %type <declarator> english_parameter_list null_decl
266 %type <decl> english english_declaration english_parameter
268 /* Precedence declaration to avoid conflict in english_parameter; see below. */
274 input: cdecl { *out = $1; }
275 cdecl: english | declaration
278 declaration: declspecs declarators semi {
284 * We support parsing declarations using arbitrary identifiers as type
285 * specifiers (a la C typedef). To avoid confusion with identifiers that
286 * may also be used as declarators, note the following:
288 * (a) Every valid C declaration must have at least one type specifier, and
289 * (b) Valid declarations with typedef names have exactly one type specifier.
291 * So the rule applied when parsing specifiers is: an identifier is a type
292 * specifier only if we have not yet seen any type specifiers whatsoever
293 * (within one declaration specifier list).
295 * Treating identifiers as type specifiers by default can lead to strange and
296 * unexpected parses; libcdecl applies a simplification step to the resulting
297 * parse tree afterwards.
299 declspecs: declspec_notype declspecs {
302 } | typespec declspecs_noid {
307 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
312 qualifiers: { $$ = NULL; } | qualifiers qualifier {
317 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
322 declarator_wrap: declarator {
323 ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
326 declspec_simple: T_AUTO
333 typespec_simple: T_VOID
346 typespec_tagged: T_STRUCT | T_UNION | T_ENUM | { $$ = CDECL_TYPE_IDENT; }
348 qualifier_simple: T_CONST
352 declspec_notype: qualifier | declspec_simple {
353 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
356 typespec_noid: typespec_simple {
357 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
360 qualifier: qualifier_simple {
361 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
364 typespec: typespec_noid | typespec_tagged T_IDENT {
365 /* Compiler should be able to elide this assignment. */
366 $2->u.declspec.ident = $2->u.declarator.u.ident;
368 $$ = &$2->u.declspec;
372 declspec_noid: declspec_notype | typespec_noid
374 vla_ident: T_IDENT | T_ASTERISK {
375 if (!($$ = cdecl__alloc_item(1)))
380 array: T_LBRACKET array_length T_RBRACKET {
381 ALLOC_STRUCT($$, struct cdecl_declarator,
382 .type = CDECL_DECL_ARRAY,
383 .u.array.length = $2);
384 } | T_LBRACKET vla_ident T_RBRACKET {
385 $$ = &$2->u.declarator;
386 $$->type = CDECL_DECL_ARRAY;
387 $$->u.array.vla = $$->u.ident;
388 $$->u.array.length = 0;
391 parameter: declspecs declarator {
392 ALLOC_STRUCT($$, struct cdecl,
397 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
399 parameter_type_list: parameter varargs {
400 ALLOC_STRUCT($$, struct cdecl_declarator,
401 .type = CDECL_DECL_FUNCTION,
402 .u.function.parameters = $1,
403 .u.function.variadic = $2);
404 } | parameter T_COMMA parameter_type_list {
406 $1->next = $$->u.function.parameters;
407 $$->u.function.parameters = $1;
410 parens: T_LPAREN parameter_type_list T_RPAREN {
412 } | T_LPAREN declarator_ish T_RPAREN {
413 ALLOC_STRUCT($$, struct cdecl_declarator,
414 .type = CDECL_DECL_FUNCTION);
415 ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
419 pointer: T_ASTERISK qualifiers direct_declarator {
420 ALLOC_STRUCT($$, struct cdecl_declarator,
421 .type = CDECL_DECL_POINTER,
422 .u.pointer.qualifiers = $2,
424 } | T_ASTERISK qualifiers pointer {
425 ALLOC_STRUCT($$, struct cdecl_declarator,
426 .type = CDECL_DECL_POINTER,
427 .u.pointer.qualifiers = $2,
431 declarator: direct_declarator | pointer
432 declarator_ish: direct_declarator_ish | pointer
433 postfix: array | parens
435 direct_declarator_ish: {
437 } | direct_declarator_ish postfix {
445 $$ = &$1->u.declarator;
446 } | direct_declarator postfix {
451 english: T_DECLARE T_IDENT T_AS english_declaration {
452 $$ = insert_identifier($4, $2);
453 } | T_TYPE english_declaration {
458 * We use a precedence declaration to prefer shifting an identifier
459 * over reducing this empty rule; see below.
461 storage_func_specs: %prec T_TYPE { $$ = NULL; }
462 storage_func_specs: declspec_simple storage_func_specs {
463 ALLOC_STRUCT($$, struct cdecl_declspec,
468 type_qual_spec: typespec_noid | qualifier
470 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
476 * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
477 * conflicts with pointer declarators. So we end up needing to stitch
478 * together three different specifiers lists.
480 post_specs: qualifiers typespec type_qual_specs {
486 english_declaration: storage_func_specs english_declarator post_specs {
488 ALLOC_STRUCT($$, struct cdecl,
493 english_declarator: {
495 } | english_declarator qualifiers T_POINTER T_TO {
496 ALLOC_STRUCT($$, struct cdecl_declarator,
497 .type = CDECL_DECL_POINTER,
499 .u.pointer.qualifiers = $2);
500 } | english_declarator english_array {
503 } | english_declarator english_function {
508 english_function: T_FUNCTION T_RETURNING {
509 ALLOC_STRUCT($$, struct cdecl_declarator,
510 .type = CDECL_DECL_FUNCTION,
511 .u.function.parameters = NULL);
512 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
516 english_parameter_list: english_parameter varargs {
517 ALLOC_STRUCT($$, struct cdecl_declarator,
518 .type = CDECL_DECL_FUNCTION,
519 .u.function.parameters = $1,
520 .u.function.variadic = $2);
521 } | english_parameter T_COMMA english_parameter_list {
523 $1->next = $$->u.function.parameters;
524 $$->u.function.parameters = $1;
527 typedef_name_qual: T_IDENT qualifiers {
528 /* Compiler should be able to elide this assignment. */
529 $1->u.declspec.ident = $1->u.declarator.u.ident;
531 $$ = &$1->u.declspec;
532 $$->type = CDECL_TYPE_IDENT;
541 * There is a shift/reduce conflict here when an identifier appears as the
542 * first token. The conflict is between shifting T_IDENT, or reducing the
543 * empty production for storage_func_specs (cf. english_declaration).
545 * - In either case, if we reduce, we won't match T_IDENT T_AS since the
546 * stack now has the extra storage_func_specs nonterminal symbol.
547 * - And if we shift, we won't match english_declaration since it is
548 * too late to add storage_func_specs to the stack.
550 * The only valid input affected by the conflict is a simple type names,
551 * possibly followed by qualifiers. So the conflict is adequately resolved
552 * by shifting, so long as we have a special-case reduction to handle this.
554 english_parameter: english_declaration | typedef_name_qual null_decl {
555 ALLOC_STRUCT($$, struct cdecl,
558 } | T_IDENT T_AS english_declaration {
559 $$ = insert_identifier($3, $1);
562 english_array: T_VLA T_ARRAY english_vla T_OF {
563 $$ = &$3->u.declarator;
564 $$->type = CDECL_DECL_ARRAY;
565 $$->u.array.vla = $$->u.ident;
566 $$->u.array.length = 0;
567 } | T_ARRAY array_length T_OF {
568 ALLOC_STRUCT($$, struct cdecl_declarator,
569 .type = CDECL_DECL_ARRAY,
570 .u.array.length = $2);
573 array_length: { $$ = 0; }
574 array_length: T_UINT {
576 FAIL(_("array length must be positive"));
579 english_vla: T_IDENT | {
580 if (!($$ = cdecl__alloc_item(1)))
588 * Expose the token string table to the rest of the library, in order to
589 * produce strings that match parser keywords.
591 * In order for this to work properly, the Bison output must be postprocessed
592 * by fix-yytname.awk to remove pointless quotation marks from the keyword
595 const char *cdecl__token_name(unsigned token)
597 return yytname[YYTRANSLATE(token)];
601 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err)
603 if (strstr(err, yytname[YYTRANSLATE(T_LEX_ERROR)]))
606 cdecl__err(CDECL_ENOPARSE, "%s", err);