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