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 void cdecl_free(struct cdecl *decl)
87 free_declspec(decl->specifiers);
88 free_declarator(decl->declarators);
93 %destructor { free($$); } <strval>
94 %destructor { free_declspec($$); } <declspec>
95 %destructor { free_declarator($$); } <declarator>
96 %destructor { cdecl_free($$); } <decl>
100 %token <strval> T_IDENT "identifier"
101 %token T_SEMICOLON ";"
102 %token T_ASTERISK "*"
105 %token T_LBRACKET "["
106 %token T_RBRACKET "]"
109 %token T_TYPEDEF "typedef"
110 %token T_EXTERN "extern"
111 %token T_STATIC "static"
113 %token T_REGISTER "register"
115 %token T_INLINE "inline"
117 %token T_RESTRICT "restrict"
118 %token T_VOLATILE "volatile"
119 %token T_CONST "const"
123 %token T_SHORT "short"
126 %token T_FLOAT "float"
127 %token T_DOUBLE "double"
128 %token T_SIGNED "signed"
129 %token T_UNSIGNED "unsigned"
130 %token T_BOOL "_Bool"
131 %token T_COMPLEX "_Complex"
133 %token T_STRUCT "struct"
134 %token T_UNION "union"
137 %type <uintval> declspec_simple
138 %type <declspec> declspec declspecs
139 %type <declarator> declarator declarators
140 %type <decl> declaration
148 declaration: declspecs declarators T_SEMICOLON {
149 ALLOC_STRUCT($$, struct cdecl,
154 declspecs: { $$ = NULL; } | declspecs declspec {
159 declarators: declarator | declarator T_COMMA declarators {
164 declspec_simple: T_VOID { $$ = CDECL_TYPE_VOID; }
165 | T_CHAR { $$ = CDECL_TYPE_CHAR; }
166 | T_SHORT { $$ = CDECL_TYPE_SHORT; }
167 | T_INT { $$ = CDECL_TYPE_INT; }
168 | T_LONG { $$ = CDECL_TYPE_LONG; }
169 | T_FLOAT { $$ = CDECL_TYPE_FLOAT; }
170 | T_DOUBLE { $$ = CDECL_TYPE_DOUBLE; }
171 | T_SIGNED { $$ = CDECL_TYPE_SIGNED; }
172 | T_UNSIGNED { $$ = CDECL_TYPE_UNSIGNED; }
173 | T_BOOL { $$ = CDECL_TYPE_BOOL; }
174 | T_COMPLEX { $$ = CDECL_TYPE_COMPLEX; }
175 | T_TYPEDEF { $$ = CDECL_STOR_TYPEDEF; }
176 | T_EXTERN { $$ = CDECL_STOR_EXTERN; }
177 | T_STATIC { $$ = CDECL_STOR_STATIC; }
178 | T_AUTO { $$ = CDECL_STOR_AUTO; }
179 | T_REGISTER { $$ = CDECL_STOR_REGISTER; }
180 | T_RESTRICT { $$ = CDECL_QUAL_RESTRICT; }
181 | T_VOLATILE { $$ = CDECL_QUAL_VOLATILE; }
182 | T_CONST { $$ = CDECL_QUAL_CONST; }
183 | T_INLINE { $$ = CDECL_FUNC_INLINE; }
186 declspec: declspec_simple {
187 ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
188 } | T_STRUCT T_IDENT {
189 ALLOC_STRUCT($$, struct cdecl_declspec,
190 .type = CDECL_TYPE_STRUCT,
192 } | T_UNION T_IDENT {
193 ALLOC_STRUCT($$, struct cdecl_declspec,
194 .type = CDECL_TYPE_UNION,
197 ALLOC_STRUCT($$, struct cdecl_declspec,
198 .type = CDECL_TYPE_ENUM,
201 ALLOC_STRUCT($$, struct cdecl_declspec,
202 .type = CDECL_TYPE_IDENT,
206 declarator: T_IDENT {
207 ALLOC_STRUCT($$, struct cdecl_declarator,
208 .type = CDECL_DECL_IDENT,
210 } | T_LPAREN declarator T_RPAREN {
215 void yyerror(YYLTYPE *loc, struct cdecl **out, const char *err)
217 if (strstr(err, "T_LEX_ERROR"))
220 fprintf(stderr, "%s\n", err);