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