aboutsummaryrefslogtreecommitdiffstats
path: root/test/littlesemantics/little.y
blob: 0c4bd6108c70d92f6cc3a1882462a29657a4b31e (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
%{
#include <stdio.h>
#include <string.h>
#include "little.h"
#define YYSTYPE term *

  char *lextext;
  int lexnum;

  term *revert_env(term *t) {
    term *r = NULL, *t1;
    while(NULL != t) {
      t1 = r;
      r = t;
      t = child(t,2);
      r->term_body.children[1] = t1;
    }
    return r;
  }

  void yyerror(char const *s) {
    fprintf(stderr, "%s\n", s);
  }
%}

%token T_VARIABLES T_IN T_END T_WHILE T_DO T_DONE T_GT
%token T_ASSIGN T_PLUS T_SCOLUMN T_OPEN T_CLOSE T_OPEN_B T_CLOSE_B
%token T_SKIP NUM ID 
%left T_PLUS
%right T_SCOLUMN
%%
program : T_VARIABLES environment T_IN inst T_END
{ print_env(execute(revert_env($2),$4)); exit(0) }
;
b_exp: exp T_GT exp {$$=greater($1, $3); }
;
num : NUM { $$=numeral(lexnum); }
;
identifier : ID { $$=variable(lextext); }
;
variable_value : identifier num { $$=variable_value($1,$2);}
;
environment : variable_value { $$ = variable_list($1, NULL); }
| environment T_SCOLUMN variable_value { $$ = variable_list($3, $1); }
;
inst: identifier T_ASSIGN exp {$$ = assignment($1,$3); }
|  inst T_SCOLUMN inst {$$ = sequence($1,$3); }
|  T_WHILE b_exp T_DO inst T_DONE {$$ = term_while($2,$4); } 
|  T_SKIP { $$ = skip(); }
|  T_OPEN_B exp T_CLOSE_B { $$ = $2; }
;
exp: num {$$= $1;}
|    identifier {$$=$1; }
|    exp T_PLUS exp {$$=addition($1, $3); }
|    T_OPEN exp T_CLOSE { $$= $2; }
;
%%

main(int argc, char **argv) {
  yyparse();
}