3 * Parser for C declarations.
4 * Copyright © 2011 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 {yyscan_t scanner}
23 %parse-param {struct cdecl **out}
24 %lex-param {yyscan_t scanner}
38 #define FAIL(msg) do { \
39 yyerror(&yylloc, NULL, NULL, msg); \
43 #define ALLOC(ptr, size) do { \
44 (ptr) = malloc(size); \
46 FAIL("failed to allocate memory"); \
49 #define ALLOC_STRUCT(ptr, type, ...) do { \
50 ALLOC(ptr, sizeof (type)); \
51 *(ptr) = (type) { __VA_ARGS__ }; \
60 void cdecl__yyerror(YYLTYPE *, void *, struct cdecl **, const char *);
61 int cdecl__yyparse(void *scanner, struct cdecl **out);
68 struct cdecl_declspec *declspec;
69 struct cdecl_declarator *declarator;
74 static void free_decl(struct cdecl *);
76 static void free_declspec(struct cdecl_declspec *x)
78 struct cdecl_declspec *p;
87 static void free_declarator(struct cdecl_declarator *x)
89 struct cdecl_declarator *p;
97 case CDECL_DECL_IDENT:
100 case CDECL_DECL_POINTER:
101 free_declspec(x->u.pointer.qualifiers);
103 case CDECL_DECL_ARRAY:
104 free(x->u.array.vla);
106 case CDECL_DECL_FUNCTION:
107 free_decl(x->u.function.parameters);
118 static void free_decl(struct cdecl *x)
125 /* The specifiers may be shared by an entire chain. */
126 if (!p || p->specifiers != x->specifiers)
127 free_declspec(x->specifiers);
129 free_declarator(x->declarators);
135 void cdecl_free(struct cdecl *decl)
141 %destructor { free($$); } <strval>
142 %destructor { free_declspec($$); } <declspec>
143 %destructor { free_declarator($$); } <declarator>
144 %destructor { free_decl($$); } <decl>
150 %token <strval> T_IDENT "identifier"
151 %token <uintval> T_UINT "integer constant"
153 %token T_SEMICOLON ";"
154 %token T_ASTERISK "*"
157 %token T_LBRACKET "["
158 %token T_RBRACKET "]"
160 %token T_ELLIPSIS "..."
162 %token T_TYPEDEF "typedef"
163 %token T_EXTERN "extern"
164 %token T_STATIC "static"
166 %token T_REGISTER "register"
168 %token T_INLINE "inline"
170 %token T_RESTRICT "restrict"
171 %token T_VOLATILE "volatile"
172 %token T_CONST "const"
176 %token T_SHORT "short"
179 %token T_FLOAT "float"
180 %token T_DOUBLE "double"
181 %token T_SIGNED "signed"
182 %token T_UNSIGNED "unsigned"
183 %token T_BOOL "_Bool"
184 %token T_COMPLEX "_Complex"
185 %token T_IMAGINARY "_Imaginary"
187 %token T_STRUCT "struct"
188 %token T_UNION "union"
195 %token T_DECLARE "declare"
196 %token T_POINTER "pointer"
197 %token T_FUNCTION "function"
198 %token T_RETURNING "returning"
199 %token T_ARRAY "array"
203 %token T_VLA "variable-length"
205 %type <strval> vla_ident
206 %type <boolval> varargs
207 %type <uintval> declspec_simple typespec_simple qualifier_simple
208 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
209 %type <declspec> qualifier qualifiers
210 %type <declspec> declspecs declspecs_noid
211 %type <declarator> direct_declarator declarator pointer array parens postfix
212 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
213 %type <decl> declaration declarators declarator_wrap
214 %type <decl> parameter parameters
216 %type <strval> english_vla
217 %type <declspec> storage_func_specs post_specs
218 %type <declspec> type_qual_spec type_qual_specs typedef_name_qual
219 %type <declarator> english_declarator english_array english_function
220 %type <declarator> english_parameter_list null_decl
221 %type <decl> english_parameter english_parameters
222 %type <decl> english english_declaration
225 * Harmless shift/reduce conflicts in english_parameter. See comments below
232 input: T_ENGLISH english {
240 declaration: declspecs declarators semi {
243 for (struct cdecl *i = $$; i; i = i->next)
247 declspecs: declspec_notype declspecs {
250 } | typespec declspecs_noid {
255 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
260 qualifiers: { $$ = NULL; } | qualifiers qualifier {
265 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
270 declarator_wrap: declarator {
271 ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
274 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO; }
275 | T_TYPEDEF { $$ = CDECL_STOR_TYPEDEF; }
276 | T_EXTERN { $$ = CDECL_STOR_EXTERN; }
277 | T_STATIC { $$ = CDECL_STOR_STATIC; }
278 | T_REGISTER { $$ = CDECL_STOR_REGISTER; }
279 | T_INLINE { $$ = CDECL_FUNC_INLINE; }
281 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID; }
282 | T_CHAR { $$ = CDECL_TYPE_CHAR; }
283 | T_SHORT { $$ = CDECL_TYPE_SHORT; }
284 | T_INT { $$ = CDECL_TYPE_INT; }
285 | T_LONG { $$ = CDECL_TYPE_LONG; }
286 | T_FLOAT { $$ = CDECL_TYPE_FLOAT; }
287 | T_DOUBLE { $$ = CDECL_TYPE_DOUBLE; }
288 | T_SIGNED { $$ = CDECL_TYPE_SIGNED; }
289 | T_UNSIGNED { $$ = CDECL_TYPE_UNSIGNED; }
290 | T_BOOL { $$ = CDECL_TYPE_BOOL; }
291 | T_COMPLEX { $$ = CDECL_TYPE_COMPLEX; }
292 | T_IMAGINARY { $$ = CDECL_TYPE_IMAGINARY; }
294 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST; }
295 | T_RESTRICT { $$ = CDECL_QUAL_RESTRICT; }
296 | T_VOLATILE { $$ = CDECL_QUAL_VOLATILE; }
298 declspec_notype: qualifier | declspec_simple {
299 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
302 typespec_noid: typespec_simple {
303 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
306 qualifier: qualifier_simple {
307 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
310 typespec: typespec_noid | T_STRUCT T_IDENT {
311 ALLOC_STRUCT($$, struct cdecl_declspec,
312 .type = CDECL_TYPE_STRUCT,
314 } | T_UNION T_IDENT {
315 ALLOC_STRUCT($$, struct cdecl_declspec,
316 .type = CDECL_TYPE_UNION,
319 ALLOC_STRUCT($$, struct cdecl_declspec,
320 .type = CDECL_TYPE_ENUM,
323 ALLOC_STRUCT($$, struct cdecl_declspec,
324 .type = CDECL_TYPE_IDENT,
328 declspec_noid: declspec_notype | typespec_noid
330 vla_ident: T_IDENT | T_ASTERISK {
331 ALLOC($$, sizeof "");
335 array: T_LBRACKET T_UINT T_RBRACKET {
337 FAIL("array length must be positive");
339 ALLOC_STRUCT($$, struct cdecl_declarator,
340 .type = CDECL_DECL_ARRAY,
341 .u.array.length = $2);
342 } | T_LBRACKET vla_ident T_RBRACKET {
343 ALLOC_STRUCT($$, struct cdecl_declarator,
344 .type = CDECL_DECL_ARRAY,
346 } | T_LBRACKET T_RBRACKET {
347 ALLOC_STRUCT($$, struct cdecl_declarator,
348 .type = CDECL_DECL_ARRAY);
351 parameter: declspecs declarator {
352 ALLOC_STRUCT($$, struct cdecl,
357 parameters: parameter | parameters T_COMMA parameter {
362 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
364 parameter_type_list: parameters varargs {
365 struct cdecl *p, *c, *n;
367 /* Parameters were accumulated in reverse order. */
368 for (p = NULL, c = $1; c; p = c, c = n) {
373 ALLOC_STRUCT($$, struct cdecl_declarator,
374 .type = CDECL_DECL_FUNCTION,
375 .u.function.parameters = p,
376 .u.function.variadic = $2);
379 parens: T_LPAREN parameter_type_list T_RPAREN {
381 } | T_LPAREN declarator_ish T_RPAREN {
382 ALLOC_STRUCT($$, struct cdecl_declarator,
383 .type = CDECL_DECL_FUNCTION);
384 ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
388 pointer: T_ASTERISK qualifiers direct_declarator {
389 ALLOC_STRUCT($$, struct cdecl_declarator,
390 .type = CDECL_DECL_POINTER,
391 .u.pointer.qualifiers = $2,
393 } | T_ASTERISK qualifiers pointer {
394 ALLOC_STRUCT($$, struct cdecl_declarator,
395 .type = CDECL_DECL_POINTER,
396 .u.pointer.qualifiers = $2,
400 declarator: direct_declarator | pointer
401 declarator_ish: direct_declarator_ish | pointer
402 postfix: array | parens
404 direct_declarator_ish: {
405 ALLOC_STRUCT($$, struct cdecl_declarator,
406 .type = CDECL_DECL_NULL);
407 } | direct_declarator_ish postfix {
413 ALLOC_STRUCT($$, struct cdecl_declarator,
414 .type = CDECL_DECL_NULL);
416 ALLOC_STRUCT($$, struct cdecl_declarator,
417 .type = CDECL_DECL_IDENT,
419 } | direct_declarator postfix {
424 english: T_DECLARE T_IDENT T_AS english_declaration {
426 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
427 if (d->type == CDECL_DECL_NULL) {
428 d->type = CDECL_DECL_IDENT;
432 } | T_TYPE english_declaration {
436 storage_func_specs: { $$ = NULL; } | declspec_simple storage_func_specs {
437 ALLOC_STRUCT($$, struct cdecl_declspec,
442 type_qual_spec: typespec_noid | qualifier
444 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
450 * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
451 * conflicts with pointer declarators. So we end up needing to stitch
452 * together three different specifiers lists.
454 post_specs: qualifiers typespec type_qual_specs {
457 for (struct cdecl_declspec *s = $$; s; s = s->next) {
465 english_declaration: storage_func_specs english_declarator post_specs {
466 ALLOC_STRUCT($$, struct cdecl,
470 for (struct cdecl_declspec *s = $$->specifiers; s; s = s->next) {
478 english_declarator: {
479 ALLOC_STRUCT($$, struct cdecl_declarator,
480 .type = CDECL_DECL_NULL);
481 } | english_declarator qualifiers T_POINTER T_TO {
482 ALLOC_STRUCT($$, struct cdecl_declarator,
483 .type = CDECL_DECL_POINTER,
485 .u.pointer.qualifiers = $2);
486 } | english_declarator english_array {
489 } | english_declarator english_function {
494 english_function: T_FUNCTION T_RETURNING {
495 ALLOC_STRUCT($$, struct cdecl_declarator,
496 .type = CDECL_DECL_FUNCTION,
497 .u.function.parameters = NULL);
498 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
502 english_parameter_list: english_parameters varargs {
503 struct cdecl *p, *c, *n;
505 /* Parameters were accumulated in reverse order. */
506 for (p = NULL, c = $1; c; p = c, c = n) {
511 ALLOC_STRUCT($$, struct cdecl_declarator,
512 .type = CDECL_DECL_FUNCTION,
513 .u.function.parameters = p,
514 .u.function.variadic = $2);
517 english_parameters: english_parameters T_COMMA english_parameter {
520 } | english_parameter
522 typedef_name_qual: T_IDENT qualifiers {
523 ALLOC_STRUCT($$, struct cdecl_declspec,
524 .type = CDECL_TYPE_IDENT,
530 ALLOC_STRUCT($$, struct cdecl_declarator,
531 .type = CDECL_DECL_NULL);
535 * There is a small shift/reduce conflict here. An unadorned identifier
536 * as the first thing in the parameter might be a typedef name deep in the
537 * first english_declaration (thus empty storage_func_specs and empty
538 * english_declarator need to be reduced) or it might be the identifier
539 * before the "as" (thus the identifier should be shifted).
541 * The typedef name conflict is the only issue, so treating it as a special
542 * case makes the shift harmless.
544 english_parameter: english_declaration | typedef_name_qual null_decl {
545 ALLOC_STRUCT($$, struct cdecl,
548 } | T_IDENT T_AS english_declaration {
550 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
551 if (d->type == CDECL_DECL_NULL) {
552 d->type = CDECL_DECL_IDENT;
558 english_array: T_VLA T_ARRAY english_vla T_OF {
559 ALLOC_STRUCT($$, struct cdecl_declarator,
560 .type = CDECL_DECL_ARRAY,
562 } | T_ARRAY T_UINT T_OF {
564 FAIL("array length must be positive");
566 ALLOC_STRUCT($$, struct cdecl_declarator,
567 .type = CDECL_DECL_ARRAY,
568 .u.array.length = $2);
570 ALLOC_STRUCT($$, struct cdecl_declarator,
571 .type = CDECL_DECL_ARRAY,
572 .u.array.length = 0);
575 english_vla: T_IDENT | {
576 ALLOC($$, sizeof "");
582 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out,
585 if (strstr(err, "T_LEX_ERROR"))
588 cdecl__set_error(&(const struct cdecl_error){
589 .code = CDECL_ENOPARSE,