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}
28 #define FAIL(msg) do { \
29 yyerror(&yylloc, NULL, msg); \
33 #define ALLOC(ptr, size) do { \
34 (ptr) = malloc(size); \
36 FAIL("failed to allocate memory"); \
39 #define ALLOC_STRUCT(ptr, type, ...) do { \
40 ALLOC(ptr, sizeof (type)); \
41 *(ptr) = (type) { __VA_ARGS__ }; \
50 void yyerror(YYLTYPE *, struct cdecl **, const char *);
51 int yyparse(struct cdecl **out);
57 struct cdecl_declspec *declspec;
58 struct cdecl_declarator *declarator;
63 static void free_declspec(struct cdecl_declspec *x)
65 struct cdecl_declspec *p;
74 static void free_declarator(struct cdecl_declarator *x)
76 struct cdecl_declarator *p;
80 case CDECL_DECL_IDENT:
83 case CDECL_DECL_POINTER:
84 free_declspec(x->u.pointer.qualifiers);
85 free_declarator(x->u.pointer.declarator);
95 static void free_decl(struct cdecl *decl)
97 free_declspec(decl->specifiers);
98 free_declarator(decl->declarators);
102 void cdecl_free(struct cdecl *decl)
109 %destructor { free($$); } <strval>
110 %destructor { free_declspec($$); } <declspec>
111 %destructor { free_declarator($$); } <declarator>
112 %destructor { free_decl($$); } <decl>
116 %token <strval> T_IDENT "identifier"
117 %token T_SEMICOLON ";"
118 %token T_ASTERISK "*"
121 %token T_LBRACKET "["
122 %token T_RBRACKET "]"
125 %token T_TYPEDEF "typedef"
126 %token T_EXTERN "extern"
127 %token T_STATIC "static"
129 %token T_REGISTER "register"
131 %token T_INLINE "inline"
133 %token T_RESTRICT "restrict"
134 %token T_VOLATILE "volatile"
135 %token T_CONST "const"
139 %token T_SHORT "short"
142 %token T_FLOAT "float"
143 %token T_DOUBLE "double"
144 %token T_SIGNED "signed"
145 %token T_UNSIGNED "unsigned"
146 %token T_BOOL "_Bool"
147 %token T_COMPLEX "_Complex"
149 %token T_STRUCT "struct"
150 %token T_UNION "union"
153 %type <uintval> declspec_simple typespec_simple qualifier_simple
154 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
155 %type <declspec> qualifier qualifiers pointer
156 %type <declspec> declspecs declspecs_noid
157 %type <declarator> declarator declarators
158 %type <decl> declaration
166 declaration: declspecs declarators T_SEMICOLON {
167 ALLOC_STRUCT($$, struct cdecl,
172 declspecs: declspec_notype declspecs {
175 } | typespec declspecs_noid {
180 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
185 qualifiers: { $$ = NULL; } | qualifiers qualifier {
190 declarators: declarator | declarator T_COMMA declarators {
195 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO; }
196 | T_TYPEDEF { $$ = CDECL_STOR_TYPEDEF; }
197 | T_EXTERN { $$ = CDECL_STOR_EXTERN; }
198 | T_STATIC { $$ = CDECL_STOR_STATIC; }
199 | T_REGISTER { $$ = CDECL_STOR_REGISTER; }
200 | T_INLINE { $$ = CDECL_FUNC_INLINE; }
202 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID; }
203 | T_CHAR { $$ = CDECL_TYPE_CHAR; }
204 | T_SHORT { $$ = CDECL_TYPE_SHORT; }
205 | T_INT { $$ = CDECL_TYPE_INT; }
206 | T_LONG { $$ = CDECL_TYPE_LONG; }
207 | T_FLOAT { $$ = CDECL_TYPE_FLOAT; }
208 | T_DOUBLE { $$ = CDECL_TYPE_DOUBLE; }
209 | T_SIGNED { $$ = CDECL_TYPE_SIGNED; }
210 | T_UNSIGNED { $$ = CDECL_TYPE_UNSIGNED; }
211 | T_BOOL { $$ = CDECL_TYPE_BOOL; }
212 | T_COMPLEX { $$ = CDECL_TYPE_COMPLEX; }
214 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST; }
215 | T_RESTRICT { $$ = CDECL_QUAL_RESTRICT; }
216 | T_VOLATILE { $$ = CDECL_QUAL_VOLATILE; }
218 declspec_notype: qualifier | declspec_simple {
219 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
222 typespec_noid: typespec_simple {
223 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
226 qualifier: qualifier_simple {
227 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
230 typespec: typespec_noid | T_STRUCT T_IDENT {
231 ALLOC_STRUCT($$, struct cdecl_declspec,
232 .type = CDECL_TYPE_STRUCT,
234 } | T_UNION T_IDENT {
235 ALLOC_STRUCT($$, struct cdecl_declspec,
236 .type = CDECL_TYPE_UNION,
239 ALLOC_STRUCT($$, struct cdecl_declspec,
240 .type = CDECL_TYPE_ENUM,
243 ALLOC_STRUCT($$, struct cdecl_declspec,
244 .type = CDECL_TYPE_IDENT,
248 declspec_noid: declspec_notype | typespec_noid
250 pointer: T_ASTERISK qualifiers { $$ = $2; }
252 declarator: T_IDENT {
253 ALLOC_STRUCT($$, struct cdecl_declarator,
254 .type = CDECL_DECL_IDENT,
256 } | pointer declarator {
257 ALLOC_STRUCT($$, struct cdecl_declarator,
258 .type = CDECL_DECL_POINTER,
259 .u.pointer.qualifiers = $1,
260 .u.pointer.declarator = $2);
261 } | T_LPAREN declarator T_RPAREN {
266 void yyerror(YYLTYPE *loc, struct cdecl **out, const char *err)
268 if (strstr(err, "T_LEX_ERROR"))
271 fprintf(stderr, "%s\n", err);