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}
36 #define FAIL(msg) do { \
37 yyerror(&yylloc, NULL, NULL, msg); \
41 #define ALLOC(ptr, size) do { \
42 (ptr) = malloc(size); \
44 FAIL("failed to allocate memory"); \
47 #define ALLOC_STRUCT(ptr, type, ...) do { \
48 ALLOC(ptr, sizeof (type)); \
49 *(ptr) = (type) { __VA_ARGS__ }; \
58 void cdecl__yyerror(YYLTYPE *, void *, struct cdecl **, const char *);
59 int cdecl__yyparse(void *scanner, struct cdecl **out);
66 struct cdecl_declspec *declspec;
67 struct cdecl_declarator *declarator;
72 static void free_decl(struct cdecl *);
74 static void free_declspec(struct cdecl_declspec *x)
76 struct cdecl_declspec *p;
85 static void free_declarator(struct cdecl_declarator *x)
87 struct cdecl_declarator *p;
95 case CDECL_DECL_IDENT:
98 case CDECL_DECL_POINTER:
99 free_declspec(x->u.pointer.qualifiers);
101 case CDECL_DECL_ARRAY:
102 free(x->u.array.vla);
104 case CDECL_DECL_FUNCTION:
105 free_decl(x->u.function.parameters);
116 static void free_decl(struct cdecl *x)
123 /* The specifiers may be shared by an entire chain. */
124 if (!p || p->specifiers != x->specifiers)
125 free_declspec(x->specifiers);
127 free_declarator(x->declarators);
133 void cdecl_free(struct cdecl *decl)
139 %destructor { free($$); } <strval>
140 %destructor { free_declspec($$); } <declspec>
141 %destructor { free_declarator($$); } <declarator>
142 %destructor { free_decl($$); } <decl>
148 %token <strval> T_IDENT "identifier"
149 %token <uintval> T_UINT "integer constant"
151 %token T_SEMICOLON ";"
152 %token T_ASTERISK "*"
155 %token T_LBRACKET "["
156 %token T_RBRACKET "]"
158 %token T_ELLIPSIS "..."
160 %token T_TYPEDEF "typedef"
161 %token T_EXTERN "extern"
162 %token T_STATIC "static"
164 %token T_REGISTER "register"
166 %token T_INLINE "inline"
168 %token T_RESTRICT "restrict"
169 %token T_VOLATILE "volatile"
170 %token T_CONST "const"
174 %token T_SHORT "short"
177 %token T_FLOAT "float"
178 %token T_DOUBLE "double"
179 %token T_SIGNED "signed"
180 %token T_UNSIGNED "unsigned"
181 %token T_BOOL "_Bool"
182 %token T_COMPLEX "_Complex"
183 %token T_IMAGINARY "_Imaginary"
185 %token T_STRUCT "struct"
186 %token T_UNION "union"
193 %token T_DECLARE "declare"
194 %token T_POINTER "pointer"
195 %token T_FUNCTION "function"
196 %token T_RETURNING "returning"
197 %token T_ARRAY "array"
201 %token T_VLA "variable-length"
203 %type <strval> vla_ident
204 %type <boolval> varargs
205 %type <uintval> declspec_simple typespec_simple qualifier_simple
206 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
207 %type <declspec> qualifier qualifiers
208 %type <declspec> declspecs declspecs_noid
209 %type <declarator> direct_declarator declarator pointer array parens postfix
210 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
211 %type <decl> declaration declarators declarator_wrap
212 %type <decl> parameter parameters
214 %type <strval> english_vla
215 %type <declspec> storage_func_specs post_specs
216 %type <declspec> type_qual_spec type_qual_specs typedef_name_qual
217 %type <declarator> english_declarator english_array english_function
218 %type <declarator> english_parameter_list null_decl
219 %type <decl> english_parameter english_parameters
220 %type <decl> english english_declaration
223 * Harmless shift/reduce conflicts in english_parameter. See comments below
230 input: T_ENGLISH english {
236 declaration: declspecs declarators T_SEMICOLON {
239 for (struct cdecl *i = $$; i; i = i->next)
243 declspecs: declspec_notype declspecs {
246 } | typespec declspecs_noid {
251 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
256 qualifiers: { $$ = NULL; } | qualifiers qualifier {
261 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
266 declarator_wrap: declarator {
267 ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
270 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO; }
271 | T_TYPEDEF { $$ = CDECL_STOR_TYPEDEF; }
272 | T_EXTERN { $$ = CDECL_STOR_EXTERN; }
273 | T_STATIC { $$ = CDECL_STOR_STATIC; }
274 | T_REGISTER { $$ = CDECL_STOR_REGISTER; }
275 | T_INLINE { $$ = CDECL_FUNC_INLINE; }
277 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID; }
278 | T_CHAR { $$ = CDECL_TYPE_CHAR; }
279 | T_SHORT { $$ = CDECL_TYPE_SHORT; }
280 | T_INT { $$ = CDECL_TYPE_INT; }
281 | T_LONG { $$ = CDECL_TYPE_LONG; }
282 | T_FLOAT { $$ = CDECL_TYPE_FLOAT; }
283 | T_DOUBLE { $$ = CDECL_TYPE_DOUBLE; }
284 | T_SIGNED { $$ = CDECL_TYPE_SIGNED; }
285 | T_UNSIGNED { $$ = CDECL_TYPE_UNSIGNED; }
286 | T_BOOL { $$ = CDECL_TYPE_BOOL; }
287 | T_COMPLEX { $$ = CDECL_TYPE_COMPLEX; }
288 | T_IMAGINARY { $$ = CDECL_TYPE_IMAGINARY; }
290 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST; }
291 | T_RESTRICT { $$ = CDECL_QUAL_RESTRICT; }
292 | T_VOLATILE { $$ = CDECL_QUAL_VOLATILE; }
294 declspec_notype: qualifier | declspec_simple {
295 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
298 typespec_noid: typespec_simple {
299 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
302 qualifier: qualifier_simple {
303 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
306 typespec: typespec_noid | T_STRUCT T_IDENT {
307 ALLOC_STRUCT($$, struct cdecl_declspec,
308 .type = CDECL_TYPE_STRUCT,
310 } | T_UNION T_IDENT {
311 ALLOC_STRUCT($$, struct cdecl_declspec,
312 .type = CDECL_TYPE_UNION,
315 ALLOC_STRUCT($$, struct cdecl_declspec,
316 .type = CDECL_TYPE_ENUM,
319 ALLOC_STRUCT($$, struct cdecl_declspec,
320 .type = CDECL_TYPE_IDENT,
324 declspec_noid: declspec_notype | typespec_noid
326 vla_ident: T_IDENT | T_ASTERISK {
327 ALLOC($$, sizeof "");
331 array: T_LBRACKET T_UINT T_RBRACKET {
333 FAIL("array length must be positive");
335 ALLOC_STRUCT($$, struct cdecl_declarator,
336 .type = CDECL_DECL_ARRAY,
337 .u.array.length = $2);
338 } | T_LBRACKET vla_ident T_RBRACKET {
339 ALLOC_STRUCT($$, struct cdecl_declarator,
340 .type = CDECL_DECL_ARRAY,
342 } | T_LBRACKET T_RBRACKET {
343 ALLOC_STRUCT($$, struct cdecl_declarator,
344 .type = CDECL_DECL_ARRAY);
347 parameter: declspecs declarator {
348 ALLOC_STRUCT($$, struct cdecl,
353 parameters: parameter | parameters T_COMMA parameter {
358 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
360 parameter_type_list: parameters varargs {
361 struct cdecl *p, *c, *n;
363 /* Parameters were accumulated in reverse order. */
364 for (p = NULL, c = $1; c; p = c, c = n) {
369 ALLOC_STRUCT($$, struct cdecl_declarator,
370 .type = CDECL_DECL_FUNCTION,
371 .u.function.parameters = p,
372 .u.function.variadic = $2);
375 parens: T_LPAREN parameter_type_list T_RPAREN {
377 } | T_LPAREN declarator_ish T_RPAREN {
378 ALLOC_STRUCT($$, struct cdecl_declarator,
379 .type = CDECL_DECL_FUNCTION);
380 ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
384 pointer: T_ASTERISK qualifiers direct_declarator {
385 ALLOC_STRUCT($$, struct cdecl_declarator,
386 .type = CDECL_DECL_POINTER,
387 .u.pointer.qualifiers = $2,
389 } | T_ASTERISK qualifiers pointer {
390 ALLOC_STRUCT($$, struct cdecl_declarator,
391 .type = CDECL_DECL_POINTER,
392 .u.pointer.qualifiers = $2,
396 declarator: direct_declarator | pointer
397 declarator_ish: direct_declarator_ish | pointer
398 postfix: array | parens
400 direct_declarator_ish: {
401 ALLOC_STRUCT($$, struct cdecl_declarator,
402 .type = CDECL_DECL_NULL);
403 } | direct_declarator_ish postfix {
409 ALLOC_STRUCT($$, struct cdecl_declarator,
410 .type = CDECL_DECL_NULL);
412 ALLOC_STRUCT($$, struct cdecl_declarator,
413 .type = CDECL_DECL_IDENT,
415 } | direct_declarator postfix {
420 english: T_DECLARE T_IDENT T_AS english_declaration {
422 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
423 if (d->type == CDECL_DECL_NULL) {
424 d->type = CDECL_DECL_IDENT;
428 } | T_TYPE english_declaration {
432 storage_func_specs: { $$ = NULL; } | declspec_simple storage_func_specs {
433 ALLOC_STRUCT($$, struct cdecl_declspec,
438 type_qual_spec: typespec_noid | qualifier
440 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
446 * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
447 * conflicts with pointer declarators. So we end up needing to stitch
448 * together three different specifiers lists.
450 post_specs: qualifiers typespec type_qual_specs {
453 for (struct cdecl_declspec *s = $$; s; s = s->next) {
461 english_declaration: storage_func_specs english_declarator post_specs {
462 ALLOC_STRUCT($$, struct cdecl,
466 for (struct cdecl_declspec *s = $$->specifiers; s; s = s->next) {
474 english_declarator: {
475 ALLOC_STRUCT($$, struct cdecl_declarator,
476 .type = CDECL_DECL_NULL);
477 } | english_declarator qualifiers T_POINTER T_TO {
478 ALLOC_STRUCT($$, struct cdecl_declarator,
479 .type = CDECL_DECL_POINTER,
481 .u.pointer.qualifiers = $2);
482 } | english_declarator english_array {
485 } | english_declarator english_function {
490 english_function: T_FUNCTION T_RETURNING {
491 ALLOC_STRUCT($$, struct cdecl_declarator,
492 .type = CDECL_DECL_FUNCTION,
493 .u.function.parameters = NULL);
494 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
498 english_parameter_list: english_parameters varargs {
499 struct cdecl *p, *c, *n;
501 /* Parameters were accumulated in reverse order. */
502 for (p = NULL, c = $1; c; p = c, c = n) {
507 ALLOC_STRUCT($$, struct cdecl_declarator,
508 .type = CDECL_DECL_FUNCTION,
509 .u.function.parameters = p,
510 .u.function.variadic = $2);
513 english_parameters: english_parameters T_COMMA english_parameter {
516 } | english_parameter
518 typedef_name_qual: T_IDENT qualifiers {
519 ALLOC_STRUCT($$, struct cdecl_declspec,
520 .type = CDECL_TYPE_IDENT,
526 ALLOC_STRUCT($$, struct cdecl_declarator,
527 .type = CDECL_DECL_NULL);
531 * There is a small shift/reduce conflict here. An unadorned identifier
532 * as the first thing in the parameter might be a typedef name deep in the
533 * first english_declaration (thus empty storage_func_specs and empty
534 * english_declarator need to be reduced) or it might be the identifier
535 * before the "as" (thus the identifier should be shifted).
537 * The typedef name conflict is the only issue, so treating it as a special
538 * case makes the shift harmless.
540 english_parameter: english_declaration | typedef_name_qual null_decl {
541 ALLOC_STRUCT($$, struct cdecl,
544 } | T_IDENT T_AS english_declaration {
546 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
547 if (d->type == CDECL_DECL_NULL) {
548 d->type = CDECL_DECL_IDENT;
554 english_array: T_VLA T_ARRAY english_vla T_OF {
555 ALLOC_STRUCT($$, struct cdecl_declarator,
556 .type = CDECL_DECL_ARRAY,
558 } | T_ARRAY T_UINT T_OF {
560 FAIL("array length must be positive");
562 ALLOC_STRUCT($$, struct cdecl_declarator,
563 .type = CDECL_DECL_ARRAY,
564 .u.array.length = $2);
566 ALLOC_STRUCT($$, struct cdecl_declarator,
567 .type = CDECL_DECL_ARRAY,
568 .u.array.length = 0);
571 english_vla: T_IDENT | {
572 ALLOC($$, sizeof "");
578 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out,
581 if (strstr(err, "T_LEX_ERROR"))
584 fprintf(stderr, "%s\n", err);