summaryrefslogtreecommitdiff
path: root/parse_expr.yy
blob: c2943c220b43c691f81e666d28b1ca638346c364 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
%{
#include "chill_run_util.hh"
#include "parse_expr.ll.hh"

extern int yydebug;

void yyerror(const char*);
int yyparse(simap_vec_t** rel);

static simap_vec_t* return_rel; // used as the return value for yyparse

%}

%union {
  int val;
  char* str_val;
  simap_t* cond_item;
  simap_vec_t* cond;
}

%token <val> NUMBER
%token <val> LEVEL
%token <str_val> VARIABLE

%left LE GE EQ '<' '>'
%left '-' '+' '*' '/'

/*the final output from this language should be an Omega Relation object*/
%type <cond> cond prog
%type <cond_item> expr add_expr mul_expr neg_expr

%%
prog : cond                      { return_rel = make_prog($1); }
;

cond : expr '>' expr             { $$ = make_cond_gt($1, $3); }
     | expr '<' expr             { $$ = make_cond_lt($1, $3); }
     | expr GE expr              { $$ = make_cond_ge($1, $3); }
     | expr LE expr              { $$ = make_cond_le($1, $3); }
     | expr EQ expr              { $$ = make_cond_eq($1, $3); }
;

expr : add_expr                  { $$ = $1; }
;

add_expr : add_expr '+' mul_expr { $$ = make_cond_item_add($1,$3); }
         | add_expr '-' mul_expr { $$ = make_cond_item_sub($1,$3); }
         | mul_expr              { $$ = $1; }
;

mul_expr : mul_expr '*' neg_expr { $$ = make_cond_item_mul($1,$3); }
         | neg_expr              { $$ = $1; }
;

neg_expr : '-' neg_expr          { $$ = make_cond_item_neg($2); }
         | '(' expr ')'          { $$ = $2; }
         | NUMBER                { $$ = make_cond_item_number($1); }
         | LEVEL                 { $$ = make_cond_item_level($1); }
         | VARIABLE              { $$ = make_cond_item_variable($1); }
;
%%

void yyerror(const char* msg) {
  fprintf(stderr, "Parse error: %s", msg);
}

simap_vec_t* parse_relation_vector(const char* expr) {
  yydebug=0;
  YY_BUFFER_STATE state;
  
  //if(yylex_init()) {
  //   TODO: error out or something
  //}
  
  state = yy_scan_string(expr);
  
  if(yyparse()) {
    // TODO: error out or something
  }
  
  yy_delete_buffer(state);
  yylex_destroy();
  return return_rel;
}