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