2017-03-20 20:17:05 +00:00
|
|
|
/* Simple expression parser */
|
|
|
|
%{
|
2020-02-28 09:36:13 +00:00
|
|
|
#define YYDEBUG 1
|
|
|
|
#include <stdio.h>
|
2017-03-20 20:17:05 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "util/debug.h"
|
2019-08-29 19:18:59 +00:00
|
|
|
#include <stdlib.h> // strtod()
|
2017-03-20 20:17:05 +00:00
|
|
|
#define IN_EXPR_Y 1
|
|
|
|
#include "expr.h"
|
2017-08-11 23:26:23 +00:00
|
|
|
#include "smt.h"
|
2017-03-20 20:17:05 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2020-06-10 23:58:22 +00:00
|
|
|
static double d_ratio(double val0, double val1)
|
|
|
|
{
|
|
|
|
if (val1 == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return val0 / val1;
|
|
|
|
}
|
|
|
|
|
2017-03-20 20:17:05 +00:00
|
|
|
%}
|
|
|
|
|
2020-01-12 19:22:59 +00:00
|
|
|
%define api.pure full
|
|
|
|
|
2017-03-20 20:17:05 +00:00
|
|
|
%parse-param { double *final_val }
|
2020-04-01 20:33:34 +00:00
|
|
|
%parse-param { struct expr_parse_ctx *ctx }
|
2020-02-28 09:36:13 +00:00
|
|
|
%parse-param {void *scanner}
|
|
|
|
%lex-param {void* scanner}
|
2017-03-20 20:17:05 +00:00
|
|
|
|
|
|
|
%union {
|
2020-02-28 09:36:13 +00:00
|
|
|
double num;
|
|
|
|
char *str;
|
2017-03-20 20:17:05 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 09:36:13 +00:00
|
|
|
%token EXPR_PARSE EXPR_OTHER EXPR_ERROR
|
2017-03-20 20:17:05 +00:00
|
|
|
%token <num> NUMBER
|
2020-02-28 09:36:13 +00:00
|
|
|
%token <str> ID
|
2020-05-13 00:03:18 +00:00
|
|
|
%destructor { free ($$); } <str>
|
2020-06-10 23:58:22 +00:00
|
|
|
%token MIN MAX IF ELSE SMT_ON D_RATIO
|
2017-08-11 23:26:23 +00:00
|
|
|
%left MIN MAX IF
|
2017-03-20 20:17:05 +00:00
|
|
|
%left '|'
|
|
|
|
%left '^'
|
|
|
|
%left '&'
|
2020-06-10 23:58:23 +00:00
|
|
|
%left '<' '>'
|
2017-03-20 20:17:05 +00:00
|
|
|
%left '-' '+'
|
|
|
|
%left '*' '/' '%'
|
|
|
|
%left NEG NOT
|
2017-08-11 23:26:23 +00:00
|
|
|
%type <num> expr if_expr
|
2017-03-20 20:17:05 +00:00
|
|
|
|
|
|
|
%{
|
2020-02-28 09:36:13 +00:00
|
|
|
static void expr_error(double *final_val __maybe_unused,
|
2020-04-01 20:33:34 +00:00
|
|
|
struct expr_parse_ctx *ctx __maybe_unused,
|
2020-02-28 09:36:13 +00:00
|
|
|
void *scanner,
|
2017-03-20 20:17:05 +00:00
|
|
|
const char *s)
|
|
|
|
{
|
|
|
|
pr_debug("%s\n", s);
|
|
|
|
}
|
|
|
|
|
|
|
|
%}
|
|
|
|
%%
|
|
|
|
|
2020-02-28 09:36:13 +00:00
|
|
|
start:
|
|
|
|
EXPR_PARSE all_expr
|
|
|
|
|
|
|
|
|
EXPR_OTHER all_other
|
|
|
|
|
|
|
|
all_other: all_other other
|
|
|
|
|
|
|
|
|
|
|
|
|
other: ID
|
|
|
|
{
|
2020-07-19 18:13:03 +00:00
|
|
|
expr__add_id(ctx, $1);
|
2020-02-28 09:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
2020-05-01 17:33:25 +00:00
|
|
|
MIN | MAX | IF | ELSE | SMT_ON | NUMBER | '|' | '^' | '&' | '-' | '+' | '*' | '/' | '%' | '(' | ')' | ','
|
2020-06-10 23:58:22 +00:00
|
|
|
|
|
2020-06-10 23:58:23 +00:00
|
|
|
'<' | '>' | D_RATIO
|
2020-02-28 09:36:13 +00:00
|
|
|
|
2017-08-11 23:26:23 +00:00
|
|
|
all_expr: if_expr { *final_val = $1; }
|
|
|
|
;
|
|
|
|
|
|
|
|
if_expr:
|
|
|
|
expr IF expr ELSE expr { $$ = $3 ? $1 : $5; }
|
|
|
|
| expr
|
2017-03-20 20:17:05 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
expr: NUMBER
|
2020-07-19 18:13:04 +00:00
|
|
|
| ID {
|
|
|
|
struct expr_id_data *data;
|
|
|
|
|
2020-07-19 18:13:12 +00:00
|
|
|
if (expr__resolve_id(ctx, $1, &data)) {
|
2020-07-19 18:13:04 +00:00
|
|
|
free($1);
|
|
|
|
YYABORT;
|
|
|
|
}
|
2020-07-19 18:13:12 +00:00
|
|
|
|
2020-07-19 18:13:04 +00:00
|
|
|
$$ = data->val;
|
2020-05-13 00:03:18 +00:00
|
|
|
free($1);
|
2017-03-20 20:17:05 +00:00
|
|
|
}
|
2017-08-11 23:26:23 +00:00
|
|
|
| expr '|' expr { $$ = (long)$1 | (long)$3; }
|
|
|
|
| expr '&' expr { $$ = (long)$1 & (long)$3; }
|
|
|
|
| expr '^' expr { $$ = (long)$1 ^ (long)$3; }
|
2020-06-10 23:58:23 +00:00
|
|
|
| expr '<' expr { $$ = $1 < $3; }
|
|
|
|
| expr '>' expr { $$ = $1 > $3; }
|
2017-03-20 20:17:05 +00:00
|
|
|
| expr '+' expr { $$ = $1 + $3; }
|
|
|
|
| expr '-' expr { $$ = $1 - $3; }
|
|
|
|
| expr '*' expr { $$ = $1 * $3; }
|
2020-05-01 17:33:31 +00: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-20 20:17:05 +00:00
|
|
|
| '-' expr %prec NEG { $$ = -$2; }
|
2017-08-11 23:26:23 +00: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-10 23:58:22 +00:00
|
|
|
| D_RATIO '(' expr ',' expr ')' { $$ = d_ratio($3,$5); }
|
2017-03-20 20:17:05 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
%%
|