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 free_decl(struct cdecl *);
81 static void free_declspec(struct cdecl_declspec *x)
83 struct cdecl_declspec *p;
92 static void free_declarator(struct cdecl_declarator *x)
94 struct cdecl_declarator *p;
100 case CDECL_DECL_NULL:
102 case CDECL_DECL_IDENT:
105 case CDECL_DECL_POINTER:
106 free_declspec(x->u.pointer.qualifiers);
108 case CDECL_DECL_ARRAY:
109 free(x->u.array.vla);
111 case CDECL_DECL_FUNCTION:
112 free_decl(x->u.function.parameters);
123 static void free_decl(struct cdecl *x)
130 /* The specifiers may be shared by an entire chain. */
131 if (!p || p->specifiers != x->specifiers)
132 free_declspec(x->specifiers);
134 free_declarator(x->declarators);
140 void cdecl__free(struct cdecl *decl)
146 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err)
148 if (strstr(err, "T_LEX_ERROR"))
151 cdecl__err(CDECL_ENOPARSE, "%s", err);
155 %destructor { free($$); } <strval>
156 %destructor { free_declspec($$); } <declspec>
157 %destructor { free_declarator($$); } <declarator>
158 %destructor { free_decl($$); } <decl>
164 %token <strval> T_IDENT "identifier"
165 %token <uintval> T_UINT "integer constant"
167 %token T_SEMICOLON ";"
168 %token T_ASTERISK "*"
171 %token T_LBRACKET "["
172 %token T_RBRACKET "]"
174 %token T_ELLIPSIS "..."
176 %token T_TYPEDEF "typedef"
177 %token T_EXTERN "extern"
178 %token T_STATIC "static"
180 %token T_REGISTER "register"
182 %token T_INLINE "inline"
184 %token T_RESTRICT "restrict"
185 %token T_VOLATILE "volatile"
186 %token T_CONST "const"
190 %token T_SHORT "short"
193 %token T_FLOAT "float"
194 %token T_DOUBLE "double"
195 %token T_SIGNED "signed"
196 %token T_UNSIGNED "unsigned"
197 %token T_BOOL "_Bool"
198 %token T_COMPLEX "_Complex"
199 %token T_IMAGINARY "_Imaginary"
201 %token T_STRUCT "struct"
202 %token T_UNION "union"
209 %token T_DECLARE "declare"
210 %token T_POINTER "pointer"
211 %token T_FUNCTION "function"
212 %token T_RETURNING "returning"
213 %token T_ARRAY "array"
217 %token T_VLA "variable-length"
219 %type <strval> vla_ident
220 %type <boolval> varargs
221 %type <spectype> declspec_simple typespec_simple qualifier_simple
222 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
223 %type <declspec> qualifier qualifiers
224 %type <declspec> declspecs declspecs_noid
225 %type <declarator> direct_declarator declarator pointer array parens postfix
226 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
227 %type <decl> declaration declarators declarator_wrap
228 %type <decl> parameter parameters
230 %type <strval> english_vla
231 %type <declspec> storage_func_specs post_specs
232 %type <declspec> type_qual_spec type_qual_specs typedef_name_qual
233 %type <declarator> english_declarator english_array english_function
234 %type <declarator> english_parameter_list null_decl
235 %type <decl> english_parameter english_parameters
236 %type <decl> english english_declaration
239 * Harmless shift/reduce conflicts in english_parameter. See comments below
246 input: T_ENGLISH english {
254 declaration: declspecs declarators semi {
257 for (struct cdecl *i = $$; i; i = i->next)
261 declspecs: declspec_notype declspecs {
264 } | typespec declspecs_noid {
269 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
274 qualifiers: { $$ = NULL; } | qualifiers qualifier {
279 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
284 declarator_wrap: declarator {
285 ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
288 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO; }
289 | T_TYPEDEF { $$ = CDECL_STOR_TYPEDEF; }
290 | T_EXTERN { $$ = CDECL_STOR_EXTERN; }
291 | T_STATIC { $$ = CDECL_STOR_STATIC; }
292 | T_REGISTER { $$ = CDECL_STOR_REGISTER; }
293 | T_INLINE { $$ = CDECL_FUNC_INLINE; }
295 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID; }
296 | T_CHAR { $$ = CDECL_TYPE_CHAR; }
297 | T_SHORT { $$ = CDECL_TYPE_SHORT; }
298 | T_INT { $$ = CDECL_TYPE_INT; }
299 | T_LONG { $$ = CDECL_TYPE_LONG; }
300 | T_FLOAT { $$ = CDECL_TYPE_FLOAT; }
301 | T_DOUBLE { $$ = CDECL_TYPE_DOUBLE; }
302 | T_SIGNED { $$ = CDECL_TYPE_SIGNED; }
303 | T_UNSIGNED { $$ = CDECL_TYPE_UNSIGNED; }
304 | T_BOOL { $$ = CDECL_TYPE_BOOL; }
305 | T_COMPLEX { $$ = CDECL_TYPE_COMPLEX; }
306 | T_IMAGINARY { $$ = CDECL_TYPE_IMAGINARY; }
308 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST; }
309 | T_RESTRICT { $$ = CDECL_QUAL_RESTRICT; }
310 | T_VOLATILE { $$ = CDECL_QUAL_VOLATILE; }
312 declspec_notype: qualifier | declspec_simple {
313 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
316 typespec_noid: typespec_simple {
317 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
320 qualifier: qualifier_simple {
321 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
324 typespec: typespec_noid | T_STRUCT T_IDENT {
325 ALLOC_STRUCT($$, struct cdecl_declspec,
326 .type = CDECL_TYPE_STRUCT,
328 } | T_UNION T_IDENT {
329 ALLOC_STRUCT($$, struct cdecl_declspec,
330 .type = CDECL_TYPE_UNION,
333 ALLOC_STRUCT($$, struct cdecl_declspec,
334 .type = CDECL_TYPE_ENUM,
337 ALLOC_STRUCT($$, struct cdecl_declspec,
338 .type = CDECL_TYPE_IDENT,
342 declspec_noid: declspec_notype | typespec_noid
344 vla_ident: T_IDENT | T_ASTERISK {
345 ALLOC($$, sizeof "");
349 array: T_LBRACKET T_UINT T_RBRACKET {
351 FAIL(_("array length must be positive"));
353 ALLOC_STRUCT($$, struct cdecl_declarator,
354 .type = CDECL_DECL_ARRAY,
355 .u.array.length = $2);
356 } | T_LBRACKET vla_ident T_RBRACKET {
357 ALLOC_STRUCT($$, struct cdecl_declarator,
358 .type = CDECL_DECL_ARRAY,
360 } | T_LBRACKET T_RBRACKET {
361 ALLOC_STRUCT($$, struct cdecl_declarator,
362 .type = CDECL_DECL_ARRAY);
365 parameter: declspecs declarator {
366 ALLOC_STRUCT($$, struct cdecl,
371 parameters: parameter | parameters T_COMMA parameter {
376 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
378 parameter_type_list: parameters varargs {
379 struct cdecl *p, *c, *n;
381 /* Parameters were accumulated in reverse order. */
382 for (p = NULL, c = $1; c; p = c, c = n) {
387 ALLOC_STRUCT($$, struct cdecl_declarator,
388 .type = CDECL_DECL_FUNCTION,
389 .u.function.parameters = p,
390 .u.function.variadic = $2);
393 parens: T_LPAREN parameter_type_list T_RPAREN {
395 } | T_LPAREN declarator_ish T_RPAREN {
396 ALLOC_STRUCT($$, struct cdecl_declarator,
397 .type = CDECL_DECL_FUNCTION);
398 ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
402 pointer: T_ASTERISK qualifiers direct_declarator {
403 ALLOC_STRUCT($$, struct cdecl_declarator,
404 .type = CDECL_DECL_POINTER,
405 .u.pointer.qualifiers = $2,
407 } | T_ASTERISK qualifiers pointer {
408 ALLOC_STRUCT($$, struct cdecl_declarator,
409 .type = CDECL_DECL_POINTER,
410 .u.pointer.qualifiers = $2,
414 declarator: direct_declarator | pointer
415 declarator_ish: direct_declarator_ish | pointer
416 postfix: array | parens
418 direct_declarator_ish: {
419 ALLOC_STRUCT($$, struct cdecl_declarator,
420 .type = CDECL_DECL_NULL);
421 } | direct_declarator_ish postfix {
427 ALLOC_STRUCT($$, struct cdecl_declarator,
428 .type = CDECL_DECL_NULL);
430 ALLOC_STRUCT($$, struct cdecl_declarator,
431 .type = CDECL_DECL_IDENT,
433 } | direct_declarator postfix {
438 english: T_DECLARE T_IDENT T_AS english_declaration {
440 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
441 if (d->type == CDECL_DECL_NULL) {
442 d->type = CDECL_DECL_IDENT;
446 } | T_TYPE english_declaration {
450 storage_func_specs: { $$ = NULL; } | declspec_simple storage_func_specs {
451 ALLOC_STRUCT($$, struct cdecl_declspec,
456 type_qual_spec: typespec_noid | qualifier
458 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
464 * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
465 * conflicts with pointer declarators. So we end up needing to stitch
466 * together three different specifiers lists.
468 post_specs: qualifiers typespec type_qual_specs {
471 for (struct cdecl_declspec *s = $$; s; s = s->next) {
479 english_declaration: storage_func_specs english_declarator post_specs {
480 ALLOC_STRUCT($$, struct cdecl,
484 for (struct cdecl_declspec *s = $$->specifiers; s; s = s->next) {
492 english_declarator: {
493 ALLOC_STRUCT($$, struct cdecl_declarator,
494 .type = CDECL_DECL_NULL);
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_parameters varargs {
517 struct cdecl *p, *c, *n;
519 /* Parameters were accumulated in reverse order. */
520 for (p = NULL, c = $1; c; p = c, c = n) {
525 ALLOC_STRUCT($$, struct cdecl_declarator,
526 .type = CDECL_DECL_FUNCTION,
527 .u.function.parameters = p,
528 .u.function.variadic = $2);
531 english_parameters: english_parameters T_COMMA english_parameter {
534 } | english_parameter
536 typedef_name_qual: T_IDENT qualifiers {
537 ALLOC_STRUCT($$, struct cdecl_declspec,
538 .type = CDECL_TYPE_IDENT,
544 ALLOC_STRUCT($$, struct cdecl_declarator,
545 .type = CDECL_DECL_NULL);
549 * There is a small shift/reduce conflict here. An unadorned identifier
550 * as the first thing in the parameter might be a typedef name deep in the
551 * first english_declaration (thus empty storage_func_specs and empty
552 * english_declarator need to be reduced) or it might be the identifier
553 * before the "as" (thus the identifier should be shifted).
555 * The typedef name conflict is the only issue, so treating it as a special
556 * case makes the shift harmless.
558 english_parameter: english_declaration | typedef_name_qual null_decl {
559 ALLOC_STRUCT($$, struct cdecl,
562 } | T_IDENT T_AS english_declaration {
564 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
565 if (d->type == CDECL_DECL_NULL) {
566 d->type = CDECL_DECL_IDENT;
572 english_array: T_VLA T_ARRAY english_vla T_OF {
573 ALLOC_STRUCT($$, struct cdecl_declarator,
574 .type = CDECL_DECL_ARRAY,
576 } | T_ARRAY T_UINT T_OF {
578 FAIL(_("array length must be positive"));
580 ALLOC_STRUCT($$, struct cdecl_declarator,
581 .type = CDECL_DECL_ARRAY,
582 .u.array.length = $2);
584 ALLOC_STRUCT($$, struct cdecl_declarator,
585 .type = CDECL_DECL_ARRAY,
586 .u.array.length = 0);
589 english_vla: T_IDENT | {
590 ALLOC($$, sizeof "");
597 * Expose the token string table to the rest of the library, in order to
598 * produce strings that match parser keywords.
600 * In order for this to work properly, the Bison output must be postprocessed
601 * by fix-yytname.awk to remove pointless quotation marks from the keyword
604 const char *cdecl__token_name(unsigned token)
606 return yytname[YYTRANSLATE(token)];