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