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