5 * LTTng filter expression parser
7 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
22 * Grammar inspired from http://www.quut.com/c/ANSI-C-grammar-y.html
32 #include "filter-ast.h"
33 #include "filter-parser.h"
35 #include <common/macros.h>
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 */
45 int filter_parser_debug = 0;
48 int yyparse(struct filter_parser_ctx *parser_ctx, yyscan_t scanner);
50 int yylex(union YYSTYPE *yyval, yyscan_t scanner);
52 int yylex_init_extra(struct filter_parser_ctx *parser_ctx, yyscan_t * ptr_yy_globals);
54 int yylex_destroy(yyscan_t yyparser_ctx);
56 void yyrestart(FILE * in_str, yyscan_t parser_ctx);
59 struct cds_list_head gc;
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",
73 const char *node_type(struct filter_node *node)
75 if (node->type < NR_NODE_TYPES)
76 return node_type_to_str[node->type];
81 static struct gc_string *gc_string_alloc(struct filter_parser_ctx *parser_ctx,
84 struct gc_string *gstr;
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;
92 gstr = zmalloc(alloclen);
96 cds_list_add(&gstr->gc, &parser_ctx->allocated_strings);
97 gstr->alloclen = alloclen;
103 * note: never use gc_string_append on a string that has external references.
104 * gsrc will be garbage collected immediately, and gstr might be.
105 * Should only be used to append characters to a string literal or constant.
108 struct gc_string *gc_string_append(struct filter_parser_ctx *parser_ctx,
109 struct gc_string *gstr,
110 struct gc_string *gsrc)
112 size_t newlen = strlen(gsrc->s) + strlen(gstr->s) + 1;
115 /* TODO: could be faster with find first bit or glib Gstring */
116 /* sizeof long to account for malloc header (int or long ?) */
117 for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + newlen;
120 if (alloclen > gstr->alloclen) {
121 struct gc_string *newgstr;
123 newgstr = gc_string_alloc(parser_ctx, newlen);
124 strcpy(newgstr->s, gstr->s);
125 strcat(newgstr->s, gsrc->s);
126 cds_list_del(&gstr->gc);
130 strcat(gstr->s, gsrc->s);
132 cds_list_del(&gsrc->gc);
138 void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src)
140 lvalp->gs = gc_string_alloc(parser_ctx, strlen(src) + 1);
141 strcpy(lvalp->gs->s, src);
144 static struct filter_node *make_node(struct filter_parser_ctx *scanner,
147 struct filter_ast *ast = filter_parser_get_ast(scanner);
148 struct filter_node *node;
150 node = zmalloc(sizeof(*node));
153 memset(node, 0, sizeof(*node));
155 cds_list_add(&node->gc, &ast->allocated_nodes);
159 fprintf(stderr, "[error] %s: trying to create root node\n", __func__);
162 case NODE_EXPRESSION:
171 fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
179 static struct filter_node *make_op_node(struct filter_parser_ctx *scanner,
181 struct filter_node *lchild,
182 struct filter_node *rchild)
184 struct filter_ast *ast = filter_parser_get_ast(scanner);
185 struct filter_node *node;
187 node = zmalloc(sizeof(*node));
190 memset(node, 0, sizeof(*node));
191 node->type = NODE_OP;
192 cds_list_add(&node->gc, &ast->allocated_nodes);
193 node->u.op.type = type;
194 node->u.op.lchild = lchild;
195 node->u.op.rchild = rchild;
200 void yyerror(struct filter_parser_ctx *parser_ctx, yyscan_t scanner, const char *str)
202 fprintf(stderr, "error %s\n", str);
211 #define parse_error(parser_ctx, str) \
213 yyerror(parser_ctx, parser_ctx->scanner, YY_("parse error: " str "\n")); \
217 static void free_strings(struct cds_list_head *list)
219 struct gc_string *gstr, *tmp;
221 cds_list_for_each_entry_safe(gstr, tmp, list, gc)
225 static struct filter_ast *filter_ast_alloc(void)
227 struct filter_ast *ast;
229 ast = zmalloc(sizeof(*ast));
232 memset(ast, 0, sizeof(*ast));
233 CDS_INIT_LIST_HEAD(&ast->allocated_nodes);
234 ast->root.type = NODE_ROOT;
238 static void filter_ast_free(struct filter_ast *ast)
240 struct filter_node *node, *tmp;
242 cds_list_for_each_entry_safe(node, tmp, &ast->allocated_nodes, gc)
248 int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx)
250 return yyparse(parser_ctx, parser_ctx->scanner);
254 struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input)
256 struct filter_parser_ctx *parser_ctx;
259 yydebug = filter_parser_debug;
261 parser_ctx = zmalloc(sizeof(*parser_ctx));
264 memset(parser_ctx, 0, sizeof(*parser_ctx));
266 ret = yylex_init_extra(parser_ctx, &parser_ctx->scanner);
268 fprintf(stderr, "yylex_init error\n");
269 goto cleanup_parser_ctx;
271 /* Start processing new stream */
272 yyrestart(input, parser_ctx->scanner);
274 parser_ctx->ast = filter_ast_alloc();
275 if (!parser_ctx->ast)
277 CDS_INIT_LIST_HEAD(&parser_ctx->allocated_strings);
280 fprintf(stdout, "parser_ctx input is a%s.\n",
281 isatty(fileno(input)) ? "n interactive tty" :
282 " noninteractive file");
287 ret = yylex_destroy(parser_ctx->scanner);
289 fprintf(stderr, "yylex_destroy error\n");
296 void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx)
300 ret = yylex_destroy(parser_ctx->scanner);
302 fprintf(stderr, "yylex_destroy error\n");
304 filter_ast_free(parser_ctx->ast);
305 free_strings(&parser_ctx->allocated_strings);
306 filter_ir_free(parser_ctx);
307 free(parser_ctx->bytecode);
308 free(parser_ctx->bytecode_reloc);
317 %parse-param {struct filter_parser_ctx *parser_ctx}
318 %parse-param {yyscan_t scanner}
319 %lex-param {yyscan_t scanner}
320 %start translation_unit
321 %token CHARACTER_CONSTANT_START SQUOTE STRING_LITERAL_START DQUOTE
322 %token ESCSEQ CHAR_STRING_TOKEN
323 %token DECIMAL_CONSTANT OCTAL_CONSTANT HEXADECIMAL_CONSTANT FLOAT_CONSTANT
324 %token LSBRAC RSBRAC LPAREN RPAREN LBRAC RBRAC RARROW
325 %token STAR PLUS MINUS
326 %token MOD_OP DIV_OP RIGHT_OP LEFT_OP
327 %token EQ_OP NE_OP LE_OP GE_OP LT_OP GT_OP AND_OP OR_OP NOT_OP
328 %token ASSIGN COLON SEMICOLON DOTDOTDOT DOT EQUAL COMMA
329 %token XOR_BIN AND_BIN OR_BIN NOT_BIN
331 %token <gs> IDENTIFIER GLOBAL_IDENTIFIER
337 struct gc_string *gs;
338 struct filter_node *n;
341 %type <gs> s_char s_char_sequence c_char c_char_sequence
343 %type <n> primary_expression
344 %type <n> prefix_expression
345 %type <n> prefix_expression_rec
346 %type <n> postfix_expression
347 %type <n> unary_expression
348 %type <n> unary_operator
349 %type <n> multiplicative_expression
350 %type <n> additive_expression
351 %type <n> shift_expression
352 %type <n> relational_expression
353 %type <n> equality_expression
354 %type <n> and_expression
355 %type <n> exclusive_or_expression
356 %type <n> inclusive_or_expression
357 %type <n> logical_and_expression
358 %type <n> logical_or_expression
360 %type <n> identifiers
370 | c_char_sequence c_char
371 { $$ = gc_string_append(parser_ctx, $1, $2); }
379 parse_error(parser_ctx, "escape sequences not supported yet");
383 /* 1.6 String literals */
388 | s_char_sequence s_char
389 { $$ = gc_string_append(parser_ctx, $1, $2); }
397 parse_error(parser_ctx, "escape sequences not supported yet");
404 $$ = make_node(parser_ctx, NODE_EXPRESSION);
405 $$->u.expression.type = AST_EXP_CONSTANT;
406 if (sscanf(yylval.gs->s, "%" WIDTH_u64_SCANF_IS_A_BROKEN_API SCNu64,
407 &$$->u.expression.u.constant) != 1) {
408 parse_error(parser_ctx, "cannot scanf decimal constant");
413 $$ = make_node(parser_ctx, NODE_EXPRESSION);
414 $$->u.expression.type = AST_EXP_CONSTANT;
415 if (!strcmp(yylval.gs->s, "0")) {
416 $$->u.expression.u.constant = 0;
417 } else if (sscanf(yylval.gs->s, "0%" WIDTH_o64_SCANF_IS_A_BROKEN_API SCNo64,
418 &$$->u.expression.u.constant) != 1) {
419 parse_error(parser_ctx, "cannot scanf octal constant");
422 | HEXADECIMAL_CONSTANT
424 $$ = make_node(parser_ctx, NODE_EXPRESSION);
425 $$->u.expression.type = AST_EXP_CONSTANT;
426 if (sscanf(yylval.gs->s, "0x%" WIDTH_x64_SCANF_IS_A_BROKEN_API SCNx64,
427 &$$->u.expression.u.constant) != 1) {
428 parse_error(parser_ctx, "cannot scanf hexadecimal constant");
433 $$ = make_node(parser_ctx, NODE_EXPRESSION);
434 $$->u.expression.type = AST_EXP_FLOAT_CONSTANT;
435 if (sscanf(yylval.gs->s, "%" WIDTH_lg_SCANF_IS_A_BROKEN_API "lg",
436 &$$->u.expression.u.float_constant) != 1) {
437 parse_error(parser_ctx, "cannot scanf float constant");
440 | STRING_LITERAL_START DQUOTE
442 $$ = make_node(parser_ctx, NODE_EXPRESSION);
443 $$->u.expression.type = AST_EXP_STRING;
444 $$->u.expression.u.string = "";
446 | STRING_LITERAL_START s_char_sequence DQUOTE
448 $$ = make_node(parser_ctx, NODE_EXPRESSION);
449 $$->u.expression.type = AST_EXP_STRING;
450 $$->u.expression.u.string = $2->s;
452 | CHARACTER_CONSTANT_START c_char_sequence SQUOTE
454 $$ = make_node(parser_ctx, NODE_EXPRESSION);
455 $$->u.expression.type = AST_EXP_STRING;
456 $$->u.expression.u.string = $2->s;
458 | LPAREN expression RPAREN
460 $$ = make_node(parser_ctx, NODE_EXPRESSION);
461 $$->u.expression.type = AST_EXP_NESTED;
462 $$->u.expression.u.child = $2;
469 $$ = make_node(parser_ctx, NODE_EXPRESSION);
470 $$->u.expression.type = AST_EXP_IDENTIFIER;
471 $$->u.expression.u.identifier = yylval.gs->s;
475 $$ = make_node(parser_ctx, NODE_EXPRESSION);
476 $$->u.expression.type = AST_EXP_GLOBAL_IDENTIFIER;
477 $$->u.expression.u.identifier = yylval.gs->s;
481 prefix_expression_rec
482 : LSBRAC unary_expression RSBRAC
486 | LSBRAC unary_expression RSBRAC prefix_expression_rec
489 $$->u.expression.pre_op = AST_LINK_BRACKET;
490 $$->u.expression.prev = $4;
499 | identifiers prefix_expression_rec
502 $$->u.expression.pre_op = AST_LINK_BRACKET;
503 $$->u.expression.next_bracket = $2;
512 | postfix_expression DOT prefix_expression
515 $$->u.expression.post_op = AST_LINK_DOT;
516 $$->u.expression.prev = $1;
518 | postfix_expression RARROW prefix_expression
521 $$->u.expression.post_op = AST_LINK_RARROW;
522 $$->u.expression.prev = $1;
531 | unary_operator unary_expression
534 $$->u.unary_op.child = $2;
541 $$ = make_node(parser_ctx, NODE_UNARY_OP);
542 $$->u.unary_op.type = AST_UNARY_PLUS;
546 $$ = make_node(parser_ctx, NODE_UNARY_OP);
547 $$->u.unary_op.type = AST_UNARY_MINUS;
551 $$ = make_node(parser_ctx, NODE_UNARY_OP);
552 $$->u.unary_op.type = AST_UNARY_NOT;
556 $$ = make_node(parser_ctx, NODE_UNARY_OP);
557 $$->u.unary_op.type = AST_UNARY_BIT_NOT;
561 multiplicative_expression
564 | multiplicative_expression STAR unary_expression
566 $$ = make_op_node(parser_ctx, AST_OP_MUL, $1, $3);
568 | multiplicative_expression DIV_OP unary_expression
570 $$ = make_op_node(parser_ctx, AST_OP_DIV, $1, $3);
572 | multiplicative_expression MOD_OP unary_expression
574 $$ = make_op_node(parser_ctx, AST_OP_MOD, $1, $3);
579 : multiplicative_expression
581 | additive_expression PLUS multiplicative_expression
583 $$ = make_op_node(parser_ctx, AST_OP_PLUS, $1, $3);
585 | additive_expression MINUS multiplicative_expression
587 $$ = make_op_node(parser_ctx, AST_OP_MINUS, $1, $3);
592 : additive_expression
594 | shift_expression LEFT_OP additive_expression
596 $$ = make_op_node(parser_ctx, AST_OP_BIT_LSHIFT, $1, $3);
598 | shift_expression RIGHT_OP additive_expression
600 $$ = make_op_node(parser_ctx, AST_OP_BIT_RSHIFT, $1, $3);
607 | and_expression AND_BIN shift_expression
609 $$ = make_op_node(parser_ctx, AST_OP_BIT_AND, $1, $3);
613 exclusive_or_expression
616 | exclusive_or_expression XOR_BIN and_expression
618 $$ = make_op_node(parser_ctx, AST_OP_BIT_XOR, $1, $3);
622 inclusive_or_expression
623 : exclusive_or_expression
625 | inclusive_or_expression OR_BIN exclusive_or_expression
627 $$ = make_op_node(parser_ctx, AST_OP_BIT_OR, $1, $3);
631 relational_expression
632 : inclusive_or_expression
634 | relational_expression LT_OP inclusive_or_expression
636 $$ = make_op_node(parser_ctx, AST_OP_LT, $1, $3);
638 | relational_expression GT_OP inclusive_or_expression
640 $$ = make_op_node(parser_ctx, AST_OP_GT, $1, $3);
642 | relational_expression LE_OP inclusive_or_expression
644 $$ = make_op_node(parser_ctx, AST_OP_LE, $1, $3);
646 | relational_expression GE_OP inclusive_or_expression
648 $$ = make_op_node(parser_ctx, AST_OP_GE, $1, $3);
653 : relational_expression
655 | equality_expression EQ_OP relational_expression
657 $$ = make_op_node(parser_ctx, AST_OP_EQ, $1, $3);
659 | equality_expression NE_OP relational_expression
661 $$ = make_op_node(parser_ctx, AST_OP_NE, $1, $3);
665 logical_and_expression
666 : equality_expression
668 | logical_and_expression AND_OP equality_expression
670 $$ = make_op_node(parser_ctx, AST_OP_AND, $1, $3);
674 logical_or_expression
675 : logical_and_expression
677 | logical_or_expression OR_OP logical_and_expression
679 $$ = make_op_node(parser_ctx, AST_OP_OR, $1, $3);
684 : logical_or_expression
691 parser_ctx->ast->root.u.root.child = $1;