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 %parse-param {yyscan_t scanner}
22 %parse-param {struct cdecl **out}
23 %lex-param {yyscan_t scanner}
35 #define FAIL(msg) do { \
36 yyerror(&yylloc, NULL, NULL, msg); \
40 #define ALLOC(ptr, size) do { \
41 (ptr) = malloc(size); \
43 FAIL("failed to allocate memory"); \
46 #define ALLOC_STRUCT(ptr, type, ...) do { \
47 ALLOC(ptr, sizeof (type)); \
48 *(ptr) = (type) { __VA_ARGS__ }; \
57 void yyerror(YYLTYPE *, void *, struct cdecl **, const char *);
58 int yyparse(void *scanner, struct cdecl **out);
65 struct cdecl_declspec *declspec;
66 struct cdecl_declarator *declarator;
71 static void free_decl(struct cdecl *);
73 static void free_declspec(struct cdecl_declspec *x)
75 struct cdecl_declspec *p;
84 static void free_declarator(struct cdecl_declarator *x)
86 struct cdecl_declarator *p;
94 case CDECL_DECL_IDENT:
97 case CDECL_DECL_POINTER:
98 free_declspec(x->u.pointer.qualifiers);
100 case CDECL_DECL_ARRAY:
101 free(x->u.array.vla);
103 case CDECL_DECL_FUNCTION:
104 free_decl(x->u.function.parameters);
115 static void free_decl(struct cdecl *x)
122 /* The specifiers may be shared by an entire chain. */
123 if (!p || p->specifiers != x->specifiers)
124 free_declspec(x->specifiers);
126 free_declarator(x->declarators);
132 void cdecl_free(struct cdecl *decl)
138 %destructor { free($$); } <strval>
139 %destructor { free_declspec($$); } <declspec>
140 %destructor { free_declarator($$); } <declarator>
141 %destructor { free_decl($$); } <decl>
147 %token <strval> T_IDENT "identifier"
148 %token <uintval> T_UINT "integer constant"
150 %token T_SEMICOLON ";"
151 %token T_ASTERISK "*"
154 %token T_LBRACKET "["
155 %token T_RBRACKET "]"
157 %token T_ELLIPSIS "..."
159 %token T_TYPEDEF "typedef"
160 %token T_EXTERN "extern"
161 %token T_STATIC "static"
163 %token T_REGISTER "register"
165 %token T_INLINE "inline"
167 %token T_RESTRICT "restrict"
168 %token T_VOLATILE "volatile"
169 %token T_CONST "const"
173 %token T_SHORT "short"
176 %token T_FLOAT "float"
177 %token T_DOUBLE "double"
178 %token T_SIGNED "signed"
179 %token T_UNSIGNED "unsigned"
180 %token T_BOOL "_Bool"
181 %token T_COMPLEX "_Complex"
182 %token T_IMAGINARY "_Imaginary"
184 %token T_STRUCT "struct"
185 %token T_UNION "union"
192 %token T_DECLARE "declare"
193 %token T_POINTER "pointer"
194 %token T_FUNCTION "function"
195 %token T_RETURNING "returning"
196 %token T_ARRAY "array"
200 %token T_VLA "variable-length"
202 %type <strval> vla_ident
203 %type <boolval> varargs
204 %type <uintval> declspec_simple typespec_simple qualifier_simple
205 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
206 %type <declspec> qualifier qualifiers
207 %type <declspec> declspecs declspecs_noid
208 %type <declarator> direct_declarator declarator pointer array parens postfix
209 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
210 %type <decl> declaration declarators declarator_wrap
211 %type <decl> parameter parameters
213 %type <strval> english_vla
214 %type <declspec> storage_func_specs post_specs
215 %type <declspec> type_qual_spec type_qual_specs typedef_name_qual
216 %type <declarator> english_declarator english_array english_function
217 %type <declarator> english_parameter_list null_decl
218 %type <decl> english_parameter english_parameters
219 %type <decl> english english_declaration
222 * Harmless shift/reduce conflicts in english_parameter. See comments below
229 input: T_ENGLISH english {
235 declaration: declspecs declarators T_SEMICOLON {
238 for (struct cdecl *i = $$; i; i = i->next)
242 declspecs: declspec_notype declspecs {
245 } | typespec declspecs_noid {
250 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
255 qualifiers: { $$ = NULL; } | qualifiers qualifier {
260 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
265 declarator_wrap: declarator {
266 ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
269 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO; }
270 | T_TYPEDEF { $$ = CDECL_STOR_TYPEDEF; }
271 | T_EXTERN { $$ = CDECL_STOR_EXTERN; }
272 | T_STATIC { $$ = CDECL_STOR_STATIC; }
273 | T_REGISTER { $$ = CDECL_STOR_REGISTER; }
274 | T_INLINE { $$ = CDECL_FUNC_INLINE; }
276 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID; }
277 | T_CHAR { $$ = CDECL_TYPE_CHAR; }
278 | T_SHORT { $$ = CDECL_TYPE_SHORT; }
279 | T_INT { $$ = CDECL_TYPE_INT; }
280 | T_LONG { $$ = CDECL_TYPE_LONG; }
281 | T_FLOAT { $$ = CDECL_TYPE_FLOAT; }
282 | T_DOUBLE { $$ = CDECL_TYPE_DOUBLE; }
283 | T_SIGNED { $$ = CDECL_TYPE_SIGNED; }
284 | T_UNSIGNED { $$ = CDECL_TYPE_UNSIGNED; }
285 | T_BOOL { $$ = CDECL_TYPE_BOOL; }
286 | T_COMPLEX { $$ = CDECL_TYPE_COMPLEX; }
287 | T_IMAGINARY { $$ = CDECL_TYPE_IMAGINARY; }
289 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST; }
290 | T_RESTRICT { $$ = CDECL_QUAL_RESTRICT; }
291 | T_VOLATILE { $$ = CDECL_QUAL_VOLATILE; }
293 declspec_notype: qualifier | declspec_simple {
294 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
297 typespec_noid: typespec_simple {
298 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
301 qualifier: qualifier_simple {
302 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
305 typespec: typespec_noid | T_STRUCT T_IDENT {
306 ALLOC_STRUCT($$, struct cdecl_declspec,
307 .type = CDECL_TYPE_STRUCT,
309 } | T_UNION T_IDENT {
310 ALLOC_STRUCT($$, struct cdecl_declspec,
311 .type = CDECL_TYPE_UNION,
314 ALLOC_STRUCT($$, struct cdecl_declspec,
315 .type = CDECL_TYPE_ENUM,
318 ALLOC_STRUCT($$, struct cdecl_declspec,
319 .type = CDECL_TYPE_IDENT,
323 declspec_noid: declspec_notype | typespec_noid
325 vla_ident: T_IDENT | T_ASTERISK {
326 ALLOC($$, sizeof "");
330 array: T_LBRACKET T_UINT T_RBRACKET {
332 FAIL("array length must be positive");
334 ALLOC_STRUCT($$, struct cdecl_declarator,
335 .type = CDECL_DECL_ARRAY,
336 .u.array.length = $2);
337 } | T_LBRACKET vla_ident T_RBRACKET {
338 ALLOC_STRUCT($$, struct cdecl_declarator,
339 .type = CDECL_DECL_ARRAY,
341 } | T_LBRACKET T_RBRACKET {
342 ALLOC_STRUCT($$, struct cdecl_declarator,
343 .type = CDECL_DECL_ARRAY);
346 parameter: declspecs declarator {
347 ALLOC_STRUCT($$, struct cdecl,
352 parameters: parameter | parameters T_COMMA parameter {
357 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
359 parameter_type_list: parameters varargs {
360 struct cdecl *p, *c, *n;
362 /* Parameters were accumulated in reverse order. */
363 for (p = NULL, c = $1; c; p = c, c = n) {
368 ALLOC_STRUCT($$, struct cdecl_declarator,
369 .type = CDECL_DECL_FUNCTION,
370 .u.function.parameters = p,
371 .u.function.variadic = $2);
374 parens: T_LPAREN parameter_type_list T_RPAREN {
376 } | T_LPAREN declarator_ish T_RPAREN {
377 ALLOC_STRUCT($$, struct cdecl_declarator,
378 .type = CDECL_DECL_FUNCTION);
379 ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
383 pointer: T_ASTERISK qualifiers direct_declarator {
384 ALLOC_STRUCT($$, struct cdecl_declarator,
385 .type = CDECL_DECL_POINTER,
386 .u.pointer.qualifiers = $2,
388 } | T_ASTERISK qualifiers pointer {
389 ALLOC_STRUCT($$, struct cdecl_declarator,
390 .type = CDECL_DECL_POINTER,
391 .u.pointer.qualifiers = $2,
395 declarator: direct_declarator | pointer
396 declarator_ish: direct_declarator_ish | pointer
397 postfix: array | parens
399 direct_declarator_ish: {
400 ALLOC_STRUCT($$, struct cdecl_declarator,
401 .type = CDECL_DECL_NULL);
402 } | direct_declarator_ish postfix {
408 ALLOC_STRUCT($$, struct cdecl_declarator,
409 .type = CDECL_DECL_NULL);
411 ALLOC_STRUCT($$, struct cdecl_declarator,
412 .type = CDECL_DECL_IDENT,
414 } | direct_declarator postfix {
419 english: T_DECLARE T_IDENT T_AS english_declaration {
421 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
422 if (d->type == CDECL_DECL_NULL) {
423 d->type = CDECL_DECL_IDENT;
427 } | T_TYPE english_declaration {
431 storage_func_specs: { $$ = NULL; } | declspec_simple storage_func_specs {
432 ALLOC_STRUCT($$, struct cdecl_declspec,
437 type_qual_spec: typespec_noid | qualifier
439 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
445 * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
446 * conflicts with pointer declarators. So we end up needing to stitch
447 * together three different specifiers lists.
449 post_specs: qualifiers typespec type_qual_specs {
452 for (struct cdecl_declspec *s = $$; s; s = s->next) {
460 english_declaration: storage_func_specs english_declarator post_specs {
461 ALLOC_STRUCT($$, struct cdecl,
465 for (struct cdecl_declspec *s = $$->specifiers; s; s = s->next) {
473 english_declarator: {
474 ALLOC_STRUCT($$, struct cdecl_declarator,
475 .type = CDECL_DECL_NULL);
476 } | english_declarator qualifiers T_POINTER T_TO {
477 ALLOC_STRUCT($$, struct cdecl_declarator,
478 .type = CDECL_DECL_POINTER,
480 .u.pointer.qualifiers = $2);
481 } | english_declarator english_array {
484 } | english_declarator english_function {
489 english_function: T_FUNCTION T_RETURNING {
490 ALLOC_STRUCT($$, struct cdecl_declarator,
491 .type = CDECL_DECL_FUNCTION,
492 .u.function.parameters = NULL);
493 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
497 english_parameter_list: english_parameters varargs {
498 struct cdecl *p, *c, *n;
500 /* Parameters were accumulated in reverse order. */
501 for (p = NULL, c = $1; c; p = c, c = n) {
506 ALLOC_STRUCT($$, struct cdecl_declarator,
507 .type = CDECL_DECL_FUNCTION,
508 .u.function.parameters = p,
509 .u.function.variadic = $2);
512 english_parameters: english_parameters T_COMMA english_parameter {
515 } | english_parameter
517 typedef_name_qual: T_IDENT qualifiers {
518 ALLOC_STRUCT($$, struct cdecl_declspec,
519 .type = CDECL_TYPE_IDENT,
525 ALLOC_STRUCT($$, struct cdecl_declarator,
526 .type = CDECL_DECL_NULL);
530 * There is a small shift/reduce conflict here. An unadorned identifier
531 * as the first thing in the parameter might be a typedef name deep in the
532 * first english_declaration (thus empty storage_func_specs and empty
533 * english_declarator need to be reduced) or it might be the identifier
534 * before the "as" (thus the identifier should be shifted).
536 * The typedef name conflict is the only issue, so treating it as a special
537 * case makes the shift harmless.
539 english_parameter: english_declaration | typedef_name_qual null_decl {
540 ALLOC_STRUCT($$, struct cdecl,
543 } | T_IDENT T_AS english_declaration {
545 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
546 if (d->type == CDECL_DECL_NULL) {
547 d->type = CDECL_DECL_IDENT;
553 english_array: T_VLA T_ARRAY english_vla T_OF {
554 ALLOC_STRUCT($$, struct cdecl_declarator,
555 .type = CDECL_DECL_ARRAY,
557 } | T_ARRAY T_UINT T_OF {
559 FAIL("array length must be positive");
561 ALLOC_STRUCT($$, struct cdecl_declarator,
562 .type = CDECL_DECL_ARRAY,
563 .u.array.length = $2);
565 ALLOC_STRUCT($$, struct cdecl_declarator,
566 .type = CDECL_DECL_ARRAY,
567 .u.array.length = 0);
570 english_vla: T_IDENT | {
571 ALLOC($$, sizeof "");
577 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out,
580 if (strstr(err, "T_LEX_ERROR"))
583 fprintf(stderr, "%s\n", err);