-
Notifications
You must be signed in to change notification settings - Fork 2
/
pp_expr_parser.yy
364 lines (305 loc) · 7.95 KB
/
pp_expr_parser.yy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
* Copyright (C) 2019 SUSE Software Solutions Germany GmbH
*
* This file is part of klp-ccp.
*
* klp-ccp is free software: you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* klp-ccp is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with klp-ccp. If not, see <https://www.gnu.org/licenses/>.
*/
%language "C++"
%skeleton "lalr1.cc"
%defines
%define api.namespace {klp::ccp::yy}
%define parser_class_name {pp_expr_parser}
%name-prefix "pd."
%define api.location.type {pp_tokens_range}
%locations
%code requires {
#ifdef DEBUG_PARSER
#define YYDEBUG 1
#endif
#include "ast.hh"
namespace klp {
namespace ccp
{
namespace yy
{
// Bison doesn't accept namespace specifications at
// %define api.location.type. Pull the type in.
typedef klp::ccp::pp_tokens_range pp_tokens_range;
class pp_expr_parser_driver;
}
}
}
}
%code {
using namespace klp::ccp;
using namespace klp::ccp::ast;
#define YYLLOC_DEFAULT(Cur, Rhs, N) \
do { \
if (N) { \
Cur = pp_tokens_range{YYRHSLOC(Rhs, 1).begin, \
YYRHSLOC(Rhs, N).end}; \
} else { \
Cur = YYRHSLOC(Rhs, 0); \
} \
} while (0)
#include "pp_expr_parser_driver.hh"
template<typename T>
static T* mv_p(T* &&p)
{
T *_p = nullptr;
std::swap(p, _p);
return _p;
}
#define MV_P(p) mv_p(std::move(p))
}
%parse-param {klp::ccp::yy::pp_expr_parser_driver &pd}
%union {
klp::ccp::pp_token_index token_index;
klp::ccp::ast::binary_op binary_op;
klp::ccp::ast::unary_op_pre unary_op_pre;
klp::ccp::ast::expr *expr;
}
%destructor {} <token_index>
%destructor {} <binary_op>
%destructor {} <unary_op_pre>
%destructor { delete $$; } <*>
/* Preprocessor tokens. */
%token <token_index> TOK_STRING_LITERAL
%token <token_index> TOK_CONSTANT
/* Punctuators which aren't necessarily operators. */
%token TOK_LPAREN
%token TOK_RPAREN
%token TOK_LBRACE
%token TOK_RBRACE
%token TOK_LBRACKET
%token TOK_RBRACKET
%token TOK_TRIPLE_DOT
%token TOK_DOT
%token TOK_COLON
%token TOK_SEMICOLON
%token TOK_COMMA
%token TOK_ASTERISK
%token TOK_EQ
/* Punctuators which are always operators. */
%token TOK_OP_ASSIGN_MUL
%token TOK_OP_ASSIGN_DIV
%token TOK_OP_ASSIGN_MOD
%token TOK_OP_ASSIGN_ADD
%token TOK_OP_ASSIGN_SUB
%token TOK_OP_ASSIGN_LSHIFT
%token TOK_OP_ASSIGN_RSHIFT
%token TOK_OP_ASSIGN_BIN_AND
%token TOK_OP_ASSIGN_BIN_XOR
%token TOK_OP_ASSIGN_BIN_OR
%token TOK_OP_LOGICAL_OR
%token TOK_OP_DOUBLE_AMPERSAND
%token TOK_OP_QUESTION_MARK
%token TOK_OP_DEREF_MEMBER
%token TOK_OP_INC
%token TOK_OP_DEC
%token TOK_OP_BIN_OR
%token TOK_OP_BIN_XOR
%token TOK_OP_REL_LT
%token TOK_OP_REL_GT
%token TOK_OP_REL_LEQ
%token TOK_OP_REL_GEQ
%token TOK_OP_EQ
%token TOK_OP_NEQ
%token TOK_OP_PLUS
%token TOK_OP_MINUS
%token TOK_OP_AMPERSAND
%token TOK_OP_DIV
%token TOK_OP_MODULO
%token TOK_OP_BIN_NEG
%token TOK_OP_LOGICAL_NOT
%token TOK_OP_LSHIFT
%token TOK_OP_RSHIFT
%type <expr> pp_expr
%type <expr> conditional_expression
%type <expr> logical_or_expression
%type <expr> logical_and_expression
%type <expr> inclusive_or_expression
%type <expr> exclusive_or_expression
%type <expr> and_expression
%type <expr> equality_expression
%type <binary_op> equality_operator
%type <expr> relational_expression
%type <binary_op> relational_operator
%type <expr> shift_expression
%type <binary_op> shift_operator
%type <expr> additive_expression
%type <expr> multiplicative_expression
%type <expr> unary_expression
%type <unary_op_pre> unary_operator
%type <expr> primary_expression
%%
start:
pp_expr
{ pd._result = MV_P($1); }
;
pp_expr:
conditional_expression
{ $$ = MV_P($1); }
| pp_expr TOK_COMMA conditional_expression
{ $$ = new expr_comma(std::move($1), std::move($3)); }
;
conditional_expression:
logical_or_expression
{ $$ = MV_P($1); }
| logical_or_expression TOK_OP_QUESTION_MARK pp_expr TOK_COLON conditional_expression
{
$$ = new expr_conditional(std::move($1), std::move($3),
std::move($5));
}
| logical_or_expression TOK_OP_QUESTION_MARK TOK_COLON conditional_expression
{ $$ = new expr_conditional(std::move($1), std::move($4)); }
;
logical_or_expression:
logical_and_expression
{ $$ = MV_P($1); }
| logical_or_expression TOK_OP_LOGICAL_OR logical_and_expression
{
$$ = new expr_binop(binary_op::logical_or,
std::move($1), std::move($3));
}
;
logical_and_expression:
inclusive_or_expression
{ $$ = MV_P($1); }
| logical_and_expression TOK_OP_DOUBLE_AMPERSAND inclusive_or_expression
{
$$ = new expr_binop(binary_op::logical_and,
std::move($1), std::move($3));
}
;
inclusive_or_expression:
exclusive_or_expression
{ $$ = MV_P($1); }
| inclusive_or_expression TOK_OP_BIN_OR exclusive_or_expression
{
$$ = new expr_binop(binary_op::bin_or,
std::move($1), std::move($3));
}
;
exclusive_or_expression:
and_expression
{ $$ = MV_P($1); }
| exclusive_or_expression TOK_OP_BIN_XOR and_expression
{
$$ = new expr_binop(binary_op::bin_xor,
std::move($1), std::move($3));
}
;
and_expression:
equality_expression
{ $$ = MV_P($1); }
| and_expression TOK_OP_AMPERSAND equality_expression
{
$$ = new expr_binop(binary_op::bin_and,
std::move($1), std::move($3));
}
;
equality_expression:
relational_expression
{ $$ = MV_P($1); }
| equality_expression equality_operator relational_expression
{ $$ = new expr_binop($2, std::move($1), std::move($3)); }
;
equality_operator:
TOK_OP_EQ { $$ = binary_op::eq; }
| TOK_OP_NEQ { $$ = binary_op::neq; }
;
relational_expression:
shift_expression
{ $$ = MV_P($1); }
| relational_expression relational_operator shift_expression
{ $$ = new expr_binop($2, std::move($1), std::move($3)); }
;
relational_operator:
TOK_OP_REL_LT { $$ = binary_op::lt; }
| TOK_OP_REL_GT { $$ = binary_op::gt; }
| TOK_OP_REL_LEQ { $$ = binary_op::leq; }
| TOK_OP_REL_GEQ { $$ = binary_op::geq; }
;
shift_expression:
additive_expression
{ $$ = MV_P($1); }
| shift_expression shift_operator additive_expression
{ $$ = new expr_binop($2, std::move($1), std::move($3)); }
;
shift_operator:
TOK_OP_LSHIFT { $$ = binary_op::lshift; }
| TOK_OP_RSHIFT { $$ = binary_op::rshift; }
;
additive_expression:
multiplicative_expression
{ $$ = MV_P($1); }
| additive_expression TOK_OP_PLUS multiplicative_expression
{
$$ = new expr_binop(binary_op::add,
std::move($1), std::move($3));
}
| additive_expression TOK_OP_MINUS multiplicative_expression
{
$$ = new expr_binop(binary_op::sub,
std::move($1), std::move($3));
}
;
multiplicative_expression:
unary_expression
{ $$ = MV_P($1); }
| multiplicative_expression TOK_ASTERISK unary_expression
{
$$ = new expr_binop(binary_op::mul,
std::move($1), std::move($3));
}
| multiplicative_expression TOK_OP_DIV unary_expression
{
$$ = new expr_binop(binary_op::div,
std::move($1), std::move($3));
}
| multiplicative_expression TOK_OP_MODULO unary_expression
{
$$ = new expr_binop(binary_op::mod,
std::move($1), std::move($3));
}
;
unary_expression:
primary_expression
{ $$ = MV_P($1); }
| unary_operator unary_expression
{ $$ = new expr_unop_pre(@$, $1, std::move($2)); }
;
unary_operator:
TOK_OP_PLUS
{ $$ = unary_op_pre::plus; }
| TOK_OP_MINUS
{ $$ = unary_op_pre::neg; }
| TOK_OP_BIN_NEG
{ $$ = unary_op_pre::bin_neg; }
| TOK_OP_LOGICAL_NOT
{ $$ = unary_op_pre::logical_not; }
;
primary_expression:
TOK_CONSTANT
{ $$ = new expr_constant($1); }
| TOK_LPAREN pp_expr TOK_RPAREN
{ $$ = new expr_parenthesized(@$, std::move($2)); }
;
%%
void klp::ccp::yy::pp_expr_parser::error(const location_type& loc,
const std::string& msg)
{
pd.error(loc, msg);
}