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 %destructor { free($$); } <strval>
161 %destructor { free_declspec($$); } <declspec>
162 %destructor { free_declarator($$); } <declarator>
163 %destructor { free_decl($$); } <decl>
169 %token <strval> T_IDENT "identifier"
170 %token <uintval> T_UINT "integer constant"
172 %token T_SEMICOLON ";"
173 %token T_ASTERISK "*"
176 %token T_LBRACKET "["
177 %token T_RBRACKET "]"
179 %token T_ELLIPSIS "..."
181 %token <spectype> T_TYPEDEF "typedef"
182 %token <spectype> T_EXTERN "extern"
183 %token <spectype> T_STATIC "static"
184 %token <spectype> T_AUTO "auto"
185 %token <spectype> T_REGISTER "register"
187 %token <spectype> T_INLINE "inline"
189 %token <spectype> T_RESTRICT "restrict"
190 %token <spectype> T_VOLATILE "volatile"
191 %token <spectype> T_CONST "const"
193 %token <spectype> T_VOID "void"
194 %token <spectype> T_CHAR "char"
195 %token <spectype> T_SHORT "short"
196 %token <spectype> T_INT "int"
197 %token <spectype> T_LONG "long"
198 %token <spectype> T_FLOAT "float"
199 %token <spectype> T_DOUBLE "double"
200 %token <spectype> T_SIGNED "signed"
201 %token <spectype> T_UNSIGNED "unsigned"
202 %token <spectype> T_BOOL "_Bool"
203 %token <spectype> T_COMPLEX "_Complex"
204 %token <spectype> T_IMAGINARY "_Imaginary"
206 %token <spectype> T_STRUCT "struct"
207 %token <spectype> T_UNION "union"
208 %token <spectype> T_ENUM "enum"
214 %token T_DECLARE "declare"
215 %token T_POINTER "pointer"
216 %token T_FUNCTION "function"
217 %token T_RETURNING "returning"
218 %token T_ARRAY "array"
222 %token T_VLA "variable-length"
224 %type <strval> vla_ident
225 %type <boolval> varargs
226 %type <spectype> declspec_simple qualifier_simple
227 %type <spectype> typespec_simple typespec_tagged
228 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
229 %type <declspec> qualifier qualifiers
230 %type <declspec> declspecs declspecs_noid
231 %type <declarator> direct_declarator declarator pointer array parens postfix
232 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
233 %type <decl> declaration declarators declarator_wrap
234 %type <decl> parameter parameters
236 %type <strval> english_vla
237 %type <declspec> storage_func_specs post_specs
238 %type <declspec> type_qual_spec type_qual_specs typedef_name_qual
239 %type <declarator> english_declarator english_array english_function
240 %type <declarator> english_parameter_list null_decl
241 %type <decl> english_parameter english_parameters
242 %type <decl> english english_declaration
245 * Harmless shift/reduce conflicts in english_parameter. See comments below
252 input: T_ENGLISH english {
260 declaration: declspecs declarators semi {
266 * We support parsing declarations using arbitrary identifiers as type
267 * specifiers (a la C typedef). To avoid confusion with identifiers that
268 * may also be used as declarators, note the following:
270 * (a) Every valid C declaration must have at least one type specifier, and
271 * (b) Valid declarations with typedef names have exactly one type specifier.
273 * So the rule applied when parsing specifiers is: an identifier is a type
274 * specifier only if we have not yet seen any type specifiers whatsoever
275 * (within one declaration specifier list).
277 * Treating identifiers as type specifiers by default can lead to strange and
278 * unexpected parses; libcdecl applies a simplification step to the resulting
279 * parse tree afterwards.
281 declspecs: declspec_notype declspecs {
284 } | typespec declspecs_noid {
289 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
294 qualifiers: { $$ = NULL; } | qualifiers qualifier {
299 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
304 declarator_wrap: declarator {
305 ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
308 declspec_simple: T_AUTO
315 typespec_simple: T_VOID
328 typespec_tagged: T_STRUCT | T_UNION | T_ENUM
330 qualifier_simple: T_CONST
334 declspec_notype: qualifier | declspec_simple {
335 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
338 typespec_noid: typespec_simple {
339 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
342 qualifier: qualifier_simple {
343 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
346 typespec: typespec_noid | typespec_tagged T_IDENT {
347 ALLOC_STRUCT($$, struct cdecl_declspec,
351 ALLOC_STRUCT($$, struct cdecl_declspec,
352 .type = CDECL_TYPE_IDENT,
356 declspec_noid: declspec_notype | typespec_noid
358 vla_ident: T_IDENT | T_ASTERISK {
359 ALLOC($$, sizeof "");
363 array: T_LBRACKET T_UINT T_RBRACKET {
365 FAIL(_("array length must be positive"));
367 ALLOC_STRUCT($$, struct cdecl_declarator,
368 .type = CDECL_DECL_ARRAY,
369 .u.array.length = $2);
370 } | T_LBRACKET vla_ident T_RBRACKET {
371 ALLOC_STRUCT($$, struct cdecl_declarator,
372 .type = CDECL_DECL_ARRAY,
374 } | T_LBRACKET T_RBRACKET {
375 ALLOC_STRUCT($$, struct cdecl_declarator,
376 .type = CDECL_DECL_ARRAY);
379 parameter: declspecs declarator {
380 ALLOC_STRUCT($$, struct cdecl,
385 parameters: parameter | parameters T_COMMA parameter {
390 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
392 parameter_type_list: parameters varargs {
393 struct cdecl *p, *c, *n;
395 /* Parameters were accumulated in reverse order. */
396 for (p = NULL, c = $1; c; p = c, c = n) {
401 ALLOC_STRUCT($$, struct cdecl_declarator,
402 .type = CDECL_DECL_FUNCTION,
403 .u.function.parameters = p,
404 .u.function.variadic = $2);
407 parens: T_LPAREN parameter_type_list T_RPAREN {
409 } | T_LPAREN declarator_ish T_RPAREN {
410 ALLOC_STRUCT($$, struct cdecl_declarator,
411 .type = CDECL_DECL_FUNCTION);
412 ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
416 pointer: T_ASTERISK qualifiers direct_declarator {
417 ALLOC_STRUCT($$, struct cdecl_declarator,
418 .type = CDECL_DECL_POINTER,
419 .u.pointer.qualifiers = $2,
421 } | T_ASTERISK qualifiers pointer {
422 ALLOC_STRUCT($$, struct cdecl_declarator,
423 .type = CDECL_DECL_POINTER,
424 .u.pointer.qualifiers = $2,
428 declarator: direct_declarator | pointer
429 declarator_ish: direct_declarator_ish | pointer
430 postfix: array | parens
432 direct_declarator_ish: {
433 ALLOC_STRUCT($$, struct cdecl_declarator,
434 .type = CDECL_DECL_NULL);
435 } | direct_declarator_ish postfix {
441 ALLOC_STRUCT($$, struct cdecl_declarator,
442 .type = CDECL_DECL_NULL);
444 ALLOC_STRUCT($$, struct cdecl_declarator,
445 .type = CDECL_DECL_IDENT,
447 } | direct_declarator postfix {
452 english: T_DECLARE T_IDENT T_AS english_declaration {
454 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
455 if (d->type == CDECL_DECL_NULL) {
456 d->type = CDECL_DECL_IDENT;
460 } | T_TYPE english_declaration {
464 storage_func_specs: { $$ = NULL; } | declspec_simple storage_func_specs {
465 ALLOC_STRUCT($$, struct cdecl_declspec,
470 type_qual_spec: typespec_noid | qualifier
472 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
478 * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
479 * conflicts with pointer declarators. So we end up needing to stitch
480 * together three different specifiers lists.
482 post_specs: qualifiers typespec type_qual_specs {
488 english_declaration: storage_func_specs english_declarator post_specs {
490 ALLOC_STRUCT($$, struct cdecl,
495 english_declarator: {
496 ALLOC_STRUCT($$, struct cdecl_declarator,
497 .type = CDECL_DECL_NULL);
498 } | english_declarator qualifiers T_POINTER T_TO {
499 ALLOC_STRUCT($$, struct cdecl_declarator,
500 .type = CDECL_DECL_POINTER,
502 .u.pointer.qualifiers = $2);
503 } | english_declarator english_array {
506 } | english_declarator english_function {
511 english_function: T_FUNCTION T_RETURNING {
512 ALLOC_STRUCT($$, struct cdecl_declarator,
513 .type = CDECL_DECL_FUNCTION,
514 .u.function.parameters = NULL);
515 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
519 english_parameter_list: english_parameters varargs {
520 struct cdecl *p, *c, *n;
522 /* Parameters were accumulated in reverse order. */
523 for (p = NULL, c = $1; c; p = c, c = n) {
528 ALLOC_STRUCT($$, struct cdecl_declarator,
529 .type = CDECL_DECL_FUNCTION,
530 .u.function.parameters = p,
531 .u.function.variadic = $2);
534 english_parameters: english_parameters T_COMMA english_parameter {
537 } | english_parameter
539 typedef_name_qual: T_IDENT qualifiers {
540 ALLOC_STRUCT($$, struct cdecl_declspec,
541 .type = CDECL_TYPE_IDENT,
547 ALLOC_STRUCT($$, struct cdecl_declarator,
548 .type = CDECL_DECL_NULL);
552 * There is a small shift/reduce conflict here. An unadorned identifier
553 * as the first thing in the parameter might be a typedef name deep in the
554 * first english_declaration (thus empty storage_func_specs and empty
555 * english_declarator need to be reduced) or it might be the identifier
556 * before the "as" (thus the identifier should be shifted).
558 * The typedef name conflict is the only issue, so treating it as a special
559 * case makes the shift harmless.
561 english_parameter: english_declaration | typedef_name_qual null_decl {
562 ALLOC_STRUCT($$, struct cdecl,
565 } | T_IDENT T_AS english_declaration {
567 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
568 if (d->type == CDECL_DECL_NULL) {
569 d->type = CDECL_DECL_IDENT;
575 english_array: T_VLA T_ARRAY english_vla T_OF {
576 ALLOC_STRUCT($$, struct cdecl_declarator,
577 .type = CDECL_DECL_ARRAY,
579 } | T_ARRAY T_UINT T_OF {
581 FAIL(_("array length must be positive"));
583 ALLOC_STRUCT($$, struct cdecl_declarator,
584 .type = CDECL_DECL_ARRAY,
585 .u.array.length = $2);
587 ALLOC_STRUCT($$, struct cdecl_declarator,
588 .type = CDECL_DECL_ARRAY,
589 .u.array.length = 0);
592 english_vla: T_IDENT | {
593 ALLOC($$, sizeof "");
600 * Expose the token string table to the rest of the library, in order to
601 * produce strings that match parser keywords.
603 * In order for this to work properly, the Bison output must be postprocessed
604 * by fix-yytname.awk to remove pointless quotation marks from the keyword
607 const char *cdecl__token_name(unsigned token)
609 return yytname[YYTRANSLATE(token)];
613 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err)
615 if (strstr(err, yytname[YYTRANSLATE(T_LEX_ERROR)]))
618 cdecl__err(CDECL_ENOPARSE, "%s", err);