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