]> git.draconx.ca Git - cdecl99.git/blob - src/parse.y
Make an explicit null declarator type.
[cdecl99.git] / src / parse.y
1 /*
2  *  Parser for C declarations.
3  *  Copyright © 2011 Nick Bowler
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 %parse-param {struct cdecl **out}
20 %define api.pure
21 %error-verbose
22 %locations
23
24 %{
25 #include <assert.h>
26
27 #include "scan.h"
28 #include "cdecl.h"
29
30 #define FAIL(msg) do { \
31         yyerror(&yylloc, NULL, msg); \
32         YYERROR; \
33 } while (0)
34
35 #define ALLOC(ptr, size) do { \
36         (ptr) = malloc(size); \
37         if (!(ptr)) \
38                 FAIL("failed to allocate memory"); \
39 } while (0)
40
41 #define ALLOC_STRUCT(ptr, type, ...) do { \
42         ALLOC(ptr, sizeof (type)); \
43         *(ptr) = (type) { __VA_ARGS__ }; \
44 } while (0)
45 %}
46
47 %code requires {
48 #include <inttypes.h>
49 }
50
51 %code provides {
52 void yyerror(YYLTYPE *, struct cdecl **, const char *);
53 int yyparse(struct cdecl **out);
54 }
55
56 %union {
57         uintmax_t uintval;
58         char *strval;
59         struct cdecl_declspec *declspec;
60         struct cdecl_declarator *declarator;
61         struct cdecl *decl;
62 }
63
64 %{
65 static void free_declspec(struct cdecl_declspec *x)
66 {
67         struct cdecl_declspec *p;
68         while (x) {
69                 p = x->next;
70                 free(x->ident);
71                 free(x);
72                 x = p;
73         }
74 }
75
76 static void free_declarator(struct cdecl_declarator *x)
77 {
78         struct cdecl_declarator *p;
79         while (x) {
80                 p = x->next;
81                 switch (x->type) {
82                 case CDECL_DECL_NULL:
83                         break;
84                 case CDECL_DECL_IDENT:
85                         free(x->u.ident);
86                         break;
87                 case CDECL_DECL_POINTER:
88                         free_declspec(x->u.pointer.qualifiers);
89                         break;
90                 case CDECL_DECL_ARRAY:
91                         free(x->u.array.vla);
92                         break;
93                 default:
94                         assert(0);
95                 }
96
97                 free_declarator(x->child);
98                 free(x);
99                 x = p;
100         }
101 }
102
103 static void free_decl(struct cdecl *decl)
104 {
105         free_declspec(decl->specifiers);
106         free_declarator(decl->declarators);
107         free(decl);
108 }
109
110 void cdecl_free(struct cdecl *decl)
111 {
112         if (decl)
113                 free_decl(decl);
114 }
115 %}
116
117 %destructor { free($$); }            <strval>
118 %destructor { free_declspec($$); }   <declspec>
119 %destructor { free_declarator($$); } <declarator>
120 %destructor { free_decl($$); }       <decl>
121
122 %token T_LEX_ERROR
123
124 %token <strval> T_IDENT "identifier"
125 %token <uintval> T_UINT "integer constant"
126
127 %token T_SEMICOLON ";"
128 %token T_ASTERISK  "*"
129 %token T_LPAREN    "("
130 %token T_RPAREN    ")"
131 %token T_LBRACKET  "["
132 %token T_RBRACKET  "]"
133 %token T_COMMA     ","
134
135 %token T_TYPEDEF  "typedef"
136 %token T_EXTERN   "extern"
137 %token T_STATIC   "static"
138 %token T_AUTO     "auto"
139 %token T_REGISTER "register"
140
141 %token T_INLINE   "inline"
142
143 %token T_RESTRICT "restrict"
144 %token T_VOLATILE "volatile"
145 %token T_CONST    "const"
146
147 %token T_VOID     "void"
148 %token T_CHAR     "char"
149 %token T_SHORT    "short"
150 %token T_INT      "int"
151 %token T_LONG     "long"
152 %token T_FLOAT    "float"
153 %token T_DOUBLE   "double"
154 %token T_SIGNED   "signed"
155 %token T_UNSIGNED "unsigned"
156 %token T_BOOL     "_Bool"
157 %token T_COMPLEX  "_Complex"
158
159 %token T_STRUCT   "struct"
160 %token T_UNION    "union"
161 %token T_ENUM     "enum"
162
163 %type <strval>     vla_ident
164 %type <uintval>    declspec_simple typespec_simple qualifier_simple
165 %type <declspec>   declspec_notype declspec_noid typespec_noid typespec
166 %type <declspec>   qualifier qualifiers
167 %type <declspec>   declspecs declspecs_noid
168 %type <declarator> direct_declarator declarator declarators pointer array
169 %type <decl>       declaration
170
171 %%
172
173 input: declaration {
174         *out = $1;
175 };
176
177 declaration: declspecs declarators T_SEMICOLON {
178         ALLOC_STRUCT($$, struct cdecl,
179                 .specifiers = $1,
180                 .declarators = $2);
181 };
182
183 declspecs: declspec_notype declspecs {
184         $$ = $1;
185         $$->next = $2;
186 } | typespec declspecs_noid {
187         $$ = $1;
188         $$->next = $2;
189 }
190
191 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
192         $$ = $1;
193         $$->next = $2;
194 }
195
196 qualifiers: { $$ = NULL; } | qualifiers qualifier {
197         $$ = $2;
198         $$->next = $1;
199 }
200
201 declarators: declarator | declarator T_COMMA declarators {
202         $$ = $1;
203         $$->next = $3;
204 };
205
206 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO;     }
207         | T_TYPEDEF     { $$ = CDECL_STOR_TYPEDEF;  }
208         | T_EXTERN      { $$ = CDECL_STOR_EXTERN;   }
209         | T_STATIC      { $$ = CDECL_STOR_STATIC;   }
210         | T_REGISTER    { $$ = CDECL_STOR_REGISTER; }
211         | T_INLINE      { $$ = CDECL_FUNC_INLINE;   }
212
213 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID;     }
214         | T_CHAR        { $$ = CDECL_TYPE_CHAR;     }
215         | T_SHORT       { $$ = CDECL_TYPE_SHORT;    }
216         | T_INT         { $$ = CDECL_TYPE_INT;      }
217         | T_LONG        { $$ = CDECL_TYPE_LONG;     }
218         | T_FLOAT       { $$ = CDECL_TYPE_FLOAT;    }
219         | T_DOUBLE      { $$ = CDECL_TYPE_DOUBLE;   }
220         | T_SIGNED      { $$ = CDECL_TYPE_SIGNED;   }
221         | T_UNSIGNED    { $$ = CDECL_TYPE_UNSIGNED; }
222         | T_BOOL        { $$ = CDECL_TYPE_BOOL;     }
223         | T_COMPLEX     { $$ = CDECL_TYPE_COMPLEX;  }
224
225 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST;    }
226         | T_RESTRICT      { $$ = CDECL_QUAL_RESTRICT; }
227         | T_VOLATILE      { $$ = CDECL_QUAL_VOLATILE; }
228
229 declspec_notype: qualifier | declspec_simple {
230         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
231 }
232
233 typespec_noid: typespec_simple {
234         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
235 }
236
237 qualifier: qualifier_simple {
238         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
239 }
240
241 typespec: typespec_noid | T_STRUCT T_IDENT {
242         ALLOC_STRUCT($$, struct cdecl_declspec,
243                 .type = CDECL_TYPE_STRUCT,
244                 .ident = $2);
245 } | T_UNION T_IDENT {
246         ALLOC_STRUCT($$, struct cdecl_declspec,
247                 .type = CDECL_TYPE_UNION,
248                 .ident = $2);
249 } | T_ENUM T_IDENT {
250         ALLOC_STRUCT($$, struct cdecl_declspec,
251                 .type = CDECL_TYPE_ENUM,
252                 .ident = $2);
253 } | T_IDENT {
254         ALLOC_STRUCT($$, struct cdecl_declspec,
255                 .type = CDECL_TYPE_IDENT,
256                 .ident = $1);
257 }
258
259 declspec_noid: declspec_notype | typespec_noid
260
261 vla_ident: T_IDENT | T_ASTERISK {
262         ALLOC($$, sizeof "");
263         strcpy($$, "");
264 }
265
266 array: T_LBRACKET T_UINT T_RBRACKET {
267         if ($2 == 0)
268                 FAIL("array length must be positive");
269
270         ALLOC_STRUCT($$, struct cdecl_declarator,
271                 .type = CDECL_DECL_ARRAY,
272                 .u.array.length = $2);
273 } | T_LBRACKET vla_ident T_RBRACKET {
274         ALLOC_STRUCT($$, struct cdecl_declarator,
275                 .type = CDECL_DECL_ARRAY,
276                 .u.array.vla = $2);
277 } | T_LBRACKET T_RBRACKET {
278         ALLOC_STRUCT($$, struct cdecl_declarator,
279                 .type = CDECL_DECL_ARRAY);
280 }
281
282 pointer: T_ASTERISK qualifiers direct_declarator {
283         ALLOC_STRUCT($$, struct cdecl_declarator,
284                 .type = CDECL_DECL_POINTER,
285                 .u.pointer.qualifiers = $2,
286                 .child = $3);
287 } | T_ASTERISK qualifiers pointer {
288         ALLOC_STRUCT($$, struct cdecl_declarator,
289                 .type = CDECL_DECL_POINTER,
290                 .u.pointer.qualifiers = $2,
291                 .child = $3);
292 }
293
294 declarator: direct_declarator | pointer
295
296 direct_declarator: {
297         ALLOC_STRUCT($$, struct cdecl_declarator,
298                 .type = CDECL_DECL_NULL);
299 } | T_IDENT {
300         ALLOC_STRUCT($$, struct cdecl_declarator,
301                 .type = CDECL_DECL_IDENT,
302                 .u.ident = $1);
303 } | direct_declarator array {
304         $$ = $2;
305         $$->child = $1;
306 } | T_LPAREN declarator T_RPAREN {
307         $$ = $2;
308 };
309
310 %%
311 void yyerror(YYLTYPE *loc, struct cdecl **out, const char *err)
312 {
313         if (strstr(err, "T_LEX_ERROR"))
314                 return;
315
316         fprintf(stderr, "%s\n", err);
317 }