3 * Parser for C declarations.
4 * Copyright © 2011-2012, 2021 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 {void *scanner}
23 %parse-param {struct cdecl **out}
24 %lex-param {yyscan_t scanner}
36 #include "cdecl-internal.h"
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);
70 struct cdecl_declspec *declspec;
71 struct cdecl_declarator *declarator;
76 static void free_decl(struct cdecl *);
78 static void free_declspec(struct cdecl_declspec *x)
80 struct cdecl_declspec *p;
89 static void free_declarator(struct cdecl_declarator *x)
91 struct cdecl_declarator *p;
99 case CDECL_DECL_IDENT:
102 case CDECL_DECL_POINTER:
103 free_declspec(x->u.pointer.qualifiers);
105 case CDECL_DECL_ARRAY:
106 free(x->u.array.vla);
108 case CDECL_DECL_FUNCTION:
109 free_decl(x->u.function.parameters);
120 static void free_decl(struct cdecl *x)
127 /* The specifiers may be shared by an entire chain. */
128 if (!p || p->specifiers != x->specifiers)
129 free_declspec(x->specifiers);
131 free_declarator(x->declarators);
137 void cdecl__free(struct cdecl *decl)
143 %destructor { free($$); } <strval>
144 %destructor { free_declspec($$); } <declspec>
145 %destructor { free_declarator($$); } <declarator>
146 %destructor { free_decl($$); } <decl>
152 %token <strval> T_IDENT "identifier"
153 %token <uintval> T_UINT "integer constant"
155 %token T_SEMICOLON ";"
156 %token T_ASTERISK "*"
159 %token T_LBRACKET "["
160 %token T_RBRACKET "]"
162 %token T_ELLIPSIS "..."
164 %token T_TYPEDEF "typedef"
165 %token T_EXTERN "extern"
166 %token T_STATIC "static"
168 %token T_REGISTER "register"
170 %token T_INLINE "inline"
172 %token T_RESTRICT "restrict"
173 %token T_VOLATILE "volatile"
174 %token T_CONST "const"
178 %token T_SHORT "short"
181 %token T_FLOAT "float"
182 %token T_DOUBLE "double"
183 %token T_SIGNED "signed"
184 %token T_UNSIGNED "unsigned"
185 %token T_BOOL "_Bool"
186 %token T_COMPLEX "_Complex"
187 %token T_IMAGINARY "_Imaginary"
189 %token T_STRUCT "struct"
190 %token T_UNION "union"
197 %token T_DECLARE "declare"
198 %token T_POINTER "pointer"
199 %token T_FUNCTION "function"
200 %token T_RETURNING "returning"
201 %token T_ARRAY "array"
205 %token T_VLA "variable-length"
207 %type <strval> vla_ident
208 %type <boolval> varargs
209 %type <spectype> declspec_simple typespec_simple qualifier_simple
210 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
211 %type <declspec> qualifier qualifiers
212 %type <declspec> declspecs declspecs_noid
213 %type <declarator> direct_declarator declarator pointer array parens postfix
214 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
215 %type <decl> declaration declarators declarator_wrap
216 %type <decl> parameter parameters
218 %type <strval> english_vla
219 %type <declspec> storage_func_specs post_specs
220 %type <declspec> type_qual_spec type_qual_specs typedef_name_qual
221 %type <declarator> english_declarator english_array english_function
222 %type <declarator> english_parameter_list null_decl
223 %type <decl> english_parameter english_parameters
224 %type <decl> english english_declaration
227 * Harmless shift/reduce conflicts in english_parameter. See comments below
234 input: T_ENGLISH english {
242 declaration: declspecs declarators semi {
245 for (struct cdecl *i = $$; i; i = i->next)
249 declspecs: declspec_notype declspecs {
252 } | typespec declspecs_noid {
257 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
262 qualifiers: { $$ = NULL; } | qualifiers qualifier {
267 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
272 declarator_wrap: declarator {
273 ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
276 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO; }
277 | T_TYPEDEF { $$ = CDECL_STOR_TYPEDEF; }
278 | T_EXTERN { $$ = CDECL_STOR_EXTERN; }
279 | T_STATIC { $$ = CDECL_STOR_STATIC; }
280 | T_REGISTER { $$ = CDECL_STOR_REGISTER; }
281 | T_INLINE { $$ = CDECL_FUNC_INLINE; }
283 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID; }
284 | T_CHAR { $$ = CDECL_TYPE_CHAR; }
285 | T_SHORT { $$ = CDECL_TYPE_SHORT; }
286 | T_INT { $$ = CDECL_TYPE_INT; }
287 | T_LONG { $$ = CDECL_TYPE_LONG; }
288 | T_FLOAT { $$ = CDECL_TYPE_FLOAT; }
289 | T_DOUBLE { $$ = CDECL_TYPE_DOUBLE; }
290 | T_SIGNED { $$ = CDECL_TYPE_SIGNED; }
291 | T_UNSIGNED { $$ = CDECL_TYPE_UNSIGNED; }
292 | T_BOOL { $$ = CDECL_TYPE_BOOL; }
293 | T_COMPLEX { $$ = CDECL_TYPE_COMPLEX; }
294 | T_IMAGINARY { $$ = CDECL_TYPE_IMAGINARY; }
296 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST; }
297 | T_RESTRICT { $$ = CDECL_QUAL_RESTRICT; }
298 | T_VOLATILE { $$ = CDECL_QUAL_VOLATILE; }
300 declspec_notype: qualifier | declspec_simple {
301 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
304 typespec_noid: typespec_simple {
305 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
308 qualifier: qualifier_simple {
309 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
312 typespec: typespec_noid | T_STRUCT T_IDENT {
313 ALLOC_STRUCT($$, struct cdecl_declspec,
314 .type = CDECL_TYPE_STRUCT,
316 } | T_UNION T_IDENT {
317 ALLOC_STRUCT($$, struct cdecl_declspec,
318 .type = CDECL_TYPE_UNION,
321 ALLOC_STRUCT($$, struct cdecl_declspec,
322 .type = CDECL_TYPE_ENUM,
325 ALLOC_STRUCT($$, struct cdecl_declspec,
326 .type = CDECL_TYPE_IDENT,
330 declspec_noid: declspec_notype | typespec_noid
332 vla_ident: T_IDENT | T_ASTERISK {
333 ALLOC($$, sizeof "");
337 array: T_LBRACKET T_UINT T_RBRACKET {
339 FAIL("array length must be positive");
341 ALLOC_STRUCT($$, struct cdecl_declarator,
342 .type = CDECL_DECL_ARRAY,
343 .u.array.length = $2);
344 } | T_LBRACKET vla_ident T_RBRACKET {
345 ALLOC_STRUCT($$, struct cdecl_declarator,
346 .type = CDECL_DECL_ARRAY,
348 } | T_LBRACKET T_RBRACKET {
349 ALLOC_STRUCT($$, struct cdecl_declarator,
350 .type = CDECL_DECL_ARRAY);
353 parameter: declspecs declarator {
354 ALLOC_STRUCT($$, struct cdecl,
359 parameters: parameter | parameters T_COMMA parameter {
364 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
366 parameter_type_list: parameters varargs {
367 struct cdecl *p, *c, *n;
369 /* Parameters were accumulated in reverse order. */
370 for (p = NULL, c = $1; c; p = c, c = n) {
375 ALLOC_STRUCT($$, struct cdecl_declarator,
376 .type = CDECL_DECL_FUNCTION,
377 .u.function.parameters = p,
378 .u.function.variadic = $2);
381 parens: T_LPAREN parameter_type_list T_RPAREN {
383 } | T_LPAREN declarator_ish T_RPAREN {
384 ALLOC_STRUCT($$, struct cdecl_declarator,
385 .type = CDECL_DECL_FUNCTION);
386 ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
390 pointer: T_ASTERISK qualifiers direct_declarator {
391 ALLOC_STRUCT($$, struct cdecl_declarator,
392 .type = CDECL_DECL_POINTER,
393 .u.pointer.qualifiers = $2,
395 } | T_ASTERISK qualifiers pointer {
396 ALLOC_STRUCT($$, struct cdecl_declarator,
397 .type = CDECL_DECL_POINTER,
398 .u.pointer.qualifiers = $2,
402 declarator: direct_declarator | pointer
403 declarator_ish: direct_declarator_ish | pointer
404 postfix: array | parens
406 direct_declarator_ish: {
407 ALLOC_STRUCT($$, struct cdecl_declarator,
408 .type = CDECL_DECL_NULL);
409 } | direct_declarator_ish postfix {
415 ALLOC_STRUCT($$, struct cdecl_declarator,
416 .type = CDECL_DECL_NULL);
418 ALLOC_STRUCT($$, struct cdecl_declarator,
419 .type = CDECL_DECL_IDENT,
421 } | direct_declarator postfix {
426 english: T_DECLARE T_IDENT T_AS english_declaration {
428 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
429 if (d->type == CDECL_DECL_NULL) {
430 d->type = CDECL_DECL_IDENT;
434 } | T_TYPE english_declaration {
438 storage_func_specs: { $$ = NULL; } | declspec_simple storage_func_specs {
439 ALLOC_STRUCT($$, struct cdecl_declspec,
444 type_qual_spec: typespec_noid | qualifier
446 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
452 * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
453 * conflicts with pointer declarators. So we end up needing to stitch
454 * together three different specifiers lists.
456 post_specs: qualifiers typespec type_qual_specs {
459 for (struct cdecl_declspec *s = $$; s; s = s->next) {
467 english_declaration: storage_func_specs english_declarator post_specs {
468 ALLOC_STRUCT($$, struct cdecl,
472 for (struct cdecl_declspec *s = $$->specifiers; s; s = s->next) {
480 english_declarator: {
481 ALLOC_STRUCT($$, struct cdecl_declarator,
482 .type = CDECL_DECL_NULL);
483 } | english_declarator qualifiers T_POINTER T_TO {
484 ALLOC_STRUCT($$, struct cdecl_declarator,
485 .type = CDECL_DECL_POINTER,
487 .u.pointer.qualifiers = $2);
488 } | english_declarator english_array {
491 } | english_declarator english_function {
496 english_function: T_FUNCTION T_RETURNING {
497 ALLOC_STRUCT($$, struct cdecl_declarator,
498 .type = CDECL_DECL_FUNCTION,
499 .u.function.parameters = NULL);
500 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
504 english_parameter_list: english_parameters varargs {
505 struct cdecl *p, *c, *n;
507 /* Parameters were accumulated in reverse order. */
508 for (p = NULL, c = $1; c; p = c, c = n) {
513 ALLOC_STRUCT($$, struct cdecl_declarator,
514 .type = CDECL_DECL_FUNCTION,
515 .u.function.parameters = p,
516 .u.function.variadic = $2);
519 english_parameters: english_parameters T_COMMA english_parameter {
522 } | english_parameter
524 typedef_name_qual: T_IDENT qualifiers {
525 ALLOC_STRUCT($$, struct cdecl_declspec,
526 .type = CDECL_TYPE_IDENT,
532 ALLOC_STRUCT($$, struct cdecl_declarator,
533 .type = CDECL_DECL_NULL);
537 * There is a small shift/reduce conflict here. An unadorned identifier
538 * as the first thing in the parameter might be a typedef name deep in the
539 * first english_declaration (thus empty storage_func_specs and empty
540 * english_declarator need to be reduced) or it might be the identifier
541 * before the "as" (thus the identifier should be shifted).
543 * The typedef name conflict is the only issue, so treating it as a special
544 * case makes the shift harmless.
546 english_parameter: english_declaration | typedef_name_qual null_decl {
547 ALLOC_STRUCT($$, struct cdecl,
550 } | T_IDENT T_AS english_declaration {
552 for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
553 if (d->type == CDECL_DECL_NULL) {
554 d->type = CDECL_DECL_IDENT;
560 english_array: T_VLA T_ARRAY english_vla T_OF {
561 ALLOC_STRUCT($$, struct cdecl_declarator,
562 .type = CDECL_DECL_ARRAY,
564 } | T_ARRAY T_UINT T_OF {
566 FAIL("array length must be positive");
568 ALLOC_STRUCT($$, struct cdecl_declarator,
569 .type = CDECL_DECL_ARRAY,
570 .u.array.length = $2);
572 ALLOC_STRUCT($$, struct cdecl_declarator,
573 .type = CDECL_DECL_ARRAY,
574 .u.array.length = 0);
577 english_vla: T_IDENT | {
578 ALLOC($$, sizeof "");
584 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err)
586 if (strstr(err, "T_LEX_ERROR"))
589 cdecl__set_error(&(const struct cdecl_error){
590 .code = CDECL_ENOPARSE,