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 {struct cdecl **out}
33 #define FAIL(msg) do { \
34 yyerror(&yylloc, NULL, msg); \
38 #define ALLOC(ptr, size) do { \
39 (ptr) = malloc(size); \
41 FAIL("failed to allocate memory"); \
44 #define ALLOC_STRUCT(ptr, type, ...) do { \
45 ALLOC(ptr, sizeof (type)); \
46 *(ptr) = (type) { __VA_ARGS__ }; \
55 void yyerror(YYLTYPE *, struct cdecl **, const char *);
56 int yyparse(struct cdecl **out);
63 struct cdecl_declspec *declspec;
64 struct cdecl_declarator *declarator;
69 static void free_decl(struct cdecl *);
71 static void free_declspec(struct cdecl_declspec *x)
73 struct cdecl_declspec *p;
82 static void free_declarator(struct cdecl_declarator *x)
84 struct cdecl_declarator *p;
92 case CDECL_DECL_IDENT:
95 case CDECL_DECL_POINTER:
96 free_declspec(x->u.pointer.qualifiers);
98 case CDECL_DECL_ARRAY:
101 case CDECL_DECL_FUNCTION:
102 free_decl(x->u.function.parameters);
113 static void free_decl(struct cdecl *x)
120 /* The specifiers may be shared by an entire chain. */
121 if (!p || p->specifiers != x->specifiers)
122 free_declspec(x->specifiers);
124 free_declarator(x->declarators);
130 void cdecl_free(struct cdecl *decl)
136 %destructor { free($$); } <strval>
137 %destructor { free_declspec($$); } <declspec>
138 %destructor { free_declarator($$); } <declarator>
139 %destructor { free_decl($$); } <decl>
143 %token <strval> T_IDENT "identifier"
144 %token <uintval> T_UINT "integer constant"
146 %token T_SEMICOLON ";"
147 %token T_ASTERISK "*"
150 %token T_LBRACKET "["
151 %token T_RBRACKET "]"
153 %token T_ELLIPSIS "."
155 %token T_TYPEDEF "typedef"
156 %token T_EXTERN "extern"
157 %token T_STATIC "static"
159 %token T_REGISTER "register"
161 %token T_INLINE "inline"
163 %token T_RESTRICT "restrict"
164 %token T_VOLATILE "volatile"
165 %token T_CONST "const"
169 %token T_SHORT "short"
172 %token T_FLOAT "float"
173 %token T_DOUBLE "double"
174 %token T_SIGNED "signed"
175 %token T_UNSIGNED "unsigned"
176 %token T_BOOL "_Bool"
177 %token T_COMPLEX "_Complex"
179 %token T_STRUCT "struct"
180 %token T_UNION "union"
183 %type <strval> vla_ident
184 %type <boolval> varargs
185 %type <uintval> declspec_simple typespec_simple qualifier_simple
186 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
187 %type <declspec> qualifier qualifiers
188 %type <declspec> declspecs declspecs_noid
189 %type <declarator> direct_declarator declarator pointer array parens postfix
190 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
191 %type <decl> declaration declarators declarator_wrap
192 %type <decl> parameter parameters
200 declaration: declspecs declarators T_SEMICOLON {
203 for (struct cdecl *i = $$; i; i = i->next)
207 declspecs: declspec_notype declspecs {
210 } | typespec declspecs_noid {
215 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
220 qualifiers: { $$ = NULL; } | qualifiers qualifier {
225 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
230 declarator_wrap: declarator {
231 ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
234 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO; }
235 | T_TYPEDEF { $$ = CDECL_STOR_TYPEDEF; }
236 | T_EXTERN { $$ = CDECL_STOR_EXTERN; }
237 | T_STATIC { $$ = CDECL_STOR_STATIC; }
238 | T_REGISTER { $$ = CDECL_STOR_REGISTER; }
239 | T_INLINE { $$ = CDECL_FUNC_INLINE; }
241 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID; }
242 | T_CHAR { $$ = CDECL_TYPE_CHAR; }
243 | T_SHORT { $$ = CDECL_TYPE_SHORT; }
244 | T_INT { $$ = CDECL_TYPE_INT; }
245 | T_LONG { $$ = CDECL_TYPE_LONG; }
246 | T_FLOAT { $$ = CDECL_TYPE_FLOAT; }
247 | T_DOUBLE { $$ = CDECL_TYPE_DOUBLE; }
248 | T_SIGNED { $$ = CDECL_TYPE_SIGNED; }
249 | T_UNSIGNED { $$ = CDECL_TYPE_UNSIGNED; }
250 | T_BOOL { $$ = CDECL_TYPE_BOOL; }
251 | T_COMPLEX { $$ = CDECL_TYPE_COMPLEX; }
253 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST; }
254 | T_RESTRICT { $$ = CDECL_QUAL_RESTRICT; }
255 | T_VOLATILE { $$ = CDECL_QUAL_VOLATILE; }
257 declspec_notype: qualifier | declspec_simple {
258 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
261 typespec_noid: typespec_simple {
262 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
265 qualifier: qualifier_simple {
266 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
269 typespec: typespec_noid | T_STRUCT T_IDENT {
270 ALLOC_STRUCT($$, struct cdecl_declspec,
271 .type = CDECL_TYPE_STRUCT,
273 } | T_UNION T_IDENT {
274 ALLOC_STRUCT($$, struct cdecl_declspec,
275 .type = CDECL_TYPE_UNION,
278 ALLOC_STRUCT($$, struct cdecl_declspec,
279 .type = CDECL_TYPE_ENUM,
282 ALLOC_STRUCT($$, struct cdecl_declspec,
283 .type = CDECL_TYPE_IDENT,
287 declspec_noid: declspec_notype | typespec_noid
289 vla_ident: T_IDENT | T_ASTERISK {
290 ALLOC($$, sizeof "");
294 array: T_LBRACKET T_UINT T_RBRACKET {
296 FAIL("array length must be positive");
298 ALLOC_STRUCT($$, struct cdecl_declarator,
299 .type = CDECL_DECL_ARRAY,
300 .u.array.length = $2);
301 } | T_LBRACKET vla_ident T_RBRACKET {
302 ALLOC_STRUCT($$, struct cdecl_declarator,
303 .type = CDECL_DECL_ARRAY,
305 } | T_LBRACKET T_RBRACKET {
306 ALLOC_STRUCT($$, struct cdecl_declarator,
307 .type = CDECL_DECL_ARRAY);
310 parameter: declspecs declarator {
311 ALLOC_STRUCT($$, struct cdecl,
316 parameters: parameter | parameters T_COMMA parameter {
321 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
323 parameter_type_list: parameters varargs {
324 struct cdecl *p, *c, *n;
326 /* Parameters were accumulated in reverse order. */
327 for (p = NULL, c = $1; c; p = c, c = n) {
332 ALLOC_STRUCT($$, struct cdecl_declarator,
333 .type = CDECL_DECL_FUNCTION,
334 .u.function.parameters = p,
335 .u.function.variadic = $2);
338 parens: T_LPAREN parameter_type_list T_RPAREN {
340 } | T_LPAREN declarator_ish T_RPAREN {
341 ALLOC_STRUCT($$, struct cdecl_declarator,
342 .type = CDECL_DECL_FUNCTION);
343 ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
347 pointer: T_ASTERISK qualifiers direct_declarator {
348 ALLOC_STRUCT($$, struct cdecl_declarator,
349 .type = CDECL_DECL_POINTER,
350 .u.pointer.qualifiers = $2,
352 } | T_ASTERISK qualifiers pointer {
353 ALLOC_STRUCT($$, struct cdecl_declarator,
354 .type = CDECL_DECL_POINTER,
355 .u.pointer.qualifiers = $2,
359 declarator: direct_declarator | pointer
360 declarator_ish: direct_declarator_ish | pointer
361 postfix: array | parens
363 direct_declarator_ish: {
364 ALLOC_STRUCT($$, struct cdecl_declarator,
365 .type = CDECL_DECL_NULL);
366 } | direct_declarator_ish postfix {
372 ALLOC_STRUCT($$, struct cdecl_declarator,
373 .type = CDECL_DECL_NULL);
375 ALLOC_STRUCT($$, struct cdecl_declarator,
376 .type = CDECL_DECL_IDENT,
378 } | direct_declarator postfix {
384 void yyerror(YYLTYPE *loc, struct cdecl **out, const char *err)
386 if (strstr(err, "T_LEX_ERROR"))
389 fprintf(stderr, "%s\n", err);