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 <spectype> T_TYPEDEF "typedef"
177 %token <spectype> T_EXTERN "extern"
178 %token <spectype> T_STATIC "static"
179 %token <spectype> T_AUTO "auto"
180 %token <spectype> T_REGISTER "register"
182 %token <spectype> T_INLINE "inline"
184 %token <spectype> T_RESTRICT "restrict"
185 %token <spectype> T_VOLATILE "volatile"
186 %token <spectype> T_CONST "const"
188 %token <spectype> T_VOID "void"
189 %token <spectype> T_CHAR "char"
190 %token <spectype> T_SHORT "short"
191 %token <spectype> T_INT "int"
192 %token <spectype> T_LONG "long"
193 %token <spectype> T_FLOAT "float"
194 %token <spectype> T_DOUBLE "double"
195 %token <spectype> T_SIGNED "signed"
196 %token <spectype> T_UNSIGNED "unsigned"
197 %token <spectype> T_BOOL "_Bool"
198 %token <spectype> T_COMPLEX "_Complex"
199 %token <spectype> T_IMAGINARY "_Imaginary"
201 %token <spectype> T_STRUCT "struct"
202 %token <spectype> T_UNION "union"
203 %token <spectype> T_ENUM "enum"
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 qualifier_simple
222 %type <spectype> typespec_simple typespec_tagged
223 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
224 %type <declspec> qualifier qualifiers
225 %type <declspec> declspecs declspecs_noid
226 %type <declarator> direct_declarator declarator pointer array parens postfix
227 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
228 %type <decl> declaration declarators declarator_wrap
229 %type <decl> parameter parameters
231 %type <strval> english_vla
232 %type <declspec> storage_func_specs post_specs
233 %type <declspec> type_qual_spec type_qual_specs typedef_name_qual
234 %type <declarator> english_declarator english_array english_function
235 %type <declarator> english_parameter_list null_decl
236 %type <decl> english_parameter english_parameters
237 %type <decl> english english_declaration
240 * Harmless shift/reduce conflicts in english_parameter. See comments below
247 input: T_ENGLISH english {
255 declaration: declspecs declarators semi {
258 for (struct cdecl *i = $$; i; i = i->next)
262 declspecs: declspec_notype declspecs {
265 } | typespec declspecs_noid {
270 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
275 qualifiers: { $$ = NULL; } | qualifiers qualifier {
280 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
285 declarator_wrap: declarator {
286 ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
289 declspec_simple: T_AUTO
296 typespec_simple: T_VOID
309 typespec_tagged: T_STRUCT | T_UNION | T_ENUM
311 qualifier_simple: T_CONST
315 declspec_notype: qualifier | declspec_simple {
316 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
319 typespec_noid: typespec_simple {
320 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
323 qualifier: qualifier_simple {
324 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
327 typespec: typespec_noid | typespec_tagged T_IDENT {
328 ALLOC_STRUCT($$, struct cdecl_declspec,
332 ALLOC_STRUCT($$, struct cdecl_declspec,
333 .type = CDECL_TYPE_IDENT,
337 declspec_noid: declspec_notype | typespec_noid
339 vla_ident: T_IDENT | T_ASTERISK {
340 ALLOC($$, sizeof "");
344 array: T_LBRACKET T_UINT T_RBRACKET {
346 FAIL(_("array length must be positive"));
348 ALLOC_STRUCT($$, struct cdecl_declarator,
349 .type = CDECL_DECL_ARRAY,
350 .u.array.length = $2);
351 } | T_LBRACKET vla_ident T_RBRACKET {
352 ALLOC_STRUCT($$, struct cdecl_declarator,
353 .type = CDECL_DECL_ARRAY,
355 } | T_LBRACKET T_RBRACKET {
356 ALLOC_STRUCT($$, struct cdecl_declarator,
357 .type = CDECL_DECL_ARRAY);
360 parameter: declspecs declarator {
361 ALLOC_STRUCT($$, struct cdecl,
366 parameters: parameter | parameters T_COMMA parameter {
371 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
373 parameter_type_list: parameters varargs {
374 struct cdecl *p, *c, *n;
376 /* Parameters were accumulated in reverse order. */
377 for (p = NULL, c = $1; c; p = c, c = n) {
382 ALLOC_STRUCT($$, struct cdecl_declarator,
383 .type = CDECL_DECL_FUNCTION,
384 .u.function.parameters = p,
385 .u.function.variadic = $2);
388 parens: T_LPAREN parameter_type_list T_RPAREN {
390 } | T_LPAREN declarator_ish T_RPAREN {
391 ALLOC_STRUCT($$, struct cdecl_declarator,
392 .type = CDECL_DECL_FUNCTION);
393 ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
397 pointer: T_ASTERISK qualifiers direct_declarator {
398 ALLOC_STRUCT($$, struct cdecl_declarator,
399 .type = CDECL_DECL_POINTER,
400 .u.pointer.qualifiers = $2,
402 } | T_ASTERISK qualifiers pointer {
403 ALLOC_STRUCT($$, struct cdecl_declarator,
404 .type = CDECL_DECL_POINTER,
405 .u.pointer.qualifiers = $2,
409 declarator: direct_declarator | pointer
410 declarator_ish: direct_declarator_ish | pointer
411 postfix: array | parens
413 direct_declarator_ish: {
414 ALLOC_STRUCT($$, struct cdecl_declarator,
415 .type = CDECL_DECL_NULL);
416 } | direct_declarator_ish postfix {
422 ALLOC_STRUCT($$, struct cdecl_declarator,
423 .type = CDECL_DECL_NULL);
425 ALLOC_STRUCT($$, struct cdecl_declarator,
426 .type = CDECL_DECL_IDENT,
428 } | direct_declarator postfix {
433 english: T_DECLARE T_IDENT T_AS english_declaration {
435 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
436 if (d->type == CDECL_DECL_NULL) {
437 d->type = CDECL_DECL_IDENT;
441 } | T_TYPE english_declaration {
445 storage_func_specs: { $$ = NULL; } | declspec_simple storage_func_specs {
446 ALLOC_STRUCT($$, struct cdecl_declspec,
451 type_qual_spec: typespec_noid | qualifier
453 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
459 * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
460 * conflicts with pointer declarators. So we end up needing to stitch
461 * together three different specifiers lists.
463 post_specs: qualifiers typespec type_qual_specs {
466 for (struct cdecl_declspec *s = $$; s; s = s->next) {
474 english_declaration: storage_func_specs english_declarator post_specs {
475 ALLOC_STRUCT($$, struct cdecl,
479 for (struct cdecl_declspec *s = $$->specifiers; s; s = s->next) {
487 english_declarator: {
488 ALLOC_STRUCT($$, struct cdecl_declarator,
489 .type = CDECL_DECL_NULL);
490 } | english_declarator qualifiers T_POINTER T_TO {
491 ALLOC_STRUCT($$, struct cdecl_declarator,
492 .type = CDECL_DECL_POINTER,
494 .u.pointer.qualifiers = $2);
495 } | english_declarator english_array {
498 } | english_declarator english_function {
503 english_function: T_FUNCTION T_RETURNING {
504 ALLOC_STRUCT($$, struct cdecl_declarator,
505 .type = CDECL_DECL_FUNCTION,
506 .u.function.parameters = NULL);
507 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
511 english_parameter_list: english_parameters varargs {
512 struct cdecl *p, *c, *n;
514 /* Parameters were accumulated in reverse order. */
515 for (p = NULL, c = $1; c; p = c, c = n) {
520 ALLOC_STRUCT($$, struct cdecl_declarator,
521 .type = CDECL_DECL_FUNCTION,
522 .u.function.parameters = p,
523 .u.function.variadic = $2);
526 english_parameters: english_parameters T_COMMA english_parameter {
529 } | english_parameter
531 typedef_name_qual: T_IDENT qualifiers {
532 ALLOC_STRUCT($$, struct cdecl_declspec,
533 .type = CDECL_TYPE_IDENT,
539 ALLOC_STRUCT($$, struct cdecl_declarator,
540 .type = CDECL_DECL_NULL);
544 * There is a small shift/reduce conflict here. An unadorned identifier
545 * as the first thing in the parameter might be a typedef name deep in the
546 * first english_declaration (thus empty storage_func_specs and empty
547 * english_declarator need to be reduced) or it might be the identifier
548 * before the "as" (thus the identifier should be shifted).
550 * The typedef name conflict is the only issue, so treating it as a special
551 * case makes the shift harmless.
553 english_parameter: english_declaration | typedef_name_qual null_decl {
554 ALLOC_STRUCT($$, struct cdecl,
557 } | T_IDENT T_AS english_declaration {
559 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
560 if (d->type == CDECL_DECL_NULL) {
561 d->type = CDECL_DECL_IDENT;
567 english_array: T_VLA T_ARRAY english_vla T_OF {
568 ALLOC_STRUCT($$, struct cdecl_declarator,
569 .type = CDECL_DECL_ARRAY,
571 } | T_ARRAY T_UINT T_OF {
573 FAIL(_("array length must be positive"));
575 ALLOC_STRUCT($$, struct cdecl_declarator,
576 .type = CDECL_DECL_ARRAY,
577 .u.array.length = $2);
579 ALLOC_STRUCT($$, struct cdecl_declarator,
580 .type = CDECL_DECL_ARRAY,
581 .u.array.length = 0);
584 english_vla: T_IDENT | {
585 ALLOC($$, sizeof "");
592 * Expose the token string table to the rest of the library, in order to
593 * produce strings that match parser keywords.
595 * In order for this to work properly, the Bison output must be postprocessed
596 * by fix-yytname.awk to remove pointless quotation marks from the keyword
599 const char *cdecl__token_name(unsigned token)
601 return yytname[YYTRANSLATE(token)];