-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.y
68 lines (56 loc) · 1.23 KB
/
expression.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
%{
#include <iostream>
#include <string>
#include "ROBDD.hpp"
int yylex(void);
void yyerror(const char *s);
extern ROBDD *T;
extern bool frontend_err;
%}
%code requires {
#include "ROBDD.hpp"
}
%token RET LPAREN RPAREN
%token OR AND NOT THEN XNOR XOR
%token <var> VAR
%type <node> boolean_expr expr func term atom
%type <op> binary_op
%start boolean_expr
%union {
char *var;
BDD_node *node;
char op;
}
%%
boolean_expr
: expr {T->set_root($1);} RET {YYABORT;} //ret work as EOF
expr
: expr THEN func {$$ = T->apply(OP_THEN, $1, $3);}
| func {$$ = $1;}
func
: func binary_op term {
switch($2){
case '|': $$ = T->apply(OP_OR, $1, $3); break;
case '&': $$ = T->apply(OP_AND, $1, $3); break;
case '=': $$ = T->apply(OP_XNOR, $1, $3); break;
case 'x': $$ = T->apply(OP_XOR, $1, $3); break;
}
}
| term {$$ = $1;}
term
: NOT atom {$$ = T->make_node($2->var, $2->high, $2->low);}
| atom {$$ = $1;}
atom
: LPAREN expr RPAREN {$$ = $2;}
| VAR {unsigned int ID = T->get_ID(std::string($1)); $$ = T->make_node(ID, T->get_zero(), T->get_one());}
binary_op
: OR {$$ = '|';}
| AND {$$ = '&';}
| XNOR {$$ = '=';}
| XOR {$$ = 'x';}
%%
void yyerror (const char *s)
{
std::cout << s << std::endl;
frontend_err = true;
}