]> git.draconx.ca Git - cdecl99.git/blob - src/parse.y
1542c0904e5bea5320ec864646fcfd4ddb63bd64
[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 yyerror(YYLTYPE *, yyscan_t, struct cdecl **, const char *);
80 static void free_decl(struct cdecl *);
81
82 static void free_declspec(struct cdecl_declspec *x)
83 {
84         struct cdecl_declspec *p;
85         while (x) {
86                 p = x->next;
87                 free(x->ident);
88                 free(x);
89                 x = p;
90         }
91 }
92
93 static void free_declarator(struct cdecl_declarator *x)
94 {
95         struct cdecl_declarator *p;
96
97         while (x) {
98                 p = x->child;
99
100                 switch (x->type) {
101                 case CDECL_DECL_NULL:
102                         break;
103                 case CDECL_DECL_IDENT:
104                         free(x->u.ident);
105                         break;
106                 case CDECL_DECL_POINTER:
107                         free_declspec(x->u.pointer.qualifiers);
108                         break;
109                 case CDECL_DECL_ARRAY:
110                         free(x->u.array.vla);
111                         break;
112                 case CDECL_DECL_FUNCTION:
113                         free_decl(x->u.function.parameters);
114                         break;
115                 default:
116                         assert(0);
117                 }
118
119                 free(x);
120                 x = p;
121         }
122 }
123
124 static void free_decl(struct cdecl *x)
125 {
126         struct cdecl *p;
127
128         while (x) {
129                 p = x->next;
130
131                 /* The specifiers may be shared by an entire chain. */
132                 if (!p || p->specifiers != x->specifiers)
133                         free_declspec(x->specifiers);
134
135                 free_declarator(x->declarators);
136                 free(x);
137                 x = p;
138         }
139 }
140
141 void cdecl__free(struct cdecl *decl)
142 {
143         free_decl(decl);
144 }
145
146 /*
147  * Join two declaration specifier lists into a single list, with "a" being the
148  * head of the new list.
149  *
150  * The list "a" is assumed to be nonempty.
151  */
152 static void join_specs(struct cdecl_declspec *a, struct cdecl_declspec *b)
153 {
154         while (a->next)
155                 a = a->next;
156         a->next = b;
157 }
158
159 /*
160  * Alter an abstract declarator (type name) to declare an identifier instead,
161  * used by the English parser rules to reduce "identifier as type" sequences.
162  */
163 static struct cdecl *insert_identifier(struct cdecl *decl, char *ident)
164 {
165         struct cdecl_declarator *d = decl->declarators;
166
167         while (d->child)
168                 d = d->child;
169
170         d->type = CDECL_DECL_IDENT;
171         d->u.ident = ident;
172
173         return decl;
174 }
175 %}
176
177 %destructor { free($$); }            <strval>
178 %destructor { free_declspec($$); }   <declspec>
179 %destructor { free_declarator($$); } <declarator>
180 %destructor { free_decl($$); }       <decl>
181
182 /* Magic tokens */
183 %token T_LEX_ERROR
184 %token T_ENGLISH
185
186 %token <strval> 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 <strval>     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 <strval>     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 /*
263  * Harmless shift/reduce conflicts in english_parameter.  See comments below
264  * for more details.
265  */
266 %expect 2
267
268 %%
269
270 input: T_ENGLISH english {
271         *out = $2;
272 } | declaration {
273         *out = $1;
274 };
275
276 semi: | T_SEMICOLON
277
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
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         ALLOC_STRUCT($$, struct cdecl_declspec,
366                 .type  = $1,
367                 .ident = $2);
368 } | T_IDENT {
369         ALLOC_STRUCT($$, struct cdecl_declspec,
370                 .type = CDECL_TYPE_IDENT,
371                 .ident = $1);
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 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->next = $3;
481         join_specs($2, $1);
482         $$ = $2;
483 }
484
485 english_declaration: storage_func_specs english_declarator post_specs {
486         join_specs($3, $1);
487         ALLOC_STRUCT($$, struct cdecl,
488                 .specifiers = $3,
489                 .declarators = $2);
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_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         ALLOC_STRUCT($$, struct cdecl_declspec,
529                 .type = CDECL_TYPE_IDENT,
530                 .ident = $1,
531                 .next = $2);
532 }
533
534 null_decl: {
535         ALLOC_STRUCT($$, struct cdecl_declarator,
536                 .type = CDECL_DECL_NULL);
537 }
538
539 /*
540  * There is a small shift/reduce conflict here.  An unadorned identifier
541  * as the first thing in the parameter might be a typedef name deep in the
542  * first english_declaration (thus empty storage_func_specs and empty
543  * english_declarator need to be reduced) or it might be the identifier
544  * before the "as" (thus the identifier should be shifted).
545  *
546  * The typedef name conflict is the only issue, so treating it as a special
547  * case makes the shift harmless.
548  */
549 english_parameter: english_declaration | typedef_name_qual null_decl {
550         ALLOC_STRUCT($$, struct cdecl,
551                 .specifiers = $1,
552                 .declarators = $2);
553 } | T_IDENT T_AS english_declaration {
554         $$ = insert_identifier($3, $1);
555 }
556
557 english_array: T_VLA T_ARRAY english_vla T_OF {
558         ALLOC_STRUCT($$, struct cdecl_declarator,
559                 .type = CDECL_DECL_ARRAY,
560                 .u.array.vla = $3);
561 } | T_ARRAY array_length T_OF {
562         ALLOC_STRUCT($$, struct cdecl_declarator,
563                 .type = CDECL_DECL_ARRAY,
564                 .u.array.length = $2);
565 }
566
567 array_length: { $$ = 0; }
568 array_length: T_UINT {
569         if (!($$ = $1))
570                 FAIL(_("array length must be positive"));
571 }
572
573 english_vla: T_IDENT | {
574         ALLOC($$, sizeof "");
575         *$$ = 0;
576 }
577
578 %%
579
580 /*
581  * Expose the token string table to the rest of the library, in order to
582  * produce strings that match parser keywords.
583  *
584  * In order for this to work properly, the Bison output must be postprocessed
585  * by fix-yytname.awk to remove pointless quotation marks from the keyword
586  * strings.
587  */
588 const char *cdecl__token_name(unsigned token)
589 {
590         return yytname[YYTRANSLATE(token)];
591 }
592
593 static void
594 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err)
595 {
596         if (strstr(err, yytname[YYTRANSLATE(T_LEX_ERROR)]))
597                 return;
598
599         cdecl__err(CDECL_ENOPARSE, "%s", err);
600 }