]> git.draconx.ca Git - cdecl99.git/blob - src/parse.y
Add cdecl__ prefix to flex/bison functions.
[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 %name-prefix "cdecl__yy"
22 %parse-param {yyscan_t scanner}
23 %parse-param {struct cdecl **out}
24 %lex-param {yyscan_t scanner}
25 %define api.pure
26 %error-verbose
27 %locations
28
29 %{
30 #include <assert.h>
31 #include <stdbool.h>
32
33 #include "scan.h"
34 #include "cdecl.h"
35
36 #define FAIL(msg) do { \
37         yyerror(&yylloc, NULL, NULL, msg); \
38         YYERROR; \
39 } while (0)
40
41 #define ALLOC(ptr, size) do { \
42         (ptr) = malloc(size); \
43         if (!(ptr)) \
44                 FAIL("failed to allocate memory"); \
45 } while (0)
46
47 #define ALLOC_STRUCT(ptr, type, ...) do { \
48         ALLOC(ptr, sizeof (type)); \
49         *(ptr) = (type) { __VA_ARGS__ }; \
50 } while (0)
51 %}
52
53 %code requires {
54 #include <inttypes.h>
55 }
56
57 %code provides {
58 void cdecl__yyerror(YYLTYPE *, void *, struct cdecl **, const char *);
59 int cdecl__yyparse(void *scanner, struct cdecl **out);
60 }
61
62 %union {
63         uintmax_t uintval;
64         _Bool boolval;
65         char *strval;
66         struct cdecl_declspec *declspec;
67         struct cdecl_declarator *declarator;
68         struct cdecl *decl;
69 }
70
71 %{
72 static void free_decl(struct cdecl *);
73
74 static void free_declspec(struct cdecl_declspec *x)
75 {
76         struct cdecl_declspec *p;
77         while (x) {
78                 p = x->next;
79                 free(x->ident);
80                 free(x);
81                 x = p;
82         }
83 }
84
85 static void free_declarator(struct cdecl_declarator *x)
86 {
87         struct cdecl_declarator *p;
88
89         while (x) {
90                 p = x->child;
91
92                 switch (x->type) {
93                 case CDECL_DECL_NULL:
94                         break;
95                 case CDECL_DECL_IDENT:
96                         free(x->u.ident);
97                         break;
98                 case CDECL_DECL_POINTER:
99                         free_declspec(x->u.pointer.qualifiers);
100                         break;
101                 case CDECL_DECL_ARRAY:
102                         free(x->u.array.vla);
103                         break;
104                 case CDECL_DECL_FUNCTION:
105                         free_decl(x->u.function.parameters);
106                         break;
107                 default:
108                         assert(0);
109                 }
110
111                 free(x);
112                 x = p;
113         }
114 }
115
116 static void free_decl(struct cdecl *x)
117 {
118         struct cdecl *p;
119
120         while (x) {
121                 p = x->next;
122
123                 /* The specifiers may be shared by an entire chain. */
124                 if (!p || p->specifiers != x->specifiers)
125                         free_declspec(x->specifiers);
126
127                 free_declarator(x->declarators);
128                 free(x);
129                 x = p;
130         }
131 }
132
133 void cdecl_free(struct cdecl *decl)
134 {
135         free_decl(decl);
136 }
137 %}
138
139 %destructor { free($$); }            <strval>
140 %destructor { free_declspec($$); }   <declspec>
141 %destructor { free_declarator($$); } <declarator>
142 %destructor { free_decl($$); }       <decl>
143
144 /* Magic tokens */
145 %token T_LEX_ERROR
146 %token T_ENGLISH
147
148 %token <strval> T_IDENT "identifier"
149 %token <uintval> T_UINT "integer constant"
150
151 %token T_SEMICOLON ";"
152 %token T_ASTERISK  "*"
153 %token T_LPAREN    "("
154 %token T_RPAREN    ")"
155 %token T_LBRACKET  "["
156 %token T_RBRACKET  "]"
157 %token T_COMMA     ","
158 %token T_ELLIPSIS  "..."
159
160 %token T_TYPEDEF   "typedef"
161 %token T_EXTERN    "extern"
162 %token T_STATIC    "static"
163 %token T_AUTO      "auto"
164 %token T_REGISTER  "register"
165
166 %token T_INLINE    "inline"
167
168 %token T_RESTRICT  "restrict"
169 %token T_VOLATILE  "volatile"
170 %token T_CONST     "const"
171
172 %token T_VOID      "void"
173 %token T_CHAR      "char"
174 %token T_SHORT     "short"
175 %token T_INT       "int"
176 %token T_LONG      "long"
177 %token T_FLOAT     "float"
178 %token T_DOUBLE    "double"
179 %token T_SIGNED    "signed"
180 %token T_UNSIGNED  "unsigned"
181 %token T_BOOL      "_Bool"
182 %token T_COMPLEX   "_Complex"
183 %token T_IMAGINARY "_Imaginary"
184
185 %token T_STRUCT    "struct"
186 %token T_UNION     "union"
187 %token T_ENUM      "enum"
188
189 /*
190  * English keywords.
191  */
192 %token T_TYPE      "type"
193 %token T_DECLARE   "declare"
194 %token T_POINTER   "pointer"
195 %token T_FUNCTION  "function"
196 %token T_RETURNING "returning"
197 %token T_ARRAY     "array"
198 %token T_TO        "to"
199 %token T_OF        "of"
200 %token T_AS        "as"
201 %token T_VLA       "variable-length"
202
203 %type <strval>     vla_ident
204 %type <boolval>    varargs
205 %type <uintval>    declspec_simple typespec_simple qualifier_simple
206 %type <declspec>   declspec_notype declspec_noid typespec_noid typespec
207 %type <declspec>   qualifier qualifiers
208 %type <declspec>   declspecs declspecs_noid
209 %type <declarator> direct_declarator declarator pointer array parens postfix
210 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
211 %type <decl>       declaration declarators declarator_wrap
212 %type <decl>       parameter parameters
213
214 %type <strval>     english_vla
215 %type <declspec>   storage_func_specs post_specs
216 %type <declspec>   type_qual_spec type_qual_specs typedef_name_qual
217 %type <declarator> english_declarator english_array english_function
218 %type <declarator> english_parameter_list null_decl
219 %type <decl>       english_parameter english_parameters
220 %type <decl>       english english_declaration
221
222 /*
223  * Harmless shift/reduce conflicts in english_parameter.  See comments below
224  * for more details.
225  */
226 %expect 2
227
228 %%
229
230 input: T_ENGLISH english {
231         *out = $2;
232 } | declaration {
233         *out = $1;
234 };
235
236 declaration: declspecs declarators T_SEMICOLON {
237         $$ = $2;
238
239         for (struct cdecl *i = $$; i; i = i->next)
240                 i->specifiers = $1;
241 };
242
243 declspecs: declspec_notype declspecs {
244         $$ = $1;
245         $$->next = $2;
246 } | typespec declspecs_noid {
247         $$ = $1;
248         $$->next = $2;
249 }
250
251 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
252         $$ = $1;
253         $$->next = $2;
254 }
255
256 qualifiers: { $$ = NULL; } | qualifiers qualifier {
257         $$ = $2;
258         $$->next = $1;
259 }
260
261 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
262         $$ = $1;
263         $$->next = $3;
264 }
265
266 declarator_wrap: declarator {
267         ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
268 }
269
270 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO;     }
271         | T_TYPEDEF     { $$ = CDECL_STOR_TYPEDEF;  }
272         | T_EXTERN      { $$ = CDECL_STOR_EXTERN;   }
273         | T_STATIC      { $$ = CDECL_STOR_STATIC;   }
274         | T_REGISTER    { $$ = CDECL_STOR_REGISTER; }
275         | T_INLINE      { $$ = CDECL_FUNC_INLINE;   }
276
277 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID;      }
278         | T_CHAR        { $$ = CDECL_TYPE_CHAR;      }
279         | T_SHORT       { $$ = CDECL_TYPE_SHORT;     }
280         | T_INT         { $$ = CDECL_TYPE_INT;       }
281         | T_LONG        { $$ = CDECL_TYPE_LONG;      }
282         | T_FLOAT       { $$ = CDECL_TYPE_FLOAT;     }
283         | T_DOUBLE      { $$ = CDECL_TYPE_DOUBLE;    }
284         | T_SIGNED      { $$ = CDECL_TYPE_SIGNED;    }
285         | T_UNSIGNED    { $$ = CDECL_TYPE_UNSIGNED;  }
286         | T_BOOL        { $$ = CDECL_TYPE_BOOL;      }
287         | T_COMPLEX     { $$ = CDECL_TYPE_COMPLEX;   }
288         | T_IMAGINARY   { $$ = CDECL_TYPE_IMAGINARY; }
289
290 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST;    }
291         | T_RESTRICT      { $$ = CDECL_QUAL_RESTRICT; }
292         | T_VOLATILE      { $$ = CDECL_QUAL_VOLATILE; }
293
294 declspec_notype: qualifier | declspec_simple {
295         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
296 }
297
298 typespec_noid: typespec_simple {
299         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
300 }
301
302 qualifier: qualifier_simple {
303         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
304 }
305
306 typespec: typespec_noid | T_STRUCT T_IDENT {
307         ALLOC_STRUCT($$, struct cdecl_declspec,
308                 .type = CDECL_TYPE_STRUCT,
309                 .ident = $2);
310 } | T_UNION T_IDENT {
311         ALLOC_STRUCT($$, struct cdecl_declspec,
312                 .type = CDECL_TYPE_UNION,
313                 .ident = $2);
314 } | T_ENUM T_IDENT {
315         ALLOC_STRUCT($$, struct cdecl_declspec,
316                 .type = CDECL_TYPE_ENUM,
317                 .ident = $2);
318 } | T_IDENT {
319         ALLOC_STRUCT($$, struct cdecl_declspec,
320                 .type = CDECL_TYPE_IDENT,
321                 .ident = $1);
322 }
323
324 declspec_noid: declspec_notype | typespec_noid
325
326 vla_ident: T_IDENT | T_ASTERISK {
327         ALLOC($$, sizeof "");
328         strcpy($$, "");
329 }
330
331 array: T_LBRACKET T_UINT T_RBRACKET {
332         if ($2 == 0)
333                 FAIL("array length must be positive");
334
335         ALLOC_STRUCT($$, struct cdecl_declarator,
336                 .type = CDECL_DECL_ARRAY,
337                 .u.array.length = $2);
338 } | T_LBRACKET vla_ident T_RBRACKET {
339         ALLOC_STRUCT($$, struct cdecl_declarator,
340                 .type = CDECL_DECL_ARRAY,
341                 .u.array.vla = $2);
342 } | T_LBRACKET T_RBRACKET {
343         ALLOC_STRUCT($$, struct cdecl_declarator,
344                 .type = CDECL_DECL_ARRAY);
345 }
346
347 parameter: declspecs declarator {
348         ALLOC_STRUCT($$, struct cdecl,
349                 .specifiers = $1,
350                 .declarators = $2);
351 }
352
353 parameters: parameter | parameters T_COMMA parameter {
354         $$ = $3;
355         $$->next = $1;
356 }
357
358 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
359
360 parameter_type_list: parameters varargs {
361         struct cdecl *p, *c, *n;
362
363         /* Parameters were accumulated in reverse order. */
364         for (p = NULL, c = $1; c; p = c, c = n) {
365                 n = c->next;
366                 c->next = p;
367         }
368
369         ALLOC_STRUCT($$, struct cdecl_declarator,
370                 .type = CDECL_DECL_FUNCTION,
371                 .u.function.parameters = p,
372                 .u.function.variadic = $2);
373 }
374
375 parens: T_LPAREN parameter_type_list T_RPAREN {
376         $$ = $2;
377 } | T_LPAREN declarator_ish T_RPAREN {
378         ALLOC_STRUCT($$, struct cdecl_declarator,
379                 .type = CDECL_DECL_FUNCTION);
380         ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
381                 .declarators = $2);
382 }
383
384 pointer: T_ASTERISK qualifiers direct_declarator {
385         ALLOC_STRUCT($$, struct cdecl_declarator,
386                 .type = CDECL_DECL_POINTER,
387                 .u.pointer.qualifiers = $2,
388                 .child = $3);
389 } | T_ASTERISK qualifiers pointer {
390         ALLOC_STRUCT($$, struct cdecl_declarator,
391                 .type = CDECL_DECL_POINTER,
392                 .u.pointer.qualifiers = $2,
393                 .child = $3);
394 }
395
396 declarator: direct_declarator | pointer
397 declarator_ish: direct_declarator_ish | pointer
398 postfix: array | parens
399
400 direct_declarator_ish: {
401         ALLOC_STRUCT($$, struct cdecl_declarator,
402                 .type = CDECL_DECL_NULL);
403 } | direct_declarator_ish postfix {
404         $$ = $2;
405         $$->child = $1;
406 }
407
408 direct_declarator: {
409         ALLOC_STRUCT($$, struct cdecl_declarator,
410                 .type = CDECL_DECL_NULL);
411 } | T_IDENT {
412         ALLOC_STRUCT($$, struct cdecl_declarator,
413                 .type = CDECL_DECL_IDENT,
414                 .u.ident = $1);
415 } | direct_declarator postfix {
416         $$ = $2;
417         $$->child = $1;
418 }
419
420 english: T_DECLARE T_IDENT T_AS english_declaration {
421         $$ = $4;
422         for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
423                 if (d->type == CDECL_DECL_NULL) {
424                         d->type = CDECL_DECL_IDENT;
425                         d->u.ident = $2;
426                 }
427         }
428 } | T_TYPE english_declaration {
429         $$ = $2;
430 }
431
432 storage_func_specs: { $$ = NULL; } | declspec_simple storage_func_specs {
433         ALLOC_STRUCT($$, struct cdecl_declspec,
434                 .type = $1,
435                 .next = $2);
436 }
437
438 type_qual_spec: typespec_noid | qualifier
439
440 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
441         $$ = $1;
442         $$->next = $2;
443 }
444
445 /*
446  * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
447  * conflicts with pointer declarators.  So we end up needing to stitch
448  * together three different specifiers lists.
449  */
450 post_specs: qualifiers typespec type_qual_specs {
451         $$ = $2;
452         $$->next = $1;
453         for (struct cdecl_declspec *s = $$; s; s = s->next) {
454                 if (!s->next) {
455                         s->next = $3;
456                         break;
457                 }
458         }
459 }
460
461 english_declaration: storage_func_specs english_declarator post_specs {
462         ALLOC_STRUCT($$, struct cdecl,
463                 .specifiers = $3,
464                 .declarators = $2);
465
466         for (struct cdecl_declspec *s = $$->specifiers; s; s = s->next) {
467                 if (!s->next) {
468                         s->next = $1;
469                         break;
470                 }
471         }
472 }
473
474 english_declarator: {
475         ALLOC_STRUCT($$, struct cdecl_declarator,
476                 .type = CDECL_DECL_NULL);
477 } | english_declarator qualifiers T_POINTER T_TO {
478         ALLOC_STRUCT($$, struct cdecl_declarator,
479                 .type = CDECL_DECL_POINTER,
480                 .child = $1,
481                 .u.pointer.qualifiers = $2);
482 } | english_declarator english_array {
483         $$ = $2;
484         $$->child = $1;
485 } | english_declarator english_function {
486         $$ = $2;
487         $$->child = $1;
488 }
489
490 english_function: T_FUNCTION T_RETURNING {
491         ALLOC_STRUCT($$, struct cdecl_declarator,
492                 .type = CDECL_DECL_FUNCTION,
493                 .u.function.parameters = NULL);
494 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
495         $$ = $3;
496 }
497
498 english_parameter_list: english_parameters varargs {
499         struct cdecl *p, *c, *n;
500
501         /* Parameters were accumulated in reverse order. */
502         for (p = NULL, c = $1; c; p = c, c = n) {
503                 n = c->next;
504                 c->next = p;
505         }
506
507         ALLOC_STRUCT($$, struct cdecl_declarator,
508                 .type = CDECL_DECL_FUNCTION,
509                 .u.function.parameters = p,
510                 .u.function.variadic = $2);
511 }
512
513 english_parameters: english_parameters T_COMMA english_parameter {
514         $$ = $3;
515         $$->next = $1;
516 } | english_parameter
517
518 typedef_name_qual: T_IDENT qualifiers {
519         ALLOC_STRUCT($$, struct cdecl_declspec,
520                 .type = CDECL_TYPE_IDENT,
521                 .ident = $1,
522                 .next = $2);
523 }
524
525 null_decl: {
526         ALLOC_STRUCT($$, struct cdecl_declarator,
527                 .type = CDECL_DECL_NULL);
528 }
529
530 /*
531  * There is a small shift/reduce conflict here.  An unadorned identifier
532  * as the first thing in the parameter might be a typedef name deep in the
533  * first english_declaration (thus empty storage_func_specs and empty
534  * english_declarator need to be reduced) or it might be the identifier
535  * before the "as" (thus the identifier should be shifted).
536  *
537  * The typedef name conflict is the only issue, so treating it as a special
538  * case makes the shift harmless.
539  */
540 english_parameter: english_declaration | typedef_name_qual null_decl {
541         ALLOC_STRUCT($$, struct cdecl,
542                 .specifiers = $1,
543                 .declarators = $2);
544 } | T_IDENT T_AS english_declaration {
545         $$ = $3;
546         for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
547                 if (d->type == CDECL_DECL_NULL) {
548                         d->type = CDECL_DECL_IDENT;
549                         d->u.ident = $1;
550                 }
551         }
552 }
553
554 english_array: T_VLA T_ARRAY english_vla T_OF {
555         ALLOC_STRUCT($$, struct cdecl_declarator,
556                 .type = CDECL_DECL_ARRAY,
557                 .u.array.vla = $3);
558 } | T_ARRAY T_UINT T_OF {
559         if ($2 == 0)
560                 FAIL("array length must be positive");
561
562         ALLOC_STRUCT($$, struct cdecl_declarator,
563                 .type = CDECL_DECL_ARRAY,
564                 .u.array.length = $2);
565 } | T_ARRAY T_OF {
566         ALLOC_STRUCT($$, struct cdecl_declarator,
567                 .type = CDECL_DECL_ARRAY,
568                 .u.array.length = 0);
569 }
570
571 english_vla: T_IDENT | {
572         ALLOC($$, sizeof "");
573         strcpy($$, "");
574 }
575
576 %%
577 void
578 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out,
579         const char *err)
580 {
581         if (strstr(err, "T_LEX_ERROR"))
582                 return;
583
584         fprintf(stderr, "%s\n", err);
585 }