]> git.draconx.ca Git - cdecl99.git/blob - src/parse.y
Fix yyerror's signature.
[cdecl99.git] / src / parse.y
1 %code top {
2 /*
3  *  Parser for C declarations.
4  *  Copyright © 2011 Nick Bowler
5  *
6  *  This program is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 }
20
21 %parse-param {yyscan_t scanner}
22 %parse-param {struct cdecl **out}
23 %lex-param {yyscan_t scanner}
24 %define api.pure
25 %error-verbose
26 %locations
27
28 %{
29 #include <assert.h>
30 #include <stdbool.h>
31
32 #include "scan.h"
33 #include "cdecl.h"
34
35 #define FAIL(msg) do { \
36         yyerror(&yylloc, NULL, NULL, msg); \
37         YYERROR; \
38 } while (0)
39
40 #define ALLOC(ptr, size) do { \
41         (ptr) = malloc(size); \
42         if (!(ptr)) \
43                 FAIL("failed to allocate memory"); \
44 } while (0)
45
46 #define ALLOC_STRUCT(ptr, type, ...) do { \
47         ALLOC(ptr, sizeof (type)); \
48         *(ptr) = (type) { __VA_ARGS__ }; \
49 } while (0)
50 %}
51
52 %code requires {
53 #include <inttypes.h>
54 }
55
56 %code provides {
57 void yyerror(YYLTYPE *, void *, struct cdecl **, const char *);
58 int yyparse(void *scanner, struct cdecl **out);
59 }
60
61 %union {
62         uintmax_t uintval;
63         _Bool boolval;
64         char *strval;
65         struct cdecl_declspec *declspec;
66         struct cdecl_declarator *declarator;
67         struct cdecl *decl;
68 }
69
70 %{
71 static void free_decl(struct cdecl *);
72
73 static void free_declspec(struct cdecl_declspec *x)
74 {
75         struct cdecl_declspec *p;
76         while (x) {
77                 p = x->next;
78                 free(x->ident);
79                 free(x);
80                 x = p;
81         }
82 }
83
84 static void free_declarator(struct cdecl_declarator *x)
85 {
86         struct cdecl_declarator *p;
87
88         while (x) {
89                 p = x->child;
90
91                 switch (x->type) {
92                 case CDECL_DECL_NULL:
93                         break;
94                 case CDECL_DECL_IDENT:
95                         free(x->u.ident);
96                         break;
97                 case CDECL_DECL_POINTER:
98                         free_declspec(x->u.pointer.qualifiers);
99                         break;
100                 case CDECL_DECL_ARRAY:
101                         free(x->u.array.vla);
102                         break;
103                 case CDECL_DECL_FUNCTION:
104                         free_decl(x->u.function.parameters);
105                         break;
106                 default:
107                         assert(0);
108                 }
109
110                 free(x);
111                 x = p;
112         }
113 }
114
115 static void free_decl(struct cdecl *x)
116 {
117         struct cdecl *p;
118
119         while (x) {
120                 p = x->next;
121
122                 /* The specifiers may be shared by an entire chain. */
123                 if (!p || p->specifiers != x->specifiers)
124                         free_declspec(x->specifiers);
125
126                 free_declarator(x->declarators);
127                 free(x);
128                 x = p;
129         }
130 }
131
132 void cdecl_free(struct cdecl *decl)
133 {
134         free_decl(decl);
135 }
136 %}
137
138 %destructor { free($$); }            <strval>
139 %destructor { free_declspec($$); }   <declspec>
140 %destructor { free_declarator($$); } <declarator>
141 %destructor { free_decl($$); }       <decl>
142
143 %token T_LEX_ERROR
144
145 %token <strval> T_IDENT "identifier"
146 %token <uintval> T_UINT "integer constant"
147
148 %token T_SEMICOLON ";"
149 %token T_ASTERISK  "*"
150 %token T_LPAREN    "("
151 %token T_RPAREN    ")"
152 %token T_LBRACKET  "["
153 %token T_RBRACKET  "]"
154 %token T_COMMA     ","
155 %token T_ELLIPSIS  "."
156
157 %token T_TYPEDEF   "typedef"
158 %token T_EXTERN    "extern"
159 %token T_STATIC    "static"
160 %token T_AUTO      "auto"
161 %token T_REGISTER  "register"
162
163 %token T_INLINE    "inline"
164
165 %token T_RESTRICT  "restrict"
166 %token T_VOLATILE  "volatile"
167 %token T_CONST     "const"
168
169 %token T_VOID      "void"
170 %token T_CHAR      "char"
171 %token T_SHORT     "short"
172 %token T_INT       "int"
173 %token T_LONG      "long"
174 %token T_FLOAT     "float"
175 %token T_DOUBLE    "double"
176 %token T_SIGNED    "signed"
177 %token T_UNSIGNED  "unsigned"
178 %token T_BOOL      "_Bool"
179 %token T_COMPLEX   "_Complex"
180 %token T_IMAGINARY "_Imaginary"
181
182 %token T_STRUCT    "struct"
183 %token T_UNION     "union"
184 %token T_ENUM      "enum"
185
186 %type <strval>     vla_ident
187 %type <boolval>    varargs
188 %type <uintval>    declspec_simple typespec_simple qualifier_simple
189 %type <declspec>   declspec_notype declspec_noid typespec_noid typespec
190 %type <declspec>   qualifier qualifiers
191 %type <declspec>   declspecs declspecs_noid
192 %type <declarator> direct_declarator declarator pointer array parens postfix
193 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
194 %type <decl>       declaration declarators declarator_wrap
195 %type <decl>       parameter parameters
196
197 %%
198
199 input: declaration {
200         *out = $1;
201 };
202
203 declaration: declspecs declarators T_SEMICOLON {
204         $$ = $2;
205
206         for (struct cdecl *i = $$; i; i = i->next)
207                 i->specifiers = $1;
208 };
209
210 declspecs: declspec_notype declspecs {
211         $$ = $1;
212         $$->next = $2;
213 } | typespec declspecs_noid {
214         $$ = $1;
215         $$->next = $2;
216 }
217
218 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
219         $$ = $1;
220         $$->next = $2;
221 }
222
223 qualifiers: { $$ = NULL; } | qualifiers qualifier {
224         $$ = $2;
225         $$->next = $1;
226 }
227
228 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
229         $$ = $1;
230         $$->next = $3;
231 }
232
233 declarator_wrap: declarator {
234         ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
235 }
236
237 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO;     }
238         | T_TYPEDEF     { $$ = CDECL_STOR_TYPEDEF;  }
239         | T_EXTERN      { $$ = CDECL_STOR_EXTERN;   }
240         | T_STATIC      { $$ = CDECL_STOR_STATIC;   }
241         | T_REGISTER    { $$ = CDECL_STOR_REGISTER; }
242         | T_INLINE      { $$ = CDECL_FUNC_INLINE;   }
243
244 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID;      }
245         | T_CHAR        { $$ = CDECL_TYPE_CHAR;      }
246         | T_SHORT       { $$ = CDECL_TYPE_SHORT;     }
247         | T_INT         { $$ = CDECL_TYPE_INT;       }
248         | T_LONG        { $$ = CDECL_TYPE_LONG;      }
249         | T_FLOAT       { $$ = CDECL_TYPE_FLOAT;     }
250         | T_DOUBLE      { $$ = CDECL_TYPE_DOUBLE;    }
251         | T_SIGNED      { $$ = CDECL_TYPE_SIGNED;    }
252         | T_UNSIGNED    { $$ = CDECL_TYPE_UNSIGNED;  }
253         | T_BOOL        { $$ = CDECL_TYPE_BOOL;      }
254         | T_COMPLEX     { $$ = CDECL_TYPE_COMPLEX;   }
255         | T_IMAGINARY   { $$ = CDECL_TYPE_IMAGINARY; }
256
257 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST;    }
258         | T_RESTRICT      { $$ = CDECL_QUAL_RESTRICT; }
259         | T_VOLATILE      { $$ = CDECL_QUAL_VOLATILE; }
260
261 declspec_notype: qualifier | declspec_simple {
262         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
263 }
264
265 typespec_noid: typespec_simple {
266         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
267 }
268
269 qualifier: qualifier_simple {
270         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
271 }
272
273 typespec: typespec_noid | T_STRUCT T_IDENT {
274         ALLOC_STRUCT($$, struct cdecl_declspec,
275                 .type = CDECL_TYPE_STRUCT,
276                 .ident = $2);
277 } | T_UNION T_IDENT {
278         ALLOC_STRUCT($$, struct cdecl_declspec,
279                 .type = CDECL_TYPE_UNION,
280                 .ident = $2);
281 } | T_ENUM T_IDENT {
282         ALLOC_STRUCT($$, struct cdecl_declspec,
283                 .type = CDECL_TYPE_ENUM,
284                 .ident = $2);
285 } | T_IDENT {
286         ALLOC_STRUCT($$, struct cdecl_declspec,
287                 .type = CDECL_TYPE_IDENT,
288                 .ident = $1);
289 }
290
291 declspec_noid: declspec_notype | typespec_noid
292
293 vla_ident: T_IDENT | T_ASTERISK {
294         ALLOC($$, sizeof "");
295         strcpy($$, "");
296 }
297
298 array: T_LBRACKET T_UINT T_RBRACKET {
299         if ($2 == 0)
300                 FAIL("array length must be positive");
301
302         ALLOC_STRUCT($$, struct cdecl_declarator,
303                 .type = CDECL_DECL_ARRAY,
304                 .u.array.length = $2);
305 } | T_LBRACKET vla_ident T_RBRACKET {
306         ALLOC_STRUCT($$, struct cdecl_declarator,
307                 .type = CDECL_DECL_ARRAY,
308                 .u.array.vla = $2);
309 } | T_LBRACKET T_RBRACKET {
310         ALLOC_STRUCT($$, struct cdecl_declarator,
311                 .type = CDECL_DECL_ARRAY);
312 }
313
314 parameter: declspecs declarator {
315         ALLOC_STRUCT($$, struct cdecl,
316                 .specifiers = $1,
317                 .declarators = $2);
318 }
319
320 parameters: parameter | parameters T_COMMA parameter {
321         $$ = $3;
322         $$->next = $1;
323 }
324
325 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
326
327 parameter_type_list: parameters varargs {
328         struct cdecl *p, *c, *n;
329
330         /* Parameters were accumulated in reverse order. */
331         for (p = NULL, c = $1; c; p = c, c = n) {
332                 n = c->next;
333                 c->next = p;
334         }
335
336         ALLOC_STRUCT($$, struct cdecl_declarator,
337                 .type = CDECL_DECL_FUNCTION,
338                 .u.function.parameters = p,
339                 .u.function.variadic = $2);
340 }
341
342 parens: T_LPAREN parameter_type_list T_RPAREN {
343         $$ = $2;
344 } | T_LPAREN declarator_ish T_RPAREN {
345         ALLOC_STRUCT($$, struct cdecl_declarator,
346                 .type = CDECL_DECL_FUNCTION);
347         ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
348                 .declarators = $2);
349 }
350
351 pointer: T_ASTERISK qualifiers direct_declarator {
352         ALLOC_STRUCT($$, struct cdecl_declarator,
353                 .type = CDECL_DECL_POINTER,
354                 .u.pointer.qualifiers = $2,
355                 .child = $3);
356 } | T_ASTERISK qualifiers pointer {
357         ALLOC_STRUCT($$, struct cdecl_declarator,
358                 .type = CDECL_DECL_POINTER,
359                 .u.pointer.qualifiers = $2,
360                 .child = $3);
361 }
362
363 declarator: direct_declarator | pointer
364 declarator_ish: direct_declarator_ish | pointer
365 postfix: array | parens
366
367 direct_declarator_ish: {
368         ALLOC_STRUCT($$, struct cdecl_declarator,
369                 .type = CDECL_DECL_NULL);
370 } | direct_declarator_ish postfix {
371         $$ = $2;
372         $$->child = $1;
373 }
374
375 direct_declarator: {
376         ALLOC_STRUCT($$, struct cdecl_declarator,
377                 .type = CDECL_DECL_NULL);
378 } | T_IDENT {
379         ALLOC_STRUCT($$, struct cdecl_declarator,
380                 .type = CDECL_DECL_IDENT,
381                 .u.ident = $1);
382 } | direct_declarator postfix {
383         $$ = $2;
384         $$->child = $1;
385 }
386
387 %%
388 void
389 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out,
390         const char *err)
391 {
392         if (strstr(err, "T_LEX_ERROR"))
393                 return;
394
395         fprintf(stderr, "%s\n", err);
396 }