Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / perf / util / expr.y
blobb2ada8f8309a91f0cbd70d6dcc6593502b098d04
1 /* Simple expression parser */
2 %{
3 #define YYDEBUG 1
4 #include <stdio.h>
5 #include "util.h"
6 #include "util/debug.h"
7 #include <stdlib.h> // strtod()
8 #define IN_EXPR_Y 1
9 #include "expr.h"
10 #include "smt.h"
11 #include <string.h>
13 static double d_ratio(double val0, double val1)
15 if (val1 == 0) {
16 return 0;
18 return val0 / val1;
23 %define api.pure full
25 %parse-param { double *final_val }
26 %parse-param { struct expr_parse_ctx *ctx }
27 %parse-param {void *scanner}
28 %lex-param {void* scanner}
30 %union {
31 double num;
32 char *str;
35 %token EXPR_PARSE EXPR_OTHER EXPR_ERROR
36 %token <num> NUMBER
37 %token <str> ID
38 %destructor { free ($$); } <str>
39 %token MIN MAX IF ELSE SMT_ON D_RATIO
40 %left MIN MAX IF
41 %left '|'
42 %left '^'
43 %left '&'
44 %left '<' '>'
45 %left '-' '+'
46 %left '*' '/' '%'
47 %left NEG NOT
48 %type <num> expr if_expr
51 static void expr_error(double *final_val __maybe_unused,
52 struct expr_parse_ctx *ctx __maybe_unused,
53 void *scanner,
54 const char *s)
56 pr_debug("%s\n", s);
62 start:
63 EXPR_PARSE all_expr
65 EXPR_OTHER all_other
67 all_other: all_other other
70 other: ID
72 expr__add_id(ctx, $1);
75 MIN | MAX | IF | ELSE | SMT_ON | NUMBER | '|' | '^' | '&' | '-' | '+' | '*' | '/' | '%' | '(' | ')' | ','
77 '<' | '>' | D_RATIO
79 all_expr: if_expr { *final_val = $1; }
82 if_expr:
83 expr IF expr ELSE expr { $$ = $3 ? $1 : $5; }
84 | expr
87 expr: NUMBER
88 | ID {
89 struct expr_id_data *data;
91 if (expr__resolve_id(ctx, $1, &data)) {
92 free($1);
93 YYABORT;
96 $$ = expr_id_data__value(data);
97 free($1);
99 | expr '|' expr { $$ = (long)$1 | (long)$3; }
100 | expr '&' expr { $$ = (long)$1 & (long)$3; }
101 | expr '^' expr { $$ = (long)$1 ^ (long)$3; }
102 | expr '<' expr { $$ = $1 < $3; }
103 | expr '>' expr { $$ = $1 > $3; }
104 | expr '+' expr { $$ = $1 + $3; }
105 | expr '-' expr { $$ = $1 - $3; }
106 | expr '*' expr { $$ = $1 * $3; }
107 | expr '/' expr { if ($3 == 0) {
108 pr_debug("division by zero\n");
109 YYABORT;
111 $$ = $1 / $3;
113 | expr '%' expr { if ((long)$3 == 0) {
114 pr_debug("division by zero\n");
115 YYABORT;
117 $$ = (long)$1 % (long)$3;
119 | '-' expr %prec NEG { $$ = -$2; }
120 | '(' if_expr ')' { $$ = $2; }
121 | MIN '(' expr ',' expr ')' { $$ = $3 < $5 ? $3 : $5; }
122 | MAX '(' expr ',' expr ')' { $$ = $3 > $5 ? $3 : $5; }
123 | SMT_ON { $$ = smt_on() > 0; }
124 | D_RATIO '(' expr ',' expr ')' { $$ = d_ratio($3,$5); }