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;
84 case CDECL_DECL_IDENT:
87 case CDECL_DECL_POINTER:
88 free_declspec(x->u.pointer.qualifiers);
90 case CDECL_DECL_ARRAY:
97 free_declarator(x->child);
103 static void free_decl(struct cdecl *decl)
105 free_declspec(decl->specifiers);
106 free_declarator(decl->declarators);
110 void cdecl_free(struct cdecl *decl)
117 %destructor { free($$); } <strval>
118 %destructor { free_declspec($$); } <declspec>
119 %destructor { free_declarator($$); } <declarator>
120 %destructor { free_decl($$); } <decl>
124 %token <strval> T_IDENT "identifier"
125 %token <uintval> T_UINT "integer constant"
127 %token T_SEMICOLON ";"
128 %token T_ASTERISK "*"
131 %token T_LBRACKET "["
132 %token T_RBRACKET "]"
135 %token T_TYPEDEF "typedef"
136 %token T_EXTERN "extern"
137 %token T_STATIC "static"
139 %token T_REGISTER "register"
141 %token T_INLINE "inline"
143 %token T_RESTRICT "restrict"
144 %token T_VOLATILE "volatile"
145 %token T_CONST "const"
149 %token T_SHORT "short"
152 %token T_FLOAT "float"
153 %token T_DOUBLE "double"
154 %token T_SIGNED "signed"
155 %token T_UNSIGNED "unsigned"
156 %token T_BOOL "_Bool"
157 %token T_COMPLEX "_Complex"
159 %token T_STRUCT "struct"
160 %token T_UNION "union"
163 %type <strval> vla_ident
164 %type <uintval> declspec_simple typespec_simple qualifier_simple
165 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
166 %type <declspec> qualifier qualifiers
167 %type <declspec> declspecs declspecs_noid
168 %type <declarator> direct_declarator declarator declarators pointer array
169 %type <decl> declaration
177 declaration: declspecs declarators T_SEMICOLON {
178 ALLOC_STRUCT($$, struct cdecl,
183 declspecs: declspec_notype declspecs {
186 } | typespec declspecs_noid {
191 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
196 qualifiers: { $$ = NULL; } | qualifiers qualifier {
201 declarators: declarator | declarator T_COMMA declarators {
206 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO; }
207 | T_TYPEDEF { $$ = CDECL_STOR_TYPEDEF; }
208 | T_EXTERN { $$ = CDECL_STOR_EXTERN; }
209 | T_STATIC { $$ = CDECL_STOR_STATIC; }
210 | T_REGISTER { $$ = CDECL_STOR_REGISTER; }
211 | T_INLINE { $$ = CDECL_FUNC_INLINE; }
213 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID; }
214 | T_CHAR { $$ = CDECL_TYPE_CHAR; }
215 | T_SHORT { $$ = CDECL_TYPE_SHORT; }
216 | T_INT { $$ = CDECL_TYPE_INT; }
217 | T_LONG { $$ = CDECL_TYPE_LONG; }
218 | T_FLOAT { $$ = CDECL_TYPE_FLOAT; }
219 | T_DOUBLE { $$ = CDECL_TYPE_DOUBLE; }
220 | T_SIGNED { $$ = CDECL_TYPE_SIGNED; }
221 | T_UNSIGNED { $$ = CDECL_TYPE_UNSIGNED; }
222 | T_BOOL { $$ = CDECL_TYPE_BOOL; }
223 | T_COMPLEX { $$ = CDECL_TYPE_COMPLEX; }
225 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST; }
226 | T_RESTRICT { $$ = CDECL_QUAL_RESTRICT; }
227 | T_VOLATILE { $$ = CDECL_QUAL_VOLATILE; }
229 declspec_notype: qualifier | declspec_simple {
230 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
233 typespec_noid: typespec_simple {
234 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
237 qualifier: qualifier_simple {
238 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
241 typespec: typespec_noid | T_STRUCT T_IDENT {
242 ALLOC_STRUCT($$, struct cdecl_declspec,
243 .type = CDECL_TYPE_STRUCT,
245 } | T_UNION T_IDENT {
246 ALLOC_STRUCT($$, struct cdecl_declspec,
247 .type = CDECL_TYPE_UNION,
250 ALLOC_STRUCT($$, struct cdecl_declspec,
251 .type = CDECL_TYPE_ENUM,
254 ALLOC_STRUCT($$, struct cdecl_declspec,
255 .type = CDECL_TYPE_IDENT,
259 declspec_noid: declspec_notype | typespec_noid
261 vla_ident: T_IDENT | T_ASTERISK {
262 ALLOC($$, sizeof "");
266 array: T_LBRACKET T_UINT T_RBRACKET {
268 FAIL("array length must be positive");
270 ALLOC_STRUCT($$, struct cdecl_declarator,
271 .type = CDECL_DECL_ARRAY,
272 .u.array.length = $2);
273 } | T_LBRACKET vla_ident T_RBRACKET {
274 ALLOC_STRUCT($$, struct cdecl_declarator,
275 .type = CDECL_DECL_ARRAY,
277 } | T_LBRACKET T_RBRACKET {
278 ALLOC_STRUCT($$, struct cdecl_declarator,
279 .type = CDECL_DECL_ARRAY);
282 pointer: T_ASTERISK qualifiers direct_declarator {
283 ALLOC_STRUCT($$, struct cdecl_declarator,
284 .type = CDECL_DECL_POINTER,
285 .u.pointer.qualifiers = $2,
287 } | T_ASTERISK qualifiers pointer {
288 ALLOC_STRUCT($$, struct cdecl_declarator,
289 .type = CDECL_DECL_POINTER,
290 .u.pointer.qualifiers = $2,
294 declarator: direct_declarator | pointer
297 ALLOC_STRUCT($$, struct cdecl_declarator,
298 .type = CDECL_DECL_NULL);
300 ALLOC_STRUCT($$, struct cdecl_declarator,
301 .type = CDECL_DECL_IDENT,
303 } | direct_declarator array {
306 } | T_LPAREN declarator T_RPAREN {
311 void yyerror(YYLTYPE *loc, struct cdecl **out, const char *err)
313 if (strstr(err, "T_LEX_ERROR"))
316 fprintf(stderr, "%s\n", err);