Commit | Line | Data |
---|---|---|
953192ba MD |
1 | %{ |
2 | /* | |
3 | * filter-parser.y | |
4 | * | |
5 | * LTTng filter expression parser | |
6 | * | |
7 | * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
8 | * | |
9 | * This library is free software; you can redistribute it and/or modify it | |
10 | * under the terms of the GNU Lesser General Public License, version 2.1 only, | |
11 | * as published by the Free Software Foundation. | |
12 | * | |
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public License | |
19 | * along with this library; if not, write to the Free Software Foundation, | |
20 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | * | |
22 | * Grammar inspired from http://www.quut.com/c/ANSI-C-grammar-y.html | |
23 | */ | |
24 | ||
25 | #include <stdio.h> | |
26 | #include <unistd.h> | |
27 | #include <string.h> | |
28 | #include <stdlib.h> | |
29 | #include <assert.h> | |
30 | #include <errno.h> | |
31 | #include <inttypes.h> | |
953192ba | 32 | #include "filter-ast.h" |
95b9bd90 | 33 | #include "filter-parser.h" |
953192ba | 34 | |
a187da1a DG |
35 | #include <common/macros.h> |
36 | ||
8ab7c0d9 MD |
37 | #define WIDTH_u64_SCANF_IS_A_BROKEN_API "20" |
38 | #define WIDTH_o64_SCANF_IS_A_BROKEN_API "22" | |
39 | #define WIDTH_x64_SCANF_IS_A_BROKEN_API "17" | |
40 | #define WIDTH_lg_SCANF_IS_A_BROKEN_API "4096" /* Hugely optimistic approximation */ | |
41 | ||
a187da1a | 42 | LTTNG_HIDDEN |
953192ba | 43 | int yydebug; |
a187da1a | 44 | LTTNG_HIDDEN |
953192ba MD |
45 | int filter_parser_debug = 0; |
46 | ||
a187da1a | 47 | LTTNG_HIDDEN |
9039edd4 | 48 | int yyparse(struct filter_parser_ctx *parser_ctx, yyscan_t scanner); |
a187da1a | 49 | LTTNG_HIDDEN |
9039edd4 | 50 | int yylex(union YYSTYPE *yyval, yyscan_t scanner); |
a187da1a | 51 | LTTNG_HIDDEN |
953192ba | 52 | int yylex_init_extra(struct filter_parser_ctx *parser_ctx, yyscan_t * ptr_yy_globals); |
a187da1a | 53 | LTTNG_HIDDEN |
953192ba | 54 | int yylex_destroy(yyscan_t yyparser_ctx); |
a187da1a | 55 | LTTNG_HIDDEN |
953192ba MD |
56 | void yyrestart(FILE * in_str, yyscan_t parser_ctx); |
57 | ||
58 | struct gc_string { | |
59 | struct cds_list_head gc; | |
60 | size_t alloclen; | |
61 | char s[]; | |
62 | }; | |
63 | ||
64 | static const char *node_type_to_str[] = { | |
65 | [ NODE_UNKNOWN ] = "NODE_UNKNOWN", | |
66 | [ NODE_ROOT ] = "NODE_ROOT", | |
67 | [ NODE_EXPRESSION ] = "NODE_EXPRESSION", | |
68 | [ NODE_OP ] = "NODE_OP", | |
69 | [ NODE_UNARY_OP ] = "NODE_UNARY_OP", | |
70 | }; | |
71 | ||
a187da1a | 72 | LTTNG_HIDDEN |
953192ba MD |
73 | const char *node_type(struct filter_node *node) |
74 | { | |
75 | if (node->type < NR_NODE_TYPES) | |
76 | return node_type_to_str[node->type]; | |
77 | else | |
78 | return NULL; | |
79 | } | |
80 | ||
81 | static struct gc_string *gc_string_alloc(struct filter_parser_ctx *parser_ctx, | |
82 | size_t len) | |
83 | { | |
84 | struct gc_string *gstr; | |
85 | size_t alloclen; | |
86 | ||
87 | /* TODO: could be faster with find first bit or glib Gstring */ | |
88 | /* sizeof long to account for malloc header (int or long ?) */ | |
89 | for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + len; | |
90 | alloclen *= 2); | |
91 | ||
92 | gstr = malloc(alloclen); | |
93 | cds_list_add(&gstr->gc, &parser_ctx->allocated_strings); | |
94 | gstr->alloclen = alloclen; | |
95 | return gstr; | |
96 | } | |
97 | ||
98 | /* | |
99 | * note: never use gc_string_append on a string that has external references. | |
100 | * gsrc will be garbage collected immediately, and gstr might be. | |
101 | * Should only be used to append characters to a string literal or constant. | |
102 | */ | |
a187da1a | 103 | LTTNG_HIDDEN |
953192ba MD |
104 | struct gc_string *gc_string_append(struct filter_parser_ctx *parser_ctx, |
105 | struct gc_string *gstr, | |
106 | struct gc_string *gsrc) | |
107 | { | |
108 | size_t newlen = strlen(gsrc->s) + strlen(gstr->s) + 1; | |
109 | size_t alloclen; | |
110 | ||
111 | /* TODO: could be faster with find first bit or glib Gstring */ | |
112 | /* sizeof long to account for malloc header (int or long ?) */ | |
113 | for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + newlen; | |
114 | alloclen *= 2); | |
115 | ||
116 | if (alloclen > gstr->alloclen) { | |
117 | struct gc_string *newgstr; | |
118 | ||
119 | newgstr = gc_string_alloc(parser_ctx, newlen); | |
120 | strcpy(newgstr->s, gstr->s); | |
121 | strcat(newgstr->s, gsrc->s); | |
122 | cds_list_del(&gstr->gc); | |
123 | free(gstr); | |
124 | gstr = newgstr; | |
125 | } else { | |
126 | strcat(gstr->s, gsrc->s); | |
127 | } | |
128 | cds_list_del(&gsrc->gc); | |
129 | free(gsrc); | |
130 | return gstr; | |
131 | } | |
132 | ||
a187da1a | 133 | LTTNG_HIDDEN |
953192ba MD |
134 | void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src) |
135 | { | |
136 | lvalp->gs = gc_string_alloc(parser_ctx, strlen(src) + 1); | |
137 | strcpy(lvalp->gs->s, src); | |
138 | } | |
139 | ||
140 | static struct filter_node *make_node(struct filter_parser_ctx *scanner, | |
141 | enum node_type type) | |
142 | { | |
143 | struct filter_ast *ast = filter_parser_get_ast(scanner); | |
144 | struct filter_node *node; | |
145 | ||
146 | node = malloc(sizeof(*node)); | |
147 | if (!node) | |
148 | return NULL; | |
149 | memset(node, 0, sizeof(*node)); | |
150 | node->type = type; | |
151 | cds_list_add(&node->gc, &ast->allocated_nodes); | |
152 | ||
153 | switch (type) { | |
154 | case NODE_ROOT: | |
155 | fprintf(stderr, "[error] %s: trying to create root node\n", __func__); | |
156 | break; | |
157 | ||
158 | case NODE_EXPRESSION: | |
159 | break; | |
160 | case NODE_OP: | |
161 | break; | |
162 | case NODE_UNARY_OP: | |
163 | break; | |
164 | ||
165 | case NODE_UNKNOWN: | |
166 | default: | |
167 | fprintf(stderr, "[error] %s: unknown node type %d\n", __func__, | |
168 | (int) type); | |
169 | break; | |
170 | } | |
171 | ||
172 | return node; | |
173 | } | |
174 | ||
175 | static struct filter_node *make_op_node(struct filter_parser_ctx *scanner, | |
176 | enum op_type type, | |
177 | struct filter_node *lchild, | |
178 | struct filter_node *rchild) | |
179 | { | |
180 | struct filter_ast *ast = filter_parser_get_ast(scanner); | |
181 | struct filter_node *node; | |
182 | ||
183 | node = malloc(sizeof(*node)); | |
184 | if (!node) | |
185 | return NULL; | |
186 | memset(node, 0, sizeof(*node)); | |
187 | node->type = NODE_OP; | |
188 | cds_list_add(&node->gc, &ast->allocated_nodes); | |
189 | node->u.op.type = type; | |
190 | node->u.op.lchild = lchild; | |
191 | node->u.op.rchild = rchild; | |
192 | return node; | |
193 | } | |
194 | ||
a187da1a | 195 | LTTNG_HIDDEN |
9039edd4 | 196 | void yyerror(struct filter_parser_ctx *parser_ctx, yyscan_t scanner, const char *str) |
953192ba MD |
197 | { |
198 | fprintf(stderr, "error %s\n", str); | |
199 | } | |
200 | ||
a187da1a | 201 | LTTNG_HIDDEN |
953192ba MD |
202 | int yywrap(void) |
203 | { | |
204 | return 1; | |
205 | } | |
206 | ||
207 | #define parse_error(parser_ctx, str) \ | |
208 | do { \ | |
9039edd4 | 209 | yyerror(parser_ctx, parser_ctx->scanner, YY_("parse error: " str "\n")); \ |
953192ba MD |
210 | YYERROR; \ |
211 | } while (0) | |
212 | ||
213 | static void free_strings(struct cds_list_head *list) | |
214 | { | |
215 | struct gc_string *gstr, *tmp; | |
216 | ||
217 | cds_list_for_each_entry_safe(gstr, tmp, list, gc) | |
218 | free(gstr); | |
219 | } | |
220 | ||
221 | static struct filter_ast *filter_ast_alloc(void) | |
222 | { | |
223 | struct filter_ast *ast; | |
224 | ||
225 | ast = malloc(sizeof(*ast)); | |
226 | if (!ast) | |
227 | return NULL; | |
228 | memset(ast, 0, sizeof(*ast)); | |
229 | CDS_INIT_LIST_HEAD(&ast->allocated_nodes); | |
230 | ast->root.type = NODE_ROOT; | |
231 | return ast; | |
232 | } | |
233 | ||
234 | static void filter_ast_free(struct filter_ast *ast) | |
235 | { | |
236 | struct filter_node *node, *tmp; | |
237 | ||
238 | cds_list_for_each_entry_safe(node, tmp, &ast->allocated_nodes, gc) | |
239 | free(node); | |
37600d79 | 240 | free(ast); |
953192ba MD |
241 | } |
242 | ||
a187da1a | 243 | LTTNG_HIDDEN |
953192ba MD |
244 | int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx) |
245 | { | |
9039edd4 | 246 | return yyparse(parser_ctx, parser_ctx->scanner); |
953192ba MD |
247 | } |
248 | ||
a187da1a | 249 | LTTNG_HIDDEN |
953192ba MD |
250 | struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input) |
251 | { | |
252 | struct filter_parser_ctx *parser_ctx; | |
253 | int ret; | |
254 | ||
255 | yydebug = filter_parser_debug; | |
256 | ||
257 | parser_ctx = malloc(sizeof(*parser_ctx)); | |
258 | if (!parser_ctx) | |
259 | return NULL; | |
260 | memset(parser_ctx, 0, sizeof(*parser_ctx)); | |
261 | ||
262 | ret = yylex_init_extra(parser_ctx, &parser_ctx->scanner); | |
263 | if (ret) { | |
264 | fprintf(stderr, "yylex_init error\n"); | |
265 | goto cleanup_parser_ctx; | |
266 | } | |
267 | /* Start processing new stream */ | |
268 | yyrestart(input, parser_ctx->scanner); | |
269 | ||
270 | parser_ctx->ast = filter_ast_alloc(); | |
271 | if (!parser_ctx->ast) | |
272 | goto cleanup_lexer; | |
273 | CDS_INIT_LIST_HEAD(&parser_ctx->allocated_strings); | |
274 | ||
275 | if (yydebug) | |
276 | fprintf(stdout, "parser_ctx input is a%s.\n", | |
277 | isatty(fileno(input)) ? "n interactive tty" : | |
278 | " noninteractive file"); | |
279 | ||
280 | return parser_ctx; | |
281 | ||
282 | cleanup_lexer: | |
283 | ret = yylex_destroy(parser_ctx->scanner); | |
284 | if (!ret) | |
285 | fprintf(stderr, "yylex_destroy error\n"); | |
286 | cleanup_parser_ctx: | |
287 | free(parser_ctx); | |
288 | return NULL; | |
289 | } | |
290 | ||
a187da1a | 291 | LTTNG_HIDDEN |
953192ba MD |
292 | void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx) |
293 | { | |
294 | int ret; | |
295 | ||
296 | free_strings(&parser_ctx->allocated_strings); | |
297 | filter_ast_free(parser_ctx->ast); | |
298 | ret = yylex_destroy(parser_ctx->scanner); | |
299 | if (ret) | |
300 | fprintf(stderr, "yylex_destroy error\n"); | |
301 | free(parser_ctx); | |
302 | } | |
303 | ||
304 | %} | |
305 | ||
306 | %define api.pure | |
307 | /* %locations */ | |
308 | %parse-param {struct filter_parser_ctx *parser_ctx} | |
9039edd4 ZT |
309 | %parse-param {yyscan_t scanner} |
310 | %lex-param {yyscan_t scanner} | |
953192ba MD |
311 | %start translation_unit |
312 | %token CHARACTER_CONSTANT_START SQUOTE STRING_LITERAL_START DQUOTE | |
313 | %token ESCSEQ CHAR_STRING_TOKEN | |
e90d8561 | 314 | %token DECIMAL_CONSTANT OCTAL_CONSTANT HEXADECIMAL_CONSTANT FLOAT_CONSTANT |
953192ba MD |
315 | %token LSBRAC RSBRAC LPAREN RPAREN LBRAC RBRAC RARROW |
316 | %token STAR PLUS MINUS | |
317 | %token MOD_OP DIV_OP RIGHT_OP LEFT_OP | |
318 | %token EQ_OP NE_OP LE_OP GE_OP LT_OP GT_OP AND_OP OR_OP NOT_OP | |
319 | %token ASSIGN COLON SEMICOLON DOTDOTDOT DOT EQUAL COMMA | |
320 | %token XOR_BIN AND_BIN OR_BIN NOT_BIN | |
321 | ||
586dc72f | 322 | %token <gs> IDENTIFIER GLOBAL_IDENTIFIER |
953192ba MD |
323 | %token ERROR |
324 | %union | |
325 | { | |
326 | long long ll; | |
327 | char c; | |
328 | struct gc_string *gs; | |
329 | struct filter_node *n; | |
330 | } | |
331 | ||
332 | %type <gs> s_char s_char_sequence c_char c_char_sequence | |
333 | ||
334 | %type <n> primary_expression | |
335 | %type <n> postfix_expression | |
336 | %type <n> unary_expression | |
337 | %type <n> unary_operator | |
338 | %type <n> multiplicative_expression | |
339 | %type <n> additive_expression | |
340 | %type <n> shift_expression | |
341 | %type <n> relational_expression | |
342 | %type <n> equality_expression | |
343 | %type <n> and_expression | |
344 | %type <n> exclusive_or_expression | |
345 | %type <n> inclusive_or_expression | |
346 | %type <n> logical_and_expression | |
347 | %type <n> logical_or_expression | |
348 | %type <n> expression | |
349 | ||
350 | %% | |
351 | ||
352 | ||
353 | /* 1.5 Constants */ | |
354 | ||
355 | c_char_sequence: | |
356 | c_char | |
357 | { $$ = $1; } | |
358 | | c_char_sequence c_char | |
359 | { $$ = gc_string_append(parser_ctx, $1, $2); } | |
360 | ; | |
361 | ||
362 | c_char: | |
363 | CHAR_STRING_TOKEN | |
364 | { $$ = yylval.gs; } | |
365 | | ESCSEQ | |
366 | { | |
367 | parse_error(parser_ctx, "escape sequences not supported yet"); | |
368 | } | |
369 | ; | |
370 | ||
371 | /* 1.6 String literals */ | |
372 | ||
373 | s_char_sequence: | |
374 | s_char | |
375 | { $$ = $1; } | |
376 | | s_char_sequence s_char | |
377 | { $$ = gc_string_append(parser_ctx, $1, $2); } | |
378 | ; | |
379 | ||
380 | s_char: | |
381 | CHAR_STRING_TOKEN | |
382 | { $$ = yylval.gs; } | |
383 | | ESCSEQ | |
384 | { | |
385 | parse_error(parser_ctx, "escape sequences not supported yet"); | |
386 | } | |
387 | ; | |
388 | ||
389 | primary_expression | |
390 | : IDENTIFIER | |
391 | { | |
392 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
393 | $$->u.expression.type = AST_EXP_IDENTIFIER; | |
394 | $$->u.expression.u.identifier = yylval.gs->s; | |
395 | } | |
586dc72f MD |
396 | | GLOBAL_IDENTIFIER |
397 | { | |
398 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
399 | $$->u.expression.type = AST_EXP_GLOBAL_IDENTIFIER; | |
400 | $$->u.expression.u.identifier = yylval.gs->s; | |
401 | } | |
402 | ||
953192ba MD |
403 | | DECIMAL_CONSTANT |
404 | { | |
405 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
406 | $$->u.expression.type = AST_EXP_CONSTANT; | |
8ab7c0d9 MD |
407 | if (sscanf(yylval.gs->s, "%" WIDTH_u64_SCANF_IS_A_BROKEN_API SCNu64, |
408 | &$$->u.expression.u.constant) != 1) { | |
409 | parse_error(parser_ctx, "cannot scanf decimal constant"); | |
410 | } | |
953192ba MD |
411 | } |
412 | | OCTAL_CONSTANT | |
413 | { | |
414 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
415 | $$->u.expression.type = AST_EXP_CONSTANT; | |
8ab7c0d9 MD |
416 | if (!strcmp(yylval.gs->s, "0")) { |
417 | $$->u.expression.u.constant = 0; | |
418 | } else if (sscanf(yylval.gs->s, "0%" WIDTH_o64_SCANF_IS_A_BROKEN_API SCNo64, | |
419 | &$$->u.expression.u.constant) != 1) { | |
420 | parse_error(parser_ctx, "cannot scanf octal constant"); | |
421 | } | |
953192ba MD |
422 | } |
423 | | HEXADECIMAL_CONSTANT | |
424 | { | |
425 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
426 | $$->u.expression.type = AST_EXP_CONSTANT; | |
8ab7c0d9 MD |
427 | if (sscanf(yylval.gs->s, "0x%" WIDTH_x64_SCANF_IS_A_BROKEN_API SCNx64, |
428 | &$$->u.expression.u.constant) != 1) { | |
429 | parse_error(parser_ctx, "cannot scanf hexadecimal constant"); | |
430 | } | |
953192ba | 431 | } |
e90d8561 MD |
432 | | FLOAT_CONSTANT |
433 | { | |
434 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
435 | $$->u.expression.type = AST_EXP_FLOAT_CONSTANT; | |
8ab7c0d9 MD |
436 | if (sscanf(yylval.gs->s, "%" WIDTH_lg_SCANF_IS_A_BROKEN_API "lg", |
437 | &$$->u.expression.u.float_constant) != 1) { | |
438 | parse_error(parser_ctx, "cannot scanf float constant"); | |
439 | } | |
e90d8561 | 440 | } |
953192ba MD |
441 | | STRING_LITERAL_START DQUOTE |
442 | { | |
443 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
444 | $$->u.expression.type = AST_EXP_STRING; | |
445 | $$->u.expression.u.string = ""; | |
446 | } | |
447 | | STRING_LITERAL_START s_char_sequence DQUOTE | |
448 | { | |
449 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
450 | $$->u.expression.type = AST_EXP_STRING; | |
451 | $$->u.expression.u.string = $2->s; | |
452 | } | |
453 | | CHARACTER_CONSTANT_START c_char_sequence SQUOTE | |
454 | { | |
455 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
456 | $$->u.expression.type = AST_EXP_STRING; | |
457 | $$->u.expression.u.string = $2->s; | |
458 | } | |
459 | | LPAREN expression RPAREN | |
460 | { | |
461 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
462 | $$->u.expression.type = AST_EXP_NESTED; | |
463 | $$->u.expression.u.child = $2; | |
464 | } | |
465 | ; | |
466 | ||
467 | postfix_expression | |
468 | : primary_expression | |
469 | { $$ = $1; } | |
470 | | postfix_expression DOT IDENTIFIER | |
471 | { | |
472 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
473 | $$->u.expression.type = AST_EXP_IDENTIFIER; | |
474 | $$->u.expression.post_op = AST_LINK_DOT; | |
475 | $$->u.expression.u.identifier = $3->s; | |
476 | $$->u.expression.prev = $1; | |
477 | } | |
478 | | postfix_expression RARROW IDENTIFIER | |
479 | { | |
480 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
481 | $$->u.expression.type = AST_EXP_IDENTIFIER; | |
482 | $$->u.expression.post_op = AST_LINK_RARROW; | |
483 | $$->u.expression.u.identifier = $3->s; | |
484 | $$->u.expression.prev = $1; | |
485 | } | |
486 | ; | |
487 | ||
488 | unary_expression | |
489 | : postfix_expression | |
490 | { $$ = $1; } | |
491 | | unary_operator unary_expression | |
492 | { | |
493 | $$ = $1; | |
494 | $$->u.unary_op.child = $2; | |
495 | } | |
496 | ; | |
497 | ||
498 | unary_operator | |
499 | : PLUS | |
500 | { | |
501 | $$ = make_node(parser_ctx, NODE_UNARY_OP); | |
502 | $$->u.unary_op.type = AST_UNARY_PLUS; | |
503 | } | |
504 | | MINUS | |
505 | { | |
506 | $$ = make_node(parser_ctx, NODE_UNARY_OP); | |
507 | $$->u.unary_op.type = AST_UNARY_MINUS; | |
508 | } | |
509 | | NOT_OP | |
510 | { | |
511 | $$ = make_node(parser_ctx, NODE_UNARY_OP); | |
512 | $$->u.unary_op.type = AST_UNARY_NOT; | |
513 | } | |
ab78f161 CB |
514 | | NOT_BIN |
515 | { | |
516 | $$ = make_node(parser_ctx, NODE_UNARY_OP); | |
517 | $$->u.unary_op.type = AST_UNARY_BIN_NOT; | |
518 | } | |
953192ba MD |
519 | ; |
520 | ||
521 | multiplicative_expression | |
522 | : unary_expression | |
523 | { $$ = $1; } | |
524 | | multiplicative_expression STAR unary_expression | |
525 | { | |
526 | $$ = make_op_node(parser_ctx, AST_OP_MUL, $1, $3); | |
527 | } | |
528 | | multiplicative_expression DIV_OP unary_expression | |
529 | { | |
530 | $$ = make_op_node(parser_ctx, AST_OP_DIV, $1, $3); | |
531 | } | |
532 | | multiplicative_expression MOD_OP unary_expression | |
533 | { | |
534 | $$ = make_op_node(parser_ctx, AST_OP_MOD, $1, $3); | |
535 | } | |
536 | ; | |
537 | ||
538 | additive_expression | |
539 | : multiplicative_expression | |
540 | { $$ = $1; } | |
541 | | additive_expression PLUS multiplicative_expression | |
542 | { | |
543 | $$ = make_op_node(parser_ctx, AST_OP_PLUS, $1, $3); | |
544 | } | |
545 | | additive_expression MINUS multiplicative_expression | |
546 | { | |
547 | $$ = make_op_node(parser_ctx, AST_OP_MINUS, $1, $3); | |
548 | } | |
549 | ; | |
550 | ||
551 | shift_expression | |
552 | : additive_expression | |
553 | { $$ = $1; } | |
554 | | shift_expression LEFT_OP additive_expression | |
555 | { | |
556 | $$ = make_op_node(parser_ctx, AST_OP_LSHIFT, $1, $3); | |
557 | } | |
558 | | shift_expression RIGHT_OP additive_expression | |
559 | { | |
560 | $$ = make_op_node(parser_ctx, AST_OP_RSHIFT, $1, $3); | |
561 | } | |
562 | ; | |
563 | ||
564 | relational_expression | |
565 | : shift_expression | |
566 | { $$ = $1; } | |
567 | | relational_expression LT_OP shift_expression | |
568 | { | |
569 | $$ = make_op_node(parser_ctx, AST_OP_LT, $1, $3); | |
570 | } | |
571 | | relational_expression GT_OP shift_expression | |
572 | { | |
573 | $$ = make_op_node(parser_ctx, AST_OP_GT, $1, $3); | |
574 | } | |
575 | | relational_expression LE_OP shift_expression | |
576 | { | |
577 | $$ = make_op_node(parser_ctx, AST_OP_LE, $1, $3); | |
578 | } | |
579 | | relational_expression GE_OP shift_expression | |
580 | { | |
581 | $$ = make_op_node(parser_ctx, AST_OP_GE, $1, $3); | |
582 | } | |
583 | ; | |
584 | ||
585 | equality_expression | |
586 | : relational_expression | |
587 | { $$ = $1; } | |
588 | | equality_expression EQ_OP relational_expression | |
589 | { | |
590 | $$ = make_op_node(parser_ctx, AST_OP_EQ, $1, $3); | |
591 | } | |
592 | | equality_expression NE_OP relational_expression | |
593 | { | |
594 | $$ = make_op_node(parser_ctx, AST_OP_NE, $1, $3); | |
595 | } | |
596 | ; | |
597 | ||
598 | and_expression | |
599 | : equality_expression | |
600 | { $$ = $1; } | |
601 | | and_expression AND_BIN equality_expression | |
602 | { | |
603 | $$ = make_op_node(parser_ctx, AST_OP_BIN_AND, $1, $3); | |
604 | } | |
605 | ; | |
606 | ||
607 | exclusive_or_expression | |
608 | : and_expression | |
609 | { $$ = $1; } | |
610 | | exclusive_or_expression XOR_BIN and_expression | |
611 | { | |
612 | $$ = make_op_node(parser_ctx, AST_OP_BIN_XOR, $1, $3); | |
613 | } | |
614 | ; | |
615 | ||
616 | inclusive_or_expression | |
617 | : exclusive_or_expression | |
618 | { $$ = $1; } | |
619 | | inclusive_or_expression OR_BIN exclusive_or_expression | |
620 | { | |
621 | $$ = make_op_node(parser_ctx, AST_OP_BIN_OR, $1, $3); | |
622 | } | |
623 | ; | |
624 | ||
625 | logical_and_expression | |
626 | : inclusive_or_expression | |
627 | { $$ = $1; } | |
628 | | logical_and_expression AND_OP inclusive_or_expression | |
629 | { | |
630 | $$ = make_op_node(parser_ctx, AST_OP_AND, $1, $3); | |
631 | } | |
632 | ; | |
633 | ||
634 | logical_or_expression | |
635 | : logical_and_expression | |
636 | { $$ = $1; } | |
637 | | logical_or_expression OR_OP logical_and_expression | |
638 | { | |
639 | $$ = make_op_node(parser_ctx, AST_OP_OR, $1, $3); | |
640 | } | |
641 | ; | |
642 | ||
643 | expression | |
644 | : logical_or_expression | |
645 | { $$ = $1; } | |
646 | ; | |
647 | ||
648 | translation_unit | |
649 | : expression | |
650 | { | |
651 | parser_ctx->ast->root.u.root.child = $1; | |
652 | } | |
653 | ; |