]> git.draconx.ca Git - cdecl99.git/blob - src/parse.y
Use assert(0) instead of abort for exceptional cases.
[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_IDENT:
83                         free(x->u.ident);
84                         break;
85                 case CDECL_DECL_POINTER:
86                         free_declspec(x->u.pointer.qualifiers);
87                         free_declarator(x->u.pointer.declarator);
88                         break;
89                 case CDECL_DECL_ARRAY:
90                         free(x->u.array.vla);
91                         free_declarator(x->u.array.declarator);
92                         break;
93                 default:
94                         assert(0);
95                 }
96                 free(x);
97                 x = p;
98         }
99 }
100
101 static void free_decl(struct cdecl *decl)
102 {
103         free_declspec(decl->specifiers);
104         free_declarator(decl->declarators);
105         free(decl);
106 }
107
108 void cdecl_free(struct cdecl *decl)
109 {
110         if (decl)
111                 free_decl(decl);
112 }
113 %}
114
115 %destructor { free($$); }            <strval>
116 %destructor { free_declspec($$); }   <declspec>
117 %destructor { free_declarator($$); } <declarator>
118 %destructor { free_decl($$); }       <decl>
119
120 %token T_LEX_ERROR
121
122 %token <strval> T_IDENT "identifier"
123 %token <uintval> T_UINT "integer constant"
124
125 %token T_SEMICOLON ";"
126 %token T_ASTERISK  "*"
127 %token T_LPAREN    "("
128 %token T_RPAREN    ")"
129 %token T_LBRACKET  "["
130 %token T_RBRACKET  "]"
131 %token T_COMMA     ","
132
133 %token T_TYPEDEF  "typedef"
134 %token T_EXTERN   "extern"
135 %token T_STATIC   "static"
136 %token T_AUTO     "auto"
137 %token T_REGISTER "register"
138
139 %token T_INLINE   "inline"
140
141 %token T_RESTRICT "restrict"
142 %token T_VOLATILE "volatile"
143 %token T_CONST    "const"
144
145 %token T_VOID     "void"
146 %token T_CHAR     "char"
147 %token T_SHORT    "short"
148 %token T_INT      "int"
149 %token T_LONG     "long"
150 %token T_FLOAT    "float"
151 %token T_DOUBLE   "double"
152 %token T_SIGNED   "signed"
153 %token T_UNSIGNED "unsigned"
154 %token T_BOOL     "_Bool"
155 %token T_COMPLEX  "_Complex"
156
157 %token T_STRUCT   "struct"
158 %token T_UNION    "union"
159 %token T_ENUM     "enum"
160
161 %type <strval>     vla_ident
162 %type <uintval>    declspec_simple typespec_simple qualifier_simple
163 %type <declspec>   declspec_notype declspec_noid typespec_noid typespec
164 %type <declspec>   qualifier qualifiers pointer
165 %type <declspec>   declspecs declspecs_noid
166 %type <declarator> direct_declarator declarator declarators array
167 %type <decl>       declaration
168
169 %%
170
171 input: declaration {
172         *out = $1;
173 };
174
175 declaration: declspecs declarators T_SEMICOLON {
176         ALLOC_STRUCT($$, struct cdecl,
177                 .specifiers = $1,
178                 .declarators = $2);
179 };
180
181 declspecs: declspec_notype declspecs {
182         $$ = $1;
183         $$->next = $2;
184 } | typespec declspecs_noid {
185         $$ = $1;
186         $$->next = $2;
187 }
188
189 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
190         $$ = $1;
191         $$->next = $2;
192 }
193
194 qualifiers: { $$ = NULL; } | qualifiers qualifier {
195         $$ = $2;
196         $$->next = $1;
197 }
198
199 declarators: declarator | declarator T_COMMA declarators {
200         $$ = $1;
201         $$->next = $3;
202 };
203
204 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO;     }
205         | T_TYPEDEF     { $$ = CDECL_STOR_TYPEDEF;  }
206         | T_EXTERN      { $$ = CDECL_STOR_EXTERN;   }
207         | T_STATIC      { $$ = CDECL_STOR_STATIC;   }
208         | T_REGISTER    { $$ = CDECL_STOR_REGISTER; }
209         | T_INLINE      { $$ = CDECL_FUNC_INLINE;   }
210
211 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID;     }
212         | T_CHAR        { $$ = CDECL_TYPE_CHAR;     }
213         | T_SHORT       { $$ = CDECL_TYPE_SHORT;    }
214         | T_INT         { $$ = CDECL_TYPE_INT;      }
215         | T_LONG        { $$ = CDECL_TYPE_LONG;     }
216         | T_FLOAT       { $$ = CDECL_TYPE_FLOAT;    }
217         | T_DOUBLE      { $$ = CDECL_TYPE_DOUBLE;   }
218         | T_SIGNED      { $$ = CDECL_TYPE_SIGNED;   }
219         | T_UNSIGNED    { $$ = CDECL_TYPE_UNSIGNED; }
220         | T_BOOL        { $$ = CDECL_TYPE_BOOL;     }
221         | T_COMPLEX     { $$ = CDECL_TYPE_COMPLEX;  }
222
223 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST;    }
224         | T_RESTRICT      { $$ = CDECL_QUAL_RESTRICT; }
225         | T_VOLATILE      { $$ = CDECL_QUAL_VOLATILE; }
226
227 declspec_notype: qualifier | declspec_simple {
228         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
229 }
230
231 typespec_noid: typespec_simple {
232         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
233 }
234
235 qualifier: qualifier_simple {
236         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
237 }
238
239 typespec: typespec_noid | T_STRUCT T_IDENT {
240         ALLOC_STRUCT($$, struct cdecl_declspec,
241                 .type = CDECL_TYPE_STRUCT,
242                 .ident = $2);
243 } | T_UNION T_IDENT {
244         ALLOC_STRUCT($$, struct cdecl_declspec,
245                 .type = CDECL_TYPE_UNION,
246                 .ident = $2);
247 } | T_ENUM T_IDENT {
248         ALLOC_STRUCT($$, struct cdecl_declspec,
249                 .type = CDECL_TYPE_ENUM,
250                 .ident = $2);
251 } | T_IDENT {
252         ALLOC_STRUCT($$, struct cdecl_declspec,
253                 .type = CDECL_TYPE_IDENT,
254                 .ident = $1);
255 }
256
257 declspec_noid: declspec_notype | typespec_noid
258
259 pointer: T_ASTERISK qualifiers { $$ = $2; }
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 declarator: direct_declarator | pointer direct_declarator {
283         ALLOC_STRUCT($$, struct cdecl_declarator,
284                 .type = CDECL_DECL_POINTER,
285                 .u.pointer.qualifiers = $1,
286                 .u.pointer.declarator = $2);
287 }
288
289 direct_declarator: T_IDENT {
290         ALLOC_STRUCT($$, struct cdecl_declarator,
291                 .type = CDECL_DECL_IDENT,
292                 .u.ident = $1);
293 } | direct_declarator array {
294         $$ = $2;
295         $$->u.array.declarator = $1;
296 } | T_LPAREN declarator T_RPAREN {
297         $$ = $2;
298 };
299
300 %%
301 void yyerror(YYLTYPE *loc, struct cdecl **out, const char *err)
302 {
303         if (strstr(err, "T_LEX_ERROR"))
304                 return;
305
306         fprintf(stderr, "%s\n", err);
307 }