3 * Parser for C declarations.
4 * Copyright © 2011-2012, 2021, 2023 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"
39 #define FAIL(msg) do { \
40 yyerror(&yylloc, NULL, NULL, msg); \
44 #define ALLOC(ptr, size) do { \
45 (ptr) = malloc(size); \
47 cdecl__errmsg(CDECL__ENOMEM); \
52 #define ALLOC_STRUCT(ptr, type, ...) do { \
53 ALLOC(ptr, sizeof (type)); \
54 *(ptr) = (type) { __VA_ARGS__ }; \
58 * With the postprocessing performed by fix-yytname.awk, all the symbol
59 * name strings can be used directly in error messages and there is no
60 * need for any string processing.
62 #define yytnamerr(a, b) cdecl__strlcpy(a, b, (a) ? INT_MAX : 0)
70 void cdecl__free(struct cdecl *);
71 int cdecl__yyparse(void *scanner, struct cdecl **out);
72 const char *cdecl__token_name(unsigned token);
80 struct cdecl_declspec *declspec;
81 struct cdecl_declarator *declarator;
86 static void yyerror(YYLTYPE *, yyscan_t, struct cdecl **, const char *);
87 static void free_decl(struct cdecl *);
89 static void free_declspec(struct cdecl_declspec *x)
91 struct cdecl_declspec *p;
100 static void free_declarator(struct cdecl_declarator *x)
102 struct cdecl_declarator *p;
108 case CDECL_DECL_NULL:
110 case CDECL_DECL_IDENT:
113 case CDECL_DECL_POINTER:
114 free_declspec(x->u.pointer.qualifiers);
116 case CDECL_DECL_ARRAY:
117 free(x->u.array.vla);
119 case CDECL_DECL_FUNCTION:
120 free_decl(x->u.function.parameters);
131 static void free_decl(struct cdecl *x)
138 /* The specifiers may be shared by an entire chain. */
139 if (!p || p->specifiers != x->specifiers)
140 free_declspec(x->specifiers);
142 free_declarator(x->declarators);
148 void cdecl__free(struct cdecl *decl)
154 * Join two declaration specifier lists into a single list, with "a" being the
155 * head of the new list.
157 * The list "a" is assumed to be nonempty.
159 static void join_specs(struct cdecl_declspec *a, struct cdecl_declspec *b)
167 * Alter an abstract declarator (type name) to declare an identifier instead,
168 * used by the English parser rules to reduce "identifier as type" sequences.
170 static struct cdecl *insert_identifier(struct cdecl *decl, char *ident)
172 struct cdecl_declarator *d = decl->declarators;
177 d->type = CDECL_DECL_IDENT;
184 %destructor { free($$); } <strval>
185 %destructor { free_declspec($$); } <declspec>
186 %destructor { free_declarator($$); } <declarator>
187 %destructor { free_decl($$); } <decl>
192 %token <strval> T_IDENT "identifier"
193 %token <uintval> T_UINT "integer constant"
195 %token T_SEMICOLON ";"
196 %token T_ASTERISK "*"
199 %token T_LBRACKET "["
200 %token T_RBRACKET "]"
202 %token T_ELLIPSIS "..."
204 %token <spectype> T_TYPEDEF "typedef"
205 %token <spectype> T_EXTERN "extern"
206 %token <spectype> T_STATIC "static"
207 %token <spectype> T_AUTO "auto"
208 %token <spectype> T_REGISTER "register"
210 %token <spectype> T_INLINE "inline"
212 %token <spectype> T_RESTRICT "restrict"
213 %token <spectype> T_VOLATILE "volatile"
214 %token <spectype> T_CONST "const"
216 %token <spectype> T_VOID "void"
217 %token <spectype> T_CHAR "char"
218 %token <spectype> T_SHORT "short"
219 %token <spectype> T_INT "int"
220 %token <spectype> T_LONG "long"
221 %token <spectype> T_FLOAT "float"
222 %token <spectype> T_DOUBLE "double"
223 %token <spectype> T_SIGNED "signed"
224 %token <spectype> T_UNSIGNED "unsigned"
225 %token <spectype> T_BOOL "_Bool"
226 %token <spectype> T_COMPLEX "_Complex"
227 %token <spectype> T_IMAGINARY "_Imaginary"
229 %token <spectype> T_STRUCT "struct"
230 %token <spectype> T_UNION "union"
231 %token <spectype> T_ENUM "enum"
237 %token T_DECLARE "declare"
238 %token T_POINTER "pointer"
239 %token T_FUNCTION "function"
240 %token T_RETURNING "returning"
241 %token T_ARRAY "array"
245 %token T_VLA "variable-length"
247 %type <strval> vla_ident
248 %type <uintval> array_length
249 %type <boolval> varargs
250 %type <spectype> declspec_simple qualifier_simple
251 %type <spectype> typespec_simple typespec_tagged
252 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
253 %type <declspec> qualifier qualifiers
254 %type <declspec> declspecs declspecs_noid
255 %type <declarator> direct_declarator declarator pointer array parens postfix
256 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
257 %type <decl> declaration declarators declarator_wrap
258 %type <decl> parameter
260 %type <strval> english_vla
261 %type <declspec> storage_func_specs post_specs
262 %type <declspec> type_qual_spec type_qual_specs typedef_name_qual
263 %type <declarator> english_declarator english_array english_function
264 %type <declarator> english_parameter_list null_decl
265 %type <decl> english english_declaration
266 %type <decl> english_parameter
269 * Harmless shift/reduce conflicts in english_parameter. See comments below
284 declaration: declspecs declarators semi {
290 * We support parsing declarations using arbitrary identifiers as type
291 * specifiers (a la C typedef). To avoid confusion with identifiers that
292 * may also be used as declarators, note the following:
294 * (a) Every valid C declaration must have at least one type specifier, and
295 * (b) Valid declarations with typedef names have exactly one type specifier.
297 * So the rule applied when parsing specifiers is: an identifier is a type
298 * specifier only if we have not yet seen any type specifiers whatsoever
299 * (within one declaration specifier list).
301 * Treating identifiers as type specifiers by default can lead to strange and
302 * unexpected parses; libcdecl applies a simplification step to the resulting
303 * parse tree afterwards.
305 declspecs: declspec_notype declspecs {
308 } | typespec declspecs_noid {
313 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
318 qualifiers: { $$ = NULL; } | qualifiers qualifier {
323 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
328 declarator_wrap: declarator {
329 ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
332 declspec_simple: T_AUTO
339 typespec_simple: T_VOID
352 typespec_tagged: T_STRUCT | T_UNION | T_ENUM | { $$ = CDECL_TYPE_IDENT; }
354 qualifier_simple: T_CONST
358 declspec_notype: qualifier | declspec_simple {
359 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
362 typespec_noid: typespec_simple {
363 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
366 qualifier: qualifier_simple {
367 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
370 typespec: typespec_noid | typespec_tagged T_IDENT {
371 ALLOC_STRUCT($$, struct cdecl_declspec,
376 declspec_noid: declspec_notype | typespec_noid
378 vla_ident: T_IDENT | T_ASTERISK {
379 ALLOC($$, sizeof "");
383 array: T_LBRACKET array_length T_RBRACKET {
384 ALLOC_STRUCT($$, struct cdecl_declarator,
385 .type = CDECL_DECL_ARRAY,
386 .u.array.length = $2);
387 } | T_LBRACKET vla_ident T_RBRACKET {
388 ALLOC_STRUCT($$, struct cdecl_declarator,
389 .type = CDECL_DECL_ARRAY,
393 parameter: declspecs declarator {
394 ALLOC_STRUCT($$, struct cdecl,
399 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
401 parameter_type_list: parameter varargs {
402 ALLOC_STRUCT($$, struct cdecl_declarator,
403 .type = CDECL_DECL_FUNCTION,
404 .u.function.parameters = $1,
405 .u.function.variadic = $2);
406 } | parameter T_COMMA parameter_type_list {
408 $1->next = $$->u.function.parameters;
409 $$->u.function.parameters = $1;
412 parens: T_LPAREN parameter_type_list T_RPAREN {
414 } | T_LPAREN declarator_ish T_RPAREN {
415 ALLOC_STRUCT($$, struct cdecl_declarator,
416 .type = CDECL_DECL_FUNCTION);
417 ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
421 pointer: T_ASTERISK qualifiers direct_declarator {
422 ALLOC_STRUCT($$, struct cdecl_declarator,
423 .type = CDECL_DECL_POINTER,
424 .u.pointer.qualifiers = $2,
426 } | T_ASTERISK qualifiers pointer {
427 ALLOC_STRUCT($$, struct cdecl_declarator,
428 .type = CDECL_DECL_POINTER,
429 .u.pointer.qualifiers = $2,
433 declarator: direct_declarator | pointer
434 declarator_ish: direct_declarator_ish | pointer
435 postfix: array | parens
437 direct_declarator_ish: {
438 ALLOC_STRUCT($$, struct cdecl_declarator,
439 .type = CDECL_DECL_NULL);
440 } | direct_declarator_ish postfix {
446 ALLOC_STRUCT($$, struct cdecl_declarator,
447 .type = CDECL_DECL_NULL);
449 ALLOC_STRUCT($$, struct cdecl_declarator,
450 .type = CDECL_DECL_IDENT,
452 } | direct_declarator postfix {
457 english: T_DECLARE T_IDENT T_AS english_declaration {
458 $$ = insert_identifier($4, $2);
459 } | T_TYPE english_declaration {
463 storage_func_specs: { $$ = NULL; } | declspec_simple storage_func_specs {
464 ALLOC_STRUCT($$, struct cdecl_declspec,
469 type_qual_spec: typespec_noid | qualifier
471 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
477 * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
478 * conflicts with pointer declarators. So we end up needing to stitch
479 * together three different specifiers lists.
481 post_specs: qualifiers typespec type_qual_specs {
487 english_declaration: storage_func_specs english_declarator post_specs {
489 ALLOC_STRUCT($$, struct cdecl,
494 english_declarator: {
495 ALLOC_STRUCT($$, struct cdecl_declarator,
496 .type = CDECL_DECL_NULL);
497 } | english_declarator qualifiers T_POINTER T_TO {
498 ALLOC_STRUCT($$, struct cdecl_declarator,
499 .type = CDECL_DECL_POINTER,
501 .u.pointer.qualifiers = $2);
502 } | english_declarator english_array {
505 } | english_declarator english_function {
510 english_function: T_FUNCTION T_RETURNING {
511 ALLOC_STRUCT($$, struct cdecl_declarator,
512 .type = CDECL_DECL_FUNCTION,
513 .u.function.parameters = NULL);
514 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
518 english_parameter_list: english_parameter varargs {
519 ALLOC_STRUCT($$, struct cdecl_declarator,
520 .type = CDECL_DECL_FUNCTION,
521 .u.function.parameters = $1,
522 .u.function.variadic = $2);
523 } | english_parameter T_COMMA english_parameter_list {
525 $1->next = $$->u.function.parameters;
526 $$->u.function.parameters = $1;
529 typedef_name_qual: T_IDENT qualifiers {
530 ALLOC_STRUCT($$, struct cdecl_declspec,
531 .type = CDECL_TYPE_IDENT,
537 ALLOC_STRUCT($$, struct cdecl_declarator,
538 .type = CDECL_DECL_NULL);
542 * There is a small shift/reduce conflict here. An unadorned identifier
543 * as the first thing in the parameter might be a typedef name deep in the
544 * first english_declaration (thus empty storage_func_specs and empty
545 * english_declarator need to be reduced) or it might be the identifier
546 * before the "as" (thus the identifier should be shifted).
548 * The typedef name conflict is the only issue, so treating it as a special
549 * case makes the shift harmless.
551 english_parameter: english_declaration | typedef_name_qual null_decl {
552 ALLOC_STRUCT($$, struct cdecl,
555 } | T_IDENT T_AS english_declaration {
556 $$ = insert_identifier($3, $1);
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 array_length T_OF {
564 ALLOC_STRUCT($$, struct cdecl_declarator,
565 .type = CDECL_DECL_ARRAY,
566 .u.array.length = $2);
569 array_length: { $$ = 0; }
570 array_length: T_UINT {
572 FAIL(_("array length must be positive"));
575 english_vla: T_IDENT | {
576 ALLOC($$, sizeof "");
583 * Expose the token string table to the rest of the library, in order to
584 * produce strings that match parser keywords.
586 * In order for this to work properly, the Bison output must be postprocessed
587 * by fix-yytname.awk to remove pointless quotation marks from the keyword
590 const char *cdecl__token_name(unsigned token)
592 return yytname[YYTRANSLATE(token)];
596 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err)
598 if (strstr(err, yytname[YYTRANSLATE(T_LEX_ERROR)]))
601 cdecl__err(CDECL_ENOPARSE, "%s", err);