]> git.draconx.ca Git - cdecl99.git/blob - src/parse.y
libcdecl: Combine identifier and struct allocation.
[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                 case CDECL_DECL_IDENT:
109                 case CDECL_DECL_ARRAY:
110                         break;
111                 case CDECL_DECL_POINTER:
112                         free_declspec(x->u.pointer.qualifiers);
113                         break;
114                 case CDECL_DECL_FUNCTION:
115                         free_decl(x->u.function.parameters);
116                         break;
117                 default:
118                         assert(0);
119                 }
120
121                 free(x);
122                 x = p;
123         }
124 }
125
126 static void free_decl(struct cdecl *x)
127 {
128         struct cdecl *p;
129
130         while (x) {
131                 p = x->next;
132
133                 /* The specifiers may be shared by an entire chain. */
134                 if (!p || p->specifiers != x->specifiers)
135                         free_declspec(x->specifiers);
136
137                 free_declarator(x->declarators);
138                 free(x);
139                 x = p;
140         }
141 }
142
143 void cdecl__free(struct cdecl *decl)
144 {
145         free_decl(decl);
146 }
147
148 /*
149  * Join two declaration specifier lists into a single list, with "a" being the
150  * head of the new list.
151  *
152  * The list "a" is assumed to be nonempty.
153  */
154 static void join_specs(struct cdecl_declspec *a, struct cdecl_declspec *b)
155 {
156         while (a->next)
157                 a = a->next;
158         a->next = b;
159 }
160
161 /*
162  * Alter an abstract declarator (type name) to declare an identifier instead,
163  * used by the English parser rules to reduce "identifier as type" sequences.
164  */
165 static struct cdecl *insert_identifier(struct cdecl *decl, struct parse_item *ident)
166 {
167         struct cdecl_declarator *d, **p = &decl->declarators;
168
169         while ((d = *p)->child)
170                 p = &d->child;
171         free(d);
172
173         *p = d = &ident->u.declarator;
174         return decl;
175 }
176 %}
177
178 %destructor { free($$); }            <item>
179 %destructor { free_declspec($$); }   <declspec>
180 %destructor { free_declarator($$); } <declarator>
181 %destructor { free_decl($$); }       <decl>
182
183 /* Magic tokens */
184 %token T_LEX_ERROR
185
186 %token <item>    T_IDENT "identifier"
187 %token <uintval> T_UINT  "integer constant"
188
189 %token T_SEMICOLON ";"
190 %token T_ASTERISK  "*"
191 %token T_LPAREN    "("
192 %token T_RPAREN    ")"
193 %token T_LBRACKET  "["
194 %token T_RBRACKET  "]"
195 %token T_COMMA     ","
196 %token T_ELLIPSIS  "..."
197
198 %token <spectype> T_TYPEDEF   "typedef"
199 %token <spectype> T_EXTERN    "extern"
200 %token <spectype> T_STATIC    "static"
201 %token <spectype> T_AUTO      "auto"
202 %token <spectype> T_REGISTER  "register"
203
204 %token <spectype> T_INLINE    "inline"
205
206 %token <spectype> T_RESTRICT  "restrict"
207 %token <spectype> T_VOLATILE  "volatile"
208 %token <spectype> T_CONST     "const"
209
210 %token <spectype> T_VOID      "void"
211 %token <spectype> T_CHAR      "char"
212 %token <spectype> T_SHORT     "short"
213 %token <spectype> T_INT       "int"
214 %token <spectype> T_LONG      "long"
215 %token <spectype> T_FLOAT     "float"
216 %token <spectype> T_DOUBLE    "double"
217 %token <spectype> T_SIGNED    "signed"
218 %token <spectype> T_UNSIGNED  "unsigned"
219 %token <spectype> T_BOOL      "_Bool"
220 %token <spectype> T_COMPLEX   "_Complex"
221 %token <spectype> T_IMAGINARY "_Imaginary"
222
223 %token <spectype> T_STRUCT    "struct"
224 %token <spectype> T_UNION     "union"
225 %token <spectype> T_ENUM      "enum"
226
227 /*
228  * English keywords.
229  */
230 %token T_TYPE      "type"
231 %token T_DECLARE   "declare"
232 %token T_POINTER   "pointer"
233 %token T_FUNCTION  "function"
234 %token T_RETURNING "returning"
235 %token T_ARRAY     "array"
236 %token T_TO        "to"
237 %token T_OF        "of"
238 %token T_AS        "as"
239 %token T_VLA       "variable-length"
240
241 %type <item>       vla_ident
242 %type <uintval>    array_length
243 %type <boolval>    varargs
244 %type <spectype>   declspec_simple qualifier_simple
245 %type <spectype>   typespec_simple typespec_tagged
246 %type <declspec>   declspec_notype declspec_noid typespec_noid typespec
247 %type <declspec>   qualifier qualifiers
248 %type <declspec>   declspecs declspecs_noid
249 %type <declarator> direct_declarator declarator pointer array parens postfix
250 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
251 %type <decl>       declaration declarators declarator_wrap
252 %type <decl>       parameter
253
254 %type <item>       english_vla
255 %type <declspec>   storage_func_specs post_specs
256 %type <declspec>   type_qual_spec type_qual_specs typedef_name_qual
257 %type <declarator> english_declarator english_array english_function
258 %type <declarator> english_parameter_list null_decl
259 %type <decl>       english english_declaration
260 %type <decl>       english_parameter
261
262 /* Precedence declaration to avoid conflict in english_parameter; see below. */
263 %right T_TYPE
264 %right T_IDENT
265
266 %%
267
268 input: english {
269         *out = $1;
270 } | declaration {
271         *out = $1;
272 };
273
274 semi: | T_SEMICOLON
275
276 declaration: declspecs declarators semi {
277         $$ = $2;
278         $$->specifiers = $1;
279 };
280
281 /*
282  * We support parsing declarations using arbitrary identifiers as type
283  * specifiers (a la C typedef).  To avoid confusion with identifiers that
284  * may also be used as declarators, note the following:
285  *
286  *  (a) Every valid C declaration must have at least one type specifier, and
287  *  (b) Valid declarations with typedef names have exactly one type specifier.
288  *
289  * So the rule applied when parsing specifiers is: an identifier is a type
290  * specifier only if we have not yet seen any type specifiers whatsoever
291  * (within one declaration specifier list).
292  *
293  * Treating identifiers as type specifiers by default can lead to strange and
294  * unexpected parses; libcdecl applies a simplification step to the resulting
295  * parse tree afterwards.
296  */
297 declspecs: declspec_notype declspecs {
298         $$ = $1;
299         $$->next = $2;
300 } | typespec declspecs_noid {
301         $$ = $1;
302         $$->next = $2;
303 }
304
305 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
306         $$ = $1;
307         $$->next = $2;
308 }
309
310 qualifiers: { $$ = NULL; } | qualifiers qualifier {
311         $$ = $2;
312         $$->next = $1;
313 }
314
315 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
316         $$ = $1;
317         $$->next = $3;
318 }
319
320 declarator_wrap: declarator {
321         ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
322 }
323
324 declspec_simple: T_AUTO
325         | T_TYPEDEF
326         | T_EXTERN
327         | T_STATIC
328         | T_REGISTER
329         | T_INLINE
330
331 typespec_simple: T_VOID
332         | T_CHAR
333         | T_SHORT
334         | T_INT
335         | T_LONG
336         | T_FLOAT
337         | T_DOUBLE
338         | T_SIGNED
339         | T_UNSIGNED
340         | T_BOOL
341         | T_COMPLEX
342         | T_IMAGINARY
343
344 typespec_tagged: T_STRUCT | T_UNION | T_ENUM | { $$ = CDECL_TYPE_IDENT; }
345
346 qualifier_simple: T_CONST
347         | T_RESTRICT
348         | T_VOLATILE
349
350 declspec_notype: qualifier | declspec_simple {
351         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
352 }
353
354 typespec_noid: typespec_simple {
355         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
356 }
357
358 qualifier: qualifier_simple {
359         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
360 }
361
362 typespec: typespec_noid | typespec_tagged T_IDENT {
363         /* Compiler should be able to elide this assignment. */
364         $2->u.declspec.ident = $2->u.declarator.u.ident;
365
366         $$ = &$2->u.declspec;
367         $$->type = $1;
368 }
369
370 declspec_noid: declspec_notype | typespec_noid
371
372 vla_ident: T_IDENT | T_ASTERISK {
373         if (!($$ = cdecl__alloc_item(1)))
374                 YYERROR;
375         *$$->s = 0;
376 }
377
378 array: T_LBRACKET array_length T_RBRACKET {
379         ALLOC_STRUCT($$, struct cdecl_declarator,
380                 .type = CDECL_DECL_ARRAY,
381                 .u.array.length = $2);
382 } | T_LBRACKET vla_ident T_RBRACKET {
383         $$ = &$2->u.declarator;
384         $$->type = CDECL_DECL_ARRAY;
385         $$->u.array.vla = $$->u.ident;
386         $$->u.array.length = 0;
387 }
388
389 parameter: declspecs declarator {
390         ALLOC_STRUCT($$, struct cdecl,
391                 .specifiers = $1,
392                 .declarators = $2);
393 }
394
395 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
396
397 parameter_type_list: parameter varargs {
398         ALLOC_STRUCT($$, struct cdecl_declarator,
399                 .type = CDECL_DECL_FUNCTION,
400                 .u.function.parameters = $1,
401                 .u.function.variadic = $2);
402 } | parameter T_COMMA parameter_type_list {
403         $$ = $3;
404         $1->next = $$->u.function.parameters;
405         $$->u.function.parameters = $1;
406 }
407
408 parens: T_LPAREN parameter_type_list T_RPAREN {
409         $$ = $2;
410 } | T_LPAREN declarator_ish T_RPAREN {
411         ALLOC_STRUCT($$, struct cdecl_declarator,
412                 .type = CDECL_DECL_FUNCTION);
413         ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
414                 .declarators = $2);
415 }
416
417 pointer: T_ASTERISK qualifiers direct_declarator {
418         ALLOC_STRUCT($$, struct cdecl_declarator,
419                 .type = CDECL_DECL_POINTER,
420                 .u.pointer.qualifiers = $2,
421                 .child = $3);
422 } | T_ASTERISK qualifiers pointer {
423         ALLOC_STRUCT($$, struct cdecl_declarator,
424                 .type = CDECL_DECL_POINTER,
425                 .u.pointer.qualifiers = $2,
426                 .child = $3);
427 }
428
429 declarator: direct_declarator | pointer
430 declarator_ish: direct_declarator_ish | pointer
431 postfix: array | parens
432
433 direct_declarator_ish: {
434         ALLOC_STRUCT($$, struct cdecl_declarator,
435                 .type = CDECL_DECL_NULL);
436 } | direct_declarator_ish postfix {
437         $$ = $2;
438         $$->child = $1;
439 }
440
441 direct_declarator: {
442         ALLOC_STRUCT($$, struct cdecl_declarator,
443                 .type = CDECL_DECL_NULL);
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         ALLOC_STRUCT($$, struct cdecl_declarator,
495                 .type = CDECL_DECL_NULL);
496 } | english_declarator qualifiers T_POINTER T_TO {
497         ALLOC_STRUCT($$, struct cdecl_declarator,
498                 .type = CDECL_DECL_POINTER,
499                 .child = $1,
500                 .u.pointer.qualifiers = $2);
501 } | english_declarator english_array {
502         $$ = $2;
503         $$->child = $1;
504 } | english_declarator english_function {
505         $$ = $2;
506         $$->child = $1;
507 }
508
509 english_function: T_FUNCTION T_RETURNING {
510         ALLOC_STRUCT($$, struct cdecl_declarator,
511                 .type = CDECL_DECL_FUNCTION,
512                 .u.function.parameters = NULL);
513 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
514         $$ = $3;
515 }
516
517 english_parameter_list: english_parameter varargs {
518         ALLOC_STRUCT($$, struct cdecl_declarator,
519                 .type = CDECL_DECL_FUNCTION,
520                 .u.function.parameters = $1,
521                 .u.function.variadic = $2);
522 } | english_parameter T_COMMA english_parameter_list {
523         $$ = $3;
524         $1->next = $$->u.function.parameters;
525         $$->u.function.parameters = $1;
526 }
527
528 typedef_name_qual: T_IDENT qualifiers {
529         /* Compiler should be able to elide this assignment. */
530         $1->u.declspec.ident = $1->u.declarator.u.ident;
531
532         $$ = &$1->u.declspec;
533         $$->type = CDECL_TYPE_IDENT;
534         $$->next = $2;
535 }
536
537 null_decl: {
538         ALLOC_STRUCT($$, struct cdecl_declarator,
539                 .type = CDECL_DECL_NULL);
540 }
541
542 /*
543  * There is a shift/reduce conflict here when an identifier appears as the
544  * first token.  The conflict is between shifting T_IDENT, or reducing the
545  * empty production for storage_func_specs (cf. english_declaration).
546  *
547  *   - In either case, if we reduce, we won't match T_IDENT T_AS since the
548  *     stack now has the extra storage_func_specs nonterminal symbol.
549  *   - And if we shift, we won't match english_declaration since it is
550  *     too late to add storage_func_specs to the stack.
551  *
552  * The only valid input affected by the conflict is a simple type names,
553  * possibly followed by qualifiers.  So the conflict is adequately resolved
554  * by shifting, so long as we have a special-case reduction to handle this.
555  */
556 english_parameter: english_declaration | typedef_name_qual null_decl {
557         ALLOC_STRUCT($$, struct cdecl,
558                 .specifiers = $1,
559                 .declarators = $2);
560 } | T_IDENT T_AS english_declaration {
561         $$ = insert_identifier($3, $1);
562 }
563
564 english_array: T_VLA T_ARRAY english_vla T_OF {
565         $$ = &$3->u.declarator;
566         $$->type = CDECL_DECL_ARRAY;
567         $$->u.array.vla = $$->u.ident;
568         $$->u.array.length = 0;
569 } | T_ARRAY array_length T_OF {
570         ALLOC_STRUCT($$, struct cdecl_declarator,
571                 .type = CDECL_DECL_ARRAY,
572                 .u.array.length = $2);
573 }
574
575 array_length: { $$ = 0; }
576 array_length: T_UINT {
577         if (!($$ = $1))
578                 FAIL(_("array length must be positive"));
579 }
580
581 english_vla: T_IDENT | {
582         if (!($$ = cdecl__alloc_item(1)))
583                 YYERROR;
584         *$$->s = 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 }