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