aboutsummaryrefslogtreecommitdiffstats
path: root/src/lfsc/lfscParser.mly
blob: 9195695a373a6f9e468edb172e76fd144a69a54b (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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
%{
(**************************************************************************)
(*                                                                        *)
(*     SMTCoq                                                             *)
(*     Copyright (C) 2011 - 2022                                          *)
(*                                                                        *)
(*     See file "AUTHORS" for the list of authors                         *)
(*                                                                        *)
(*   This file is distributed under the terms of the CeCILL-C licence     *)
(*                                                                        *)
(**************************************************************************)


(* This parser is adapted from Jane Street sexplib parser *)

open Ast
open Lexing
open Format

let parse_failure what =
  let pos = Parsing.symbol_start_pos () in
  let msg =
    Printf.sprintf "Sexplib.Parser: failed to parse line %d char %d: %s"
      pos.pos_lnum (pos.pos_cnum - pos.pos_bol) what in
  failwith msg

let scope = ref []

let renamings = Hashtbl.create 21

let register_rename = Hashtbl.add renamings

let remove_rename = Hashtbl.remove renamings



%}

%token <string> STRING
%token <Big_int.big_int> INT
%token LPAREN RPAREN EOF HASH_SEMI
%token LAMBDA PI BIGLAMBDA COLON
%token CHECK DEFINE DECLARE
%token MPQ MPZ HOLE TYPE KIND
%token SC PROGRAM AT UNSAT SAT

%start proof
%type <Ast.proof> proof

%start last_command
%type <Ast.command option> last_command

%start ignore_commands
%type <unit> ignore_commands

%start proof_print
%type <unit> proof_print

%start proof_ignore
%type <unit> proof_ignore

%start one_command
%type <Ast.command option> one_command

%start sexp
%type <Type.t> sexp

%start sexp_opt
%type <Type.t option> sexp_opt

%start sexps
%type <Type.t list> sexps

%start rev_sexps
%type <Type.t list> rev_sexps

%%
sexp:
| sexp_comments sexp_but_no_comment { $2 }
| sexp_but_no_comment { $1 }

sexp_but_no_comment
  : STRING { Type.Atom $1 }
  | LPAREN RPAREN { Type.List [] }
  | LPAREN rev_sexps_aux RPAREN { Type.List (List.rev $2) }
  | error { parse_failure "sexp" }

sexp_comment
  : HASH_SEMI sexp_but_no_comment { () }
  | HASH_SEMI sexp_comments sexp_but_no_comment { () }

sexp_comments
  : sexp_comment { () }
  | sexp_comments sexp_comment { () }

sexp_opt
  : sexp_but_no_comment { Some $1 }
  | sexp_comments sexp_but_no_comment { Some $2 }
  | EOF { None }
  | sexp_comments EOF { None }

rev_sexps_aux
  : sexp_but_no_comment { [$1] }
  | sexp_comment { [] }
  | rev_sexps_aux sexp_but_no_comment { $2 :: $1 }
  | rev_sexps_aux sexp_comment { $1 }

rev_sexps
  : rev_sexps_aux EOF { $1 }
  | EOF { [] }

sexps
  : rev_sexps_aux EOF { List.rev $1 }
  | EOF { [] }
;


atom_ignore:
  | STRING {}
  | CHECK {}
  | DEFINE {}
  | DECLARE {}
  | TYPE {}
  | KIND {}
  | MPZ {}
  | MPQ {}
  | PROGRAM {}
  | INT {}
  | LAMBDA {}
  | PI {}
  | HOLE {}
  | SC {}
  | AT {}
  | COLON {}
;

sexp_ignore :
  | atom_ignore {}
  | LPAREN ignore_sexp_list RPAREN {}
;
  
ignore_sexp_list :
  | { }
  | sexp_ignore ignore_sexp_list { }
;


term_list:
  | term { [$1]}
  | term term_list { $1 :: $2 }
;

binding:
  | STRING term {
    let n = String.concat "." (List.rev ($1 :: !scope)) in
    let s = mk_symbol n $2 in
    register_symbol s;
    register_rename $1 n;
    s, $1
  }
;

untyped_sym:
  | STRING {
    let s = mk_symbol $1 (mk_hole_hole ()) in
    register_symbol s;
    s
  }
;

let_binding:
  | STRING term {
    let x = $1 in
    let t = $2 in
    let s = mk_symbol x t.ttype in
    register_symbol s;
    add_definition s.sname t;
    s.sname
  }
;

/*
ignore_string_or_hole:
  | STRING { }
  | HOLE { }
;
*/

term:
  | TYPE { lfsc_type }
  | KIND { kind }
  | MPQ { mpq }
  | MPZ { mpz }
  | INT { mk_mpz $1 }
  | STRING
    {
      let n = try Hashtbl.find renamings $1 with Not_found -> $1 in
      mk_const n
    }
  | HOLE { mk_hole_hole () }
  | LPAREN AT let_binding term RPAREN { remove_definition $3; $4 }
  | LPAREN term term_list RPAREN { mk_app $2 $3 }
  | LPAREN LAMBDA untyped_sym term RPAREN
    { let s = $3  in
      let t = $4 in
      let r = mk_lambda s t in
      remove_symbol s;
      r
    }
  | LPAREN LAMBDA HOLE term RPAREN
    { let s = mk_symbol_hole (mk_hole_hole ()) in
      let t = $4 in
      mk_lambda s t }
  | LPAREN BIGLAMBDA binding term RPAREN
    { let s, old = $3 in
      let t = $4 in
      let r = mk_lambda s t in
      remove_symbol s;
      remove_rename old;
      r
    }
  | LPAREN BIGLAMBDA HOLE term term RPAREN
    { let t = $5 in
      let s = mk_symbol_hole $4 in
      mk_lambda s t }
  | LPAREN PI binding term RPAREN
    { let s, old = $3 in
      let t = $4 in
      let r = mk_pi s t in
      remove_symbol s;
      remove_rename old;
      r
    }
  | LPAREN PI HOLE term term RPAREN
    { let s = mk_symbol_hole $4 in
      let t = $5 in
      mk_pi s t }
  | LPAREN PI STRING /* ignore_string_or_hole */
    LPAREN SC LPAREN STRING term_list RPAREN term RPAREN term RPAREN
    {
      add_sc $7 $8 $10 $12
    }
  | LPAREN COLON term term RPAREN
    { mk_ascr $3 $4 }
;


  
declare:
  | DECLARE STRING { scope := [$2]; $2 }
;

define:
  | DEFINE STRING { scope := [$2]; $2 }
;

declare_command:
  | LPAREN declare term RPAREN {
    mk_declare $2 $3;
    scope := [];
    Declare (Hstring.make $2, $3)
  }
;


define_command:
  | LPAREN define term RPAREN {
    mk_define $2 $3;
    scope := [];
    Define (Hstring.make $2, $3) }
;

check_command:
  | LPAREN CHECK term RPAREN {
    mk_check $3;
    Check $3 }
;

command:
  | check_command { $1 }
  | define_command { $1 }
  | declare_command { $1 }
;

command_print:
  | command { printf "@[<hov 1>%a@]@\n@." print_command $1 }
  | LPAREN PROGRAM STRING ignore_sexp_list RPAREN
    { printf "Ignored program %s\n@." $3 }
;

command_ignore:
  | command { () }
  | LPAREN PROGRAM STRING ignore_sexp_list RPAREN { () }
;

command_or_prog_or_unsat:
  | command { Some $1 }
  | SAT { raise CVC4Sat }
  | UNSAT { None }
  | LPAREN PROGRAM STRING ignore_sexp_list RPAREN
    { None }
;

  
command_list:
  | { [] }
  | command_or_prog_or_unsat command_list
    { match $1 with Some c -> c :: $2 | None -> $2 }
;

command_print_list:
  | { }
  | command_print command_print_list { }
;

command_ignore_list:
  | { }
  | command_ignore command_ignore_list { }
;

proof:
  | command_list EOF { $1 }
;

proof_print:
  | command_print_list EOF { }
;

proof_ignore:
  | command_ignore_list EOF { }
;


last_command:
  | command_or_prog_or_unsat { $1 }
  | command_or_prog_or_unsat last_command { $2 }
;

one_command:
  | command_or_prog_or_unsat { $1 }
;

ignore_commands:
  | command_or_prog_or_unsat { () }
  | command_or_prog_or_unsat ignore_commands { () }
;