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