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 = malloc(alloclen);
93 cds_list_add(&gstr->gc, &parser_ctx->allocated_strings);
94 gstr->alloclen = alloclen;
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.
104 struct gc_string *gc_string_append(struct filter_parser_ctx *parser_ctx,
105 struct gc_string *gstr,
106 struct gc_string *gsrc)
108 size_t newlen = strlen(gsrc->s) + strlen(gstr->s) + 1;
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;
116 if (alloclen > gstr->alloclen) {
117 struct gc_string *newgstr;
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);
126 strcat(gstr->s, gsrc->s);
128 cds_list_del(&gsrc->gc);
134 void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src)
136 lvalp->gs = gc_string_alloc(parser_ctx, strlen(src) + 1);
137 strcpy(lvalp->gs->s, src);
140 static struct filter_node *make_node(struct filter_parser_ctx *scanner,
143 struct filter_ast *ast = filter_parser_get_ast(scanner);
144 struct filter_node *node;
146 node = malloc(sizeof(*node));
149 memset(node, 0, sizeof(*node));
151 cds_list_add(&node->gc, &ast->allocated_nodes);
155 fprintf(stderr, "[error] %s: trying to create root node\n", __func__);
158 case NODE_EXPRESSION:
167 fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
175 static struct filter_node *make_op_node(struct filter_parser_ctx *scanner,
177 struct filter_node *lchild,
178 struct filter_node *rchild)
180 struct filter_ast *ast = filter_parser_get_ast(scanner);
181 struct filter_node *node;
183 node = malloc(sizeof(*node));
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;
196 void yyerror(struct filter_parser_ctx *parser_ctx, yyscan_t scanner, const char *str)
198 fprintf(stderr, "error %s\n", str);
207 #define parse_error(parser_ctx, str) \
209 yyerror(parser_ctx, parser_ctx->scanner, YY_("parse error: " str "\n")); \
213 static void free_strings(struct cds_list_head *list)
215 struct gc_string *gstr, *tmp;
217 cds_list_for_each_entry_safe(gstr, tmp, list, gc)
221 static struct filter_ast *filter_ast_alloc(void)
223 struct filter_ast *ast;
225 ast = malloc(sizeof(*ast));
228 memset(ast, 0, sizeof(*ast));
229 CDS_INIT_LIST_HEAD(&ast->allocated_nodes);
230 ast->root.type = NODE_ROOT;
234 static void filter_ast_free(struct filter_ast *ast)
236 struct filter_node *node, *tmp;
238 cds_list_for_each_entry_safe(node, tmp, &ast->allocated_nodes, gc)
244 int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx)
246 return yyparse(parser_ctx, parser_ctx->scanner);
250 struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input)
252 struct filter_parser_ctx *parser_ctx;
255 yydebug = filter_parser_debug;
257 parser_ctx = malloc(sizeof(*parser_ctx));
260 memset(parser_ctx, 0, sizeof(*parser_ctx));
262 ret = yylex_init_extra(parser_ctx, &parser_ctx->scanner);
264 fprintf(stderr, "yylex_init error\n");
265 goto cleanup_parser_ctx;
267 /* Start processing new stream */
268 yyrestart(input, parser_ctx->scanner);
270 parser_ctx->ast = filter_ast_alloc();
271 if (!parser_ctx->ast)
273 CDS_INIT_LIST_HEAD(&parser_ctx->allocated_strings);
276 fprintf(stdout, "parser_ctx input is a%s.\n",
277 isatty(fileno(input)) ? "n interactive tty" :
278 " noninteractive file");
283 ret = yylex_destroy(parser_ctx->scanner);
285 fprintf(stderr, "yylex_destroy error\n");
292 void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx)
296 free_strings(&parser_ctx->allocated_strings);
297 filter_ast_free(parser_ctx->ast);
298 ret = yylex_destroy(parser_ctx->scanner);
300 fprintf(stderr, "yylex_destroy error\n");
308 %parse-param {struct filter_parser_ctx *parser_ctx}
309 %parse-param {yyscan_t scanner}
310 %lex-param {yyscan_t scanner}
311 %start translation_unit
312 %token CHARACTER_CONSTANT_START SQUOTE STRING_LITERAL_START DQUOTE
313 %token ESCSEQ CHAR_STRING_TOKEN
314 %token DECIMAL_CONSTANT OCTAL_CONSTANT HEXADECIMAL_CONSTANT FLOAT_CONSTANT
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
322 %token <gs> IDENTIFIER GLOBAL_IDENTIFIER
328 struct gc_string *gs;
329 struct filter_node *n;
332 %type <gs> s_char s_char_sequence c_char c_char_sequence
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
358 | c_char_sequence c_char
359 { $$ = gc_string_append(parser_ctx, $1, $2); }
367 parse_error(parser_ctx, "escape sequences not supported yet");
371 /* 1.6 String literals */
376 | s_char_sequence s_char
377 { $$ = gc_string_append(parser_ctx, $1, $2); }
385 parse_error(parser_ctx, "escape sequences not supported yet");
392 $$ = make_node(parser_ctx, NODE_EXPRESSION);
393 $$->u.expression.type = AST_EXP_IDENTIFIER;
394 $$->u.expression.u.identifier = yylval.gs->s;
398 $$ = make_node(parser_ctx, NODE_EXPRESSION);
399 $$->u.expression.type = AST_EXP_GLOBAL_IDENTIFIER;
400 $$->u.expression.u.identifier = yylval.gs->s;
405 $$ = make_node(parser_ctx, NODE_EXPRESSION);
406 $$->u.expression.type = AST_EXP_CONSTANT;
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");
414 $$ = make_node(parser_ctx, NODE_EXPRESSION);
415 $$->u.expression.type = AST_EXP_CONSTANT;
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");
423 | HEXADECIMAL_CONSTANT
425 $$ = make_node(parser_ctx, NODE_EXPRESSION);
426 $$->u.expression.type = AST_EXP_CONSTANT;
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");
434 $$ = make_node(parser_ctx, NODE_EXPRESSION);
435 $$->u.expression.type = AST_EXP_FLOAT_CONSTANT;
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");
441 | STRING_LITERAL_START DQUOTE
443 $$ = make_node(parser_ctx, NODE_EXPRESSION);
444 $$->u.expression.type = AST_EXP_STRING;
445 $$->u.expression.u.string = "";
447 | STRING_LITERAL_START s_char_sequence DQUOTE
449 $$ = make_node(parser_ctx, NODE_EXPRESSION);
450 $$->u.expression.type = AST_EXP_STRING;
451 $$->u.expression.u.string = $2->s;
453 | CHARACTER_CONSTANT_START c_char_sequence SQUOTE
455 $$ = make_node(parser_ctx, NODE_EXPRESSION);
456 $$->u.expression.type = AST_EXP_STRING;
457 $$->u.expression.u.string = $2->s;
459 | LPAREN expression RPAREN
461 $$ = make_node(parser_ctx, NODE_EXPRESSION);
462 $$->u.expression.type = AST_EXP_NESTED;
463 $$->u.expression.u.child = $2;
470 | postfix_expression DOT IDENTIFIER
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;
478 | postfix_expression RARROW IDENTIFIER
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;
491 | unary_operator unary_expression
494 $$->u.unary_op.child = $2;
501 $$ = make_node(parser_ctx, NODE_UNARY_OP);
502 $$->u.unary_op.type = AST_UNARY_PLUS;
506 $$ = make_node(parser_ctx, NODE_UNARY_OP);
507 $$->u.unary_op.type = AST_UNARY_MINUS;
511 $$ = make_node(parser_ctx, NODE_UNARY_OP);
512 $$->u.unary_op.type = AST_UNARY_NOT;
516 $$ = make_node(parser_ctx, NODE_UNARY_OP);
517 $$->u.unary_op.type = AST_UNARY_BIN_NOT;
521 multiplicative_expression
524 | multiplicative_expression STAR unary_expression
526 $$ = make_op_node(parser_ctx, AST_OP_MUL, $1, $3);
528 | multiplicative_expression DIV_OP unary_expression
530 $$ = make_op_node(parser_ctx, AST_OP_DIV, $1, $3);
532 | multiplicative_expression MOD_OP unary_expression
534 $$ = make_op_node(parser_ctx, AST_OP_MOD, $1, $3);
539 : multiplicative_expression
541 | additive_expression PLUS multiplicative_expression
543 $$ = make_op_node(parser_ctx, AST_OP_PLUS, $1, $3);
545 | additive_expression MINUS multiplicative_expression
547 $$ = make_op_node(parser_ctx, AST_OP_MINUS, $1, $3);
552 : additive_expression
554 | shift_expression LEFT_OP additive_expression
556 $$ = make_op_node(parser_ctx, AST_OP_LSHIFT, $1, $3);
558 | shift_expression RIGHT_OP additive_expression
560 $$ = make_op_node(parser_ctx, AST_OP_RSHIFT, $1, $3);
564 relational_expression
567 | relational_expression LT_OP shift_expression
569 $$ = make_op_node(parser_ctx, AST_OP_LT, $1, $3);
571 | relational_expression GT_OP shift_expression
573 $$ = make_op_node(parser_ctx, AST_OP_GT, $1, $3);
575 | relational_expression LE_OP shift_expression
577 $$ = make_op_node(parser_ctx, AST_OP_LE, $1, $3);
579 | relational_expression GE_OP shift_expression
581 $$ = make_op_node(parser_ctx, AST_OP_GE, $1, $3);
586 : relational_expression
588 | equality_expression EQ_OP relational_expression
590 $$ = make_op_node(parser_ctx, AST_OP_EQ, $1, $3);
592 | equality_expression NE_OP relational_expression
594 $$ = make_op_node(parser_ctx, AST_OP_NE, $1, $3);
599 : equality_expression
601 | and_expression AND_BIN equality_expression
603 $$ = make_op_node(parser_ctx, AST_OP_BIN_AND, $1, $3);
607 exclusive_or_expression
610 | exclusive_or_expression XOR_BIN and_expression
612 $$ = make_op_node(parser_ctx, AST_OP_BIN_XOR, $1, $3);
616 inclusive_or_expression
617 : exclusive_or_expression
619 | inclusive_or_expression OR_BIN exclusive_or_expression
621 $$ = make_op_node(parser_ctx, AST_OP_BIN_OR, $1, $3);
625 logical_and_expression
626 : inclusive_or_expression
628 | logical_and_expression AND_OP inclusive_or_expression
630 $$ = make_op_node(parser_ctx, AST_OP_AND, $1, $3);
634 logical_or_expression
635 : logical_and_expression
637 | logical_or_expression OR_OP logical_and_expression
639 $$ = make_op_node(parser_ctx, AST_OP_OR, $1, $3);
644 : logical_or_expression
651 parser_ctx->ast->root.u.root.child = $1;