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-parser.h"
33 #include "filter-ast.h"
36 int filter_parser_debug = 0;
38 int yyparse(struct filter_parser_ctx *parser_ctx);
39 int yylex(union YYSTYPE *yyval, struct filter_parser_ctx *parser_ctx);
40 int yylex_init_extra(struct filter_parser_ctx *parser_ctx, yyscan_t * ptr_yy_globals);
41 int yylex_destroy(yyscan_t yyparser_ctx);
42 void yyrestart(FILE * in_str, yyscan_t parser_ctx);
45 struct cds_list_head gc;
50 static const char *node_type_to_str[] = {
51 [ NODE_UNKNOWN ] = "NODE_UNKNOWN",
52 [ NODE_ROOT ] = "NODE_ROOT",
53 [ NODE_EXPRESSION ] = "NODE_EXPRESSION",
54 [ NODE_OP ] = "NODE_OP",
55 [ NODE_UNARY_OP ] = "NODE_UNARY_OP",
58 const char *node_type(struct filter_node *node)
60 if (node->type < NR_NODE_TYPES)
61 return node_type_to_str[node->type];
66 static struct gc_string *gc_string_alloc(struct filter_parser_ctx *parser_ctx,
69 struct gc_string *gstr;
72 /* TODO: could be faster with find first bit or glib Gstring */
73 /* sizeof long to account for malloc header (int or long ?) */
74 for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + len;
77 gstr = malloc(alloclen);
78 cds_list_add(&gstr->gc, &parser_ctx->allocated_strings);
79 gstr->alloclen = alloclen;
84 * note: never use gc_string_append on a string that has external references.
85 * gsrc will be garbage collected immediately, and gstr might be.
86 * Should only be used to append characters to a string literal or constant.
88 struct gc_string *gc_string_append(struct filter_parser_ctx *parser_ctx,
89 struct gc_string *gstr,
90 struct gc_string *gsrc)
92 size_t newlen = strlen(gsrc->s) + strlen(gstr->s) + 1;
95 /* TODO: could be faster with find first bit or glib Gstring */
96 /* sizeof long to account for malloc header (int or long ?) */
97 for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + newlen;
100 if (alloclen > gstr->alloclen) {
101 struct gc_string *newgstr;
103 newgstr = gc_string_alloc(parser_ctx, newlen);
104 strcpy(newgstr->s, gstr->s);
105 strcat(newgstr->s, gsrc->s);
106 cds_list_del(&gstr->gc);
110 strcat(gstr->s, gsrc->s);
112 cds_list_del(&gsrc->gc);
117 void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src)
119 lvalp->gs = gc_string_alloc(parser_ctx, strlen(src) + 1);
120 strcpy(lvalp->gs->s, src);
123 static struct filter_node *make_node(struct filter_parser_ctx *scanner,
126 struct filter_ast *ast = filter_parser_get_ast(scanner);
127 struct filter_node *node;
129 node = malloc(sizeof(*node));
132 memset(node, 0, sizeof(*node));
134 cds_list_add(&node->gc, &ast->allocated_nodes);
138 fprintf(stderr, "[error] %s: trying to create root node\n", __func__);
141 case NODE_EXPRESSION:
150 fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
158 static struct filter_node *make_op_node(struct filter_parser_ctx *scanner,
160 struct filter_node *lchild,
161 struct filter_node *rchild)
163 struct filter_ast *ast = filter_parser_get_ast(scanner);
164 struct filter_node *node;
166 node = malloc(sizeof(*node));
169 memset(node, 0, sizeof(*node));
170 node->type = NODE_OP;
171 cds_list_add(&node->gc, &ast->allocated_nodes);
172 node->u.op.type = type;
173 node->u.op.lchild = lchild;
174 node->u.op.rchild = rchild;
178 void yyerror(struct filter_parser_ctx *parser_ctx, const char *str)
180 fprintf(stderr, "error %s\n", str);
188 #define parse_error(parser_ctx, str) \
190 yyerror(parser_ctx, YY_("parse error: " str "\n")); \
194 static void free_strings(struct cds_list_head *list)
196 struct gc_string *gstr, *tmp;
198 cds_list_for_each_entry_safe(gstr, tmp, list, gc)
202 static struct filter_ast *filter_ast_alloc(void)
204 struct filter_ast *ast;
206 ast = malloc(sizeof(*ast));
209 memset(ast, 0, sizeof(*ast));
210 CDS_INIT_LIST_HEAD(&ast->allocated_nodes);
211 ast->root.type = NODE_ROOT;
215 static void filter_ast_free(struct filter_ast *ast)
217 struct filter_node *node, *tmp;
219 cds_list_for_each_entry_safe(node, tmp, &ast->allocated_nodes, gc)
223 int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx)
225 return yyparse(parser_ctx);
228 struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input)
230 struct filter_parser_ctx *parser_ctx;
233 yydebug = filter_parser_debug;
235 parser_ctx = malloc(sizeof(*parser_ctx));
238 memset(parser_ctx, 0, sizeof(*parser_ctx));
240 ret = yylex_init_extra(parser_ctx, &parser_ctx->scanner);
242 fprintf(stderr, "yylex_init error\n");
243 goto cleanup_parser_ctx;
245 /* Start processing new stream */
246 yyrestart(input, parser_ctx->scanner);
248 parser_ctx->ast = filter_ast_alloc();
249 if (!parser_ctx->ast)
251 CDS_INIT_LIST_HEAD(&parser_ctx->allocated_strings);
254 fprintf(stdout, "parser_ctx input is a%s.\n",
255 isatty(fileno(input)) ? "n interactive tty" :
256 " noninteractive file");
261 ret = yylex_destroy(parser_ctx->scanner);
263 fprintf(stderr, "yylex_destroy error\n");
269 void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx)
273 free_strings(&parser_ctx->allocated_strings);
274 filter_ast_free(parser_ctx->ast);
275 ret = yylex_destroy(parser_ctx->scanner);
277 fprintf(stderr, "yylex_destroy error\n");
285 %parse-param {struct filter_parser_ctx *parser_ctx}
286 %lex-param {struct filter_parser_ctx *parser_ctx}
287 %start translation_unit
288 %token CHARACTER_CONSTANT_START SQUOTE STRING_LITERAL_START DQUOTE
289 %token ESCSEQ CHAR_STRING_TOKEN
290 %token DECIMAL_CONSTANT OCTAL_CONSTANT HEXADECIMAL_CONSTANT FLOAT_CONSTANT
291 %token LSBRAC RSBRAC LPAREN RPAREN LBRAC RBRAC RARROW
292 %token STAR PLUS MINUS
293 %token MOD_OP DIV_OP RIGHT_OP LEFT_OP
294 %token EQ_OP NE_OP LE_OP GE_OP LT_OP GT_OP AND_OP OR_OP NOT_OP
295 %token ASSIGN COLON SEMICOLON DOTDOTDOT DOT EQUAL COMMA
296 %token XOR_BIN AND_BIN OR_BIN NOT_BIN
298 %token <gs> IDENTIFIER
304 struct gc_string *gs;
305 struct filter_node *n;
308 %type <gs> s_char s_char_sequence c_char c_char_sequence
310 %type <n> primary_expression
311 %type <n> postfix_expression
312 %type <n> unary_expression
313 %type <n> unary_operator
314 %type <n> multiplicative_expression
315 %type <n> additive_expression
316 %type <n> shift_expression
317 %type <n> relational_expression
318 %type <n> equality_expression
319 %type <n> and_expression
320 %type <n> exclusive_or_expression
321 %type <n> inclusive_or_expression
322 %type <n> logical_and_expression
323 %type <n> logical_or_expression
334 | c_char_sequence c_char
335 { $$ = gc_string_append(parser_ctx, $1, $2); }
343 parse_error(parser_ctx, "escape sequences not supported yet");
347 /* 1.6 String literals */
352 | s_char_sequence s_char
353 { $$ = gc_string_append(parser_ctx, $1, $2); }
361 parse_error(parser_ctx, "escape sequences not supported yet");
368 $$ = make_node(parser_ctx, NODE_EXPRESSION);
369 $$->u.expression.type = AST_EXP_IDENTIFIER;
370 $$->u.expression.u.identifier = yylval.gs->s;
374 $$ = make_node(parser_ctx, NODE_EXPRESSION);
375 $$->u.expression.type = AST_EXP_CONSTANT;
376 sscanf(yylval.gs->s, "%" PRIu64,
377 &$$->u.expression.u.constant);
381 $$ = make_node(parser_ctx, NODE_EXPRESSION);
382 $$->u.expression.type = AST_EXP_CONSTANT;
383 sscanf(yylval.gs->s, "0%" PRIo64,
384 &$$->u.expression.u.constant);
386 | HEXADECIMAL_CONSTANT
388 $$ = make_node(parser_ctx, NODE_EXPRESSION);
389 $$->u.expression.type = AST_EXP_CONSTANT;
390 sscanf(yylval.gs->s, "0x%" PRIx64,
391 &$$->u.expression.u.constant);
395 $$ = make_node(parser_ctx, NODE_EXPRESSION);
396 $$->u.expression.type = AST_EXP_FLOAT_CONSTANT;
397 sscanf(yylval.gs->s, "%lg",
398 &$$->u.expression.u.float_constant);
400 | STRING_LITERAL_START DQUOTE
402 $$ = make_node(parser_ctx, NODE_EXPRESSION);
403 $$->u.expression.type = AST_EXP_STRING;
404 $$->u.expression.u.string = "";
406 | STRING_LITERAL_START s_char_sequence DQUOTE
408 $$ = make_node(parser_ctx, NODE_EXPRESSION);
409 $$->u.expression.type = AST_EXP_STRING;
410 $$->u.expression.u.string = $2->s;
412 | CHARACTER_CONSTANT_START c_char_sequence SQUOTE
414 $$ = make_node(parser_ctx, NODE_EXPRESSION);
415 $$->u.expression.type = AST_EXP_STRING;
416 $$->u.expression.u.string = $2->s;
418 | LPAREN expression RPAREN
420 $$ = make_node(parser_ctx, NODE_EXPRESSION);
421 $$->u.expression.type = AST_EXP_NESTED;
422 $$->u.expression.u.child = $2;
429 | postfix_expression DOT IDENTIFIER
431 $$ = make_node(parser_ctx, NODE_EXPRESSION);
432 $$->u.expression.type = AST_EXP_IDENTIFIER;
433 $$->u.expression.post_op = AST_LINK_DOT;
434 $$->u.expression.u.identifier = $3->s;
435 $$->u.expression.prev = $1;
437 | postfix_expression RARROW IDENTIFIER
439 $$ = make_node(parser_ctx, NODE_EXPRESSION);
440 $$->u.expression.type = AST_EXP_IDENTIFIER;
441 $$->u.expression.post_op = AST_LINK_RARROW;
442 $$->u.expression.u.identifier = $3->s;
443 $$->u.expression.prev = $1;
450 | unary_operator unary_expression
453 $$->u.unary_op.child = $2;
460 $$ = make_node(parser_ctx, NODE_UNARY_OP);
461 $$->u.unary_op.type = AST_UNARY_PLUS;
465 $$ = make_node(parser_ctx, NODE_UNARY_OP);
466 $$->u.unary_op.type = AST_UNARY_MINUS;
470 $$ = make_node(parser_ctx, NODE_UNARY_OP);
471 $$->u.unary_op.type = AST_UNARY_NOT;
475 multiplicative_expression
478 | multiplicative_expression STAR unary_expression
480 $$ = make_op_node(parser_ctx, AST_OP_MUL, $1, $3);
482 | multiplicative_expression DIV_OP unary_expression
484 $$ = make_op_node(parser_ctx, AST_OP_DIV, $1, $3);
486 | multiplicative_expression MOD_OP unary_expression
488 $$ = make_op_node(parser_ctx, AST_OP_MOD, $1, $3);
493 : multiplicative_expression
495 | additive_expression PLUS multiplicative_expression
497 $$ = make_op_node(parser_ctx, AST_OP_PLUS, $1, $3);
499 | additive_expression MINUS multiplicative_expression
501 $$ = make_op_node(parser_ctx, AST_OP_MINUS, $1, $3);
506 : additive_expression
508 | shift_expression LEFT_OP additive_expression
510 $$ = make_op_node(parser_ctx, AST_OP_LSHIFT, $1, $3);
512 | shift_expression RIGHT_OP additive_expression
514 $$ = make_op_node(parser_ctx, AST_OP_RSHIFT, $1, $3);
518 relational_expression
521 | relational_expression LT_OP shift_expression
523 $$ = make_op_node(parser_ctx, AST_OP_LT, $1, $3);
525 | relational_expression GT_OP shift_expression
527 $$ = make_op_node(parser_ctx, AST_OP_GT, $1, $3);
529 | relational_expression LE_OP shift_expression
531 $$ = make_op_node(parser_ctx, AST_OP_LE, $1, $3);
533 | relational_expression GE_OP shift_expression
535 $$ = make_op_node(parser_ctx, AST_OP_GE, $1, $3);
540 : relational_expression
542 | equality_expression EQ_OP relational_expression
544 $$ = make_op_node(parser_ctx, AST_OP_EQ, $1, $3);
546 | equality_expression NE_OP relational_expression
548 $$ = make_op_node(parser_ctx, AST_OP_NE, $1, $3);
553 : equality_expression
555 | and_expression AND_BIN equality_expression
557 $$ = make_op_node(parser_ctx, AST_OP_BIN_AND, $1, $3);
561 exclusive_or_expression
564 | exclusive_or_expression XOR_BIN and_expression
566 $$ = make_op_node(parser_ctx, AST_OP_BIN_XOR, $1, $3);
570 inclusive_or_expression
571 : exclusive_or_expression
573 | inclusive_or_expression OR_BIN exclusive_or_expression
575 $$ = make_op_node(parser_ctx, AST_OP_BIN_OR, $1, $3);
579 logical_and_expression
580 : inclusive_or_expression
582 | logical_and_expression AND_OP inclusive_or_expression
584 $$ = make_op_node(parser_ctx, AST_OP_AND, $1, $3);
588 logical_or_expression
589 : logical_and_expression
591 | logical_or_expression OR_OP logical_and_expression
593 $$ = make_op_node(parser_ctx, AST_OP_OR, $1, $3);
598 : logical_or_expression
605 parser_ctx->ast->root.u.root.child = $1;