107516736SAndi Kleen /* Simple expression parser */ 207516736SAndi Kleen %{ 326226a97SJiri Olsa #define YYDEBUG 1 426226a97SJiri Olsa #include <stdio.h> 507516736SAndi Kleen #include "util.h" 607516736SAndi Kleen #include "util/debug.h" 78520a98dSArnaldo Carvalho de Melo #include <stdlib.h> // strtod() 807516736SAndi Kleen #define IN_EXPR_Y 1 907516736SAndi Kleen #include "expr.h" 10d73bad06SAndi Kleen #include "smt.h" 1107516736SAndi Kleen #include <string.h> 1207516736SAndi Kleen 13*3e21a28aSIan Rogers static double d_ratio(double val0, double val1) 14*3e21a28aSIan Rogers { 15*3e21a28aSIan Rogers if (val1 == 0) { 16*3e21a28aSIan Rogers return 0; 17*3e21a28aSIan Rogers } 18*3e21a28aSIan Rogers return val0 / val1; 19*3e21a28aSIan Rogers } 20*3e21a28aSIan Rogers 2107516736SAndi Kleen %} 2207516736SAndi Kleen 23fc8c0a99SJiri Olsa %define api.pure full 24fc8c0a99SJiri Olsa 2507516736SAndi Kleen %parse-param { double *final_val } 26aecce63eSJiri Olsa %parse-param { struct expr_parse_ctx *ctx } 2726226a97SJiri Olsa %parse-param {void *scanner} 2826226a97SJiri Olsa %lex-param {void* scanner} 2907516736SAndi Kleen 3007516736SAndi Kleen %union { 3107516736SAndi Kleen double num; 3226226a97SJiri Olsa char *str; 3307516736SAndi Kleen } 3407516736SAndi Kleen 3526226a97SJiri Olsa %token EXPR_PARSE EXPR_OTHER EXPR_ERROR 3607516736SAndi Kleen %token <num> NUMBER 3726226a97SJiri Olsa %token <str> ID 3863657578SIan Rogers %destructor { free ($$); } <str> 39*3e21a28aSIan Rogers %token MIN MAX IF ELSE SMT_ON D_RATIO 40d73bad06SAndi Kleen %left MIN MAX IF 4107516736SAndi Kleen %left '|' 4207516736SAndi Kleen %left '^' 4307516736SAndi Kleen %left '&' 4407516736SAndi Kleen %left '-' '+' 4507516736SAndi Kleen %left '*' '/' '%' 4607516736SAndi Kleen %left NEG NOT 47d73bad06SAndi Kleen %type <num> expr if_expr 4807516736SAndi Kleen 4907516736SAndi Kleen %{ 5026226a97SJiri Olsa static void expr_error(double *final_val __maybe_unused, 51aecce63eSJiri Olsa struct expr_parse_ctx *ctx __maybe_unused, 5226226a97SJiri Olsa void *scanner, 5307516736SAndi Kleen const char *s) 5407516736SAndi Kleen { 5507516736SAndi Kleen pr_debug("%s\n", s); 5607516736SAndi Kleen } 5707516736SAndi Kleen 5807516736SAndi Kleen %} 5907516736SAndi Kleen %% 6007516736SAndi Kleen 6126226a97SJiri Olsa start: 6226226a97SJiri Olsa EXPR_PARSE all_expr 6326226a97SJiri Olsa | 6426226a97SJiri Olsa EXPR_OTHER all_other 6526226a97SJiri Olsa 6626226a97SJiri Olsa all_other: all_other other 6726226a97SJiri Olsa | 6826226a97SJiri Olsa 6926226a97SJiri Olsa other: ID 7026226a97SJiri Olsa { 71ded80bdaSIan Rogers expr__add_id(ctx, $1, 0.0); 7226226a97SJiri Olsa } 7326226a97SJiri Olsa | 74cb59fa79SIan Rogers MIN | MAX | IF | ELSE | SMT_ON | NUMBER | '|' | '^' | '&' | '-' | '+' | '*' | '/' | '%' | '(' | ')' | ',' 75*3e21a28aSIan Rogers | 76*3e21a28aSIan Rogers D_RATIO 7726226a97SJiri Olsa 78d73bad06SAndi Kleen all_expr: if_expr { *final_val = $1; } 79d73bad06SAndi Kleen ; 80d73bad06SAndi Kleen 81d73bad06SAndi Kleen if_expr: 82d73bad06SAndi Kleen expr IF expr ELSE expr { $$ = $3 ? $1 : $5; } 83d73bad06SAndi Kleen | expr 8407516736SAndi Kleen ; 8507516736SAndi Kleen 8607516736SAndi Kleen expr: NUMBER 87ded80bdaSIan Rogers | ID { if (expr__get_id(ctx, $1, &$$)) { 88c295036bSAndi Kleen pr_debug("%s not found\n", $1); 8963657578SIan Rogers free($1); 9007516736SAndi Kleen YYABORT; 9107516736SAndi Kleen } 9263657578SIan Rogers free($1); 9307516736SAndi Kleen } 94d73bad06SAndi Kleen | expr '|' expr { $$ = (long)$1 | (long)$3; } 95d73bad06SAndi Kleen | expr '&' expr { $$ = (long)$1 & (long)$3; } 96d73bad06SAndi Kleen | expr '^' expr { $$ = (long)$1 ^ (long)$3; } 9707516736SAndi Kleen | expr '+' expr { $$ = $1 + $3; } 9807516736SAndi Kleen | expr '-' expr { $$ = $1 - $3; } 9907516736SAndi Kleen | expr '*' expr { $$ = $1 * $3; } 1009be27a5dSIan Rogers | expr '/' expr { if ($3 == 0) { 1019be27a5dSIan Rogers pr_debug("division by zero\n"); 1029be27a5dSIan Rogers YYABORT; 1039be27a5dSIan Rogers } 1049be27a5dSIan Rogers $$ = $1 / $3; 1059be27a5dSIan Rogers } 1069be27a5dSIan Rogers | expr '%' expr { if ((long)$3 == 0) { 1079be27a5dSIan Rogers pr_debug("division by zero\n"); 1089be27a5dSIan Rogers YYABORT; 1099be27a5dSIan Rogers } 1109be27a5dSIan Rogers $$ = (long)$1 % (long)$3; 1119be27a5dSIan Rogers } 11207516736SAndi Kleen | '-' expr %prec NEG { $$ = -$2; } 113d73bad06SAndi Kleen | '(' if_expr ')' { $$ = $2; } 114d73bad06SAndi Kleen | MIN '(' expr ',' expr ')' { $$ = $3 < $5 ? $3 : $5; } 115d73bad06SAndi Kleen | MAX '(' expr ',' expr ')' { $$ = $3 > $5 ? $3 : $5; } 116d73bad06SAndi Kleen | SMT_ON { $$ = smt_on() > 0; } 117*3e21a28aSIan Rogers | D_RATIO '(' expr ',' expr ')' { $$ = d_ratio($3,$5); } 11807516736SAndi Kleen ; 11907516736SAndi Kleen 12007516736SAndi Kleen %% 121