SoW-2019-0002: Dynamic Snapshot
[lttng-tools.git] / src / common / filter / filter-parser.y
CommitLineData
953192ba
MD
1%{
2/*
3 * filter-parser.y
4 *
5 * LTTng filter expression parser
6 *
ab5be9fa 7 * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
953192ba 8 *
ab5be9fa 9 * SPDX-License-Identifier: LGPL-2.1-only
953192ba
MD
10 *
11 * Grammar inspired from http://www.quut.com/c/ANSI-C-grammar-y.html
12 */
13
14#include <stdio.h>
15#include <unistd.h>
16#include <string.h>
17#include <stdlib.h>
18#include <assert.h>
19#include <errno.h>
20#include <inttypes.h>
953192ba 21#include "filter-ast.h"
95b9bd90 22#include "filter-parser.h"
d3a684ee
JR
23#include "filter-bytecode.h"
24#include "memstream.h"
953192ba 25
a187da1a
DG
26#include <common/macros.h>
27
8ab7c0d9
MD
28#define WIDTH_u64_SCANF_IS_A_BROKEN_API "20"
29#define WIDTH_o64_SCANF_IS_A_BROKEN_API "22"
30#define WIDTH_x64_SCANF_IS_A_BROKEN_API "17"
31#define WIDTH_lg_SCANF_IS_A_BROKEN_API "4096" /* Hugely optimistic approximation */
32
d3a684ee
JR
33#ifdef DEBUG
34static const int print_xml = 1;
35#define dbg_printf(fmt, args...) \
36 printf("[debug filter_parser] " fmt, ## args)
37#else
38static const int print_xml = 0;
39#define dbg_printf(fmt, args...) \
40do { \
41 /* do nothing but check printf format */ \
42 if (0) \
43 printf("[debug filter_parser] " fmt, ## args); \
44} while (0)
45#endif
46
a187da1a 47LTTNG_HIDDEN
953192ba 48int yydebug;
a187da1a 49LTTNG_HIDDEN
953192ba
MD
50int filter_parser_debug = 0;
51
a187da1a 52LTTNG_HIDDEN
9039edd4 53int yyparse(struct filter_parser_ctx *parser_ctx, yyscan_t scanner);
a187da1a 54LTTNG_HIDDEN
9039edd4 55int yylex(union YYSTYPE *yyval, yyscan_t scanner);
a187da1a 56LTTNG_HIDDEN
953192ba 57int yylex_init_extra(struct filter_parser_ctx *parser_ctx, yyscan_t * ptr_yy_globals);
a187da1a 58LTTNG_HIDDEN
953192ba 59int yylex_destroy(yyscan_t yyparser_ctx);
a187da1a 60LTTNG_HIDDEN
953192ba
MD
61void yyrestart(FILE * in_str, yyscan_t parser_ctx);
62
63struct gc_string {
64 struct cds_list_head gc;
65 size_t alloclen;
66 char s[];
67};
68
69static const char *node_type_to_str[] = {
70 [ NODE_UNKNOWN ] = "NODE_UNKNOWN",
71 [ NODE_ROOT ] = "NODE_ROOT",
72 [ NODE_EXPRESSION ] = "NODE_EXPRESSION",
73 [ NODE_OP ] = "NODE_OP",
74 [ NODE_UNARY_OP ] = "NODE_UNARY_OP",
75};
76
a187da1a 77LTTNG_HIDDEN
953192ba
MD
78const char *node_type(struct filter_node *node)
79{
80 if (node->type < NR_NODE_TYPES)
81 return node_type_to_str[node->type];
82 else
83 return NULL;
84}
85
86static struct gc_string *gc_string_alloc(struct filter_parser_ctx *parser_ctx,
87 size_t len)
88{
89 struct gc_string *gstr;
90 size_t alloclen;
91
92 /* TODO: could be faster with find first bit or glib Gstring */
93 /* sizeof long to account for malloc header (int or long ?) */
94 for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + len;
95 alloclen *= 2);
96
be61ee34
MD
97 gstr = zmalloc(alloclen);
98 if (!gstr) {
99 goto end;
100 }
953192ba
MD
101 cds_list_add(&gstr->gc, &parser_ctx->allocated_strings);
102 gstr->alloclen = alloclen;
be61ee34 103end:
953192ba
MD
104 return gstr;
105}
106
107/*
108 * note: never use gc_string_append on a string that has external references.
109 * gsrc will be garbage collected immediately, and gstr might be.
110 * Should only be used to append characters to a string literal or constant.
111 */
d3a684ee 112static
953192ba
MD
113struct gc_string *gc_string_append(struct filter_parser_ctx *parser_ctx,
114 struct gc_string *gstr,
115 struct gc_string *gsrc)
116{
117 size_t newlen = strlen(gsrc->s) + strlen(gstr->s) + 1;
118 size_t alloclen;
119
120 /* TODO: could be faster with find first bit or glib Gstring */
121 /* sizeof long to account for malloc header (int or long ?) */
122 for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + newlen;
123 alloclen *= 2);
124
125 if (alloclen > gstr->alloclen) {
126 struct gc_string *newgstr;
127
128 newgstr = gc_string_alloc(parser_ctx, newlen);
129 strcpy(newgstr->s, gstr->s);
130 strcat(newgstr->s, gsrc->s);
131 cds_list_del(&gstr->gc);
132 free(gstr);
133 gstr = newgstr;
134 } else {
135 strcat(gstr->s, gsrc->s);
136 }
137 cds_list_del(&gsrc->gc);
138 free(gsrc);
139 return gstr;
140}
141
d3a684ee
JR
142LTTNG_HIDDEN
143void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src);
a187da1a 144LTTNG_HIDDEN
953192ba
MD
145void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src)
146{
147 lvalp->gs = gc_string_alloc(parser_ctx, strlen(src) + 1);
148 strcpy(lvalp->gs->s, src);
149}
150
151static struct filter_node *make_node(struct filter_parser_ctx *scanner,
152 enum node_type type)
153{
154 struct filter_ast *ast = filter_parser_get_ast(scanner);
155 struct filter_node *node;
156
be61ee34 157 node = zmalloc(sizeof(*node));
953192ba
MD
158 if (!node)
159 return NULL;
160 memset(node, 0, sizeof(*node));
161 node->type = type;
162 cds_list_add(&node->gc, &ast->allocated_nodes);
163
164 switch (type) {
165 case NODE_ROOT:
166 fprintf(stderr, "[error] %s: trying to create root node\n", __func__);
167 break;
168
169 case NODE_EXPRESSION:
170 break;
171 case NODE_OP:
172 break;
173 case NODE_UNARY_OP:
174 break;
175
176 case NODE_UNKNOWN:
177 default:
178 fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
179 (int) type);
180 break;
181 }
182
183 return node;
184}
185
186static struct filter_node *make_op_node(struct filter_parser_ctx *scanner,
187 enum op_type type,
188 struct filter_node *lchild,
189 struct filter_node *rchild)
190{
191 struct filter_ast *ast = filter_parser_get_ast(scanner);
192 struct filter_node *node;
193
be61ee34 194 node = zmalloc(sizeof(*node));
953192ba
MD
195 if (!node)
196 return NULL;
197 memset(node, 0, sizeof(*node));
198 node->type = NODE_OP;
199 cds_list_add(&node->gc, &ast->allocated_nodes);
200 node->u.op.type = type;
201 node->u.op.lchild = lchild;
202 node->u.op.rchild = rchild;
203 return node;
204}
205
d3a684ee
JR
206LTTNG_HIDDEN
207void yyerror(struct filter_parser_ctx *parser_ctx, yyscan_t scanner, const char *str);
a187da1a 208LTTNG_HIDDEN
9039edd4 209void yyerror(struct filter_parser_ctx *parser_ctx, yyscan_t scanner, const char *str)
953192ba
MD
210{
211 fprintf(stderr, "error %s\n", str);
212}
213
d3a684ee
JR
214LTTNG_HIDDEN
215int yywrap(void);
a187da1a 216LTTNG_HIDDEN
953192ba
MD
217int yywrap(void)
218{
219 return 1;
220}
221
222#define parse_error(parser_ctx, str) \
223do { \
9039edd4 224 yyerror(parser_ctx, parser_ctx->scanner, YY_("parse error: " str "\n")); \
953192ba
MD
225 YYERROR; \
226} while (0)
227
228static void free_strings(struct cds_list_head *list)
229{
230 struct gc_string *gstr, *tmp;
231
232 cds_list_for_each_entry_safe(gstr, tmp, list, gc)
233 free(gstr);
234}
235
236static struct filter_ast *filter_ast_alloc(void)
237{
238 struct filter_ast *ast;
239
be61ee34 240 ast = zmalloc(sizeof(*ast));
953192ba
MD
241 if (!ast)
242 return NULL;
243 memset(ast, 0, sizeof(*ast));
244 CDS_INIT_LIST_HEAD(&ast->allocated_nodes);
245 ast->root.type = NODE_ROOT;
246 return ast;
247}
248
249static void filter_ast_free(struct filter_ast *ast)
250{
251 struct filter_node *node, *tmp;
252
253 cds_list_for_each_entry_safe(node, tmp, &ast->allocated_nodes, gc)
254 free(node);
37600d79 255 free(ast);
953192ba
MD
256}
257
a187da1a 258LTTNG_HIDDEN
953192ba
MD
259int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx)
260{
9039edd4 261 return yyparse(parser_ctx, parser_ctx->scanner);
953192ba
MD
262}
263
a187da1a 264LTTNG_HIDDEN
953192ba
MD
265struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input)
266{
267 struct filter_parser_ctx *parser_ctx;
268 int ret;
269
270 yydebug = filter_parser_debug;
271
be61ee34 272 parser_ctx = zmalloc(sizeof(*parser_ctx));
953192ba
MD
273 if (!parser_ctx)
274 return NULL;
275 memset(parser_ctx, 0, sizeof(*parser_ctx));
276
277 ret = yylex_init_extra(parser_ctx, &parser_ctx->scanner);
278 if (ret) {
279 fprintf(stderr, "yylex_init error\n");
280 goto cleanup_parser_ctx;
281 }
282 /* Start processing new stream */
283 yyrestart(input, parser_ctx->scanner);
284
285 parser_ctx->ast = filter_ast_alloc();
286 if (!parser_ctx->ast)
287 goto cleanup_lexer;
288 CDS_INIT_LIST_HEAD(&parser_ctx->allocated_strings);
289
290 if (yydebug)
291 fprintf(stdout, "parser_ctx input is a%s.\n",
292 isatty(fileno(input)) ? "n interactive tty" :
293 " noninteractive file");
294
295 return parser_ctx;
296
297cleanup_lexer:
298 ret = yylex_destroy(parser_ctx->scanner);
299 if (!ret)
300 fprintf(stderr, "yylex_destroy error\n");
301cleanup_parser_ctx:
302 free(parser_ctx);
303 return NULL;
304}
305
a187da1a 306LTTNG_HIDDEN
953192ba
MD
307void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx)
308{
309 int ret;
310
311 free_strings(&parser_ctx->allocated_strings);
312 filter_ast_free(parser_ctx->ast);
313 ret = yylex_destroy(parser_ctx->scanner);
314 if (ret)
315 fprintf(stderr, "yylex_destroy error\n");
316 free(parser_ctx);
317}
318
d3a684ee
JR
319LTTNG_HIDDEN
320int filter_parser_ctx_create_from_filter_expression(
321 const char *filter_expression, struct filter_parser_ctx **ctxp)
322{
323 int ret;
324 struct filter_parser_ctx *ctx = NULL;
325 FILE *fmem = NULL;
326
327 assert(filter_expression);
328 assert(ctxp);
329
330 /*
331 * Casting const to non-const, as the underlying function will use it in
332 * read-only mode.
333 */
334 fmem = lttng_fmemopen((void *) filter_expression,
335 strlen(filter_expression), "r");
336 if (!fmem) {
337 fprintf(stderr, "Error opening memory as stream\n");
338 ret = -LTTNG_ERR_FILTER_NOMEM;
339 goto error;
340 }
341 ctx = filter_parser_ctx_alloc(fmem);
342 if (!ctx) {
343 fprintf(stderr, "Error allocating parser\n");
344 ret = -LTTNG_ERR_FILTER_NOMEM;
345 goto filter_alloc_error;
346 }
347 ret = filter_parser_ctx_append_ast(ctx);
348 if (ret) {
349 fprintf(stderr, "Parse error\n");
350 ret = -LTTNG_ERR_FILTER_INVAL;
351 goto parse_error;
352 }
353 if (print_xml) {
354 ret = filter_visitor_print_xml(ctx, stdout, 0);
355 if (ret) {
356 fflush(stdout);
357 fprintf(stderr, "XML print error\n");
358 ret = -LTTNG_ERR_FILTER_INVAL;
359 goto parse_error;
360 }
361 }
362
363 dbg_printf("Generating IR... ");
364 fflush(stdout);
365 ret = filter_visitor_ir_generate(ctx);
366 if (ret) {
367 fprintf(stderr, "Generate IR error\n");
368 ret = -LTTNG_ERR_FILTER_INVAL;
369 goto parse_error;
370 }
371 dbg_printf("done\n");
372
373 dbg_printf("Validating IR... ");
374 fflush(stdout);
375 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
376 if (ret) {
377 ret = -LTTNG_ERR_FILTER_INVAL;
378 goto parse_error;
379 }
380
381 /* Normalize globbing patterns in the expression. */
382 ret = filter_visitor_ir_normalize_glob_patterns(ctx);
383 if (ret) {
384 ret = -LTTNG_ERR_FILTER_INVAL;
385 goto parse_error;
386 }
387
388 /* Validate strings used as literals in the expression. */
389 ret = filter_visitor_ir_validate_string(ctx);
390 if (ret) {
391 ret = -LTTNG_ERR_FILTER_INVAL;
392 goto parse_error;
393 }
394
395 /* Validate globbing patterns in the expression. */
396 ret = filter_visitor_ir_validate_globbing(ctx);
397 if (ret) {
398 ret = -LTTNG_ERR_FILTER_INVAL;
399 goto parse_error;
400 }
401
402 dbg_printf("done\n");
403
404 dbg_printf("Generating bytecode... ");
405 fflush(stdout);
406 ret = filter_visitor_bytecode_generate(ctx);
407 if (ret) {
408 fprintf(stderr, "Generate bytecode error\n");
409 ret = -LTTNG_ERR_FILTER_INVAL;
410 goto parse_error;
411 }
412 dbg_printf("done\n");
413 dbg_printf("Size of bytecode generated: %u bytes.\n",
414 bytecode_get_len(&ctx->bytecode->b));
415
416 /* No need to keep the memory stream. */
417 if (fclose(fmem) != 0) {
418 fprintf(stderr, "fclose (%d) \n", errno);
419 ret = -LTTNG_ERR_FILTER_INVAL;
420 }
421
422 *ctxp = ctx;
423 return 0;
424
425parse_error:
426 filter_ir_free(ctx);
427 filter_parser_ctx_free(ctx);
428filter_alloc_error:
429 if (fclose(fmem) != 0) {
430 fprintf(stderr, "fclose (%d) \n", errno);
431 }
432error:
433 return ret;
434}
435
953192ba
MD
436%}
437
438%define api.pure
439 /* %locations */
440%parse-param {struct filter_parser_ctx *parser_ctx}
9039edd4
ZT
441%parse-param {yyscan_t scanner}
442%lex-param {yyscan_t scanner}
953192ba
MD
443%start translation_unit
444%token CHARACTER_CONSTANT_START SQUOTE STRING_LITERAL_START DQUOTE
445%token ESCSEQ CHAR_STRING_TOKEN
e90d8561 446%token DECIMAL_CONSTANT OCTAL_CONSTANT HEXADECIMAL_CONSTANT FLOAT_CONSTANT
953192ba
MD
447%token LSBRAC RSBRAC LPAREN RPAREN LBRAC RBRAC RARROW
448%token STAR PLUS MINUS
449%token MOD_OP DIV_OP RIGHT_OP LEFT_OP
450%token EQ_OP NE_OP LE_OP GE_OP LT_OP GT_OP AND_OP OR_OP NOT_OP
451%token ASSIGN COLON SEMICOLON DOTDOTDOT DOT EQUAL COMMA
452%token XOR_BIN AND_BIN OR_BIN NOT_BIN
453
586dc72f 454%token <gs> IDENTIFIER GLOBAL_IDENTIFIER
953192ba
MD
455%token ERROR
456%union
457{
458 long long ll;
459 char c;
460 struct gc_string *gs;
461 struct filter_node *n;
462}
463
464%type <gs> s_char s_char_sequence c_char c_char_sequence
465
466%type <n> primary_expression
bff988fa
MD
467%type <n> prefix_expression
468%type <n> prefix_expression_rec
953192ba
MD
469%type <n> postfix_expression
470%type <n> unary_expression
471%type <n> unary_operator
472%type <n> multiplicative_expression
473%type <n> additive_expression
474%type <n> shift_expression
475%type <n> relational_expression
476%type <n> equality_expression
477%type <n> and_expression
478%type <n> exclusive_or_expression
479%type <n> inclusive_or_expression
480%type <n> logical_and_expression
481%type <n> logical_or_expression
482%type <n> expression
bff988fa 483%type <n> identifiers
953192ba
MD
484
485%%
486
487
488/* 1.5 Constants */
489
490c_char_sequence:
491 c_char
492 { $$ = $1; }
493 | c_char_sequence c_char
494 { $$ = gc_string_append(parser_ctx, $1, $2); }
495 ;
496
497c_char:
498 CHAR_STRING_TOKEN
499 { $$ = yylval.gs; }
500 | ESCSEQ
501 {
502 parse_error(parser_ctx, "escape sequences not supported yet");
503 }
504 ;
505
506/* 1.6 String literals */
507
508s_char_sequence:
509 s_char
510 { $$ = $1; }
511 | s_char_sequence s_char
512 { $$ = gc_string_append(parser_ctx, $1, $2); }
513 ;
514
515s_char:
516 CHAR_STRING_TOKEN
517 { $$ = yylval.gs; }
518 | ESCSEQ
519 {
520 parse_error(parser_ctx, "escape sequences not supported yet");
521 }
522 ;
523
bff988fa
MD
524primary_expression:
525 DECIMAL_CONSTANT
953192ba
MD
526 {
527 $$ = make_node(parser_ctx, NODE_EXPRESSION);
528 $$->u.expression.type = AST_EXP_CONSTANT;
8ab7c0d9
MD
529 if (sscanf(yylval.gs->s, "%" WIDTH_u64_SCANF_IS_A_BROKEN_API SCNu64,
530 &$$->u.expression.u.constant) != 1) {
531 parse_error(parser_ctx, "cannot scanf decimal constant");
532 }
953192ba
MD
533 }
534 | OCTAL_CONSTANT
535 {
536 $$ = make_node(parser_ctx, NODE_EXPRESSION);
537 $$->u.expression.type = AST_EXP_CONSTANT;
8ab7c0d9
MD
538 if (!strcmp(yylval.gs->s, "0")) {
539 $$->u.expression.u.constant = 0;
540 } else if (sscanf(yylval.gs->s, "0%" WIDTH_o64_SCANF_IS_A_BROKEN_API SCNo64,
541 &$$->u.expression.u.constant) != 1) {
542 parse_error(parser_ctx, "cannot scanf octal constant");
543 }
953192ba
MD
544 }
545 | HEXADECIMAL_CONSTANT
546 {
547 $$ = make_node(parser_ctx, NODE_EXPRESSION);
548 $$->u.expression.type = AST_EXP_CONSTANT;
8ab7c0d9
MD
549 if (sscanf(yylval.gs->s, "0x%" WIDTH_x64_SCANF_IS_A_BROKEN_API SCNx64,
550 &$$->u.expression.u.constant) != 1) {
551 parse_error(parser_ctx, "cannot scanf hexadecimal constant");
552 }
953192ba 553 }
e90d8561
MD
554 | FLOAT_CONSTANT
555 {
556 $$ = make_node(parser_ctx, NODE_EXPRESSION);
557 $$->u.expression.type = AST_EXP_FLOAT_CONSTANT;
8ab7c0d9
MD
558 if (sscanf(yylval.gs->s, "%" WIDTH_lg_SCANF_IS_A_BROKEN_API "lg",
559 &$$->u.expression.u.float_constant) != 1) {
560 parse_error(parser_ctx, "cannot scanf float constant");
561 }
e90d8561 562 }
953192ba
MD
563 | STRING_LITERAL_START DQUOTE
564 {
565 $$ = make_node(parser_ctx, NODE_EXPRESSION);
566 $$->u.expression.type = AST_EXP_STRING;
567 $$->u.expression.u.string = "";
568 }
569 | STRING_LITERAL_START s_char_sequence DQUOTE
570 {
571 $$ = make_node(parser_ctx, NODE_EXPRESSION);
572 $$->u.expression.type = AST_EXP_STRING;
573 $$->u.expression.u.string = $2->s;
574 }
575 | CHARACTER_CONSTANT_START c_char_sequence SQUOTE
576 {
577 $$ = make_node(parser_ctx, NODE_EXPRESSION);
578 $$->u.expression.type = AST_EXP_STRING;
579 $$->u.expression.u.string = $2->s;
580 }
581 | LPAREN expression RPAREN
582 {
583 $$ = make_node(parser_ctx, NODE_EXPRESSION);
584 $$->u.expression.type = AST_EXP_NESTED;
585 $$->u.expression.u.child = $2;
586 }
587 ;
588
bff988fa
MD
589identifiers
590 : IDENTIFIER
591 {
592 $$ = make_node(parser_ctx, NODE_EXPRESSION);
593 $$->u.expression.type = AST_EXP_IDENTIFIER;
594 $$->u.expression.u.identifier = yylval.gs->s;
595 }
596 | GLOBAL_IDENTIFIER
597 {
598 $$ = make_node(parser_ctx, NODE_EXPRESSION);
599 $$->u.expression.type = AST_EXP_GLOBAL_IDENTIFIER;
600 $$->u.expression.u.identifier = yylval.gs->s;
601 }
602 ;
603
604prefix_expression_rec
605 : LSBRAC unary_expression RSBRAC
606 {
607 $$ = $2;
608 }
609 | LSBRAC unary_expression RSBRAC prefix_expression_rec
610 {
611 $$ = $2;
612 $$->u.expression.pre_op = AST_LINK_BRACKET;
613 $$->u.expression.prev = $4;
614 }
615 ;
616
617prefix_expression
618 : identifiers
619 {
620 $$ = $1;
621 }
622 | identifiers prefix_expression_rec
661dfdd1
MD
623 {
624 $$ = $1;
625 $$->u.expression.pre_op = AST_LINK_BRACKET;
bff988fa 626 $$->u.expression.next_bracket = $2;
661dfdd1 627 }
bff988fa
MD
628 ;
629
630postfix_expression
631 : prefix_expression
953192ba 632 {
bff988fa
MD
633 $$ = $1;
634 }
635 | postfix_expression DOT prefix_expression
636 {
637 $$ = $3;
953192ba 638 $$->u.expression.post_op = AST_LINK_DOT;
953192ba
MD
639 $$->u.expression.prev = $1;
640 }
bff988fa 641 | postfix_expression RARROW prefix_expression
953192ba 642 {
bff988fa 643 $$ = $3;
953192ba 644 $$->u.expression.post_op = AST_LINK_RARROW;
953192ba
MD
645 $$->u.expression.prev = $1;
646 }
647 ;
648
649unary_expression
650 : postfix_expression
651 { $$ = $1; }
bff988fa
MD
652 | primary_expression
653 { $$ = $1; }
953192ba
MD
654 | unary_operator unary_expression
655 {
656 $$ = $1;
657 $$->u.unary_op.child = $2;
658 }
659 ;
660
661unary_operator
662 : PLUS
663 {
664 $$ = make_node(parser_ctx, NODE_UNARY_OP);
665 $$->u.unary_op.type = AST_UNARY_PLUS;
666 }
667 | MINUS
668 {
669 $$ = make_node(parser_ctx, NODE_UNARY_OP);
670 $$->u.unary_op.type = AST_UNARY_MINUS;
671 }
672 | NOT_OP
673 {
674 $$ = make_node(parser_ctx, NODE_UNARY_OP);
675 $$->u.unary_op.type = AST_UNARY_NOT;
676 }
ab78f161
CB
677 | NOT_BIN
678 {
679 $$ = make_node(parser_ctx, NODE_UNARY_OP);
bff988fa 680 $$->u.unary_op.type = AST_UNARY_BIT_NOT;
ab78f161 681 }
953192ba
MD
682 ;
683
684multiplicative_expression
685 : unary_expression
686 { $$ = $1; }
687 | multiplicative_expression STAR unary_expression
688 {
689 $$ = make_op_node(parser_ctx, AST_OP_MUL, $1, $3);
690 }
691 | multiplicative_expression DIV_OP unary_expression
692 {
693 $$ = make_op_node(parser_ctx, AST_OP_DIV, $1, $3);
694 }
695 | multiplicative_expression MOD_OP unary_expression
696 {
697 $$ = make_op_node(parser_ctx, AST_OP_MOD, $1, $3);
698 }
699 ;
700
701additive_expression
702 : multiplicative_expression
703 { $$ = $1; }
704 | additive_expression PLUS multiplicative_expression
705 {
706 $$ = make_op_node(parser_ctx, AST_OP_PLUS, $1, $3);
707 }
708 | additive_expression MINUS multiplicative_expression
709 {
710 $$ = make_op_node(parser_ctx, AST_OP_MINUS, $1, $3);
711 }
712 ;
713
714shift_expression
715 : additive_expression
716 { $$ = $1; }
717 | shift_expression LEFT_OP additive_expression
718 {
116d3c01 719 $$ = make_op_node(parser_ctx, AST_OP_BIT_LSHIFT, $1, $3);
953192ba
MD
720 }
721 | shift_expression RIGHT_OP additive_expression
722 {
116d3c01 723 $$ = make_op_node(parser_ctx, AST_OP_BIT_RSHIFT, $1, $3);
953192ba
MD
724 }
725 ;
726
831b702b 727and_expression
953192ba
MD
728 : shift_expression
729 { $$ = $1; }
831b702b
MD
730 | and_expression AND_BIN shift_expression
731 {
732 $$ = make_op_node(parser_ctx, AST_OP_BIT_AND, $1, $3);
733 }
734 ;
735
736exclusive_or_expression
737 : and_expression
738 { $$ = $1; }
739 | exclusive_or_expression XOR_BIN and_expression
740 {
741 $$ = make_op_node(parser_ctx, AST_OP_BIT_XOR, $1, $3);
742 }
743 ;
744
745inclusive_or_expression
746 : exclusive_or_expression
747 { $$ = $1; }
748 | inclusive_or_expression OR_BIN exclusive_or_expression
749 {
750 $$ = make_op_node(parser_ctx, AST_OP_BIT_OR, $1, $3);
751 }
752 ;
753
754relational_expression
755 : inclusive_or_expression
756 { $$ = $1; }
757 | relational_expression LT_OP inclusive_or_expression
953192ba
MD
758 {
759 $$ = make_op_node(parser_ctx, AST_OP_LT, $1, $3);
760 }
831b702b 761 | relational_expression GT_OP inclusive_or_expression
953192ba
MD
762 {
763 $$ = make_op_node(parser_ctx, AST_OP_GT, $1, $3);
764 }
831b702b 765 | relational_expression LE_OP inclusive_or_expression
953192ba
MD
766 {
767 $$ = make_op_node(parser_ctx, AST_OP_LE, $1, $3);
768 }
831b702b 769 | relational_expression GE_OP inclusive_or_expression
953192ba
MD
770 {
771 $$ = make_op_node(parser_ctx, AST_OP_GE, $1, $3);
772 }
773 ;
774
775equality_expression
776 : relational_expression
777 { $$ = $1; }
778 | equality_expression EQ_OP relational_expression
779 {
780 $$ = make_op_node(parser_ctx, AST_OP_EQ, $1, $3);
781 }
782 | equality_expression NE_OP relational_expression
783 {
784 $$ = make_op_node(parser_ctx, AST_OP_NE, $1, $3);
785 }
786 ;
787
953192ba 788logical_and_expression
831b702b 789 : equality_expression
953192ba 790 { $$ = $1; }
831b702b 791 | logical_and_expression AND_OP equality_expression
953192ba
MD
792 {
793 $$ = make_op_node(parser_ctx, AST_OP_AND, $1, $3);
794 }
795 ;
796
797logical_or_expression
798 : logical_and_expression
799 { $$ = $1; }
800 | logical_or_expression OR_OP logical_and_expression
801 {
802 $$ = make_op_node(parser_ctx, AST_OP_OR, $1, $3);
803 }
804 ;
805
806expression
807 : logical_or_expression
808 { $$ = $1; }
809 ;
810
811translation_unit
812 : expression
813 {
814 parser_ctx->ast->root.u.root.child = $1;
815 }
816 ;
This page took 0.099228 seconds and 5 git commands to generate.