]> git.draconx.ca Git - cdecl99.git/blob - src/parse.y
Use the reentrant scanner API.
[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 *, struct cdecl **, void *, 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
181 %token T_STRUCT   "struct"
182 %token T_UNION    "union"
183 %token T_ENUM     "enum"
184
185 %type <strval>     vla_ident
186 %type <boolval>    varargs
187 %type <uintval>    declspec_simple typespec_simple qualifier_simple
188 %type <declspec>   declspec_notype declspec_noid typespec_noid typespec
189 %type <declspec>   qualifier qualifiers
190 %type <declspec>   declspecs declspecs_noid
191 %type <declarator> direct_declarator declarator pointer array parens postfix
192 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
193 %type <decl>       declaration declarators declarator_wrap
194 %type <decl>       parameter parameters
195
196 %%
197
198 input: declaration {
199         *out = $1;
200 };
201
202 declaration: declspecs declarators T_SEMICOLON {
203         $$ = $2;
204
205         for (struct cdecl *i = $$; i; i = i->next)
206                 i->specifiers = $1;
207 };
208
209 declspecs: declspec_notype declspecs {
210         $$ = $1;
211         $$->next = $2;
212 } | typespec declspecs_noid {
213         $$ = $1;
214         $$->next = $2;
215 }
216
217 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
218         $$ = $1;
219         $$->next = $2;
220 }
221
222 qualifiers: { $$ = NULL; } | qualifiers qualifier {
223         $$ = $2;
224         $$->next = $1;
225 }
226
227 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
228         $$ = $1;
229         $$->next = $3;
230 }
231
232 declarator_wrap: declarator {
233         ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
234 }
235
236 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO;     }
237         | T_TYPEDEF     { $$ = CDECL_STOR_TYPEDEF;  }
238         | T_EXTERN      { $$ = CDECL_STOR_EXTERN;   }
239         | T_STATIC      { $$ = CDECL_STOR_STATIC;   }
240         | T_REGISTER    { $$ = CDECL_STOR_REGISTER; }
241         | T_INLINE      { $$ = CDECL_FUNC_INLINE;   }
242
243 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID;     }
244         | T_CHAR        { $$ = CDECL_TYPE_CHAR;     }
245         | T_SHORT       { $$ = CDECL_TYPE_SHORT;    }
246         | T_INT         { $$ = CDECL_TYPE_INT;      }
247         | T_LONG        { $$ = CDECL_TYPE_LONG;     }
248         | T_FLOAT       { $$ = CDECL_TYPE_FLOAT;    }
249         | T_DOUBLE      { $$ = CDECL_TYPE_DOUBLE;   }
250         | T_SIGNED      { $$ = CDECL_TYPE_SIGNED;   }
251         | T_UNSIGNED    { $$ = CDECL_TYPE_UNSIGNED; }
252         | T_BOOL        { $$ = CDECL_TYPE_BOOL;     }
253         | T_COMPLEX     { $$ = CDECL_TYPE_COMPLEX;  }
254
255 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST;    }
256         | T_RESTRICT      { $$ = CDECL_QUAL_RESTRICT; }
257         | T_VOLATILE      { $$ = CDECL_QUAL_VOLATILE; }
258
259 declspec_notype: qualifier | declspec_simple {
260         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
261 }
262
263 typespec_noid: typespec_simple {
264         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
265 }
266
267 qualifier: qualifier_simple {
268         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
269 }
270
271 typespec: typespec_noid | T_STRUCT T_IDENT {
272         ALLOC_STRUCT($$, struct cdecl_declspec,
273                 .type = CDECL_TYPE_STRUCT,
274                 .ident = $2);
275 } | T_UNION T_IDENT {
276         ALLOC_STRUCT($$, struct cdecl_declspec,
277                 .type = CDECL_TYPE_UNION,
278                 .ident = $2);
279 } | T_ENUM T_IDENT {
280         ALLOC_STRUCT($$, struct cdecl_declspec,
281                 .type = CDECL_TYPE_ENUM,
282                 .ident = $2);
283 } | T_IDENT {
284         ALLOC_STRUCT($$, struct cdecl_declspec,
285                 .type = CDECL_TYPE_IDENT,
286                 .ident = $1);
287 }
288
289 declspec_noid: declspec_notype | typespec_noid
290
291 vla_ident: T_IDENT | T_ASTERISK {
292         ALLOC($$, sizeof "");
293         strcpy($$, "");
294 }
295
296 array: T_LBRACKET T_UINT T_RBRACKET {
297         if ($2 == 0)
298                 FAIL("array length must be positive");
299
300         ALLOC_STRUCT($$, struct cdecl_declarator,
301                 .type = CDECL_DECL_ARRAY,
302                 .u.array.length = $2);
303 } | T_LBRACKET vla_ident T_RBRACKET {
304         ALLOC_STRUCT($$, struct cdecl_declarator,
305                 .type = CDECL_DECL_ARRAY,
306                 .u.array.vla = $2);
307 } | T_LBRACKET T_RBRACKET {
308         ALLOC_STRUCT($$, struct cdecl_declarator,
309                 .type = CDECL_DECL_ARRAY);
310 }
311
312 parameter: declspecs declarator {
313         ALLOC_STRUCT($$, struct cdecl,
314                 .specifiers = $1,
315                 .declarators = $2);
316 }
317
318 parameters: parameter | parameters T_COMMA parameter {
319         $$ = $3;
320         $$->next = $1;
321 }
322
323 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
324
325 parameter_type_list: parameters varargs {
326         struct cdecl *p, *c, *n;
327
328         /* Parameters were accumulated in reverse order. */
329         for (p = NULL, c = $1; c; p = c, c = n) {
330                 n = c->next;
331                 c->next = p;
332         }
333
334         ALLOC_STRUCT($$, struct cdecl_declarator,
335                 .type = CDECL_DECL_FUNCTION,
336                 .u.function.parameters = p,
337                 .u.function.variadic = $2);
338 }
339
340 parens: T_LPAREN parameter_type_list T_RPAREN {
341         $$ = $2;
342 } | T_LPAREN declarator_ish T_RPAREN {
343         ALLOC_STRUCT($$, struct cdecl_declarator,
344                 .type = CDECL_DECL_FUNCTION);
345         ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
346                 .declarators = $2);
347 }
348
349 pointer: T_ASTERISK qualifiers direct_declarator {
350         ALLOC_STRUCT($$, struct cdecl_declarator,
351                 .type = CDECL_DECL_POINTER,
352                 .u.pointer.qualifiers = $2,
353                 .child = $3);
354 } | T_ASTERISK qualifiers pointer {
355         ALLOC_STRUCT($$, struct cdecl_declarator,
356                 .type = CDECL_DECL_POINTER,
357                 .u.pointer.qualifiers = $2,
358                 .child = $3);
359 }
360
361 declarator: direct_declarator | pointer
362 declarator_ish: direct_declarator_ish | pointer
363 postfix: array | parens
364
365 direct_declarator_ish: {
366         ALLOC_STRUCT($$, struct cdecl_declarator,
367                 .type = CDECL_DECL_NULL);
368 } | direct_declarator_ish postfix {
369         $$ = $2;
370         $$->child = $1;
371 }
372
373 direct_declarator: {
374         ALLOC_STRUCT($$, struct cdecl_declarator,
375                 .type = CDECL_DECL_NULL);
376 } | T_IDENT {
377         ALLOC_STRUCT($$, struct cdecl_declarator,
378                 .type = CDECL_DECL_IDENT,
379                 .u.ident = $1);
380 } | direct_declarator postfix {
381         $$ = $2;
382         $$->child = $1;
383 }
384
385 %%
386 void
387 yyerror(YYLTYPE *loc, struct cdecl **out, yyscan_t scanner, const char *err)
388 {
389         if (strstr(err, "T_LEX_ERROR"))
390                 return;
391
392         fprintf(stderr, "%s\n", err);
393 }