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>
145 %token <strval> T_IDENT "identifier"
146 %token <uintval> T_UINT "integer constant"
148 %token T_SEMICOLON ";"
149 %token T_ASTERISK "*"
152 %token T_LBRACKET "["
153 %token T_RBRACKET "]"
155 %token T_ELLIPSIS "."
157 %token T_TYPEDEF "typedef"
158 %token T_EXTERN "extern"
159 %token T_STATIC "static"
161 %token T_REGISTER "register"
163 %token T_INLINE "inline"
165 %token T_RESTRICT "restrict"
166 %token T_VOLATILE "volatile"
167 %token T_CONST "const"
171 %token T_SHORT "short"
174 %token T_FLOAT "float"
175 %token T_DOUBLE "double"
176 %token T_SIGNED "signed"
177 %token T_UNSIGNED "unsigned"
178 %token T_BOOL "_Bool"
179 %token T_COMPLEX "_Complex"
180 %token T_IMAGINARY "_Imaginary"
182 %token T_STRUCT "struct"
183 %token T_UNION "union"
186 %type <strval> vla_ident
187 %type <boolval> varargs
188 %type <uintval> declspec_simple typespec_simple qualifier_simple
189 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
190 %type <declspec> qualifier qualifiers
191 %type <declspec> declspecs declspecs_noid
192 %type <declarator> direct_declarator declarator pointer array parens postfix
193 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
194 %type <decl> declaration declarators declarator_wrap
195 %type <decl> parameter parameters
203 declaration: declspecs declarators T_SEMICOLON {
206 for (struct cdecl *i = $$; i; i = i->next)
210 declspecs: declspec_notype declspecs {
213 } | typespec declspecs_noid {
218 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
223 qualifiers: { $$ = NULL; } | qualifiers qualifier {
228 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
233 declarator_wrap: declarator {
234 ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
237 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO; }
238 | T_TYPEDEF { $$ = CDECL_STOR_TYPEDEF; }
239 | T_EXTERN { $$ = CDECL_STOR_EXTERN; }
240 | T_STATIC { $$ = CDECL_STOR_STATIC; }
241 | T_REGISTER { $$ = CDECL_STOR_REGISTER; }
242 | T_INLINE { $$ = CDECL_FUNC_INLINE; }
244 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID; }
245 | T_CHAR { $$ = CDECL_TYPE_CHAR; }
246 | T_SHORT { $$ = CDECL_TYPE_SHORT; }
247 | T_INT { $$ = CDECL_TYPE_INT; }
248 | T_LONG { $$ = CDECL_TYPE_LONG; }
249 | T_FLOAT { $$ = CDECL_TYPE_FLOAT; }
250 | T_DOUBLE { $$ = CDECL_TYPE_DOUBLE; }
251 | T_SIGNED { $$ = CDECL_TYPE_SIGNED; }
252 | T_UNSIGNED { $$ = CDECL_TYPE_UNSIGNED; }
253 | T_BOOL { $$ = CDECL_TYPE_BOOL; }
254 | T_COMPLEX { $$ = CDECL_TYPE_COMPLEX; }
255 | T_IMAGINARY { $$ = CDECL_TYPE_IMAGINARY; }
257 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST; }
258 | T_RESTRICT { $$ = CDECL_QUAL_RESTRICT; }
259 | T_VOLATILE { $$ = CDECL_QUAL_VOLATILE; }
261 declspec_notype: qualifier | declspec_simple {
262 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
265 typespec_noid: typespec_simple {
266 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
269 qualifier: qualifier_simple {
270 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
273 typespec: typespec_noid | T_STRUCT T_IDENT {
274 ALLOC_STRUCT($$, struct cdecl_declspec,
275 .type = CDECL_TYPE_STRUCT,
277 } | T_UNION T_IDENT {
278 ALLOC_STRUCT($$, struct cdecl_declspec,
279 .type = CDECL_TYPE_UNION,
282 ALLOC_STRUCT($$, struct cdecl_declspec,
283 .type = CDECL_TYPE_ENUM,
286 ALLOC_STRUCT($$, struct cdecl_declspec,
287 .type = CDECL_TYPE_IDENT,
291 declspec_noid: declspec_notype | typespec_noid
293 vla_ident: T_IDENT | T_ASTERISK {
294 ALLOC($$, sizeof "");
298 array: T_LBRACKET T_UINT T_RBRACKET {
300 FAIL("array length must be positive");
302 ALLOC_STRUCT($$, struct cdecl_declarator,
303 .type = CDECL_DECL_ARRAY,
304 .u.array.length = $2);
305 } | T_LBRACKET vla_ident T_RBRACKET {
306 ALLOC_STRUCT($$, struct cdecl_declarator,
307 .type = CDECL_DECL_ARRAY,
309 } | T_LBRACKET T_RBRACKET {
310 ALLOC_STRUCT($$, struct cdecl_declarator,
311 .type = CDECL_DECL_ARRAY);
314 parameter: declspecs declarator {
315 ALLOC_STRUCT($$, struct cdecl,
320 parameters: parameter | parameters T_COMMA parameter {
325 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
327 parameter_type_list: parameters varargs {
328 struct cdecl *p, *c, *n;
330 /* Parameters were accumulated in reverse order. */
331 for (p = NULL, c = $1; c; p = c, c = n) {
336 ALLOC_STRUCT($$, struct cdecl_declarator,
337 .type = CDECL_DECL_FUNCTION,
338 .u.function.parameters = p,
339 .u.function.variadic = $2);
342 parens: T_LPAREN parameter_type_list T_RPAREN {
344 } | T_LPAREN declarator_ish T_RPAREN {
345 ALLOC_STRUCT($$, struct cdecl_declarator,
346 .type = CDECL_DECL_FUNCTION);
347 ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
351 pointer: T_ASTERISK qualifiers direct_declarator {
352 ALLOC_STRUCT($$, struct cdecl_declarator,
353 .type = CDECL_DECL_POINTER,
354 .u.pointer.qualifiers = $2,
356 } | T_ASTERISK qualifiers pointer {
357 ALLOC_STRUCT($$, struct cdecl_declarator,
358 .type = CDECL_DECL_POINTER,
359 .u.pointer.qualifiers = $2,
363 declarator: direct_declarator | pointer
364 declarator_ish: direct_declarator_ish | pointer
365 postfix: array | parens
367 direct_declarator_ish: {
368 ALLOC_STRUCT($$, struct cdecl_declarator,
369 .type = CDECL_DECL_NULL);
370 } | direct_declarator_ish postfix {
376 ALLOC_STRUCT($$, struct cdecl_declarator,
377 .type = CDECL_DECL_NULL);
379 ALLOC_STRUCT($$, struct cdecl_declarator,
380 .type = CDECL_DECL_IDENT,
382 } | direct_declarator postfix {
389 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out,
392 if (strstr(err, "T_LEX_ERROR"))
395 fprintf(stderr, "%s\n", err);