summaryrefslogtreecommitdiffstats
path: root/frontend/parser.mly
blob: 4e010eca83a332d2286caccbdf333eca1197d85f (plain) (blame)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223

%{
open Ast
open Util
%}

%token BOOL INT REAL
%token PROBE
%token TRUE FALSE
%token IF THEN ELSE
%token NOT AND OR
%token MOD
%token CONST NODE FUNCTION RETURNS
%token VAR LET TEL
%token PRE
%token ASSUME GUARANTEE
%token ACTIVATE
%token AUTOMATON STATE INITIAL UNTIL UNLESS RESUME RESTART

%token LPAREN RPAREN
%token LCURLY RCURLY
%token STAR PLUS MINUS DIVIDE
%token LESS GREATER LESS_EQUAL GREATER_EQUAL
%token DIFF EQUAL
%token SEMICOLON COLON COMMA
%token ARROW

%token <string> IDENT
%token <string> INTVAL
%token <string> REALVAL

%token EOF

(* Lowest priorities first *)
%left ARROW
%left OR
%left AND
%left EQUAL DIFF
%left LESS GREATER LESS_EQUAL GREATER_EQUAL
%left PLUS MINUS
%left STAR DIVIDE MOD

(* Root token *)
%start<Ast.toplevel list> file

%%

file:
| t=list(toplevel) EOF      { t }

toplevel:
| d=ext(const_decl)         { AST_const_decl d }
| d=ext(node_decl)          { AST_node_decl d }


primary_expr:
| LPAREN e=expr RPAREN      { e }
| e=ext(IDENT)              { AST_identifier e }
| e=ext(INTVAL)             { AST_int_const e }
| e=ext(REALVAL)            { AST_real_const e }
| TRUE                      { AST_bool_const true }
| FALSE                     { AST_bool_const false }
| e=ext(IDENT) LPAREN l=separated_list(COMMA,ext(expr)) RPAREN 
                            { AST_instance (e, l, fst e ^ uid ()) }
| INT LPAREN e=ext(expr) RPAREN
                            { AST_cast(e, AST_TINT) }
| REAL LPAREN e=ext(expr) RPAREN
                            { AST_cast(e, AST_TREAL) }

unary_expr:
| e=primary_expr                   { e }
| NOT e=ext(unary_expr)            { AST_not(e) }
| PRE e=ext(unary_expr)            { AST_pre(e, "pre"^uid()) }
| o=unary_op e=ext(unary_expr)     { AST_unary (o, e) }

%inline unary_op:
| PLUS          { AST_UPLUS }
| MINUS         { AST_UMINUS }


binary_expr:
| e=unary_expr                                        { e }
| e=ext(binary_expr) ARROW f=ext(binary_expr)         { AST_arrow(e, f) }
| e=ext(binary_expr) o=binary_op f=ext(binary_expr)   { AST_binary (o, e, f) }
| e=ext(binary_expr) o=binary_rel f=ext(binary_expr)  { AST_binary_rel (o, e, f) }
| e=ext(binary_expr) o=binary_bool f=ext(binary_expr) { AST_binary_bool (o, e, f) }

%inline binary_op:
| STAR           { AST_MUL }
| DIVIDE         { AST_DIV }
| MOD            { AST_MOD }
| PLUS           { AST_PLUS }
| MINUS          { AST_MINUS }
%inline binary_rel:
| LESS           { AST_LT }
| GREATER        { AST_GT }
| LESS_EQUAL     { AST_LE }
| GREATER_EQUAL  { AST_GE }
| EQUAL          { AST_EQ}
| DIFF           { AST_NE }
%inline binary_bool:
| AND            { AST_AND }
| OR             { AST_OR }

if_expr:
| IF c=ext(expr) THEN t=ext(expr) ELSE e=ext(expr)    { AST_if(c, t, e) }
| e=binary_expr                                       { e }

expr:
| e=if_expr      { e }


(* Equations (can be asserts, automata, ...) *)
automaton:
| AUTOMATON a=IDENT? s=list(ext(state)) RETURNS r=separated_list(COMMA, IDENT)
    { ((match a with Some n -> n | None -> "aut") ^ uid (), s, r) }

state:
| i=boption(INITIAL) STATE n=IDENT
  unless=trans(UNLESS)
  v=option(var_decl)
  b=body
  until=trans(UNTIL)
{ if unless <> [] then failwith "UNLESS transitions not supported.";
  { initial = i;
    st_name = n;
    st_locals = (match v with Some v -> v | None -> []);
    body = b;
    until = until;
} }

trans(TT):
| TT t=nonempty_list(terminated(transition, SEMICOLON))   { t }
|                                                         { [] }

transition:
| IF e=ext(expr) RESUME s=ext(IDENT)                      { (e, s, false) }
| IF e=ext(expr) RESTART s=ext(IDENT)                     { (e, s, true) }

activate:
| ACTIVATE a=activate_if RETURNS r=separated_list(COMMA, IDENT)  { (a, r) }
activate_if:
| IF c=ext(expr) THEN t=activate_if ELSE e=activate_if           { AST_activate_if(c, t, e) }
| lv=option(var_decl) b=body
{ AST_activate_body {
      act_id = "act"^uid();
      act_locals = (match lv with Some v -> v | None -> []);
      body = b;
} }

eqn:
| i=separated_list(COMMA, ext(IDENT)) EQUAL e=ext(expr)         
                                                { AST_assign(i, e) }
| ASSUME i=ext(IDENT) COLON e=ext(expr)         { AST_assume(i, e) }
| GUARANTEE i=ext(IDENT) COLON e=ext(expr)      { AST_guarantee(i, e) }
| a=automaton                                   { AST_automaton(a) }
| a=activate                                    { AST_activate(a) }

typ:
| INT                                           { AST_TINT }
| BOOL                                          { AST_TBOOL }
| REAL                                          { AST_TREAL }

(* Declarations *)
dbody:
| e=ext(eqn) SEMICOLON                          { [e] }
| l=body                                        { l }

body:
| LET l=nonempty_list(terminated(ext(eqn), SEMICOLON)) TEL
  { l }

var:
|   p=boption(PROBE) i=IDENT                    { (p, i) }

vari:
|   vn=separated_list(COMMA, var) COLON t=typ
    { List.map (fun (p, i) -> (p, i, t)) vn }

vars:
| v=separated_list(SEMICOLON, vari)
    { List.flatten v }

const_decl:
| CONST i=IDENT COLON t=typ EQUAL e=ext(expr) SEMICOLON
    { {
        c_name = i;
        typ = t;
        value = e;
    } }

var_decl:
| VAR l=nonempty_list(terminated(vari, SEMICOLON))
    { List.flatten l }

node_kw:
| NODE     {}
| FUNCTION {}
node_decl:
| node_kw id=IDENT LPAREN v=vars RPAREN
    RETURNS LPAREN rv=vars RPAREN
    e = dbody
{ { n_name = id;
    args = v;
    ret = rv;
    var = [];
    body = e;
} }
| node_kw id=IDENT LPAREN v=vars RPAREN
    RETURNS LPAREN rv=vars RPAREN
    lv=var_decl
    b=body
{ { n_name = id;
    args = v;
    ret = rv;
    var = lv;
    body = b;
} }

(* Utility : add extent information *)
%inline ext(X): 
| x=X { x, ($startpos, $endpos) }

%%