]> git.draconx.ca Git - cdecl99.git/blob - src/parse.y
Add support for array declarators.
[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 "scan.h"
26 #include "cdecl.h"
27
28 #define FAIL(msg) do { \
29         yyerror(&yylloc, NULL, msg); \
30         YYERROR; \
31 } while (0)
32
33 #define ALLOC(ptr, size) do { \
34         (ptr) = malloc(size); \
35         if (!(ptr)) \
36                 FAIL("failed to allocate memory"); \
37 } while (0)
38
39 #define ALLOC_STRUCT(ptr, type, ...) do { \
40         ALLOC(ptr, sizeof (type)); \
41         *(ptr) = (type) { __VA_ARGS__ }; \
42 } while (0)
43 %}
44
45 %code requires {
46 #include <inttypes.h>
47 }
48
49 %code provides {
50 void yyerror(YYLTYPE *, struct cdecl **, const char *);
51 int yyparse(struct cdecl **out);
52 }
53
54 %union {
55         uintmax_t uintval;
56         char *strval;
57         struct cdecl_declspec *declspec;
58         struct cdecl_declarator *declarator;
59         struct cdecl *decl;
60 }
61
62 %{
63 static void free_declspec(struct cdecl_declspec *x)
64 {
65         struct cdecl_declspec *p;
66         while (x) {
67                 p = x->next;
68                 free(x->ident);
69                 free(x);
70                 x = p;
71         }
72 }
73
74 static void free_declarator(struct cdecl_declarator *x)
75 {
76         struct cdecl_declarator *p;
77         while (x) {
78                 p = x->next;
79                 switch (x->type) {
80                 case CDECL_DECL_IDENT:
81                         free(x->u.ident);
82                         break;
83                 case CDECL_DECL_POINTER:
84                         free_declspec(x->u.pointer.qualifiers);
85                         free_declarator(x->u.pointer.declarator);
86                         break;
87                 case CDECL_DECL_ARRAY:
88                         free(x->u.array.vla);
89                         free_declarator(x->u.array.declarator);
90                         break;
91                 default:
92                         abort();
93                 }
94                 free(x);
95                 x = p;
96         }
97 }
98
99 static void free_decl(struct cdecl *decl)
100 {
101         free_declspec(decl->specifiers);
102         free_declarator(decl->declarators);
103         free(decl);
104 }
105
106 void cdecl_free(struct cdecl *decl)
107 {
108         if (decl)
109                 free_decl(decl);
110 }
111 %}
112
113 %destructor { free($$); }            <strval>
114 %destructor { free_declspec($$); }   <declspec>
115 %destructor { free_declarator($$); } <declarator>
116 %destructor { free_decl($$); }       <decl>
117
118 %token T_LEX_ERROR
119
120 %token <strval> T_IDENT "identifier"
121 %token <uintval> T_UINT "integer constant"
122
123 %token T_SEMICOLON ";"
124 %token T_ASTERISK  "*"
125 %token T_LPAREN    "("
126 %token T_RPAREN    ")"
127 %token T_LBRACKET  "["
128 %token T_RBRACKET  "]"
129 %token T_COMMA     ","
130
131 %token T_TYPEDEF  "typedef"
132 %token T_EXTERN   "extern"
133 %token T_STATIC   "static"
134 %token T_AUTO     "auto"
135 %token T_REGISTER "register"
136
137 %token T_INLINE   "inline"
138
139 %token T_RESTRICT "restrict"
140 %token T_VOLATILE "volatile"
141 %token T_CONST    "const"
142
143 %token T_VOID     "void"
144 %token T_CHAR     "char"
145 %token T_SHORT    "short"
146 %token T_INT      "int"
147 %token T_LONG     "long"
148 %token T_FLOAT    "float"
149 %token T_DOUBLE   "double"
150 %token T_SIGNED   "signed"
151 %token T_UNSIGNED "unsigned"
152 %token T_BOOL     "_Bool"
153 %token T_COMPLEX  "_Complex"
154
155 %token T_STRUCT   "struct"
156 %token T_UNION    "union"
157 %token T_ENUM     "enum"
158
159 %type <strval>     vla_ident
160 %type <uintval>    declspec_simple typespec_simple qualifier_simple
161 %type <declspec>   declspec_notype declspec_noid typespec_noid typespec
162 %type <declspec>   qualifier qualifiers pointer
163 %type <declspec>   declspecs declspecs_noid
164 %type <declarator> direct_declarator declarator declarators array
165 %type <decl>       declaration
166
167 %%
168
169 input: declaration {
170         *out = $1;
171 };
172
173 declaration: declspecs declarators T_SEMICOLON {
174         ALLOC_STRUCT($$, struct cdecl,
175                 .specifiers = $1,
176                 .declarators = $2);
177 };
178
179 declspecs: declspec_notype declspecs {
180         $$ = $1;
181         $$->next = $2;
182 } | typespec declspecs_noid {
183         $$ = $1;
184         $$->next = $2;
185 }
186
187 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
188         $$ = $1;
189         $$->next = $2;
190 }
191
192 qualifiers: { $$ = NULL; } | qualifiers qualifier {
193         $$ = $2;
194         $$->next = $1;
195 }
196
197 declarators: declarator | declarator T_COMMA declarators {
198         $$ = $1;
199         $$->next = $3;
200 };
201
202 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO;     }
203         | T_TYPEDEF     { $$ = CDECL_STOR_TYPEDEF;  }
204         | T_EXTERN      { $$ = CDECL_STOR_EXTERN;   }
205         | T_STATIC      { $$ = CDECL_STOR_STATIC;   }
206         | T_REGISTER    { $$ = CDECL_STOR_REGISTER; }
207         | T_INLINE      { $$ = CDECL_FUNC_INLINE;   }
208
209 typespec_simple: T_VOID { $$ = CDECL_TYPE_VOID;     }
210         | T_CHAR        { $$ = CDECL_TYPE_CHAR;     }
211         | T_SHORT       { $$ = CDECL_TYPE_SHORT;    }
212         | T_INT         { $$ = CDECL_TYPE_INT;      }
213         | T_LONG        { $$ = CDECL_TYPE_LONG;     }
214         | T_FLOAT       { $$ = CDECL_TYPE_FLOAT;    }
215         | T_DOUBLE      { $$ = CDECL_TYPE_DOUBLE;   }
216         | T_SIGNED      { $$ = CDECL_TYPE_SIGNED;   }
217         | T_UNSIGNED    { $$ = CDECL_TYPE_UNSIGNED; }
218         | T_BOOL        { $$ = CDECL_TYPE_BOOL;     }
219         | T_COMPLEX     { $$ = CDECL_TYPE_COMPLEX;  }
220
221 qualifier_simple: T_CONST { $$ = CDECL_QUAL_CONST;    }
222         | T_RESTRICT      { $$ = CDECL_QUAL_RESTRICT; }
223         | T_VOLATILE      { $$ = CDECL_QUAL_VOLATILE; }
224
225 declspec_notype: qualifier | declspec_simple {
226         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
227 }
228
229 typespec_noid: typespec_simple {
230         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
231 }
232
233 qualifier: qualifier_simple {
234         ALLOC_STRUCT($$, struct cdecl_declspec, .type = $1);
235 }
236
237 typespec: typespec_noid | T_STRUCT T_IDENT {
238         ALLOC_STRUCT($$, struct cdecl_declspec,
239                 .type = CDECL_TYPE_STRUCT,
240                 .ident = $2);
241 } | T_UNION T_IDENT {
242         ALLOC_STRUCT($$, struct cdecl_declspec,
243                 .type = CDECL_TYPE_UNION,
244                 .ident = $2);
245 } | T_ENUM T_IDENT {
246         ALLOC_STRUCT($$, struct cdecl_declspec,
247                 .type = CDECL_TYPE_ENUM,
248                 .ident = $2);
249 } | T_IDENT {
250         ALLOC_STRUCT($$, struct cdecl_declspec,
251                 .type = CDECL_TYPE_IDENT,
252                 .ident = $1);
253 }
254
255 declspec_noid: declspec_notype | typespec_noid
256
257 pointer: T_ASTERISK qualifiers { $$ = $2; }
258
259 vla_ident: T_IDENT | T_ASTERISK {
260         ALLOC($$, sizeof "");
261         strcpy($$, "");
262 }
263
264 array: T_LBRACKET T_UINT T_RBRACKET {
265         if ($2 == 0)
266                 FAIL("array length must be positive");
267
268         ALLOC_STRUCT($$, struct cdecl_declarator,
269                 .type = CDECL_DECL_ARRAY,
270                 .u.array.length = $2);
271 } | T_LBRACKET vla_ident T_RBRACKET {
272         ALLOC_STRUCT($$, struct cdecl_declarator,
273                 .type = CDECL_DECL_ARRAY,
274                 .u.array.vla = $2);
275 } | T_LBRACKET T_RBRACKET {
276         ALLOC_STRUCT($$, struct cdecl_declarator,
277                 .type = CDECL_DECL_ARRAY);
278 }
279
280 declarator: direct_declarator | pointer direct_declarator {
281         ALLOC_STRUCT($$, struct cdecl_declarator,
282                 .type = CDECL_DECL_POINTER,
283                 .u.pointer.qualifiers = $1,
284                 .u.pointer.declarator = $2);
285 }
286
287 direct_declarator: T_IDENT {
288         ALLOC_STRUCT($$, struct cdecl_declarator,
289                 .type = CDECL_DECL_IDENT,
290                 .u.ident = $1);
291 } | direct_declarator array {
292         $$ = $2;
293         $$->u.array.declarator = $1;
294 } | T_LPAREN declarator T_RPAREN {
295         $$ = $2;
296 };
297
298 %%
299 void yyerror(YYLTYPE *loc, struct cdecl **out, const char *err)
300 {
301         if (strstr(err, "T_LEX_ERROR"))
302                 return;
303
304         fprintf(stderr, "%s\n", err);
305 }