2017-03-21 04:17:05 +08:00
|
|
|
/* Simple expression parser */
|
|
|
|
%{
|
2020-02-28 17:36:13 +08:00
|
|
|
#define YYDEBUG 1
|
|
|
|
#include <stdio.h>
|
2017-03-21 04:17:05 +08:00
|
|
|
#include "util.h"
|
|
|
|
#include "util/debug.h"
|
2019-08-30 03:18:59 +08:00
|
|
|
#include <stdlib.h> // strtod()
|
2017-03-21 04:17:05 +08:00
|
|
|
#define IN_EXPR_Y 1
|
|
|
|
#include "expr.h"
|
2017-08-12 07:26:23 +08:00
|
|
|
#include "smt.h"
|
2017-03-21 04:17:05 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2020-06-11 07:58:22 +08:00
|
|
|
static double d_ratio(double val0, double val1)
|
|
|
|
{
|
|
|
|
if (val1 == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return val0 / val1;
|
|
|
|
}
|
|
|
|
|
2017-03-21 04:17:05 +08:00
|
|
|
%}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-28 17:36:13 +08:00
|
|
|
%token EXPR_PARSE EXPR_OTHER EXPR_ERROR
|
2017-03-21 04:17:05 +08:00
|
|
|
%token <num> NUMBER
|
2020-02-28 17:36:13 +08:00
|
|
|
%token <str> ID
|
2020-05-13 08:03:18 +08:00
|
|
|
%destructor { free ($$); } <str>
|
2020-06-11 07:58:22 +08:00
|
|
|
%token MIN MAX IF ELSE SMT_ON D_RATIO
|
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
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
%}
|
|
|
|
%%
|
|
|
|
|
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-12 21:26:17 +08:00
|
|
|
expr__add_id_val(ctx, $1, 0.0);
|
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-05-16 06:17:32 +08:00
|
|
|
| ID { if (expr__get_id(ctx, $1, &$$)) {
|
2017-07-25 07:40:05 +08:00
|
|
|
pr_debug("%s not found\n", $1);
|
2020-05-13 08:03:18 +08:00
|
|
|
free($1);
|
2017-03-21 04:17:05 +08:00
|
|
|
YYABORT;
|
|
|
|
}
|
2020-05-13 08:03:18 +08:00
|
|
|
free($1);
|
2017-03-21 04:17:05 +08:00
|
|
|
}
|
2017-08-12 07:26:23 +08:00
|
|
|
| expr '|' expr { $$ = (long)$1 | (long)$3; }
|
|
|
|
| expr '&' expr { $$ = (long)$1 & (long)$3; }
|
|
|
|
| expr '^' expr { $$ = (long)$1 ^ (long)$3; }
|
2020-06-11 07:58:23 +08:00
|
|
|
| expr '<' expr { $$ = $1 < $3; }
|
|
|
|
| expr '>' expr { $$ = $1 > $3; }
|
2017-03-21 04:17:05 +08:00
|
|
|
| expr '+' expr { $$ = $1 + $3; }
|
|
|
|
| expr '-' expr { $$ = $1 - $3; }
|
|
|
|
| expr '*' expr { $$ = $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; }
|
2020-06-11 07:58:22 +08:00
|
|
|
| D_RATIO '(' expr ',' expr ')' { $$ = d_ratio($3,$5); }
|
2017-03-21 04:17:05 +08:00
|
|
|
;
|
|
|
|
|
|
|
|
%%
|