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