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