]> git.draconx.ca Git - cdecl99.git/blob - src/parse.y
libcdecl: Re-use strings from parser in spec_string.
[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 T_TYPEDEF   "typedef"
177 %token T_EXTERN    "extern"
178 %token T_STATIC    "static"
179 %token T_AUTO      "auto"
180 %token T_REGISTER  "register"
181
182 %token T_INLINE    "inline"
183
184 %token T_RESTRICT  "restrict"
185 %token T_VOLATILE  "volatile"
186 %token T_CONST     "const"
187
188 %token T_VOID      "void"
189 %token T_CHAR      "char"
190 %token T_SHORT     "short"
191 %token T_INT       "int"
192 %token T_LONG      "long"
193 %token T_FLOAT     "float"
194 %token T_DOUBLE    "double"
195 %token T_SIGNED    "signed"
196 %token T_UNSIGNED  "unsigned"
197 %token T_BOOL      "_Bool"
198 %token T_COMPLEX   "_Complex"
199 %token T_IMAGINARY "_Imaginary"
200
201 %token T_STRUCT    "struct"
202 %token T_UNION     "union"
203 %token 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 typespec_simple qualifier_simple
222 %type <declspec>   declspec_notype declspec_noid typespec_noid typespec
223 %type <declspec>   qualifier qualifiers
224 %type <declspec>   declspecs declspecs_noid
225 %type <declarator> direct_declarator declarator pointer array parens postfix
226 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
227 %type <decl>       declaration declarators declarator_wrap
228 %type <decl>       parameter parameters
229
230 %type <strval>     english_vla
231 %type <declspec>   storage_func_specs post_specs
232 %type <declspec>   type_qual_spec type_qual_specs typedef_name_qual
233 %type <declarator> english_declarator english_array english_function
234 %type <declarator> english_parameter_list null_decl
235 %type <decl>       english_parameter english_parameters
236 %type <decl>       english english_declaration
237
238 /*
239  * Harmless shift/reduce conflicts in english_parameter.  See comments below
240  * for more details.
241  */
242 %expect 2
243
244 %%
245
246 input: T_ENGLISH english {
247         *out = $2;
248 } | declaration {
249         *out = $1;
250 };
251
252 semi: | T_SEMICOLON
253
254 declaration: declspecs declarators semi {
255         $$ = $2;
256
257         for (struct cdecl *i = $$; i; i = i->next)
258                 i->specifiers = $1;
259 };
260
261 declspecs: declspec_notype declspecs {
262         $$ = $1;
263         $$->next = $2;
264 } | typespec declspecs_noid {
265         $$ = $1;
266         $$->next = $2;
267 }
268
269 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
270         $$ = $1;
271         $$->next = $2;
272 }
273
274 qualifiers: { $$ = NULL; } | qualifiers qualifier {
275         $$ = $2;
276         $$->next = $1;
277 }
278
279 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
280         $$ = $1;
281         $$->next = $3;
282 }
283
284 declarator_wrap: declarator {
285         ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
286 }
287
288 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO;     }
289         | T_TYPEDEF     { $$ = CDECL_STOR_TYPEDEF;  }
290         | T_EXTERN      { $$ = CDECL_STOR_EXTERN;   }
291         | T_STATIC      { $$ = CDECL_STOR_STATIC;   }
292         | T_REGISTER    { $$ = CDECL_STOR_REGISTER; }
293         | T_INLINE      { $$ = CDECL_FUNC_INLINE;   }
294
295 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID;      }
296         | T_CHAR        { $$ = CDECL_TYPE_CHAR;      }
297         | T_SHORT       { $$ = CDECL_TYPE_SHORT;     }
298         | T_INT         { $$ = CDECL_TYPE_INT;       }
299         | T_LONG        { $$ = CDECL_TYPE_LONG;      }
300         | T_FLOAT       { $$ = CDECL_TYPE_FLOAT;     }
301         | T_DOUBLE      { $$ = CDECL_TYPE_DOUBLE;    }
302         | T_SIGNED      { $$ = CDECL_TYPE_SIGNED;    }
303         | T_UNSIGNED    { $$ = CDECL_TYPE_UNSIGNED;  }
304         | T_BOOL        { $$ = CDECL_TYPE_BOOL;      }
305         | T_COMPLEX     { $$ = CDECL_TYPE_COMPLEX;   }
306         | T_IMAGINARY   { $$ = CDECL_TYPE_IMAGINARY; }
307
308 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST;    }
309         | T_RESTRICT      { $$ = CDECL_QUAL_RESTRICT; }
310         | T_VOLATILE      { $$ = CDECL_QUAL_VOLATILE; }
311
312 declspec_notype: qualifier | declspec_simple {
313         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
314 }
315
316 typespec_noid: typespec_simple {
317         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
318 }
319
320 qualifier: qualifier_simple {
321         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
322 }
323
324 typespec: typespec_noid | T_STRUCT T_IDENT {
325         ALLOC_STRUCT($$, struct cdecl_declspec,
326                 .type = CDECL_TYPE_STRUCT,
327                 .ident = $2);
328 } | T_UNION T_IDENT {
329         ALLOC_STRUCT($$, struct cdecl_declspec,
330                 .type = CDECL_TYPE_UNION,
331                 .ident = $2);
332 } | T_ENUM T_IDENT {
333         ALLOC_STRUCT($$, struct cdecl_declspec,
334                 .type = CDECL_TYPE_ENUM,
335                 .ident = $2);
336 } | T_IDENT {
337         ALLOC_STRUCT($$, struct cdecl_declspec,
338                 .type = CDECL_TYPE_IDENT,
339                 .ident = $1);
340 }
341
342 declspec_noid: declspec_notype | typespec_noid
343
344 vla_ident: T_IDENT | T_ASTERISK {
345         ALLOC($$, sizeof "");
346         strcpy($$, "");
347 }
348
349 array: T_LBRACKET T_UINT T_RBRACKET {
350         if ($2 == 0)
351                 FAIL(_("array length must be positive"));
352
353         ALLOC_STRUCT($$, struct cdecl_declarator,
354                 .type = CDECL_DECL_ARRAY,
355                 .u.array.length = $2);
356 } | T_LBRACKET vla_ident T_RBRACKET {
357         ALLOC_STRUCT($$, struct cdecl_declarator,
358                 .type = CDECL_DECL_ARRAY,
359                 .u.array.vla = $2);
360 } | T_LBRACKET T_RBRACKET {
361         ALLOC_STRUCT($$, struct cdecl_declarator,
362                 .type = CDECL_DECL_ARRAY);
363 }
364
365 parameter: declspecs declarator {
366         ALLOC_STRUCT($$, struct cdecl,
367                 .specifiers = $1,
368                 .declarators = $2);
369 }
370
371 parameters: parameter | parameters T_COMMA parameter {
372         $$ = $3;
373         $$->next = $1;
374 }
375
376 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
377
378 parameter_type_list: parameters varargs {
379         struct cdecl *p, *c, *n;
380
381         /* Parameters were accumulated in reverse order. */
382         for (p = NULL, c = $1; c; p = c, c = n) {
383                 n = c->next;
384                 c->next = p;
385         }
386
387         ALLOC_STRUCT($$, struct cdecl_declarator,
388                 .type = CDECL_DECL_FUNCTION,
389                 .u.function.parameters = p,
390                 .u.function.variadic = $2);
391 }
392
393 parens: T_LPAREN parameter_type_list T_RPAREN {
394         $$ = $2;
395 } | T_LPAREN declarator_ish T_RPAREN {
396         ALLOC_STRUCT($$, struct cdecl_declarator,
397                 .type = CDECL_DECL_FUNCTION);
398         ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
399                 .declarators = $2);
400 }
401
402 pointer: T_ASTERISK qualifiers direct_declarator {
403         ALLOC_STRUCT($$, struct cdecl_declarator,
404                 .type = CDECL_DECL_POINTER,
405                 .u.pointer.qualifiers = $2,
406                 .child = $3);
407 } | T_ASTERISK qualifiers pointer {
408         ALLOC_STRUCT($$, struct cdecl_declarator,
409                 .type = CDECL_DECL_POINTER,
410                 .u.pointer.qualifiers = $2,
411                 .child = $3);
412 }
413
414 declarator: direct_declarator | pointer
415 declarator_ish: direct_declarator_ish | pointer
416 postfix: array | parens
417
418 direct_declarator_ish: {
419         ALLOC_STRUCT($$, struct cdecl_declarator,
420                 .type = CDECL_DECL_NULL);
421 } | direct_declarator_ish postfix {
422         $$ = $2;
423         $$->child = $1;
424 }
425
426 direct_declarator: {
427         ALLOC_STRUCT($$, struct cdecl_declarator,
428                 .type = CDECL_DECL_NULL);
429 } | T_IDENT {
430         ALLOC_STRUCT($$, struct cdecl_declarator,
431                 .type = CDECL_DECL_IDENT,
432                 .u.ident = $1);
433 } | direct_declarator postfix {
434         $$ = $2;
435         $$->child = $1;
436 }
437
438 english: T_DECLARE T_IDENT T_AS english_declaration {
439         $$ = $4;
440         for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
441                 if (d->type == CDECL_DECL_NULL) {
442                         d->type = CDECL_DECL_IDENT;
443                         d->u.ident = $2;
444                 }
445         }
446 } | T_TYPE english_declaration {
447         $$ = $2;
448 }
449
450 storage_func_specs: { $$ = NULL; } | declspec_simple storage_func_specs {
451         ALLOC_STRUCT($$, struct cdecl_declspec,
452                 .type = $1,
453                 .next = $2);
454 }
455
456 type_qual_spec: typespec_noid | qualifier
457
458 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
459         $$ = $1;
460         $$->next = $2;
461 }
462
463 /*
464  * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
465  * conflicts with pointer declarators.  So we end up needing to stitch
466  * together three different specifiers lists.
467  */
468 post_specs: qualifiers typespec type_qual_specs {
469         $$ = $2;
470         $$->next = $1;
471         for (struct cdecl_declspec *s = $$; s; s = s->next) {
472                 if (!s->next) {
473                         s->next = $3;
474                         break;
475                 }
476         }
477 }
478
479 english_declaration: storage_func_specs english_declarator post_specs {
480         ALLOC_STRUCT($$, struct cdecl,
481                 .specifiers = $3,
482                 .declarators = $2);
483
484         for (struct cdecl_declspec *s = $$->specifiers; s; s = s->next) {
485                 if (!s->next) {
486                         s->next = $1;
487                         break;
488                 }
489         }
490 }
491
492 english_declarator: {
493         ALLOC_STRUCT($$, struct cdecl_declarator,
494                 .type = CDECL_DECL_NULL);
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_parameters varargs {
517         struct cdecl *p, *c, *n;
518
519         /* Parameters were accumulated in reverse order. */
520         for (p = NULL, c = $1; c; p = c, c = n) {
521                 n = c->next;
522                 c->next = p;
523         }
524
525         ALLOC_STRUCT($$, struct cdecl_declarator,
526                 .type = CDECL_DECL_FUNCTION,
527                 .u.function.parameters = p,
528                 .u.function.variadic = $2);
529 }
530
531 english_parameters: english_parameters T_COMMA english_parameter {
532         $$ = $3;
533         $$->next = $1;
534 } | english_parameter
535
536 typedef_name_qual: T_IDENT qualifiers {
537         ALLOC_STRUCT($$, struct cdecl_declspec,
538                 .type = CDECL_TYPE_IDENT,
539                 .ident = $1,
540                 .next = $2);
541 }
542
543 null_decl: {
544         ALLOC_STRUCT($$, struct cdecl_declarator,
545                 .type = CDECL_DECL_NULL);
546 }
547
548 /*
549  * There is a small shift/reduce conflict here.  An unadorned identifier
550  * as the first thing in the parameter might be a typedef name deep in the
551  * first english_declaration (thus empty storage_func_specs and empty
552  * english_declarator need to be reduced) or it might be the identifier
553  * before the "as" (thus the identifier should be shifted).
554  *
555  * The typedef name conflict is the only issue, so treating it as a special
556  * case makes the shift harmless.
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         $$ = $3;
564         for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
565                 if (d->type == CDECL_DECL_NULL) {
566                         d->type = CDECL_DECL_IDENT;
567                         d->u.ident = $1;
568                 }
569         }
570 }
571
572 english_array: T_VLA T_ARRAY english_vla T_OF {
573         ALLOC_STRUCT($$, struct cdecl_declarator,
574                 .type = CDECL_DECL_ARRAY,
575                 .u.array.vla = $3);
576 } | T_ARRAY T_UINT T_OF {
577         if ($2 == 0)
578                 FAIL(_("array length must be positive"));
579
580         ALLOC_STRUCT($$, struct cdecl_declarator,
581                 .type = CDECL_DECL_ARRAY,
582                 .u.array.length = $2);
583 } | T_ARRAY T_OF {
584         ALLOC_STRUCT($$, struct cdecl_declarator,
585                 .type = CDECL_DECL_ARRAY,
586                 .u.array.length = 0);
587 }
588
589 english_vla: T_IDENT | {
590         ALLOC($$, sizeof "");
591         strcpy($$, "");
592 }
593
594 %%
595
596 /*
597  * Expose the token string table to the rest of the library, in order to
598  * produce strings that match parser keywords.
599  *
600  * In order for this to work properly, the Bison output must be postprocessed
601  * by fix-yytname.awk to remove pointless quotation marks from the keyword
602  * strings.
603  */
604 const char *cdecl__token_name(unsigned token)
605 {
606         return yytname[YYTRANSLATE(token)];
607 }