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);
72 struct cdecl_declspec *declspec;
73 struct cdecl_declarator *declarator;
78 static void free_decl(struct cdecl *);
80 static void free_declspec(struct cdecl_declspec *x)
82 struct cdecl_declspec *p;
91 static void free_declarator(struct cdecl_declarator *x)
93 struct cdecl_declarator *p;
101 case CDECL_DECL_IDENT:
104 case CDECL_DECL_POINTER:
105 free_declspec(x->u.pointer.qualifiers);
107 case CDECL_DECL_ARRAY:
108 free(x->u.array.vla);
110 case CDECL_DECL_FUNCTION:
111 free_decl(x->u.function.parameters);
122 static void free_decl(struct cdecl *x)
129 /* The specifiers may be shared by an entire chain. */
130 if (!p || p->specifiers != x->specifiers)
131 free_declspec(x->specifiers);
133 free_declarator(x->declarators);
139 void cdecl__free(struct cdecl *decl)
145 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err)
147 if (strstr(err, "T_LEX_ERROR"))
150 cdecl__err(CDECL_ENOPARSE, "%s", err);
154 %destructor { free($$); } <strval>
155 %destructor { free_declspec($$); } <declspec>
156 %destructor { free_declarator($$); } <declarator>
157 %destructor { free_decl($$); } <decl>
163 %token <strval> T_IDENT "identifier"
164 %token <uintval> T_UINT "integer constant"
166 %token T_SEMICOLON ";"
167 %token T_ASTERISK "*"
170 %token T_LBRACKET "["
171 %token T_RBRACKET "]"
173 %token T_ELLIPSIS "..."
175 %token T_TYPEDEF "typedef"
176 %token T_EXTERN "extern"
177 %token T_STATIC "static"
179 %token T_REGISTER "register"
181 %token T_INLINE "inline"
183 %token T_RESTRICT "restrict"
184 %token T_VOLATILE "volatile"
185 %token T_CONST "const"
189 %token T_SHORT "short"
192 %token T_FLOAT "float"
193 %token T_DOUBLE "double"
194 %token T_SIGNED "signed"
195 %token T_UNSIGNED "unsigned"
196 %token T_BOOL "_Bool"
197 %token T_COMPLEX "_Complex"
198 %token T_IMAGINARY "_Imaginary"
200 %token T_STRUCT "struct"
201 %token T_UNION "union"
208 %token T_DECLARE "declare"
209 %token T_POINTER "pointer"
210 %token T_FUNCTION "function"
211 %token T_RETURNING "returning"
212 %token T_ARRAY "array"
216 %token T_VLA "variable-length"
218 %type <strval> vla_ident
219 %type <boolval> varargs
220 %type <spectype> declspec_simple typespec_simple qualifier_simple
221 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
222 %type <declspec> qualifier qualifiers
223 %type <declspec> declspecs declspecs_noid
224 %type <declarator> direct_declarator declarator pointer array parens postfix
225 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
226 %type <decl> declaration declarators declarator_wrap
227 %type <decl> parameter parameters
229 %type <strval> english_vla
230 %type <declspec> storage_func_specs post_specs
231 %type <declspec> type_qual_spec type_qual_specs typedef_name_qual
232 %type <declarator> english_declarator english_array english_function
233 %type <declarator> english_parameter_list null_decl
234 %type <decl> english_parameter english_parameters
235 %type <decl> english english_declaration
238 * Harmless shift/reduce conflicts in english_parameter. See comments below
245 input: T_ENGLISH english {
253 declaration: declspecs declarators semi {
256 for (struct cdecl *i = $$; i; i = i->next)
260 declspecs: declspec_notype declspecs {
263 } | typespec declspecs_noid {
268 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
273 qualifiers: { $$ = NULL; } | qualifiers qualifier {
278 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
283 declarator_wrap: declarator {
284 ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
287 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO; }
288 | T_TYPEDEF { $$ = CDECL_STOR_TYPEDEF; }
289 | T_EXTERN { $$ = CDECL_STOR_EXTERN; }
290 | T_STATIC { $$ = CDECL_STOR_STATIC; }
291 | T_REGISTER { $$ = CDECL_STOR_REGISTER; }
292 | T_INLINE { $$ = CDECL_FUNC_INLINE; }
294 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID; }
295 | T_CHAR { $$ = CDECL_TYPE_CHAR; }
296 | T_SHORT { $$ = CDECL_TYPE_SHORT; }
297 | T_INT { $$ = CDECL_TYPE_INT; }
298 | T_LONG { $$ = CDECL_TYPE_LONG; }
299 | T_FLOAT { $$ = CDECL_TYPE_FLOAT; }
300 | T_DOUBLE { $$ = CDECL_TYPE_DOUBLE; }
301 | T_SIGNED { $$ = CDECL_TYPE_SIGNED; }
302 | T_UNSIGNED { $$ = CDECL_TYPE_UNSIGNED; }
303 | T_BOOL { $$ = CDECL_TYPE_BOOL; }
304 | T_COMPLEX { $$ = CDECL_TYPE_COMPLEX; }
305 | T_IMAGINARY { $$ = CDECL_TYPE_IMAGINARY; }
307 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST; }
308 | T_RESTRICT { $$ = CDECL_QUAL_RESTRICT; }
309 | T_VOLATILE { $$ = CDECL_QUAL_VOLATILE; }
311 declspec_notype: qualifier | declspec_simple {
312 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
315 typespec_noid: typespec_simple {
316 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
319 qualifier: qualifier_simple {
320 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
323 typespec: typespec_noid | T_STRUCT T_IDENT {
324 ALLOC_STRUCT($$, struct cdecl_declspec,
325 .type = CDECL_TYPE_STRUCT,
327 } | T_UNION T_IDENT {
328 ALLOC_STRUCT($$, struct cdecl_declspec,
329 .type = CDECL_TYPE_UNION,
332 ALLOC_STRUCT($$, struct cdecl_declspec,
333 .type = CDECL_TYPE_ENUM,
336 ALLOC_STRUCT($$, struct cdecl_declspec,
337 .type = CDECL_TYPE_IDENT,
341 declspec_noid: declspec_notype | typespec_noid
343 vla_ident: T_IDENT | T_ASTERISK {
344 ALLOC($$, sizeof "");
348 array: T_LBRACKET T_UINT T_RBRACKET {
350 FAIL(_("array length must be positive"));
352 ALLOC_STRUCT($$, struct cdecl_declarator,
353 .type = CDECL_DECL_ARRAY,
354 .u.array.length = $2);
355 } | T_LBRACKET vla_ident T_RBRACKET {
356 ALLOC_STRUCT($$, struct cdecl_declarator,
357 .type = CDECL_DECL_ARRAY,
359 } | T_LBRACKET T_RBRACKET {
360 ALLOC_STRUCT($$, struct cdecl_declarator,
361 .type = CDECL_DECL_ARRAY);
364 parameter: declspecs declarator {
365 ALLOC_STRUCT($$, struct cdecl,
370 parameters: parameter | parameters T_COMMA parameter {
375 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
377 parameter_type_list: parameters varargs {
378 struct cdecl *p, *c, *n;
380 /* Parameters were accumulated in reverse order. */
381 for (p = NULL, c = $1; c; p = c, c = n) {
386 ALLOC_STRUCT($$, struct cdecl_declarator,
387 .type = CDECL_DECL_FUNCTION,
388 .u.function.parameters = p,
389 .u.function.variadic = $2);
392 parens: T_LPAREN parameter_type_list T_RPAREN {
394 } | T_LPAREN declarator_ish T_RPAREN {
395 ALLOC_STRUCT($$, struct cdecl_declarator,
396 .type = CDECL_DECL_FUNCTION);
397 ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
401 pointer: T_ASTERISK qualifiers direct_declarator {
402 ALLOC_STRUCT($$, struct cdecl_declarator,
403 .type = CDECL_DECL_POINTER,
404 .u.pointer.qualifiers = $2,
406 } | T_ASTERISK qualifiers pointer {
407 ALLOC_STRUCT($$, struct cdecl_declarator,
408 .type = CDECL_DECL_POINTER,
409 .u.pointer.qualifiers = $2,
413 declarator: direct_declarator | pointer
414 declarator_ish: direct_declarator_ish | pointer
415 postfix: array | parens
417 direct_declarator_ish: {
418 ALLOC_STRUCT($$, struct cdecl_declarator,
419 .type = CDECL_DECL_NULL);
420 } | direct_declarator_ish postfix {
426 ALLOC_STRUCT($$, struct cdecl_declarator,
427 .type = CDECL_DECL_NULL);
429 ALLOC_STRUCT($$, struct cdecl_declarator,
430 .type = CDECL_DECL_IDENT,
432 } | direct_declarator postfix {
437 english: T_DECLARE T_IDENT T_AS english_declaration {
439 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
440 if (d->type == CDECL_DECL_NULL) {
441 d->type = CDECL_DECL_IDENT;
445 } | T_TYPE english_declaration {
449 storage_func_specs: { $$ = NULL; } | declspec_simple storage_func_specs {
450 ALLOC_STRUCT($$, struct cdecl_declspec,
455 type_qual_spec: typespec_noid | qualifier
457 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
463 * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
464 * conflicts with pointer declarators. So we end up needing to stitch
465 * together three different specifiers lists.
467 post_specs: qualifiers typespec type_qual_specs {
470 for (struct cdecl_declspec *s = $$; s; s = s->next) {
478 english_declaration: storage_func_specs english_declarator post_specs {
479 ALLOC_STRUCT($$, struct cdecl,
483 for (struct cdecl_declspec *s = $$->specifiers; s; s = s->next) {
491 english_declarator: {
492 ALLOC_STRUCT($$, struct cdecl_declarator,
493 .type = CDECL_DECL_NULL);
494 } | english_declarator qualifiers T_POINTER T_TO {
495 ALLOC_STRUCT($$, struct cdecl_declarator,
496 .type = CDECL_DECL_POINTER,
498 .u.pointer.qualifiers = $2);
499 } | english_declarator english_array {
502 } | english_declarator english_function {
507 english_function: T_FUNCTION T_RETURNING {
508 ALLOC_STRUCT($$, struct cdecl_declarator,
509 .type = CDECL_DECL_FUNCTION,
510 .u.function.parameters = NULL);
511 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
515 english_parameter_list: english_parameters varargs {
516 struct cdecl *p, *c, *n;
518 /* Parameters were accumulated in reverse order. */
519 for (p = NULL, c = $1; c; p = c, c = n) {
524 ALLOC_STRUCT($$, struct cdecl_declarator,
525 .type = CDECL_DECL_FUNCTION,
526 .u.function.parameters = p,
527 .u.function.variadic = $2);
530 english_parameters: english_parameters T_COMMA english_parameter {
533 } | english_parameter
535 typedef_name_qual: T_IDENT qualifiers {
536 ALLOC_STRUCT($$, struct cdecl_declspec,
537 .type = CDECL_TYPE_IDENT,
543 ALLOC_STRUCT($$, struct cdecl_declarator,
544 .type = CDECL_DECL_NULL);
548 * There is a small shift/reduce conflict here. An unadorned identifier
549 * as the first thing in the parameter might be a typedef name deep in the
550 * first english_declaration (thus empty storage_func_specs and empty
551 * english_declarator need to be reduced) or it might be the identifier
552 * before the "as" (thus the identifier should be shifted).
554 * The typedef name conflict is the only issue, so treating it as a special
555 * case makes the shift harmless.
557 english_parameter: english_declaration | typedef_name_qual null_decl {
558 ALLOC_STRUCT($$, struct cdecl,
561 } | T_IDENT T_AS english_declaration {
563 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
564 if (d->type == CDECL_DECL_NULL) {
565 d->type = CDECL_DECL_IDENT;
571 english_array: T_VLA T_ARRAY english_vla T_OF {
572 ALLOC_STRUCT($$, struct cdecl_declarator,
573 .type = CDECL_DECL_ARRAY,
575 } | T_ARRAY T_UINT T_OF {
577 FAIL(_("array length must be positive"));
579 ALLOC_STRUCT($$, struct cdecl_declarator,
580 .type = CDECL_DECL_ARRAY,
581 .u.array.length = $2);
583 ALLOC_STRUCT($$, struct cdecl_declarator,
584 .type = CDECL_DECL_ARRAY,
585 .u.array.length = 0);
588 english_vla: T_IDENT | {
589 ALLOC($$, sizeof "");