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__ }; \
63 void cdecl__free(struct cdecl *);
64 int cdecl__yyparse(void *scanner, struct cdecl **out);
65 const char *cdecl__token_name(unsigned token);
73 struct cdecl_declspec *declspec;
74 struct cdecl_declarator *declarator;
79 static void yyerror(YYLTYPE *, yyscan_t, struct cdecl **, const char *);
80 static void free_decl(struct cdecl *);
82 static void free_declspec(struct cdecl_declspec *x)
84 struct cdecl_declspec *p;
93 static void free_declarator(struct cdecl_declarator *x)
95 struct cdecl_declarator *p;
101 case CDECL_DECL_NULL:
103 case CDECL_DECL_IDENT:
106 case CDECL_DECL_POINTER:
107 free_declspec(x->u.pointer.qualifiers);
109 case CDECL_DECL_ARRAY:
110 free(x->u.array.vla);
112 case CDECL_DECL_FUNCTION:
113 free_decl(x->u.function.parameters);
124 static void free_decl(struct cdecl *x)
131 /* The specifiers may be shared by an entire chain. */
132 if (!p || p->specifiers != x->specifiers)
133 free_declspec(x->specifiers);
135 free_declarator(x->declarators);
141 void cdecl__free(struct cdecl *decl)
147 * Join two declaration specifier lists into a single list, with "a" being the
148 * head of the new list.
150 * The list "a" is assumed to be nonempty.
152 static void join_specs(struct cdecl_declspec *a, struct cdecl_declspec *b)
160 * Alter an abstract declarator (type name) to declare an identifier instead,
161 * used by the English parser rules to reduce "identifier as type" sequences.
163 static struct cdecl *insert_identifier(struct cdecl *decl, char *ident)
165 struct cdecl_declarator *d = decl->declarators;
170 d->type = CDECL_DECL_IDENT;
177 %destructor { free($$); } <strval>
178 %destructor { free_declspec($$); } <declspec>
179 %destructor { free_declarator($$); } <declarator>
180 %destructor { free_decl($$); } <decl>
186 %token <strval> 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 <strval> 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 <strval> 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
263 * Harmless shift/reduce conflicts in english_parameter. See comments below
270 input: T_ENGLISH english {
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 ALLOC_STRUCT($$, struct cdecl_declspec,
370 declspec_noid: declspec_notype | typespec_noid
372 vla_ident: T_IDENT | T_ASTERISK {
373 ALLOC($$, sizeof "");
377 array: T_LBRACKET array_length T_RBRACKET {
378 ALLOC_STRUCT($$, struct cdecl_declarator,
379 .type = CDECL_DECL_ARRAY,
380 .u.array.length = $2);
381 } | T_LBRACKET vla_ident T_RBRACKET {
382 ALLOC_STRUCT($$, struct cdecl_declarator,
383 .type = CDECL_DECL_ARRAY,
387 parameter: declspecs declarator {
388 ALLOC_STRUCT($$, struct cdecl,
393 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
395 parameter_type_list: parameter varargs {
396 ALLOC_STRUCT($$, struct cdecl_declarator,
397 .type = CDECL_DECL_FUNCTION,
398 .u.function.parameters = $1,
399 .u.function.variadic = $2);
400 } | parameter T_COMMA parameter_type_list {
402 $1->next = $$->u.function.parameters;
403 $$->u.function.parameters = $1;
406 parens: T_LPAREN parameter_type_list T_RPAREN {
408 } | T_LPAREN declarator_ish T_RPAREN {
409 ALLOC_STRUCT($$, struct cdecl_declarator,
410 .type = CDECL_DECL_FUNCTION);
411 ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
415 pointer: T_ASTERISK qualifiers direct_declarator {
416 ALLOC_STRUCT($$, struct cdecl_declarator,
417 .type = CDECL_DECL_POINTER,
418 .u.pointer.qualifiers = $2,
420 } | T_ASTERISK qualifiers pointer {
421 ALLOC_STRUCT($$, struct cdecl_declarator,
422 .type = CDECL_DECL_POINTER,
423 .u.pointer.qualifiers = $2,
427 declarator: direct_declarator | pointer
428 declarator_ish: direct_declarator_ish | pointer
429 postfix: array | parens
431 direct_declarator_ish: {
432 ALLOC_STRUCT($$, struct cdecl_declarator,
433 .type = CDECL_DECL_NULL);
434 } | direct_declarator_ish postfix {
440 ALLOC_STRUCT($$, struct cdecl_declarator,
441 .type = CDECL_DECL_NULL);
443 ALLOC_STRUCT($$, struct cdecl_declarator,
444 .type = CDECL_DECL_IDENT,
446 } | direct_declarator postfix {
451 english: T_DECLARE T_IDENT T_AS english_declaration {
452 $$ = insert_identifier($4, $2);
453 } | T_TYPE english_declaration {
457 storage_func_specs: { $$ = NULL; } | declspec_simple storage_func_specs {
458 ALLOC_STRUCT($$, struct cdecl_declspec,
463 type_qual_spec: typespec_noid | qualifier
465 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
471 * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
472 * conflicts with pointer declarators. So we end up needing to stitch
473 * together three different specifiers lists.
475 post_specs: qualifiers typespec type_qual_specs {
481 english_declaration: storage_func_specs english_declarator post_specs {
483 ALLOC_STRUCT($$, struct cdecl,
488 english_declarator: {
489 ALLOC_STRUCT($$, struct cdecl_declarator,
490 .type = CDECL_DECL_NULL);
491 } | english_declarator qualifiers T_POINTER T_TO {
492 ALLOC_STRUCT($$, struct cdecl_declarator,
493 .type = CDECL_DECL_POINTER,
495 .u.pointer.qualifiers = $2);
496 } | english_declarator english_array {
499 } | english_declarator english_function {
504 english_function: T_FUNCTION T_RETURNING {
505 ALLOC_STRUCT($$, struct cdecl_declarator,
506 .type = CDECL_DECL_FUNCTION,
507 .u.function.parameters = NULL);
508 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
512 english_parameter_list: english_parameter varargs {
513 ALLOC_STRUCT($$, struct cdecl_declarator,
514 .type = CDECL_DECL_FUNCTION,
515 .u.function.parameters = $1,
516 .u.function.variadic = $2);
517 } | english_parameter T_COMMA english_parameter_list {
519 $1->next = $$->u.function.parameters;
520 $$->u.function.parameters = $1;
523 typedef_name_qual: T_IDENT qualifiers {
524 ALLOC_STRUCT($$, struct cdecl_declspec,
525 .type = CDECL_TYPE_IDENT,
531 ALLOC_STRUCT($$, struct cdecl_declarator,
532 .type = CDECL_DECL_NULL);
536 * There is a small shift/reduce conflict here. An unadorned identifier
537 * as the first thing in the parameter might be a typedef name deep in the
538 * first english_declaration (thus empty storage_func_specs and empty
539 * english_declarator need to be reduced) or it might be the identifier
540 * before the "as" (thus the identifier should be shifted).
542 * The typedef name conflict is the only issue, so treating it as a special
543 * case makes the shift harmless.
545 english_parameter: english_declaration | typedef_name_qual null_decl {
546 ALLOC_STRUCT($$, struct cdecl,
549 } | T_IDENT T_AS english_declaration {
550 $$ = insert_identifier($3, $1);
553 english_array: T_VLA T_ARRAY english_vla T_OF {
554 ALLOC_STRUCT($$, struct cdecl_declarator,
555 .type = CDECL_DECL_ARRAY,
557 } | T_ARRAY array_length T_OF {
558 ALLOC_STRUCT($$, struct cdecl_declarator,
559 .type = CDECL_DECL_ARRAY,
560 .u.array.length = $2);
563 array_length: { $$ = 0; }
564 array_length: T_UINT {
566 FAIL(_("array length must be positive"));
569 english_vla: T_IDENT | {
570 ALLOC($$, sizeof "");
577 * Expose the token string table to the rest of the library, in order to
578 * produce strings that match parser keywords.
580 * In order for this to work properly, the Bison output must be postprocessed
581 * by fix-yytname.awk to remove pointless quotation marks from the keyword
584 const char *cdecl__token_name(unsigned token)
586 return yytname[YYTRANSLATE(token)];
590 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err)
592 if (strstr(err, yytname[YYTRANSLATE(T_LEX_ERROR)]))
595 cdecl__err(CDECL_ENOPARSE, "%s", err);