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:
108 case CDECL_DECL_IDENT:
109 case CDECL_DECL_ARRAY:
111 case CDECL_DECL_POINTER:
112 free_declspec(x->u.pointer.qualifiers);
114 case CDECL_DECL_FUNCTION:
115 free_decl(x->u.function.parameters);
126 static void free_decl(struct cdecl *x)
133 /* The specifiers may be shared by an entire chain. */
134 if (!p || p->specifiers != x->specifiers)
135 free_declspec(x->specifiers);
137 free_declarator(x->declarators);
143 void cdecl__free(struct cdecl *decl)
149 * Join two declaration specifier lists into a single list, with "a" being the
150 * head of the new list.
152 * The list "a" is assumed to be nonempty.
154 static void join_specs(struct cdecl_declspec *a, struct cdecl_declspec *b)
162 * Alter an abstract declarator (type name) to declare an identifier instead,
163 * used by the English parser rules to reduce "identifier as type" sequences.
165 static struct cdecl *insert_identifier(struct cdecl *decl, struct parse_item *ident)
167 struct cdecl_declarator *d, **p = &decl->declarators;
169 while ((d = *p)->child)
173 *p = d = &ident->u.declarator;
178 %destructor { free($$); } <item>
179 %destructor { free_declspec($$); } <declspec>
180 %destructor { free_declarator($$); } <declarator>
181 %destructor { free_decl($$); } <decl>
186 %token <item> T_IDENT "identifier"
187 %token <uintval> T_UINT "integer constant"
189 %token T_SEMICOLON ";"
190 %token T_ASTERISK "*"
193 %token T_LBRACKET "["
194 %token T_RBRACKET "]"
196 %token T_ELLIPSIS "..."
198 %token <spectype> T_TYPEDEF "typedef"
199 %token <spectype> T_EXTERN "extern"
200 %token <spectype> T_STATIC "static"
201 %token <spectype> T_AUTO "auto"
202 %token <spectype> T_REGISTER "register"
204 %token <spectype> T_INLINE "inline"
206 %token <spectype> T_RESTRICT "restrict"
207 %token <spectype> T_VOLATILE "volatile"
208 %token <spectype> T_CONST "const"
210 %token <spectype> T_VOID "void"
211 %token <spectype> T_CHAR "char"
212 %token <spectype> T_SHORT "short"
213 %token <spectype> T_INT "int"
214 %token <spectype> T_LONG "long"
215 %token <spectype> T_FLOAT "float"
216 %token <spectype> T_DOUBLE "double"
217 %token <spectype> T_SIGNED "signed"
218 %token <spectype> T_UNSIGNED "unsigned"
219 %token <spectype> T_BOOL "_Bool"
220 %token <spectype> T_COMPLEX "_Complex"
221 %token <spectype> T_IMAGINARY "_Imaginary"
223 %token <spectype> T_STRUCT "struct"
224 %token <spectype> T_UNION "union"
225 %token <spectype> T_ENUM "enum"
231 %token T_DECLARE "declare"
232 %token T_POINTER "pointer"
233 %token T_FUNCTION "function"
234 %token T_RETURNING "returning"
235 %token T_ARRAY "array"
239 %token T_VLA "variable-length"
241 %type <item> vla_ident
242 %type <uintval> array_length
243 %type <boolval> varargs
244 %type <spectype> declspec_simple qualifier_simple
245 %type <spectype> typespec_simple typespec_tagged
246 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
247 %type <declspec> qualifier qualifiers
248 %type <declspec> declspecs declspecs_noid
249 %type <declarator> direct_declarator declarator pointer array parens postfix
250 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
251 %type <decl> declaration declarators declarator_wrap
252 %type <decl> parameter
254 %type <item> english_vla
255 %type <declspec> storage_func_specs post_specs
256 %type <declspec> type_qual_spec type_qual_specs typedef_name_qual
257 %type <declarator> english_declarator english_array english_function
258 %type <declarator> english_parameter_list null_decl
259 %type <decl> english english_declaration
260 %type <decl> english_parameter
262 /* Precedence declaration to avoid conflict in english_parameter; see below. */
276 declaration: declspecs declarators semi {
282 * We support parsing declarations using arbitrary identifiers as type
283 * specifiers (a la C typedef). To avoid confusion with identifiers that
284 * may also be used as declarators, note the following:
286 * (a) Every valid C declaration must have at least one type specifier, and
287 * (b) Valid declarations with typedef names have exactly one type specifier.
289 * So the rule applied when parsing specifiers is: an identifier is a type
290 * specifier only if we have not yet seen any type specifiers whatsoever
291 * (within one declaration specifier list).
293 * Treating identifiers as type specifiers by default can lead to strange and
294 * unexpected parses; libcdecl applies a simplification step to the resulting
295 * parse tree afterwards.
297 declspecs: declspec_notype declspecs {
300 } | typespec declspecs_noid {
305 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
310 qualifiers: { $$ = NULL; } | qualifiers qualifier {
315 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
320 declarator_wrap: declarator {
321 ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
324 declspec_simple: T_AUTO
331 typespec_simple: T_VOID
344 typespec_tagged: T_STRUCT | T_UNION | T_ENUM | { $$ = CDECL_TYPE_IDENT; }
346 qualifier_simple: T_CONST
350 declspec_notype: qualifier | declspec_simple {
351 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
354 typespec_noid: typespec_simple {
355 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
358 qualifier: qualifier_simple {
359 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
362 typespec: typespec_noid | typespec_tagged T_IDENT {
363 /* Compiler should be able to elide this assignment. */
364 $2->u.declspec.ident = $2->u.declarator.u.ident;
366 $$ = &$2->u.declspec;
370 declspec_noid: declspec_notype | typespec_noid
372 vla_ident: T_IDENT | T_ASTERISK {
373 if (!($$ = cdecl__alloc_item(1)))
378 array: T_LBRACKET array_length T_RBRACKET {
379 ALLOC_STRUCT($$, struct cdecl_declarator,
380 .type = CDECL_DECL_ARRAY,
381 .u.array.length = $2);
382 } | T_LBRACKET vla_ident T_RBRACKET {
383 $$ = &$2->u.declarator;
384 $$->type = CDECL_DECL_ARRAY;
385 $$->u.array.vla = $$->u.ident;
386 $$->u.array.length = 0;
389 parameter: declspecs declarator {
390 ALLOC_STRUCT($$, struct cdecl,
395 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
397 parameter_type_list: parameter varargs {
398 ALLOC_STRUCT($$, struct cdecl_declarator,
399 .type = CDECL_DECL_FUNCTION,
400 .u.function.parameters = $1,
401 .u.function.variadic = $2);
402 } | parameter T_COMMA parameter_type_list {
404 $1->next = $$->u.function.parameters;
405 $$->u.function.parameters = $1;
408 parens: T_LPAREN parameter_type_list T_RPAREN {
410 } | T_LPAREN declarator_ish T_RPAREN {
411 ALLOC_STRUCT($$, struct cdecl_declarator,
412 .type = CDECL_DECL_FUNCTION);
413 ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
417 pointer: T_ASTERISK qualifiers direct_declarator {
418 ALLOC_STRUCT($$, struct cdecl_declarator,
419 .type = CDECL_DECL_POINTER,
420 .u.pointer.qualifiers = $2,
422 } | T_ASTERISK qualifiers pointer {
423 ALLOC_STRUCT($$, struct cdecl_declarator,
424 .type = CDECL_DECL_POINTER,
425 .u.pointer.qualifiers = $2,
429 declarator: direct_declarator | pointer
430 declarator_ish: direct_declarator_ish | pointer
431 postfix: array | parens
433 direct_declarator_ish: {
434 ALLOC_STRUCT($$, struct cdecl_declarator,
435 .type = CDECL_DECL_NULL);
436 } | direct_declarator_ish postfix {
442 ALLOC_STRUCT($$, struct cdecl_declarator,
443 .type = CDECL_DECL_NULL);
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: {
494 ALLOC_STRUCT($$, struct cdecl_declarator,
495 .type = CDECL_DECL_NULL);
496 } | english_declarator qualifiers T_POINTER T_TO {
497 ALLOC_STRUCT($$, struct cdecl_declarator,
498 .type = CDECL_DECL_POINTER,
500 .u.pointer.qualifiers = $2);
501 } | english_declarator english_array {
504 } | english_declarator english_function {
509 english_function: T_FUNCTION T_RETURNING {
510 ALLOC_STRUCT($$, struct cdecl_declarator,
511 .type = CDECL_DECL_FUNCTION,
512 .u.function.parameters = NULL);
513 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
517 english_parameter_list: english_parameter varargs {
518 ALLOC_STRUCT($$, struct cdecl_declarator,
519 .type = CDECL_DECL_FUNCTION,
520 .u.function.parameters = $1,
521 .u.function.variadic = $2);
522 } | english_parameter T_COMMA english_parameter_list {
524 $1->next = $$->u.function.parameters;
525 $$->u.function.parameters = $1;
528 typedef_name_qual: T_IDENT qualifiers {
529 /* Compiler should be able to elide this assignment. */
530 $1->u.declspec.ident = $1->u.declarator.u.ident;
532 $$ = &$1->u.declspec;
533 $$->type = CDECL_TYPE_IDENT;
538 ALLOC_STRUCT($$, struct cdecl_declarator,
539 .type = CDECL_DECL_NULL);
543 * There is a shift/reduce conflict here when an identifier appears as the
544 * first token. The conflict is between shifting T_IDENT, or reducing the
545 * empty production for storage_func_specs (cf. english_declaration).
547 * - In either case, if we reduce, we won't match T_IDENT T_AS since the
548 * stack now has the extra storage_func_specs nonterminal symbol.
549 * - And if we shift, we won't match english_declaration since it is
550 * too late to add storage_func_specs to the stack.
552 * The only valid input affected by the conflict is a simple type names,
553 * possibly followed by qualifiers. So the conflict is adequately resolved
554 * by shifting, so long as we have a special-case reduction to handle this.
556 english_parameter: english_declaration | typedef_name_qual null_decl {
557 ALLOC_STRUCT($$, struct cdecl,
560 } | T_IDENT T_AS english_declaration {
561 $$ = insert_identifier($3, $1);
564 english_array: T_VLA T_ARRAY english_vla T_OF {
565 $$ = &$3->u.declarator;
566 $$->type = CDECL_DECL_ARRAY;
567 $$->u.array.vla = $$->u.ident;
568 $$->u.array.length = 0;
569 } | T_ARRAY array_length T_OF {
570 ALLOC_STRUCT($$, struct cdecl_declarator,
571 .type = CDECL_DECL_ARRAY,
572 .u.array.length = $2);
575 array_length: { $$ = 0; }
576 array_length: T_UINT {
578 FAIL(_("array length must be positive"));
581 english_vla: T_IDENT | {
582 if (!($$ = cdecl__alloc_item(1)))
590 * Expose the token string table to the rest of the library, in order to
591 * produce strings that match parser keywords.
593 * In order for this to work properly, the Bison output must be postprocessed
594 * by fix-yytname.awk to remove pointless quotation marks from the keyword
597 const char *cdecl__token_name(unsigned token)
599 return yytname[YYTRANSLATE(token)];
603 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err)
605 if (strstr(err, yytname[YYTRANSLATE(T_LEX_ERROR)]))
608 cdecl__err(CDECL_ENOPARSE, "%s", err);