-
Notifications
You must be signed in to change notification settings - Fork 2
/
formula_parser.mly
323 lines (266 loc) · 7.5 KB
/
formula_parser.mly
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
%{
open Caretast
open Logic_typing
let dkey = Caret_option.register_category "parser"
let parse_formula str =
let str = String.trim (String.sub str 1 ((String.length str) - 2)) in
try
let lexbuf = Lexing.from_string str in
Caret_option.debug
~dkey
"%s lexed successfully"
str;
let lexpr =
Logic_parser.lexpr_eof Logic_lexer.token lexbuf
in
Caret_option.debug
~dkey
"%s logic_parsed successfully"
str;
let res = Formula_typer.Typer.predicate (Lenv.empty ()) lexpr
in
Caret_option.debug
~dkey
"%s parsed successfully !"
str;
res
with
Parsing.Parse_error ->
failwith
("Error during the formula parsing, "
^ str
^ " is an incorrect predicate.")
(*
let pred_to_cvc pred =
let open Cil_datatype in
let logic_vars =
Logic_var.Hashtbl.create 5
in
let term_unop = function
Neg -> "-"
| BNot -> "~"
| LNot -> "NOT"
in
let term_binop = function
| PlusA | PlusPI | IndexPI -> " + " (** arithmetic + *)
| MinusA | MinusPI | MinusPP -> " - "
| Mult -> " * "
| Div -> " / "
| Mod -> assert false
| Shiftlt -> " << "
| Shiftrt -> " >> "
| Lt -> "<"
| Gt -> ">"
| Le -> "<="
| Ge -> ">="
| Eq -> "=="
| Ne -> "/="
| BAnd -> " & "
| BXor -> assert false
| BOr -> " | "
| LAnd | LOr -> assert false
(** logical or. Unlike other expressions this one does not always
evaluate both operands. If you
want to use these, you must set
{!Cil.useLogicalOperators}. *)
in
let rec str_term = function
| TConst cst ->
match cst with
Integer (i,_) -> string_of_int i
| TLval (t_lhost, _ )->
begin (* TLval *)
match t_lhost with
| TVar v ->
begin (* TVar *)
try Logic_var.Hashtbl.find logic_vars v
with
Not_found ->
let res = v.lv_name in
let () = Logic_var.Hashtbl.add logic_vars v res
in res
end (* TVar *)
| TResult _ ->
Caret_option.abort
"Results of a call cannot be expressed in a predicate (yet)"
| TMem t -> str_term t
end(* TLval *)
| TUnOp (uop,term) ->
term_unop uop ^ "(" ^ str_term ^ ")"
| TSizeOf _ | TSizeOfE _ | TSizeOfStr _ | TAlignOf _ | TAlignOfE
-> assert false
| TBinOp (bop,t1,t2) ->
"(" ^(str_term t1)^ ")" ^ term_binop bop ^ "(" ^(str_term t2) ^ ")"
| TCastE _ | TAddrOf _ | TStartOf _ -> assert false
in
let str_rel = function
| Rlt -> "<"
| Rgt -> ">"
| Rle -> "<="
| Rge -> "=>"
| Req -> "="
| Rneq -> "/="
in
let rec __pred_to_cvc pred =
let res =
| Pfalse -> "false"
| Ptrue -> "true"
| Papp _ | Pseparated _ | Plet _ -> assert false
| Prel (rel,t1,t2) ->
let s_rel = str_rel rel in
(str_term t1) ^ s_rel ^ (str_term t2)
| Pand (p1, p2) ->
(__pred_to_cvc p1.content) ^ " AND " ^ (__pred_to_cvc p2.content)
| Por (p1, p2) ->
(__pred_to_cvc p1.content) ^ " OR " ^ (__pred_to_cvc p2.content)
| Pxor (p1, p2) ->
(__pred_to_cvc p1.content) ^ " XOR " ^ (__pred_to_cvc p2.content)
| Pimplies (p1, p2) ->
(__pred_to_cvc p1.content) ^ " => " ^ (__pred_to_cvc p2.content)
| Piff (p1, p2) ->
(__pred_to_cvc p1.content) ^ " <=> " ^ (__pred_to_cvc p2.content)
| Pnot p ->
" NOT( "^ (__pred_to_cvc p) ^ ")"
| Pif (t,p1,p2) ->
"IF " ^ (str_term t) ^
" THEN " ^ (__pred_to_cvc p1.content) ^
" ELSE " ^ (__pred_to_cvc p2.content) ^
" ENDIF"
| Pforall (qt, p) ->
"FORALL (" ^
of quantifiers * predicate named (** universal quantification. *)
| Pexists of quantifiers * predicate named (** existential quantification. *)
| Pat of predicate named * logic_label
(** predicate refers to a particular program point. *)
| Pvalid_read of logic_label * term (** the given locations are valid for reading. *)
| Pvalid of logic_label * term (** the given locations are valid. *)
(** | Pvalid_index of term * term
{b deprecated:} Use [Pvalid(TBinOp(PlusPI,p,i))] instead.
[Pvalid_index(p,i)] indicates that accessing the [i]th element
of [p] is valid.
| Pvalid_range of term * term * term
{b deprecated:} Use [Pvalid(TBinOp(PlusPI(p,Trange(i1,i2))))] instead.
similar to [Pvalid_index] but for a range of indices.*)
| Pinitialized of logic_label * term (** the given locations are initialized. *)
| Pdangling of logic_label * term (** the given locations contain dangling
adresses. *)
| Pallocable of logic_label * term (** the given locations can be allocated. *)
| Pfreeable of logic_label * term (** the given locations can be free. *)
| Pfresh of logic_label * logic_label * term * term
(** \fresh(pointer, n)
A memory block of n bytes is newly allocated to the pointer.*)
| Psubtype of term * term
(** First term is a type tag that is a subtype of the second. *)
in
"\\(" ^ res ^ "\\)"
*)
(** Transforms an "ACSL" string into a "CVC3" string. *)
let cvc_string str =
let () = Caret_option.debug ~dkey "parsing %s into cvc format" str in
let i = ref 0 in
let first_str =
String.map
(fun c ->
let () = i := !i + 1 in
match c with
'!' -> if str.[!i] = '=' then '/' else '!'
| '=' -> if str.[!i] = '=' then ' ' else c
(*| '|' -> if str.[!i] = '|' then 'O' else 'R'*)
|'\\' | '[' | ']' -> '_'
| '(' | ')'| '{' | '}' -> ' '
| _ -> c
)
str
in
let rec lower acc index str =
try
let next_l =
String.index_from str index '<'
in
lower
(acc ^ (String.sub
str
index
(next_l - index)
)
^ " \\< ")
(next_l + 1)
str
with
Not_found ->
acc
^ (String.sub
str
index
((String.length str) - index) )
in
let res =
lower "" 0 first_str
in
Caret_option.debug ~dkey "Res : %s" res ; res
%}
/* Atomic propositions */
%token <string> PROP
%token TRUE FALSE INTERN
%token <string> CALL
%token <string> RETURN
/* Unary operators */
%token NEXT FATALLY GLOBALLY
%token NOT
/* Binary operators */
%token UNTIL
%token OR AND IMPLIES EQUIV
/* Qualificators of CaRet operators */
%token GENERAL ABSTRACT PAST
/* Additionnal tokens */
%token L_PAREN R_PAREN
%token EOF
%token UNTIL OR AND IMPLIES EQUIV
%right NOT NEXT FATALLY GLOBALLY
%start main
%type <Caretast.caret_formula> main
%%
main :
formula EOF {$1}
;
opkind :
| GENERAL {General}
| ABSTRACT {Abstract}
| PAST {Past}
;
predicate :
| PROP
{(CProp (Logic_const.new_predicate (parse_formula $1), (cvc_string $1)))}
| TRUE
{CTrue}
| FALSE
{CFalse}
formula :
| predicate
{$1}
| L_PAREN formula R_PAREN
{$2}
| CALL
{CInfo (ICall (let s = String.trim $1 in
if s = "" then None else (Some s)))}
| RETURN
{CInfo (IRet (let s = String.trim $1 in
if s = "" then None else (Some s)))}
| NOT formula
{Caretast.CNot $2}
| FATALLY opkind formula
{Caretast.CFatally ($2,$3)}
| GLOBALLY opkind formula
{Caretast.CGlobally ($2, $3)}
| NEXT opkind formula
{Caretast.CNext ($2, $3)}
| formula OR formula
{Caretast.COr ($1,$3)}
| formula AND formula
{Caretast.CAnd ($1,$3)}
| formula IMPLIES formula
{Caretast.CImplies ($1,$3)}
| formula EQUIV formula
{Caretast.CIff ($1,$3)}
| formula UNTIL opkind formula
{Caretast.CUntil ($3,$1,$4)}