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}
37 #define FAIL(msg) do { \
38 yyerror(&yylloc, NULL, NULL, msg); \
42 #define ALLOC(ptr, size) do { \
43 (ptr) = malloc(size); \
45 FAIL("failed to allocate memory"); \
48 #define ALLOC_STRUCT(ptr, type, ...) do { \
49 ALLOC(ptr, sizeof (type)); \
50 *(ptr) = (type) { __VA_ARGS__ }; \
59 void cdecl__yyerror(YYLTYPE *, void *, struct cdecl **, const char *);
60 int cdecl__yyparse(void *scanner, struct cdecl **out);
67 struct cdecl_declspec *declspec;
68 struct cdecl_declarator *declarator;
73 static void free_decl(struct cdecl *);
75 static void free_declspec(struct cdecl_declspec *x)
77 struct cdecl_declspec *p;
86 static void free_declarator(struct cdecl_declarator *x)
88 struct cdecl_declarator *p;
96 case CDECL_DECL_IDENT:
99 case CDECL_DECL_POINTER:
100 free_declspec(x->u.pointer.qualifiers);
102 case CDECL_DECL_ARRAY:
103 free(x->u.array.vla);
105 case CDECL_DECL_FUNCTION:
106 free_decl(x->u.function.parameters);
117 static void free_decl(struct cdecl *x)
124 /* The specifiers may be shared by an entire chain. */
125 if (!p || p->specifiers != x->specifiers)
126 free_declspec(x->specifiers);
128 free_declarator(x->declarators);
134 void cdecl_free(struct cdecl *decl)
140 %destructor { free($$); } <strval>
141 %destructor { free_declspec($$); } <declspec>
142 %destructor { free_declarator($$); } <declarator>
143 %destructor { free_decl($$); } <decl>
149 %token <strval> T_IDENT "identifier"
150 %token <uintval> T_UINT "integer constant"
152 %token T_SEMICOLON ";"
153 %token T_ASTERISK "*"
156 %token T_LBRACKET "["
157 %token T_RBRACKET "]"
159 %token T_ELLIPSIS "..."
161 %token T_TYPEDEF "typedef"
162 %token T_EXTERN "extern"
163 %token T_STATIC "static"
165 %token T_REGISTER "register"
167 %token T_INLINE "inline"
169 %token T_RESTRICT "restrict"
170 %token T_VOLATILE "volatile"
171 %token T_CONST "const"
175 %token T_SHORT "short"
178 %token T_FLOAT "float"
179 %token T_DOUBLE "double"
180 %token T_SIGNED "signed"
181 %token T_UNSIGNED "unsigned"
182 %token T_BOOL "_Bool"
183 %token T_COMPLEX "_Complex"
184 %token T_IMAGINARY "_Imaginary"
186 %token T_STRUCT "struct"
187 %token T_UNION "union"
194 %token T_DECLARE "declare"
195 %token T_POINTER "pointer"
196 %token T_FUNCTION "function"
197 %token T_RETURNING "returning"
198 %token T_ARRAY "array"
202 %token T_VLA "variable-length"
204 %type <strval> vla_ident
205 %type <boolval> varargs
206 %type <uintval> declspec_simple typespec_simple qualifier_simple
207 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
208 %type <declspec> qualifier qualifiers
209 %type <declspec> declspecs declspecs_noid
210 %type <declarator> direct_declarator declarator pointer array parens postfix
211 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
212 %type <decl> declaration declarators declarator_wrap
213 %type <decl> parameter parameters
215 %type <strval> english_vla
216 %type <declspec> storage_func_specs post_specs
217 %type <declspec> type_qual_spec type_qual_specs typedef_name_qual
218 %type <declarator> english_declarator english_array english_function
219 %type <declarator> english_parameter_list null_decl
220 %type <decl> english_parameter english_parameters
221 %type <decl> english english_declaration
224 * Harmless shift/reduce conflicts in english_parameter. See comments below
231 input: T_ENGLISH english {
239 declaration: declspecs declarators semi {
242 for (struct cdecl *i = $$; i; i = i->next)
246 declspecs: declspec_notype declspecs {
249 } | typespec declspecs_noid {
254 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
259 qualifiers: { $$ = NULL; } | qualifiers qualifier {
264 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
269 declarator_wrap: declarator {
270 ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
273 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO; }
274 | T_TYPEDEF { $$ = CDECL_STOR_TYPEDEF; }
275 | T_EXTERN { $$ = CDECL_STOR_EXTERN; }
276 | T_STATIC { $$ = CDECL_STOR_STATIC; }
277 | T_REGISTER { $$ = CDECL_STOR_REGISTER; }
278 | T_INLINE { $$ = CDECL_FUNC_INLINE; }
280 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID; }
281 | T_CHAR { $$ = CDECL_TYPE_CHAR; }
282 | T_SHORT { $$ = CDECL_TYPE_SHORT; }
283 | T_INT { $$ = CDECL_TYPE_INT; }
284 | T_LONG { $$ = CDECL_TYPE_LONG; }
285 | T_FLOAT { $$ = CDECL_TYPE_FLOAT; }
286 | T_DOUBLE { $$ = CDECL_TYPE_DOUBLE; }
287 | T_SIGNED { $$ = CDECL_TYPE_SIGNED; }
288 | T_UNSIGNED { $$ = CDECL_TYPE_UNSIGNED; }
289 | T_BOOL { $$ = CDECL_TYPE_BOOL; }
290 | T_COMPLEX { $$ = CDECL_TYPE_COMPLEX; }
291 | T_IMAGINARY { $$ = CDECL_TYPE_IMAGINARY; }
293 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST; }
294 | T_RESTRICT { $$ = CDECL_QUAL_RESTRICT; }
295 | T_VOLATILE { $$ = CDECL_QUAL_VOLATILE; }
297 declspec_notype: qualifier | declspec_simple {
298 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
301 typespec_noid: typespec_simple {
302 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
305 qualifier: qualifier_simple {
306 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
309 typespec: typespec_noid | T_STRUCT T_IDENT {
310 ALLOC_STRUCT($$, struct cdecl_declspec,
311 .type = CDECL_TYPE_STRUCT,
313 } | T_UNION T_IDENT {
314 ALLOC_STRUCT($$, struct cdecl_declspec,
315 .type = CDECL_TYPE_UNION,
318 ALLOC_STRUCT($$, struct cdecl_declspec,
319 .type = CDECL_TYPE_ENUM,
322 ALLOC_STRUCT($$, struct cdecl_declspec,
323 .type = CDECL_TYPE_IDENT,
327 declspec_noid: declspec_notype | typespec_noid
329 vla_ident: T_IDENT | T_ASTERISK {
330 ALLOC($$, sizeof "");
334 array: T_LBRACKET T_UINT T_RBRACKET {
336 FAIL("array length must be positive");
338 ALLOC_STRUCT($$, struct cdecl_declarator,
339 .type = CDECL_DECL_ARRAY,
340 .u.array.length = $2);
341 } | T_LBRACKET vla_ident T_RBRACKET {
342 ALLOC_STRUCT($$, struct cdecl_declarator,
343 .type = CDECL_DECL_ARRAY,
345 } | T_LBRACKET T_RBRACKET {
346 ALLOC_STRUCT($$, struct cdecl_declarator,
347 .type = CDECL_DECL_ARRAY);
350 parameter: declspecs declarator {
351 ALLOC_STRUCT($$, struct cdecl,
356 parameters: parameter | parameters T_COMMA parameter {
361 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
363 parameter_type_list: parameters varargs {
364 struct cdecl *p, *c, *n;
366 /* Parameters were accumulated in reverse order. */
367 for (p = NULL, c = $1; c; p = c, c = n) {
372 ALLOC_STRUCT($$, struct cdecl_declarator,
373 .type = CDECL_DECL_FUNCTION,
374 .u.function.parameters = p,
375 .u.function.variadic = $2);
378 parens: T_LPAREN parameter_type_list T_RPAREN {
380 } | T_LPAREN declarator_ish T_RPAREN {
381 ALLOC_STRUCT($$, struct cdecl_declarator,
382 .type = CDECL_DECL_FUNCTION);
383 ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
387 pointer: T_ASTERISK qualifiers direct_declarator {
388 ALLOC_STRUCT($$, struct cdecl_declarator,
389 .type = CDECL_DECL_POINTER,
390 .u.pointer.qualifiers = $2,
392 } | T_ASTERISK qualifiers pointer {
393 ALLOC_STRUCT($$, struct cdecl_declarator,
394 .type = CDECL_DECL_POINTER,
395 .u.pointer.qualifiers = $2,
399 declarator: direct_declarator | pointer
400 declarator_ish: direct_declarator_ish | pointer
401 postfix: array | parens
403 direct_declarator_ish: {
404 ALLOC_STRUCT($$, struct cdecl_declarator,
405 .type = CDECL_DECL_NULL);
406 } | direct_declarator_ish postfix {
412 ALLOC_STRUCT($$, struct cdecl_declarator,
413 .type = CDECL_DECL_NULL);
415 ALLOC_STRUCT($$, struct cdecl_declarator,
416 .type = CDECL_DECL_IDENT,
418 } | direct_declarator postfix {
423 english: T_DECLARE T_IDENT T_AS english_declaration {
425 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
426 if (d->type == CDECL_DECL_NULL) {
427 d->type = CDECL_DECL_IDENT;
431 } | T_TYPE english_declaration {
435 storage_func_specs: { $$ = NULL; } | declspec_simple storage_func_specs {
436 ALLOC_STRUCT($$, struct cdecl_declspec,
441 type_qual_spec: typespec_noid | qualifier
443 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
449 * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
450 * conflicts with pointer declarators. So we end up needing to stitch
451 * together three different specifiers lists.
453 post_specs: qualifiers typespec type_qual_specs {
456 for (struct cdecl_declspec *s = $$; s; s = s->next) {
464 english_declaration: storage_func_specs english_declarator post_specs {
465 ALLOC_STRUCT($$, struct cdecl,
469 for (struct cdecl_declspec *s = $$->specifiers; s; s = s->next) {
477 english_declarator: {
478 ALLOC_STRUCT($$, struct cdecl_declarator,
479 .type = CDECL_DECL_NULL);
480 } | english_declarator qualifiers T_POINTER T_TO {
481 ALLOC_STRUCT($$, struct cdecl_declarator,
482 .type = CDECL_DECL_POINTER,
484 .u.pointer.qualifiers = $2);
485 } | english_declarator english_array {
488 } | english_declarator english_function {
493 english_function: T_FUNCTION T_RETURNING {
494 ALLOC_STRUCT($$, struct cdecl_declarator,
495 .type = CDECL_DECL_FUNCTION,
496 .u.function.parameters = NULL);
497 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
501 english_parameter_list: english_parameters varargs {
502 struct cdecl *p, *c, *n;
504 /* Parameters were accumulated in reverse order. */
505 for (p = NULL, c = $1; c; p = c, c = n) {
510 ALLOC_STRUCT($$, struct cdecl_declarator,
511 .type = CDECL_DECL_FUNCTION,
512 .u.function.parameters = p,
513 .u.function.variadic = $2);
516 english_parameters: english_parameters T_COMMA english_parameter {
519 } | english_parameter
521 typedef_name_qual: T_IDENT qualifiers {
522 ALLOC_STRUCT($$, struct cdecl_declspec,
523 .type = CDECL_TYPE_IDENT,
529 ALLOC_STRUCT($$, struct cdecl_declarator,
530 .type = CDECL_DECL_NULL);
534 * There is a small shift/reduce conflict here. An unadorned identifier
535 * as the first thing in the parameter might be a typedef name deep in the
536 * first english_declaration (thus empty storage_func_specs and empty
537 * english_declarator need to be reduced) or it might be the identifier
538 * before the "as" (thus the identifier should be shifted).
540 * The typedef name conflict is the only issue, so treating it as a special
541 * case makes the shift harmless.
543 english_parameter: english_declaration | typedef_name_qual null_decl {
544 ALLOC_STRUCT($$, struct cdecl,
547 } | T_IDENT T_AS english_declaration {
549 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
550 if (d->type == CDECL_DECL_NULL) {
551 d->type = CDECL_DECL_IDENT;
557 english_array: T_VLA T_ARRAY english_vla T_OF {
558 ALLOC_STRUCT($$, struct cdecl_declarator,
559 .type = CDECL_DECL_ARRAY,
561 } | T_ARRAY T_UINT T_OF {
563 FAIL("array length must be positive");
565 ALLOC_STRUCT($$, struct cdecl_declarator,
566 .type = CDECL_DECL_ARRAY,
567 .u.array.length = $2);
569 ALLOC_STRUCT($$, struct cdecl_declarator,
570 .type = CDECL_DECL_ARRAY,
571 .u.array.length = 0);
574 english_vla: T_IDENT | {
575 ALLOC($$, sizeof "");
581 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out,
584 if (strstr(err, "T_LEX_ERROR"))
587 fprintf(stderr, "%s\n", err);