2017-03-21 04:17:05 +08:00
|
|
|
/* Simple expression parser */
|
|
|
|
%{
|
2020-02-28 17:36:13 +08:00
|
|
|
#define YYDEBUG 1
|
2021-09-23 15:46:05 +08:00
|
|
|
#include <math.h>
|
2017-03-21 04:17:05 +08:00
|
|
|
#include "util/debug.h"
|
2021-09-23 15:46:06 +08:00
|
|
|
#include "smt.h"
|
2017-03-21 04:17:05 +08:00
|
|
|
#define IN_EXPR_Y 1
|
|
|
|
#include "expr.h"
|
|
|
|
%}
|
|
|
|
|
2020-01-13 03:22:59 +08:00
|
|
|
%define api.pure full
|
|
|
|
|
2017-03-21 04:17:05 +08:00
|
|
|
%parse-param { double *final_val }
|
2020-04-02 04:33:34 +08:00
|
|
|
%parse-param { struct expr_parse_ctx *ctx }
|
2020-02-28 17:36:13 +08:00
|
|
|
%parse-param {void *scanner}
|
|
|
|
%lex-param {void* scanner}
|
2017-03-21 04:17:05 +08:00
|
|
|
|
|
|
|
%union {
|
2020-02-28 17:36:13 +08:00
|
|
|
double num;
|
|
|
|
char *str;
|
2017-03-21 04:17:05 +08:00
|
|
|
}
|
|
|
|
|
2021-09-23 15:46:07 +08:00
|
|
|
%token ID NUMBER MIN MAX IF ELSE SMT_ON D_RATIO EXPR_ERROR EXPR_PARSE EXPR_OTHER
|
2017-08-12 07:26:23 +08:00
|
|
|
%left MIN MAX IF
|
2017-03-21 04:17:05 +08:00
|
|
|
%left '|'
|
|
|
|
%left '^'
|
|
|
|
%left '&'
|
2020-06-11 07:58:23 +08:00
|
|
|
%left '<' '>'
|
2017-03-21 04:17:05 +08:00
|
|
|
%left '-' '+'
|
|
|
|
%left '*' '/' '%'
|
|
|
|
%left NEG NOT
|
2021-09-23 15:46:07 +08:00
|
|
|
%type <num> NUMBER
|
|
|
|
%type <str> ID
|
|
|
|
%destructor { free ($$); } <str>
|
2017-08-12 07:26:23 +08:00
|
|
|
%type <num> expr if_expr
|
2017-03-21 04:17:05 +08:00
|
|
|
|
|
|
|
%{
|
2020-02-28 17:36:13 +08:00
|
|
|
static void expr_error(double *final_val __maybe_unused,
|
2020-04-02 04:33:34 +08:00
|
|
|
struct expr_parse_ctx *ctx __maybe_unused,
|
2020-02-28 17:36:13 +08:00
|
|
|
void *scanner,
|
2017-03-21 04:17:05 +08:00
|
|
|
const char *s)
|
|
|
|
{
|
|
|
|
pr_debug("%s\n", s);
|
|
|
|
}
|
|
|
|
|
2021-09-23 15:46:08 +08:00
|
|
|
#define BINARY_LONG_OP(RESULT, OP, LHS, RHS) \
|
|
|
|
RESULT = (long)LHS OP (long)RHS;
|
|
|
|
|
|
|
|
#define BINARY_OP(RESULT, OP, LHS, RHS) \
|
|
|
|
RESULT = LHS OP RHS;
|
|
|
|
|
2017-03-21 04:17:05 +08:00
|
|
|
%}
|
|
|
|
%%
|
|
|
|
|
2020-02-28 17:36:13 +08:00
|
|
|
start:
|
|
|
|
EXPR_PARSE all_expr
|
|
|
|
|
|
|
|
|
EXPR_OTHER all_other
|
|
|
|
|
|
|
|
all_other: all_other other
|
|
|
|
|
|
|
|
|
|
|
|
|
other: ID
|
|
|
|
{
|
2020-07-20 02:13:03 +08:00
|
|
|
expr__add_id(ctx, $1);
|
2020-02-28 17:36:13 +08:00
|
|
|
}
|
|
|
|
|
|
2020-05-02 01:33:25 +08:00
|
|
|
MIN | MAX | IF | ELSE | SMT_ON | NUMBER | '|' | '^' | '&' | '-' | '+' | '*' | '/' | '%' | '(' | ')' | ','
|
2020-06-11 07:58:22 +08:00
|
|
|
|
|
2020-06-11 07:58:23 +08:00
|
|
|
'<' | '>' | D_RATIO
|
2020-02-28 17:36:13 +08:00
|
|
|
|
2017-08-12 07:26:23 +08:00
|
|
|
all_expr: if_expr { *final_val = $1; }
|
|
|
|
;
|
|
|
|
|
|
|
|
if_expr:
|
|
|
|
expr IF expr ELSE expr { $$ = $3 ? $1 : $5; }
|
|
|
|
| expr
|
2017-03-21 04:17:05 +08:00
|
|
|
;
|
|
|
|
|
|
|
|
expr: NUMBER
|
2020-07-20 02:13:04 +08:00
|
|
|
| ID {
|
|
|
|
struct expr_id_data *data;
|
|
|
|
|
2021-09-23 15:46:05 +08:00
|
|
|
$$ = NAN;
|
|
|
|
if (expr__resolve_id(ctx, $1, &data) == 0)
|
|
|
|
$$ = expr_id_data__value(data);
|
2020-07-20 02:13:12 +08:00
|
|
|
|
2020-05-13 08:03:18 +08:00
|
|
|
free($1);
|
2017-03-21 04:17:05 +08:00
|
|
|
}
|
2021-09-23 15:46:08 +08:00
|
|
|
| expr '|' expr { BINARY_LONG_OP($$, |, $1, $3); }
|
|
|
|
| expr '&' expr { BINARY_LONG_OP($$, &, $1, $3); }
|
|
|
|
| expr '^' expr { BINARY_LONG_OP($$, ^, $1, $3); }
|
|
|
|
| expr '<' expr { BINARY_OP($$, <, $1, $3); }
|
|
|
|
| expr '>' expr { BINARY_OP($$, >, $1, $3); }
|
|
|
|
| expr '+' expr { BINARY_OP($$, +, $1, $3); }
|
|
|
|
| expr '-' expr { BINARY_OP($$, -, $1, $3); }
|
|
|
|
| expr '*' expr { BINARY_OP($$, *, $1, $3); }
|
2020-05-02 01:33:31 +08:00
|
|
|
| expr '/' expr { if ($3 == 0) {
|
|
|
|
pr_debug("division by zero\n");
|
|
|
|
YYABORT;
|
|
|
|
}
|
|
|
|
$$ = $1 / $3;
|
|
|
|
}
|
|
|
|
| expr '%' expr { if ((long)$3 == 0) {
|
|
|
|
pr_debug("division by zero\n");
|
|
|
|
YYABORT;
|
|
|
|
}
|
|
|
|
$$ = (long)$1 % (long)$3;
|
|
|
|
}
|
2017-03-21 04:17:05 +08:00
|
|
|
| '-' expr %prec NEG { $$ = -$2; }
|
2017-08-12 07:26:23 +08:00
|
|
|
| '(' if_expr ')' { $$ = $2; }
|
|
|
|
| MIN '(' expr ',' expr ')' { $$ = $3 < $5 ? $3 : $5; }
|
|
|
|
| MAX '(' expr ',' expr ')' { $$ = $3 > $5 ? $3 : $5; }
|
|
|
|
| SMT_ON { $$ = smt_on() > 0; }
|
2021-09-23 15:46:06 +08:00
|
|
|
| D_RATIO '(' expr ',' expr ')' { if ($5 == 0) {
|
|
|
|
$$ = 0;
|
|
|
|
} else {
|
|
|
|
$$ = $3 / $5;
|
|
|
|
}
|
|
|
|
}
|
2017-03-21 04:17:05 +08:00
|
|
|
;
|
|
|
|
|
|
|
|
%%
|