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__free(struct cdecl *);
61 void cdecl__yyerror(YYLTYPE *, void *, struct cdecl **, const char *);
62 int cdecl__yyparse(void *scanner, struct cdecl **out);
69 struct cdecl_declspec *declspec;
70 struct cdecl_declarator *declarator;
75 static void free_decl(struct cdecl *);
77 static void free_declspec(struct cdecl_declspec *x)
79 struct cdecl_declspec *p;
88 static void free_declarator(struct cdecl_declarator *x)
90 struct cdecl_declarator *p;
98 case CDECL_DECL_IDENT:
101 case CDECL_DECL_POINTER:
102 free_declspec(x->u.pointer.qualifiers);
104 case CDECL_DECL_ARRAY:
105 free(x->u.array.vla);
107 case CDECL_DECL_FUNCTION:
108 free_decl(x->u.function.parameters);
119 static void free_decl(struct cdecl *x)
126 /* The specifiers may be shared by an entire chain. */
127 if (!p || p->specifiers != x->specifiers)
128 free_declspec(x->specifiers);
130 free_declarator(x->declarators);
136 void cdecl__free(struct cdecl *decl)
142 %destructor { free($$); } <strval>
143 %destructor { free_declspec($$); } <declspec>
144 %destructor { free_declarator($$); } <declarator>
145 %destructor { free_decl($$); } <decl>
151 %token <strval> T_IDENT "identifier"
152 %token <uintval> T_UINT "integer constant"
154 %token T_SEMICOLON ";"
155 %token T_ASTERISK "*"
158 %token T_LBRACKET "["
159 %token T_RBRACKET "]"
161 %token T_ELLIPSIS "..."
163 %token T_TYPEDEF "typedef"
164 %token T_EXTERN "extern"
165 %token T_STATIC "static"
167 %token T_REGISTER "register"
169 %token T_INLINE "inline"
171 %token T_RESTRICT "restrict"
172 %token T_VOLATILE "volatile"
173 %token T_CONST "const"
177 %token T_SHORT "short"
180 %token T_FLOAT "float"
181 %token T_DOUBLE "double"
182 %token T_SIGNED "signed"
183 %token T_UNSIGNED "unsigned"
184 %token T_BOOL "_Bool"
185 %token T_COMPLEX "_Complex"
186 %token T_IMAGINARY "_Imaginary"
188 %token T_STRUCT "struct"
189 %token T_UNION "union"
196 %token T_DECLARE "declare"
197 %token T_POINTER "pointer"
198 %token T_FUNCTION "function"
199 %token T_RETURNING "returning"
200 %token T_ARRAY "array"
204 %token T_VLA "variable-length"
206 %type <strval> vla_ident
207 %type <boolval> varargs
208 %type <uintval> declspec_simple typespec_simple qualifier_simple
209 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
210 %type <declspec> qualifier qualifiers
211 %type <declspec> declspecs declspecs_noid
212 %type <declarator> direct_declarator declarator pointer array parens postfix
213 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
214 %type <decl> declaration declarators declarator_wrap
215 %type <decl> parameter parameters
217 %type <strval> english_vla
218 %type <declspec> storage_func_specs post_specs
219 %type <declspec> type_qual_spec type_qual_specs typedef_name_qual
220 %type <declarator> english_declarator english_array english_function
221 %type <declarator> english_parameter_list null_decl
222 %type <decl> english_parameter english_parameters
223 %type <decl> english english_declaration
226 * Harmless shift/reduce conflicts in english_parameter. See comments below
233 input: T_ENGLISH english {
241 declaration: declspecs declarators semi {
244 for (struct cdecl *i = $$; i; i = i->next)
248 declspecs: declspec_notype declspecs {
251 } | typespec declspecs_noid {
256 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
261 qualifiers: { $$ = NULL; } | qualifiers qualifier {
266 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
271 declarator_wrap: declarator {
272 ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
275 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO; }
276 | T_TYPEDEF { $$ = CDECL_STOR_TYPEDEF; }
277 | T_EXTERN { $$ = CDECL_STOR_EXTERN; }
278 | T_STATIC { $$ = CDECL_STOR_STATIC; }
279 | T_REGISTER { $$ = CDECL_STOR_REGISTER; }
280 | T_INLINE { $$ = CDECL_FUNC_INLINE; }
282 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID; }
283 | T_CHAR { $$ = CDECL_TYPE_CHAR; }
284 | T_SHORT { $$ = CDECL_TYPE_SHORT; }
285 | T_INT { $$ = CDECL_TYPE_INT; }
286 | T_LONG { $$ = CDECL_TYPE_LONG; }
287 | T_FLOAT { $$ = CDECL_TYPE_FLOAT; }
288 | T_DOUBLE { $$ = CDECL_TYPE_DOUBLE; }
289 | T_SIGNED { $$ = CDECL_TYPE_SIGNED; }
290 | T_UNSIGNED { $$ = CDECL_TYPE_UNSIGNED; }
291 | T_BOOL { $$ = CDECL_TYPE_BOOL; }
292 | T_COMPLEX { $$ = CDECL_TYPE_COMPLEX; }
293 | T_IMAGINARY { $$ = CDECL_TYPE_IMAGINARY; }
295 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST; }
296 | T_RESTRICT { $$ = CDECL_QUAL_RESTRICT; }
297 | T_VOLATILE { $$ = CDECL_QUAL_VOLATILE; }
299 declspec_notype: qualifier | declspec_simple {
300 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
303 typespec_noid: typespec_simple {
304 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
307 qualifier: qualifier_simple {
308 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
311 typespec: typespec_noid | T_STRUCT T_IDENT {
312 ALLOC_STRUCT($$, struct cdecl_declspec,
313 .type = CDECL_TYPE_STRUCT,
315 } | T_UNION T_IDENT {
316 ALLOC_STRUCT($$, struct cdecl_declspec,
317 .type = CDECL_TYPE_UNION,
320 ALLOC_STRUCT($$, struct cdecl_declspec,
321 .type = CDECL_TYPE_ENUM,
324 ALLOC_STRUCT($$, struct cdecl_declspec,
325 .type = CDECL_TYPE_IDENT,
329 declspec_noid: declspec_notype | typespec_noid
331 vla_ident: T_IDENT | T_ASTERISK {
332 ALLOC($$, sizeof "");
336 array: T_LBRACKET T_UINT T_RBRACKET {
338 FAIL("array length must be positive");
340 ALLOC_STRUCT($$, struct cdecl_declarator,
341 .type = CDECL_DECL_ARRAY,
342 .u.array.length = $2);
343 } | T_LBRACKET vla_ident T_RBRACKET {
344 ALLOC_STRUCT($$, struct cdecl_declarator,
345 .type = CDECL_DECL_ARRAY,
347 } | T_LBRACKET T_RBRACKET {
348 ALLOC_STRUCT($$, struct cdecl_declarator,
349 .type = CDECL_DECL_ARRAY);
352 parameter: declspecs declarator {
353 ALLOC_STRUCT($$, struct cdecl,
358 parameters: parameter | parameters T_COMMA parameter {
363 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
365 parameter_type_list: parameters varargs {
366 struct cdecl *p, *c, *n;
368 /* Parameters were accumulated in reverse order. */
369 for (p = NULL, c = $1; c; p = c, c = n) {
374 ALLOC_STRUCT($$, struct cdecl_declarator,
375 .type = CDECL_DECL_FUNCTION,
376 .u.function.parameters = p,
377 .u.function.variadic = $2);
380 parens: T_LPAREN parameter_type_list T_RPAREN {
382 } | T_LPAREN declarator_ish T_RPAREN {
383 ALLOC_STRUCT($$, struct cdecl_declarator,
384 .type = CDECL_DECL_FUNCTION);
385 ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
389 pointer: T_ASTERISK qualifiers direct_declarator {
390 ALLOC_STRUCT($$, struct cdecl_declarator,
391 .type = CDECL_DECL_POINTER,
392 .u.pointer.qualifiers = $2,
394 } | T_ASTERISK qualifiers pointer {
395 ALLOC_STRUCT($$, struct cdecl_declarator,
396 .type = CDECL_DECL_POINTER,
397 .u.pointer.qualifiers = $2,
401 declarator: direct_declarator | pointer
402 declarator_ish: direct_declarator_ish | pointer
403 postfix: array | parens
405 direct_declarator_ish: {
406 ALLOC_STRUCT($$, struct cdecl_declarator,
407 .type = CDECL_DECL_NULL);
408 } | direct_declarator_ish postfix {
414 ALLOC_STRUCT($$, struct cdecl_declarator,
415 .type = CDECL_DECL_NULL);
417 ALLOC_STRUCT($$, struct cdecl_declarator,
418 .type = CDECL_DECL_IDENT,
420 } | direct_declarator postfix {
425 english: T_DECLARE T_IDENT T_AS english_declaration {
427 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
428 if (d->type == CDECL_DECL_NULL) {
429 d->type = CDECL_DECL_IDENT;
433 } | T_TYPE english_declaration {
437 storage_func_specs: { $$ = NULL; } | declspec_simple storage_func_specs {
438 ALLOC_STRUCT($$, struct cdecl_declspec,
443 type_qual_spec: typespec_noid | qualifier
445 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
451 * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
452 * conflicts with pointer declarators. So we end up needing to stitch
453 * together three different specifiers lists.
455 post_specs: qualifiers typespec type_qual_specs {
458 for (struct cdecl_declspec *s = $$; s; s = s->next) {
466 english_declaration: storage_func_specs english_declarator post_specs {
467 ALLOC_STRUCT($$, struct cdecl,
471 for (struct cdecl_declspec *s = $$->specifiers; s; s = s->next) {
479 english_declarator: {
480 ALLOC_STRUCT($$, struct cdecl_declarator,
481 .type = CDECL_DECL_NULL);
482 } | english_declarator qualifiers T_POINTER T_TO {
483 ALLOC_STRUCT($$, struct cdecl_declarator,
484 .type = CDECL_DECL_POINTER,
486 .u.pointer.qualifiers = $2);
487 } | english_declarator english_array {
490 } | english_declarator english_function {
495 english_function: T_FUNCTION T_RETURNING {
496 ALLOC_STRUCT($$, struct cdecl_declarator,
497 .type = CDECL_DECL_FUNCTION,
498 .u.function.parameters = NULL);
499 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
503 english_parameter_list: english_parameters varargs {
504 struct cdecl *p, *c, *n;
506 /* Parameters were accumulated in reverse order. */
507 for (p = NULL, c = $1; c; p = c, c = n) {
512 ALLOC_STRUCT($$, struct cdecl_declarator,
513 .type = CDECL_DECL_FUNCTION,
514 .u.function.parameters = p,
515 .u.function.variadic = $2);
518 english_parameters: english_parameters T_COMMA english_parameter {
521 } | english_parameter
523 typedef_name_qual: T_IDENT qualifiers {
524 ALLOC_STRUCT($$, struct cdecl_declspec,
525 .type = CDECL_TYPE_IDENT,
531 ALLOC_STRUCT($$, struct cdecl_declarator,
532 .type = CDECL_DECL_NULL);
536 * There is a small shift/reduce conflict here. An unadorned identifier
537 * as the first thing in the parameter might be a typedef name deep in the
538 * first english_declaration (thus empty storage_func_specs and empty
539 * english_declarator need to be reduced) or it might be the identifier
540 * before the "as" (thus the identifier should be shifted).
542 * The typedef name conflict is the only issue, so treating it as a special
543 * case makes the shift harmless.
545 english_parameter: english_declaration | typedef_name_qual null_decl {
546 ALLOC_STRUCT($$, struct cdecl,
549 } | T_IDENT T_AS english_declaration {
551 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
552 if (d->type == CDECL_DECL_NULL) {
553 d->type = CDECL_DECL_IDENT;
559 english_array: T_VLA T_ARRAY english_vla T_OF {
560 ALLOC_STRUCT($$, struct cdecl_declarator,
561 .type = CDECL_DECL_ARRAY,
563 } | T_ARRAY T_UINT T_OF {
565 FAIL("array length must be positive");
567 ALLOC_STRUCT($$, struct cdecl_declarator,
568 .type = CDECL_DECL_ARRAY,
569 .u.array.length = $2);
571 ALLOC_STRUCT($$, struct cdecl_declarator,
572 .type = CDECL_DECL_ARRAY,
573 .u.array.length = 0);
576 english_vla: T_IDENT | {
577 ALLOC($$, sizeof "");
583 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out,
586 if (strstr(err, "T_LEX_ERROR"))
589 cdecl__set_error(&(const struct cdecl_error){
590 .code = CDECL_ENOPARSE,