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 %destructor { free($$); } <strval>
148 %destructor { free_declspec($$); } <declspec>
149 %destructor { free_declarator($$); } <declarator>
150 %destructor { free_decl($$); } <decl>
156 %token <strval> T_IDENT "identifier"
157 %token <uintval> T_UINT "integer constant"
159 %token T_SEMICOLON ";"
160 %token T_ASTERISK "*"
163 %token T_LBRACKET "["
164 %token T_RBRACKET "]"
166 %token T_ELLIPSIS "..."
168 %token <spectype> T_TYPEDEF "typedef"
169 %token <spectype> T_EXTERN "extern"
170 %token <spectype> T_STATIC "static"
171 %token <spectype> T_AUTO "auto"
172 %token <spectype> T_REGISTER "register"
174 %token <spectype> T_INLINE "inline"
176 %token <spectype> T_RESTRICT "restrict"
177 %token <spectype> T_VOLATILE "volatile"
178 %token <spectype> T_CONST "const"
180 %token <spectype> T_VOID "void"
181 %token <spectype> T_CHAR "char"
182 %token <spectype> T_SHORT "short"
183 %token <spectype> T_INT "int"
184 %token <spectype> T_LONG "long"
185 %token <spectype> T_FLOAT "float"
186 %token <spectype> T_DOUBLE "double"
187 %token <spectype> T_SIGNED "signed"
188 %token <spectype> T_UNSIGNED "unsigned"
189 %token <spectype> T_BOOL "_Bool"
190 %token <spectype> T_COMPLEX "_Complex"
191 %token <spectype> T_IMAGINARY "_Imaginary"
193 %token <spectype> T_STRUCT "struct"
194 %token <spectype> T_UNION "union"
195 %token <spectype> T_ENUM "enum"
201 %token T_DECLARE "declare"
202 %token T_POINTER "pointer"
203 %token T_FUNCTION "function"
204 %token T_RETURNING "returning"
205 %token T_ARRAY "array"
209 %token T_VLA "variable-length"
211 %type <strval> vla_ident
212 %type <boolval> varargs
213 %type <spectype> declspec_simple qualifier_simple
214 %type <spectype> typespec_simple typespec_tagged
215 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
216 %type <declspec> qualifier qualifiers
217 %type <declspec> declspecs declspecs_noid
218 %type <declarator> direct_declarator declarator pointer array parens postfix
219 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
220 %type <decl> declaration declarators declarator_wrap
221 %type <decl> parameter parameters
223 %type <strval> english_vla
224 %type <declspec> storage_func_specs post_specs
225 %type <declspec> type_qual_spec type_qual_specs typedef_name_qual
226 %type <declarator> english_declarator english_array english_function
227 %type <declarator> english_parameter_list null_decl
228 %type <decl> english_parameter english_parameters
229 %type <decl> english english_declaration
232 * Harmless shift/reduce conflicts in english_parameter. See comments below
239 input: T_ENGLISH english {
247 declaration: declspecs declarators semi {
253 * We support parsing declarations using arbitrary identifiers as type
254 * specifiers (a la C typedef). To avoid confusion with identifiers that
255 * may also be used as declarators, note the following:
257 * (a) Every valid C declaration must have at least one type specifier, and
258 * (b) Valid declarations with typedef names have exactly one type specifier.
260 * So the rule applied when parsing specifiers is: an identifier is a type
261 * specifier only if we have not yet seen any type specifiers whatsoever
262 * (within one declaration specifier list).
264 * Treating identifiers as type specifiers by default can lead to strange and
265 * unexpected parses; libcdecl applies a simplification step to the resulting
266 * parse tree afterwards.
268 declspecs: declspec_notype declspecs {
271 } | typespec declspecs_noid {
276 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
281 qualifiers: { $$ = NULL; } | qualifiers qualifier {
286 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
291 declarator_wrap: declarator {
292 ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
295 declspec_simple: T_AUTO
302 typespec_simple: T_VOID
315 typespec_tagged: T_STRUCT | T_UNION | T_ENUM
317 qualifier_simple: T_CONST
321 declspec_notype: qualifier | declspec_simple {
322 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
325 typespec_noid: typespec_simple {
326 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
329 qualifier: qualifier_simple {
330 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
333 typespec: typespec_noid | typespec_tagged T_IDENT {
334 ALLOC_STRUCT($$, struct cdecl_declspec,
338 ALLOC_STRUCT($$, struct cdecl_declspec,
339 .type = CDECL_TYPE_IDENT,
343 declspec_noid: declspec_notype | typespec_noid
345 vla_ident: T_IDENT | T_ASTERISK {
346 ALLOC($$, sizeof "");
350 array: T_LBRACKET T_UINT T_RBRACKET {
352 FAIL(_("array length must be positive"));
354 ALLOC_STRUCT($$, struct cdecl_declarator,
355 .type = CDECL_DECL_ARRAY,
356 .u.array.length = $2);
357 } | T_LBRACKET vla_ident T_RBRACKET {
358 ALLOC_STRUCT($$, struct cdecl_declarator,
359 .type = CDECL_DECL_ARRAY,
361 } | T_LBRACKET T_RBRACKET {
362 ALLOC_STRUCT($$, struct cdecl_declarator,
363 .type = CDECL_DECL_ARRAY);
366 parameter: declspecs declarator {
367 ALLOC_STRUCT($$, struct cdecl,
372 parameters: parameter | parameters T_COMMA parameter {
377 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
379 parameter_type_list: parameters varargs {
380 struct cdecl *p, *c, *n;
382 /* Parameters were accumulated in reverse order. */
383 for (p = NULL, c = $1; c; p = c, c = n) {
388 ALLOC_STRUCT($$, struct cdecl_declarator,
389 .type = CDECL_DECL_FUNCTION,
390 .u.function.parameters = p,
391 .u.function.variadic = $2);
394 parens: T_LPAREN parameter_type_list T_RPAREN {
396 } | T_LPAREN declarator_ish T_RPAREN {
397 ALLOC_STRUCT($$, struct cdecl_declarator,
398 .type = CDECL_DECL_FUNCTION);
399 ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
403 pointer: T_ASTERISK qualifiers direct_declarator {
404 ALLOC_STRUCT($$, struct cdecl_declarator,
405 .type = CDECL_DECL_POINTER,
406 .u.pointer.qualifiers = $2,
408 } | T_ASTERISK qualifiers pointer {
409 ALLOC_STRUCT($$, struct cdecl_declarator,
410 .type = CDECL_DECL_POINTER,
411 .u.pointer.qualifiers = $2,
415 declarator: direct_declarator | pointer
416 declarator_ish: direct_declarator_ish | pointer
417 postfix: array | parens
419 direct_declarator_ish: {
420 ALLOC_STRUCT($$, struct cdecl_declarator,
421 .type = CDECL_DECL_NULL);
422 } | direct_declarator_ish postfix {
428 ALLOC_STRUCT($$, struct cdecl_declarator,
429 .type = CDECL_DECL_NULL);
431 ALLOC_STRUCT($$, struct cdecl_declarator,
432 .type = CDECL_DECL_IDENT,
434 } | direct_declarator postfix {
439 english: T_DECLARE T_IDENT T_AS english_declaration {
441 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
442 if (d->type == CDECL_DECL_NULL) {
443 d->type = CDECL_DECL_IDENT;
447 } | T_TYPE english_declaration {
451 storage_func_specs: { $$ = NULL; } | declspec_simple storage_func_specs {
452 ALLOC_STRUCT($$, struct cdecl_declspec,
457 type_qual_spec: typespec_noid | qualifier
459 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
465 * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
466 * conflicts with pointer declarators. So we end up needing to stitch
467 * together three different specifiers lists.
469 post_specs: qualifiers typespec type_qual_specs {
472 for (struct cdecl_declspec *s = $$; s; s = s->next) {
480 english_declaration: storage_func_specs english_declarator post_specs {
481 ALLOC_STRUCT($$, struct cdecl,
485 for (struct cdecl_declspec *s = $$->specifiers; s; s = s->next) {
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_parameters varargs {
518 struct cdecl *p, *c, *n;
520 /* Parameters were accumulated in reverse order. */
521 for (p = NULL, c = $1; c; p = c, c = n) {
526 ALLOC_STRUCT($$, struct cdecl_declarator,
527 .type = CDECL_DECL_FUNCTION,
528 .u.function.parameters = p,
529 .u.function.variadic = $2);
532 english_parameters: english_parameters T_COMMA english_parameter {
535 } | english_parameter
537 typedef_name_qual: T_IDENT qualifiers {
538 ALLOC_STRUCT($$, struct cdecl_declspec,
539 .type = CDECL_TYPE_IDENT,
545 ALLOC_STRUCT($$, struct cdecl_declarator,
546 .type = CDECL_DECL_NULL);
550 * There is a small shift/reduce conflict here. An unadorned identifier
551 * as the first thing in the parameter might be a typedef name deep in the
552 * first english_declaration (thus empty storage_func_specs and empty
553 * english_declarator need to be reduced) or it might be the identifier
554 * before the "as" (thus the identifier should be shifted).
556 * The typedef name conflict is the only issue, so treating it as a special
557 * case makes the shift harmless.
559 english_parameter: english_declaration | typedef_name_qual null_decl {
560 ALLOC_STRUCT($$, struct cdecl,
563 } | T_IDENT T_AS english_declaration {
565 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
566 if (d->type == CDECL_DECL_NULL) {
567 d->type = CDECL_DECL_IDENT;
573 english_array: T_VLA T_ARRAY english_vla T_OF {
574 ALLOC_STRUCT($$, struct cdecl_declarator,
575 .type = CDECL_DECL_ARRAY,
577 } | T_ARRAY T_UINT T_OF {
579 FAIL(_("array length must be positive"));
581 ALLOC_STRUCT($$, struct cdecl_declarator,
582 .type = CDECL_DECL_ARRAY,
583 .u.array.length = $2);
585 ALLOC_STRUCT($$, struct cdecl_declarator,
586 .type = CDECL_DECL_ARRAY,
587 .u.array.length = 0);
590 english_vla: T_IDENT | {
591 ALLOC($$, sizeof "");
598 * Expose the token string table to the rest of the library, in order to
599 * produce strings that match parser keywords.
601 * In order for this to work properly, the Bison output must be postprocessed
602 * by fix-yytname.awk to remove pointless quotation marks from the keyword
605 const char *cdecl__token_name(unsigned token)
607 return yytname[YYTRANSLATE(token)];
611 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err)
613 if (strstr(err, yytname[YYTRANSLATE(T_LEX_ERROR)]))
616 cdecl__err(CDECL_ENOPARSE, "%s", err);