]> git.draconx.ca Git - cdecl99.git/blob - src/parse.y
Make semicolons after declarations optional.
[cdecl99.git] / src / parse.y
1 %code top {
2 /*
3  *  Parser for C declarations.
4  *  Copyright © 2011 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 {yyscan_t 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 <assert.h>
31 #include <stdbool.h>
32
33 #include "scan.h"
34 #include "cdecl.h"
35
36 #define FAIL(msg) do { \
37         yyerror(&yylloc, NULL, NULL, msg); \
38         YYERROR; \
39 } while (0)
40
41 #define ALLOC(ptr, size) do { \
42         (ptr) = malloc(size); \
43         if (!(ptr)) \
44                 FAIL("failed to allocate memory"); \
45 } while (0)
46
47 #define ALLOC_STRUCT(ptr, type, ...) do { \
48         ALLOC(ptr, sizeof (type)); \
49         *(ptr) = (type) { __VA_ARGS__ }; \
50 } while (0)
51 %}
52
53 %code requires {
54 #include <inttypes.h>
55 }
56
57 %code provides {
58 void cdecl__yyerror(YYLTYPE *, void *, struct cdecl **, const char *);
59 int cdecl__yyparse(void *scanner, struct cdecl **out);
60 }
61
62 %union {
63         uintmax_t uintval;
64         _Bool boolval;
65         char *strval;
66         struct cdecl_declspec *declspec;
67         struct cdecl_declarator *declarator;
68         struct cdecl *decl;
69 }
70
71 %{
72 static void free_decl(struct cdecl *);
73
74 static void free_declspec(struct cdecl_declspec *x)
75 {
76         struct cdecl_declspec *p;
77         while (x) {
78                 p = x->next;
79                 free(x->ident);
80                 free(x);
81                 x = p;
82         }
83 }
84
85 static void free_declarator(struct cdecl_declarator *x)
86 {
87         struct cdecl_declarator *p;
88
89         while (x) {
90                 p = x->child;
91
92                 switch (x->type) {
93                 case CDECL_DECL_NULL:
94                         break;
95                 case CDECL_DECL_IDENT:
96                         free(x->u.ident);
97                         break;
98                 case CDECL_DECL_POINTER:
99                         free_declspec(x->u.pointer.qualifiers);
100                         break;
101                 case CDECL_DECL_ARRAY:
102                         free(x->u.array.vla);
103                         break;
104                 case CDECL_DECL_FUNCTION:
105                         free_decl(x->u.function.parameters);
106                         break;
107                 default:
108                         assert(0);
109                 }
110
111                 free(x);
112                 x = p;
113         }
114 }
115
116 static void free_decl(struct cdecl *x)
117 {
118         struct cdecl *p;
119
120         while (x) {
121                 p = x->next;
122
123                 /* The specifiers may be shared by an entire chain. */
124                 if (!p || p->specifiers != x->specifiers)
125                         free_declspec(x->specifiers);
126
127                 free_declarator(x->declarators);
128                 free(x);
129                 x = p;
130         }
131 }
132
133 void cdecl_free(struct cdecl *decl)
134 {
135         free_decl(decl);
136 }
137 %}
138
139 %destructor { free($$); }            <strval>
140 %destructor { free_declspec($$); }   <declspec>
141 %destructor { free_declarator($$); } <declarator>
142 %destructor { free_decl($$); }       <decl>
143
144 /* Magic tokens */
145 %token T_LEX_ERROR
146 %token T_ENGLISH
147
148 %token <strval> T_IDENT "identifier"
149 %token <uintval> T_UINT "integer constant"
150
151 %token T_SEMICOLON ";"
152 %token T_ASTERISK  "*"
153 %token T_LPAREN    "("
154 %token T_RPAREN    ")"
155 %token T_LBRACKET  "["
156 %token T_RBRACKET  "]"
157 %token T_COMMA     ","
158 %token T_ELLIPSIS  "..."
159
160 %token T_TYPEDEF   "typedef"
161 %token T_EXTERN    "extern"
162 %token T_STATIC    "static"
163 %token T_AUTO      "auto"
164 %token T_REGISTER  "register"
165
166 %token T_INLINE    "inline"
167
168 %token T_RESTRICT  "restrict"
169 %token T_VOLATILE  "volatile"
170 %token T_CONST     "const"
171
172 %token T_VOID      "void"
173 %token T_CHAR      "char"
174 %token T_SHORT     "short"
175 %token T_INT       "int"
176 %token T_LONG      "long"
177 %token T_FLOAT     "float"
178 %token T_DOUBLE    "double"
179 %token T_SIGNED    "signed"
180 %token T_UNSIGNED  "unsigned"
181 %token T_BOOL      "_Bool"
182 %token T_COMPLEX   "_Complex"
183 %token T_IMAGINARY "_Imaginary"
184
185 %token T_STRUCT    "struct"
186 %token T_UNION     "union"
187 %token T_ENUM      "enum"
188
189 /*
190  * English keywords.
191  */
192 %token T_TYPE      "type"
193 %token T_DECLARE   "declare"
194 %token T_POINTER   "pointer"
195 %token T_FUNCTION  "function"
196 %token T_RETURNING "returning"
197 %token T_ARRAY     "array"
198 %token T_TO        "to"
199 %token T_OF        "of"
200 %token T_AS        "as"
201 %token T_VLA       "variable-length"
202
203 %type <strval>     vla_ident
204 %type <boolval>    varargs
205 %type <uintval>    declspec_simple typespec_simple qualifier_simple
206 %type <declspec>   declspec_notype declspec_noid typespec_noid typespec
207 %type <declspec>   qualifier qualifiers
208 %type <declspec>   declspecs declspecs_noid
209 %type <declarator> direct_declarator declarator pointer array parens postfix
210 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
211 %type <decl>       declaration declarators declarator_wrap
212 %type <decl>       parameter parameters
213
214 %type <strval>     english_vla
215 %type <declspec>   storage_func_specs post_specs
216 %type <declspec>   type_qual_spec type_qual_specs typedef_name_qual
217 %type <declarator> english_declarator english_array english_function
218 %type <declarator> english_parameter_list null_decl
219 %type <decl>       english_parameter english_parameters
220 %type <decl>       english english_declaration
221
222 /*
223  * Harmless shift/reduce conflicts in english_parameter.  See comments below
224  * for more details.
225  */
226 %expect 2
227
228 %%
229
230 input: T_ENGLISH english {
231         *out = $2;
232 } | declaration {
233         *out = $1;
234 };
235
236 semi: | T_SEMICOLON
237
238 declaration: declspecs declarators semi {
239         $$ = $2;
240
241         for (struct cdecl *i = $$; i; i = i->next)
242                 i->specifiers = $1;
243 };
244
245 declspecs: declspec_notype declspecs {
246         $$ = $1;
247         $$->next = $2;
248 } | typespec declspecs_noid {
249         $$ = $1;
250         $$->next = $2;
251 }
252
253 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
254         $$ = $1;
255         $$->next = $2;
256 }
257
258 qualifiers: { $$ = NULL; } | qualifiers qualifier {
259         $$ = $2;
260         $$->next = $1;
261 }
262
263 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
264         $$ = $1;
265         $$->next = $3;
266 }
267
268 declarator_wrap: declarator {
269         ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
270 }
271
272 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO;     }
273         | T_TYPEDEF     { $$ = CDECL_STOR_TYPEDEF;  }
274         | T_EXTERN      { $$ = CDECL_STOR_EXTERN;   }
275         | T_STATIC      { $$ = CDECL_STOR_STATIC;   }
276         | T_REGISTER    { $$ = CDECL_STOR_REGISTER; }
277         | T_INLINE      { $$ = CDECL_FUNC_INLINE;   }
278
279 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID;      }
280         | T_CHAR        { $$ = CDECL_TYPE_CHAR;      }
281         | T_SHORT       { $$ = CDECL_TYPE_SHORT;     }
282         | T_INT         { $$ = CDECL_TYPE_INT;       }
283         | T_LONG        { $$ = CDECL_TYPE_LONG;      }
284         | T_FLOAT       { $$ = CDECL_TYPE_FLOAT;     }
285         | T_DOUBLE      { $$ = CDECL_TYPE_DOUBLE;    }
286         | T_SIGNED      { $$ = CDECL_TYPE_SIGNED;    }
287         | T_UNSIGNED    { $$ = CDECL_TYPE_UNSIGNED;  }
288         | T_BOOL        { $$ = CDECL_TYPE_BOOL;      }
289         | T_COMPLEX     { $$ = CDECL_TYPE_COMPLEX;   }
290         | T_IMAGINARY   { $$ = CDECL_TYPE_IMAGINARY; }
291
292 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST;    }
293         | T_RESTRICT      { $$ = CDECL_QUAL_RESTRICT; }
294         | T_VOLATILE      { $$ = CDECL_QUAL_VOLATILE; }
295
296 declspec_notype: qualifier | declspec_simple {
297         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
298 }
299
300 typespec_noid: typespec_simple {
301         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
302 }
303
304 qualifier: qualifier_simple {
305         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
306 }
307
308 typespec: typespec_noid | T_STRUCT T_IDENT {
309         ALLOC_STRUCT($$, struct cdecl_declspec,
310                 .type = CDECL_TYPE_STRUCT,
311                 .ident = $2);
312 } | T_UNION T_IDENT {
313         ALLOC_STRUCT($$, struct cdecl_declspec,
314                 .type = CDECL_TYPE_UNION,
315                 .ident = $2);
316 } | T_ENUM T_IDENT {
317         ALLOC_STRUCT($$, struct cdecl_declspec,
318                 .type = CDECL_TYPE_ENUM,
319                 .ident = $2);
320 } | T_IDENT {
321         ALLOC_STRUCT($$, struct cdecl_declspec,
322                 .type = CDECL_TYPE_IDENT,
323                 .ident = $1);
324 }
325
326 declspec_noid: declspec_notype | typespec_noid
327
328 vla_ident: T_IDENT | T_ASTERISK {
329         ALLOC($$, sizeof "");
330         strcpy($$, "");
331 }
332
333 array: T_LBRACKET T_UINT T_RBRACKET {
334         if ($2 == 0)
335                 FAIL("array length must be positive");
336
337         ALLOC_STRUCT($$, struct cdecl_declarator,
338                 .type = CDECL_DECL_ARRAY,
339                 .u.array.length = $2);
340 } | T_LBRACKET vla_ident T_RBRACKET {
341         ALLOC_STRUCT($$, struct cdecl_declarator,
342                 .type = CDECL_DECL_ARRAY,
343                 .u.array.vla = $2);
344 } | T_LBRACKET T_RBRACKET {
345         ALLOC_STRUCT($$, struct cdecl_declarator,
346                 .type = CDECL_DECL_ARRAY);
347 }
348
349 parameter: declspecs declarator {
350         ALLOC_STRUCT($$, struct cdecl,
351                 .specifiers = $1,
352                 .declarators = $2);
353 }
354
355 parameters: parameter | parameters T_COMMA parameter {
356         $$ = $3;
357         $$->next = $1;
358 }
359
360 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
361
362 parameter_type_list: parameters varargs {
363         struct cdecl *p, *c, *n;
364
365         /* Parameters were accumulated in reverse order. */
366         for (p = NULL, c = $1; c; p = c, c = n) {
367                 n = c->next;
368                 c->next = p;
369         }
370
371         ALLOC_STRUCT($$, struct cdecl_declarator,
372                 .type = CDECL_DECL_FUNCTION,
373                 .u.function.parameters = p,
374                 .u.function.variadic = $2);
375 }
376
377 parens: T_LPAREN parameter_type_list T_RPAREN {
378         $$ = $2;
379 } | T_LPAREN declarator_ish T_RPAREN {
380         ALLOC_STRUCT($$, struct cdecl_declarator,
381                 .type = CDECL_DECL_FUNCTION);
382         ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
383                 .declarators = $2);
384 }
385
386 pointer: T_ASTERISK qualifiers direct_declarator {
387         ALLOC_STRUCT($$, struct cdecl_declarator,
388                 .type = CDECL_DECL_POINTER,
389                 .u.pointer.qualifiers = $2,
390                 .child = $3);
391 } | T_ASTERISK qualifiers pointer {
392         ALLOC_STRUCT($$, struct cdecl_declarator,
393                 .type = CDECL_DECL_POINTER,
394                 .u.pointer.qualifiers = $2,
395                 .child = $3);
396 }
397
398 declarator: direct_declarator | pointer
399 declarator_ish: direct_declarator_ish | pointer
400 postfix: array | parens
401
402 direct_declarator_ish: {
403         ALLOC_STRUCT($$, struct cdecl_declarator,
404                 .type = CDECL_DECL_NULL);
405 } | direct_declarator_ish postfix {
406         $$ = $2;
407         $$->child = $1;
408 }
409
410 direct_declarator: {
411         ALLOC_STRUCT($$, struct cdecl_declarator,
412                 .type = CDECL_DECL_NULL);
413 } | T_IDENT {
414         ALLOC_STRUCT($$, struct cdecl_declarator,
415                 .type = CDECL_DECL_IDENT,
416                 .u.ident = $1);
417 } | direct_declarator postfix {
418         $$ = $2;
419         $$->child = $1;
420 }
421
422 english: T_DECLARE T_IDENT T_AS english_declaration {
423         $$ = $4;
424         for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
425                 if (d->type == CDECL_DECL_NULL) {
426                         d->type = CDECL_DECL_IDENT;
427                         d->u.ident = $2;
428                 }
429         }
430 } | T_TYPE english_declaration {
431         $$ = $2;
432 }
433
434 storage_func_specs: { $$ = NULL; } | declspec_simple storage_func_specs {
435         ALLOC_STRUCT($$, struct cdecl_declspec,
436                 .type = $1,
437                 .next = $2);
438 }
439
440 type_qual_spec: typespec_noid | qualifier
441
442 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
443         $$ = $1;
444         $$->next = $2;
445 }
446
447 /*
448  * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
449  * conflicts with pointer declarators.  So we end up needing to stitch
450  * together three different specifiers lists.
451  */
452 post_specs: qualifiers typespec type_qual_specs {
453         $$ = $2;
454         $$->next = $1;
455         for (struct cdecl_declspec *s = $$; s; s = s->next) {
456                 if (!s->next) {
457                         s->next = $3;
458                         break;
459                 }
460         }
461 }
462
463 english_declaration: storage_func_specs english_declarator post_specs {
464         ALLOC_STRUCT($$, struct cdecl,
465                 .specifiers = $3,
466                 .declarators = $2);
467
468         for (struct cdecl_declspec *s = $$->specifiers; s; s = s->next) {
469                 if (!s->next) {
470                         s->next = $1;
471                         break;
472                 }
473         }
474 }
475
476 english_declarator: {
477         ALLOC_STRUCT($$, struct cdecl_declarator,
478                 .type = CDECL_DECL_NULL);
479 } | english_declarator qualifiers T_POINTER T_TO {
480         ALLOC_STRUCT($$, struct cdecl_declarator,
481                 .type = CDECL_DECL_POINTER,
482                 .child = $1,
483                 .u.pointer.qualifiers = $2);
484 } | english_declarator english_array {
485         $$ = $2;
486         $$->child = $1;
487 } | english_declarator english_function {
488         $$ = $2;
489         $$->child = $1;
490 }
491
492 english_function: T_FUNCTION T_RETURNING {
493         ALLOC_STRUCT($$, struct cdecl_declarator,
494                 .type = CDECL_DECL_FUNCTION,
495                 .u.function.parameters = NULL);
496 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
497         $$ = $3;
498 }
499
500 english_parameter_list: english_parameters varargs {
501         struct cdecl *p, *c, *n;
502
503         /* Parameters were accumulated in reverse order. */
504         for (p = NULL, c = $1; c; p = c, c = n) {
505                 n = c->next;
506                 c->next = p;
507         }
508
509         ALLOC_STRUCT($$, struct cdecl_declarator,
510                 .type = CDECL_DECL_FUNCTION,
511                 .u.function.parameters = p,
512                 .u.function.variadic = $2);
513 }
514
515 english_parameters: english_parameters T_COMMA english_parameter {
516         $$ = $3;
517         $$->next = $1;
518 } | english_parameter
519
520 typedef_name_qual: T_IDENT qualifiers {
521         ALLOC_STRUCT($$, struct cdecl_declspec,
522                 .type = CDECL_TYPE_IDENT,
523                 .ident = $1,
524                 .next = $2);
525 }
526
527 null_decl: {
528         ALLOC_STRUCT($$, struct cdecl_declarator,
529                 .type = CDECL_DECL_NULL);
530 }
531
532 /*
533  * There is a small shift/reduce conflict here.  An unadorned identifier
534  * as the first thing in the parameter might be a typedef name deep in the
535  * first english_declaration (thus empty storage_func_specs and empty
536  * english_declarator need to be reduced) or it might be the identifier
537  * before the "as" (thus the identifier should be shifted).
538  *
539  * The typedef name conflict is the only issue, so treating it as a special
540  * case makes the shift harmless.
541  */
542 english_parameter: english_declaration | typedef_name_qual null_decl {
543         ALLOC_STRUCT($$, struct cdecl,
544                 .specifiers = $1,
545                 .declarators = $2);
546 } | T_IDENT T_AS english_declaration {
547         $$ = $3;
548         for (struct cdecl_declarator *d = $$->declarators; d; d = d->child) {
549                 if (d->type == CDECL_DECL_NULL) {
550                         d->type = CDECL_DECL_IDENT;
551                         d->u.ident = $1;
552                 }
553         }
554 }
555
556 english_array: T_VLA T_ARRAY english_vla T_OF {
557         ALLOC_STRUCT($$, struct cdecl_declarator,
558                 .type = CDECL_DECL_ARRAY,
559                 .u.array.vla = $3);
560 } | T_ARRAY T_UINT T_OF {
561         if ($2 == 0)
562                 FAIL("array length must be positive");
563
564         ALLOC_STRUCT($$, struct cdecl_declarator,
565                 .type = CDECL_DECL_ARRAY,
566                 .u.array.length = $2);
567 } | T_ARRAY T_OF {
568         ALLOC_STRUCT($$, struct cdecl_declarator,
569                 .type = CDECL_DECL_ARRAY,
570                 .u.array.length = 0);
571 }
572
573 english_vla: T_IDENT | {
574         ALLOC($$, sizeof "");
575         strcpy($$, "");
576 }
577
578 %%
579 void
580 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out,
581         const char *err)
582 {
583         if (strstr(err, "T_LEX_ERROR"))
584                 return;
585
586         fprintf(stderr, "%s\n", err);
587 }