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