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