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;
85 static void free_decl(struct cdecl *decl)
87 free_declspec(decl->specifiers);
88 free_declarator(decl->declarators);
92 void cdecl_free(struct cdecl *decl)
99 %destructor { free($$); } <strval>
100 %destructor { free_declspec($$); } <declspec>
101 %destructor { free_declarator($$); } <declarator>
102 %destructor { free_decl($$); } <decl>
106 %token <strval> T_IDENT "identifier"
107 %token T_SEMICOLON ";"
108 %token T_ASTERISK "*"
111 %token T_LBRACKET "["
112 %token T_RBRACKET "]"
115 %token T_TYPEDEF "typedef"
116 %token T_EXTERN "extern"
117 %token T_STATIC "static"
119 %token T_REGISTER "register"
121 %token T_INLINE "inline"
123 %token T_RESTRICT "restrict"
124 %token T_VOLATILE "volatile"
125 %token T_CONST "const"
129 %token T_SHORT "short"
132 %token T_FLOAT "float"
133 %token T_DOUBLE "double"
134 %token T_SIGNED "signed"
135 %token T_UNSIGNED "unsigned"
136 %token T_BOOL "_Bool"
137 %token T_COMPLEX "_Complex"
139 %token T_STRUCT "struct"
140 %token T_UNION "union"
143 %type <uintval> declspec_simple
144 %type <declspec> declspec declspecs
145 %type <declarator> declarator declarators
146 %type <decl> declaration
154 declaration: declspecs declarators T_SEMICOLON {
155 ALLOC_STRUCT($$, struct cdecl,
160 declspecs: { $$ = NULL; } | declspecs declspec {
165 declarators: declarator | declarator T_COMMA declarators {
170 declspec_simple: T_VOID { $$ = CDECL_TYPE_VOID; }
171 | T_CHAR { $$ = CDECL_TYPE_CHAR; }
172 | T_SHORT { $$ = CDECL_TYPE_SHORT; }
173 | T_INT { $$ = CDECL_TYPE_INT; }
174 | T_LONG { $$ = CDECL_TYPE_LONG; }
175 | T_FLOAT { $$ = CDECL_TYPE_FLOAT; }
176 | T_DOUBLE { $$ = CDECL_TYPE_DOUBLE; }
177 | T_SIGNED { $$ = CDECL_TYPE_SIGNED; }
178 | T_UNSIGNED { $$ = CDECL_TYPE_UNSIGNED; }
179 | T_BOOL { $$ = CDECL_TYPE_BOOL; }
180 | T_COMPLEX { $$ = CDECL_TYPE_COMPLEX; }
181 | T_TYPEDEF { $$ = CDECL_STOR_TYPEDEF; }
182 | T_EXTERN { $$ = CDECL_STOR_EXTERN; }
183 | T_STATIC { $$ = CDECL_STOR_STATIC; }
184 | T_AUTO { $$ = CDECL_STOR_AUTO; }
185 | T_REGISTER { $$ = CDECL_STOR_REGISTER; }
186 | T_RESTRICT { $$ = CDECL_QUAL_RESTRICT; }
187 | T_VOLATILE { $$ = CDECL_QUAL_VOLATILE; }
188 | T_CONST { $$ = CDECL_QUAL_CONST; }
189 | T_INLINE { $$ = CDECL_FUNC_INLINE; }
192 declspec: declspec_simple {
193 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
194 } | T_STRUCT T_IDENT {
195 ALLOC_STRUCT($$, struct cdecl_declspec,
196 .type = CDECL_TYPE_STRUCT,
198 } | T_UNION T_IDENT {
199 ALLOC_STRUCT($$, struct cdecl_declspec,
200 .type = CDECL_TYPE_UNION,
203 ALLOC_STRUCT($$, struct cdecl_declspec,
204 .type = CDECL_TYPE_ENUM,
207 ALLOC_STRUCT($$, struct cdecl_declspec,
208 .type = CDECL_TYPE_IDENT,
212 declarator: T_IDENT {
213 ALLOC_STRUCT($$, struct cdecl_declarator,
214 .type = CDECL_DECL_IDENT,
216 } | T_LPAREN declarator T_RPAREN {
221 void yyerror(YYLTYPE *loc, struct cdecl **out, const char *err)
223 if (strstr(err, "T_LEX_ERROR"))
226 fprintf(stderr, "%s\n", err);