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