3 * Parser for C declarations.
4 * Copyright © 2011-2012, 2021 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"
38 #define FAIL(msg) do { \
39 yyerror(&yylloc, NULL, NULL, msg); \
43 #define ALLOC(ptr, size) do { \
44 (ptr) = malloc(size); \
46 cdecl__err(CDECL_ENOMEM); \
51 #define ALLOC_STRUCT(ptr, type, ...) do { \
52 ALLOC(ptr, sizeof (type)); \
53 *(ptr) = (type) { __VA_ARGS__ }; \
62 void cdecl__free(struct cdecl *);
63 int cdecl__yyparse(void *scanner, struct cdecl **out);
71 struct cdecl_declspec *declspec;
72 struct cdecl_declarator *declarator;
77 static void free_decl(struct cdecl *);
79 static void free_declspec(struct cdecl_declspec *x)
81 struct cdecl_declspec *p;
90 static void free_declarator(struct cdecl_declarator *x)
92 struct cdecl_declarator *p;
100 case CDECL_DECL_IDENT:
103 case CDECL_DECL_POINTER:
104 free_declspec(x->u.pointer.qualifiers);
106 case CDECL_DECL_ARRAY:
107 free(x->u.array.vla);
109 case CDECL_DECL_FUNCTION:
110 free_decl(x->u.function.parameters);
121 static void free_decl(struct cdecl *x)
128 /* The specifiers may be shared by an entire chain. */
129 if (!p || p->specifiers != x->specifiers)
130 free_declspec(x->specifiers);
132 free_declarator(x->declarators);
138 void cdecl__free(struct cdecl *decl)
144 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err)
146 if (strstr(err, "T_LEX_ERROR"))
149 cdecl__err(CDECL_ENOPARSE, "%s", err);
153 %destructor { free($$); } <strval>
154 %destructor { free_declspec($$); } <declspec>
155 %destructor { free_declarator($$); } <declarator>
156 %destructor { free_decl($$); } <decl>
162 %token <strval> T_IDENT "identifier"
163 %token <uintval> T_UINT "integer constant"
165 %token T_SEMICOLON ";"
166 %token T_ASTERISK "*"
169 %token T_LBRACKET "["
170 %token T_RBRACKET "]"
172 %token T_ELLIPSIS "..."
174 %token T_TYPEDEF "typedef"
175 %token T_EXTERN "extern"
176 %token T_STATIC "static"
178 %token T_REGISTER "register"
180 %token T_INLINE "inline"
182 %token T_RESTRICT "restrict"
183 %token T_VOLATILE "volatile"
184 %token T_CONST "const"
188 %token T_SHORT "short"
191 %token T_FLOAT "float"
192 %token T_DOUBLE "double"
193 %token T_SIGNED "signed"
194 %token T_UNSIGNED "unsigned"
195 %token T_BOOL "_Bool"
196 %token T_COMPLEX "_Complex"
197 %token T_IMAGINARY "_Imaginary"
199 %token T_STRUCT "struct"
200 %token T_UNION "union"
207 %token T_DECLARE "declare"
208 %token T_POINTER "pointer"
209 %token T_FUNCTION "function"
210 %token T_RETURNING "returning"
211 %token T_ARRAY "array"
215 %token T_VLA "variable-length"
217 %type <strval> vla_ident
218 %type <boolval> varargs
219 %type <spectype> declspec_simple typespec_simple qualifier_simple
220 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
221 %type <declspec> qualifier qualifiers
222 %type <declspec> declspecs declspecs_noid
223 %type <declarator> direct_declarator declarator pointer array parens postfix
224 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
225 %type <decl> declaration declarators declarator_wrap
226 %type <decl> parameter parameters
228 %type <strval> english_vla
229 %type <declspec> storage_func_specs post_specs
230 %type <declspec> type_qual_spec type_qual_specs typedef_name_qual
231 %type <declarator> english_declarator english_array english_function
232 %type <declarator> english_parameter_list null_decl
233 %type <decl> english_parameter english_parameters
234 %type <decl> english english_declaration
237 * Harmless shift/reduce conflicts in english_parameter. See comments below
244 input: T_ENGLISH english {
252 declaration: declspecs declarators semi {
255 for (struct cdecl *i = $$; i; i = i->next)
259 declspecs: declspec_notype declspecs {
262 } | typespec declspecs_noid {
267 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
272 qualifiers: { $$ = NULL; } | qualifiers qualifier {
277 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
282 declarator_wrap: declarator {
283 ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
286 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO; }
287 | T_TYPEDEF { $$ = CDECL_STOR_TYPEDEF; }
288 | T_EXTERN { $$ = CDECL_STOR_EXTERN; }
289 | T_STATIC { $$ = CDECL_STOR_STATIC; }
290 | T_REGISTER { $$ = CDECL_STOR_REGISTER; }
291 | T_INLINE { $$ = CDECL_FUNC_INLINE; }
293 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID; }
294 | T_CHAR { $$ = CDECL_TYPE_CHAR; }
295 | T_SHORT { $$ = CDECL_TYPE_SHORT; }
296 | T_INT { $$ = CDECL_TYPE_INT; }
297 | T_LONG { $$ = CDECL_TYPE_LONG; }
298 | T_FLOAT { $$ = CDECL_TYPE_FLOAT; }
299 | T_DOUBLE { $$ = CDECL_TYPE_DOUBLE; }
300 | T_SIGNED { $$ = CDECL_TYPE_SIGNED; }
301 | T_UNSIGNED { $$ = CDECL_TYPE_UNSIGNED; }
302 | T_BOOL { $$ = CDECL_TYPE_BOOL; }
303 | T_COMPLEX { $$ = CDECL_TYPE_COMPLEX; }
304 | T_IMAGINARY { $$ = CDECL_TYPE_IMAGINARY; }
306 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST; }
307 | T_RESTRICT { $$ = CDECL_QUAL_RESTRICT; }
308 | T_VOLATILE { $$ = CDECL_QUAL_VOLATILE; }
310 declspec_notype: qualifier | declspec_simple {
311 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
314 typespec_noid: typespec_simple {
315 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
318 qualifier: qualifier_simple {
319 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
322 typespec: typespec_noid | T_STRUCT T_IDENT {
323 ALLOC_STRUCT($$, struct cdecl_declspec,
324 .type = CDECL_TYPE_STRUCT,
326 } | T_UNION T_IDENT {
327 ALLOC_STRUCT($$, struct cdecl_declspec,
328 .type = CDECL_TYPE_UNION,
331 ALLOC_STRUCT($$, struct cdecl_declspec,
332 .type = CDECL_TYPE_ENUM,
335 ALLOC_STRUCT($$, struct cdecl_declspec,
336 .type = CDECL_TYPE_IDENT,
340 declspec_noid: declspec_notype | typespec_noid
342 vla_ident: T_IDENT | T_ASTERISK {
343 ALLOC($$, sizeof "");
347 array: T_LBRACKET T_UINT T_RBRACKET {
349 FAIL(_("array length must be positive"));
351 ALLOC_STRUCT($$, struct cdecl_declarator,
352 .type = CDECL_DECL_ARRAY,
353 .u.array.length = $2);
354 } | T_LBRACKET vla_ident T_RBRACKET {
355 ALLOC_STRUCT($$, struct cdecl_declarator,
356 .type = CDECL_DECL_ARRAY,
358 } | T_LBRACKET T_RBRACKET {
359 ALLOC_STRUCT($$, struct cdecl_declarator,
360 .type = CDECL_DECL_ARRAY);
363 parameter: declspecs declarator {
364 ALLOC_STRUCT($$, struct cdecl,
369 parameters: parameter | parameters T_COMMA parameter {
374 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
376 parameter_type_list: parameters varargs {
377 struct cdecl *p, *c, *n;
379 /* Parameters were accumulated in reverse order. */
380 for (p = NULL, c = $1; c; p = c, c = n) {
385 ALLOC_STRUCT($$, struct cdecl_declarator,
386 .type = CDECL_DECL_FUNCTION,
387 .u.function.parameters = p,
388 .u.function.variadic = $2);
391 parens: T_LPAREN parameter_type_list T_RPAREN {
393 } | T_LPAREN declarator_ish T_RPAREN {
394 ALLOC_STRUCT($$, struct cdecl_declarator,
395 .type = CDECL_DECL_FUNCTION);
396 ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
400 pointer: T_ASTERISK qualifiers direct_declarator {
401 ALLOC_STRUCT($$, struct cdecl_declarator,
402 .type = CDECL_DECL_POINTER,
403 .u.pointer.qualifiers = $2,
405 } | T_ASTERISK qualifiers pointer {
406 ALLOC_STRUCT($$, struct cdecl_declarator,
407 .type = CDECL_DECL_POINTER,
408 .u.pointer.qualifiers = $2,
412 declarator: direct_declarator | pointer
413 declarator_ish: direct_declarator_ish | pointer
414 postfix: array | parens
416 direct_declarator_ish: {
417 ALLOC_STRUCT($$, struct cdecl_declarator,
418 .type = CDECL_DECL_NULL);
419 } | direct_declarator_ish postfix {
425 ALLOC_STRUCT($$, struct cdecl_declarator,
426 .type = CDECL_DECL_NULL);
428 ALLOC_STRUCT($$, struct cdecl_declarator,
429 .type = CDECL_DECL_IDENT,
431 } | direct_declarator postfix {
436 english: T_DECLARE T_IDENT T_AS english_declaration {
438 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
439 if (d->type == CDECL_DECL_NULL) {
440 d->type = CDECL_DECL_IDENT;
444 } | T_TYPE english_declaration {
448 storage_func_specs: { $$ = NULL; } | declspec_simple storage_func_specs {
449 ALLOC_STRUCT($$, struct cdecl_declspec,
454 type_qual_spec: typespec_noid | qualifier
456 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
462 * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
463 * conflicts with pointer declarators. So we end up needing to stitch
464 * together three different specifiers lists.
466 post_specs: qualifiers typespec type_qual_specs {
469 for (struct cdecl_declspec *s = $$; s; s = s->next) {
477 english_declaration: storage_func_specs english_declarator post_specs {
478 ALLOC_STRUCT($$, struct cdecl,
482 for (struct cdecl_declspec *s = $$->specifiers; s; s = s->next) {
490 english_declarator: {
491 ALLOC_STRUCT($$, struct cdecl_declarator,
492 .type = CDECL_DECL_NULL);
493 } | english_declarator qualifiers T_POINTER T_TO {
494 ALLOC_STRUCT($$, struct cdecl_declarator,
495 .type = CDECL_DECL_POINTER,
497 .u.pointer.qualifiers = $2);
498 } | english_declarator english_array {
501 } | english_declarator english_function {
506 english_function: T_FUNCTION T_RETURNING {
507 ALLOC_STRUCT($$, struct cdecl_declarator,
508 .type = CDECL_DECL_FUNCTION,
509 .u.function.parameters = NULL);
510 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
514 english_parameter_list: english_parameters varargs {
515 struct cdecl *p, *c, *n;
517 /* Parameters were accumulated in reverse order. */
518 for (p = NULL, c = $1; c; p = c, c = n) {
523 ALLOC_STRUCT($$, struct cdecl_declarator,
524 .type = CDECL_DECL_FUNCTION,
525 .u.function.parameters = p,
526 .u.function.variadic = $2);
529 english_parameters: english_parameters T_COMMA english_parameter {
532 } | english_parameter
534 typedef_name_qual: T_IDENT qualifiers {
535 ALLOC_STRUCT($$, struct cdecl_declspec,
536 .type = CDECL_TYPE_IDENT,
542 ALLOC_STRUCT($$, struct cdecl_declarator,
543 .type = CDECL_DECL_NULL);
547 * There is a small shift/reduce conflict here. An unadorned identifier
548 * as the first thing in the parameter might be a typedef name deep in the
549 * first english_declaration (thus empty storage_func_specs and empty
550 * english_declarator need to be reduced) or it might be the identifier
551 * before the "as" (thus the identifier should be shifted).
553 * The typedef name conflict is the only issue, so treating it as a special
554 * case makes the shift harmless.
556 english_parameter: english_declaration | typedef_name_qual null_decl {
557 ALLOC_STRUCT($$, struct cdecl,
560 } | T_IDENT T_AS english_declaration {
562 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
563 if (d->type == CDECL_DECL_NULL) {
564 d->type = CDECL_DECL_IDENT;
570 english_array: T_VLA T_ARRAY english_vla T_OF {
571 ALLOC_STRUCT($$, struct cdecl_declarator,
572 .type = CDECL_DECL_ARRAY,
574 } | T_ARRAY T_UINT T_OF {
576 FAIL(_("array length must be positive"));
578 ALLOC_STRUCT($$, struct cdecl_declarator,
579 .type = CDECL_DECL_ARRAY,
580 .u.array.length = $2);
582 ALLOC_STRUCT($$, struct cdecl_declarator,
583 .type = CDECL_DECL_ARRAY,
584 .u.array.length = 0);
587 english_vla: T_IDENT | {
588 ALLOC($$, sizeof "");