2 * Parser for C declarations.
3 * Copyright © 2011 Nick Bowler
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 %parse-param {struct cdecl **out}
30 #define FAIL(msg) do { \
31 yyerror(&yylloc, NULL, msg); \
35 #define ALLOC(ptr, size) do { \
36 (ptr) = malloc(size); \
38 FAIL("failed to allocate memory"); \
41 #define ALLOC_STRUCT(ptr, type, ...) do { \
42 ALLOC(ptr, sizeof (type)); \
43 *(ptr) = (type) { __VA_ARGS__ }; \
52 void yyerror(YYLTYPE *, struct cdecl **, const char *);
53 int yyparse(struct cdecl **out);
59 struct cdecl_declspec *declspec;
60 struct cdecl_declarator *declarator;
65 static void free_declspec(struct cdecl_declspec *x)
67 struct cdecl_declspec *p;
76 static void free_declarator(struct cdecl_declarator *x)
78 struct cdecl_declarator *p;
86 case CDECL_DECL_IDENT:
89 case CDECL_DECL_POINTER:
90 free_declspec(x->u.pointer.qualifiers);
92 case CDECL_DECL_ARRAY:
104 static void free_decl(struct cdecl *x)
111 /* The specifiers may be shared by an entire chain. */
112 if (!p || p->specifiers != x->specifiers)
113 free_declspec(x->specifiers);
115 free_declarator(x->declarators);
121 void cdecl_free(struct cdecl *decl)
127 %destructor { free($$); } <strval>
128 %destructor { free_declspec($$); } <declspec>
129 %destructor { free_declarator($$); } <declarator>
130 %destructor { free_decl($$); } <decl>
134 %token <strval> T_IDENT "identifier"
135 %token <uintval> T_UINT "integer constant"
137 %token T_SEMICOLON ";"
138 %token T_ASTERISK "*"
141 %token T_LBRACKET "["
142 %token T_RBRACKET "]"
145 %token T_TYPEDEF "typedef"
146 %token T_EXTERN "extern"
147 %token T_STATIC "static"
149 %token T_REGISTER "register"
151 %token T_INLINE "inline"
153 %token T_RESTRICT "restrict"
154 %token T_VOLATILE "volatile"
155 %token T_CONST "const"
159 %token T_SHORT "short"
162 %token T_FLOAT "float"
163 %token T_DOUBLE "double"
164 %token T_SIGNED "signed"
165 %token T_UNSIGNED "unsigned"
166 %token T_BOOL "_Bool"
167 %token T_COMPLEX "_Complex"
169 %token T_STRUCT "struct"
170 %token T_UNION "union"
173 %type <strval> vla_ident
174 %type <uintval> declspec_simple typespec_simple qualifier_simple
175 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
176 %type <declspec> qualifier qualifiers
177 %type <declspec> declspecs declspecs_noid
178 %type <declarator> direct_declarator declarator pointer array
179 %type <decl> declaration declarators declarator_wrap
187 declaration: declspecs declarators T_SEMICOLON {
190 for (struct cdecl *i = $$; i; i = i->next)
194 declspecs: declspec_notype declspecs {
197 } | typespec declspecs_noid {
202 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
207 qualifiers: { $$ = NULL; } | qualifiers qualifier {
212 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
217 declarator_wrap: declarator {
218 ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
221 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO; }
222 | T_TYPEDEF { $$ = CDECL_STOR_TYPEDEF; }
223 | T_EXTERN { $$ = CDECL_STOR_EXTERN; }
224 | T_STATIC { $$ = CDECL_STOR_STATIC; }
225 | T_REGISTER { $$ = CDECL_STOR_REGISTER; }
226 | T_INLINE { $$ = CDECL_FUNC_INLINE; }
228 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID; }
229 | T_CHAR { $$ = CDECL_TYPE_CHAR; }
230 | T_SHORT { $$ = CDECL_TYPE_SHORT; }
231 | T_INT { $$ = CDECL_TYPE_INT; }
232 | T_LONG { $$ = CDECL_TYPE_LONG; }
233 | T_FLOAT { $$ = CDECL_TYPE_FLOAT; }
234 | T_DOUBLE { $$ = CDECL_TYPE_DOUBLE; }
235 | T_SIGNED { $$ = CDECL_TYPE_SIGNED; }
236 | T_UNSIGNED { $$ = CDECL_TYPE_UNSIGNED; }
237 | T_BOOL { $$ = CDECL_TYPE_BOOL; }
238 | T_COMPLEX { $$ = CDECL_TYPE_COMPLEX; }
240 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST; }
241 | T_RESTRICT { $$ = CDECL_QUAL_RESTRICT; }
242 | T_VOLATILE { $$ = CDECL_QUAL_VOLATILE; }
244 declspec_notype: qualifier | declspec_simple {
245 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
248 typespec_noid: typespec_simple {
249 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
252 qualifier: qualifier_simple {
253 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
256 typespec: typespec_noid | T_STRUCT T_IDENT {
257 ALLOC_STRUCT($$, struct cdecl_declspec,
258 .type = CDECL_TYPE_STRUCT,
260 } | T_UNION T_IDENT {
261 ALLOC_STRUCT($$, struct cdecl_declspec,
262 .type = CDECL_TYPE_UNION,
265 ALLOC_STRUCT($$, struct cdecl_declspec,
266 .type = CDECL_TYPE_ENUM,
269 ALLOC_STRUCT($$, struct cdecl_declspec,
270 .type = CDECL_TYPE_IDENT,
274 declspec_noid: declspec_notype | typespec_noid
276 vla_ident: T_IDENT | T_ASTERISK {
277 ALLOC($$, sizeof "");
281 array: T_LBRACKET T_UINT T_RBRACKET {
283 FAIL("array length must be positive");
285 ALLOC_STRUCT($$, struct cdecl_declarator,
286 .type = CDECL_DECL_ARRAY,
287 .u.array.length = $2);
288 } | T_LBRACKET vla_ident T_RBRACKET {
289 ALLOC_STRUCT($$, struct cdecl_declarator,
290 .type = CDECL_DECL_ARRAY,
292 } | T_LBRACKET T_RBRACKET {
293 ALLOC_STRUCT($$, struct cdecl_declarator,
294 .type = CDECL_DECL_ARRAY);
297 pointer: T_ASTERISK qualifiers direct_declarator {
298 ALLOC_STRUCT($$, struct cdecl_declarator,
299 .type = CDECL_DECL_POINTER,
300 .u.pointer.qualifiers = $2,
302 } | T_ASTERISK qualifiers pointer {
303 ALLOC_STRUCT($$, struct cdecl_declarator,
304 .type = CDECL_DECL_POINTER,
305 .u.pointer.qualifiers = $2,
309 declarator: direct_declarator | pointer
312 ALLOC_STRUCT($$, struct cdecl_declarator,
313 .type = CDECL_DECL_NULL);
315 ALLOC_STRUCT($$, struct cdecl_declarator,
316 .type = CDECL_DECL_IDENT,
318 } | direct_declarator array {
321 } | T_LPAREN declarator T_RPAREN {
326 void yyerror(YYLTYPE *loc, struct cdecl **out, const char *err)
328 if (strstr(err, "T_LEX_ERROR"))
331 fprintf(stderr, "%s\n", err);