parser.mly 12.1 KB
Newer Older
1 2
/**************************************************************************/
/*                                                                        */
Jean-Christophe Filliâtre's avatar
headers  
Jean-Christophe Filliâtre committed
3 4 5 6 7
/*  Copyright (C) 2010-                                                   */
/*    Francois Bobot                                                      */
/*    Jean-Christophe Filliatre                                           */
/*    Johannes Kanig                                                      */
/*    Andrei Paskevich                                                    */
8 9 10 11 12 13 14 15 16 17 18
/*                                                                        */
/*  This software is free software; you can redistribute it and/or        */
/*  modify it under the terms of the GNU Library General Public           */
/*  License version 2.1, with the special exception on linking            */
/*  described in file LICENSE.                                            */
/*                                                                        */
/*  This software is distributed in the hope that it will be useful,      */
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of        */
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  */
/*                                                                        */
/**************************************************************************/
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32

%{

  open Ptree
  open Parsing

  let loc () = (symbol_start_pos (), symbol_end_pos ())
  let loc_i i = (rhs_start_pos i, rhs_end_pos i)
  let loc_ij i j = (rhs_start_pos i, rhs_end_pos j)

  let mk_ppl loc d = { pp_loc = loc; pp_desc = d }
  let mk_pp d = mk_ppl (loc ()) d
  let mk_pp_i i d = mk_ppl (loc_i i) d
		    
Andrei Paskevich's avatar
Andrei Paskevich committed
33 34
  let mk_pat p = { pat_loc = loc (); pat_desc = p }

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
35 36 37 38 39 40
  let infix_ppl loc a i b = mk_ppl loc (PPinfix (a, i, b))
  let infix_pp a i b = infix_ppl (loc ()) a i b

  let prefix_ppl loc p a = mk_ppl loc (PPprefix (p, a))
  let prefix_pp p a = prefix_ppl (loc ()) p a

41 42 43 44
  let infix s = "infix " ^ s
  let prefix s = "prefix " ^ s
  let postfix s = "postfix " ^ s

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
45 46 47 48
%}

/* Tokens */ 

49
%token <string> LIDENT UIDENT
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
50
%token <string> INTEGER
51
%token <string> OP0 OP1 OP2 OP3
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
52 53
%token <Ptree.real_constant> FLOAT
%token <string> STRING
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

/* keywords */

%token AND AS AXIOM 
%token CLONE 
%token ELSE END EXISTS EXPORT FALSE FORALL  
%token GOAL
%token IF IMPORT IN INDUCTIVE LEMMA 
%token LET LOGIC MATCH 
%token NAMESPACE NOT OR  
%token THEN THEORY TRUE TYPE
%token USE WITH

/* symbols */

%token ARROW AT
%token BANG BAR 
71
%token COLON COMMA  
72 73 74 75
%token DOT EQUAL
%token LEFTB LEFTPAR LEFTSQ 
%token LRARROW
%token QUOTE RIGHTB 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
76
%token RIGHTPAR RIGHTSQ 
77 78 79 80
%token UNDERSCORE

%token EOF

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
81 82 83

/* Precedences */

84
%left LEFTB 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
85 86 87 88 89 90 91 92

%left COLON 

%left IN

%left ELSE

%right prec_named
93
%right prec_quant
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
94
%right ARROW LRARROW
95 96
%right OR 
%right AND 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
97 98
%right NOT
%right prec_if
99 100 101 102 103
%left EQUAL OP0
%left OP1
%left OP2
%left OP3
%right unary_op
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
104 105
%left LEFTSQ

106 107
%nonassoc prec_decls
%nonassoc LOGIC TYPE INDUCTIVE
108

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
109 110 111 112
/* Entry points */

%type <Ptree.lexpr> lexpr
%start lexpr
113 114
%type <Ptree.decl list> list0_decl
%start list0_decl
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
115 116 117 118 119
%type <Ptree.logic_file> logic_file
%start logic_file
%%

logic_file:
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
120 121
| list1_theory EOF
   { $1 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
122 123 124 125 126 127 128 129 130 131 132
| EOF 
   { [] }
;

list1_decl:
| decl 
   { [$1] }
| decl list1_decl 
   { $1 :: $2 }
;

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
133 134 135 136 137 138 139
list0_decl:
| /* epsilon */
   { [] }
| list1_decl 
   { $1 }
;

140
ident:
141 142 143 144 145 146 147
| uident { $1 }
| lident { $1 }
;

ident_rich:
| uident      { $1 }
| lident_rich { $1 }
148 149
;

150
lident:
151
| LIDENT
152 153 154
    { { id = $1; id_loc = loc () } }
;

155 156
lident_rich:
| lident
157 158
    { $1 }
| LEFTPAR UNDERSCORE lident_op UNDERSCORE RIGHTPAR 
159
    { { id = infix $3; id_loc = loc () } }
160
| LEFTPAR lident_op UNDERSCORE RIGHTPAR 
161
    { { id = prefix $2; id_loc = loc () } }
162 163
/*
| LEFTPAR UNDERSCORE lident_op RIGHTPAR 
164
    { { id = postfix $3; id_loc = loc () } }
165
*/
166 167
;

168 169 170 171 172 173
lident_op:
| OP0   { $1 }
| OP2   { $1 }
| OP3   { $1 }
| EQUAL { "=" }
;
174

175 176 177 178 179
any_op:
| OP0   { $1 }
| OP2   { $1 }
| OP3   { $1 }
;
180

181 182 183 184 185 186 187 188 189 190 191 192
uident:
| UIDENT { { id = $1; id_loc = loc () } }
;

lqualid:
| lident             { Qident $1 }
| uqualid DOT lident { Qdot ($1, $3) }
;

uqualid:
| uident             { Qident $1 }
| uqualid DOT uident { Qdot ($1, $3) }
193 194
;

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
195 196 197
any_qualid:
| ident                { Qident $1 }
| any_qualid DOT ident { Qdot ($1, $3) }
198 199
;

200 201 202 203
tqualid:
| uident                { Qident $1 }
| any_qualid DOT uident { Qdot ($1, $3) }

204
qualid:
205 206
| ident_rich             { Qident $1 }
| uqualid DOT ident_rich { Qdot ($1, $3) }
207

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
params:
| /* epsilon */                          { [] }
| LEFTPAR list1_param_sep_comma RIGHTPAR { $2 }
;

param:
| primitive_type              { None, $1 }
| lident COLON primitive_type { Some $1, $3 }
;

list1_param_sep_comma:
| param                             { [$1] }
| param COMMA list1_param_sep_comma { $1 :: $3 }
;

223 224 225 226
primitive_types:
| /* epsilon */                                   { [] }
| LEFTPAR list1_primitive_type_sep_comma RIGHTPAR { $2 }

227 228 229 230 231 232 233 234 235 236 237
logic_type_option:
| /* epsilon */        { None }
| COLON primitive_type { Some $2 }
;

logic_def_option:
| /* epsilon */ { None }
| EQUAL lexpr   { Some $2 }
;

logic_decl:
238
| LOGIC lident_rich params logic_type_option logic_def_option
239 240
  { { ld_loc = loc (); ld_ident = $2; ld_params = $3; 
      ld_type = $4; ld_def = $5; } }
241 242 243
;

list1_logic_decl:
244 245
| logic_decl                  %prec prec_decls { [$1] }
| logic_decl list1_logic_decl                  { $1 :: $2 }
246 247 248
;

type_decl:
249 250
| TYPE type_args lident typedefn
  { { td_loc = loc (); td_ident = $3; td_params = $2; td_def = $4 } }
251 252 253
;

list1_type_decl:
254
| type_decl                  %prec prec_decls { [$1] }
255 256 257
| type_decl list1_type_decl                   { $1 :: $2 }
;

258
inductive_decl:
259
| INDUCTIVE lident_rich primitive_types inddefn
260 261 262 263 264 265 266 267
  { { in_loc = loc (); in_ident = $2; in_params = $3; in_def = $4 } }

list1_inductive_decl:
| inductive_decl                      %prec prec_decls { [$1] }
| inductive_decl list1_inductive_decl                  { $1 :: $2 }
;

decl:
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
| list1_type_decl
    { TypeDecl $1 }
| list1_logic_decl
    { LogicDecl $1 }
| list1_inductive_decl
    { IndDecl $1 }
| AXIOM uident COLON lexpr
    { PropDecl (loc (), Kaxiom, $2, $4) }
| LEMMA uident COLON lexpr
    { PropDecl (loc (), Klemma, $2, $4) }
| GOAL uident COLON lexpr
    { PropDecl (loc (), Kgoal, $2, $4) }
| USE use
    { UseClone (loc (), $2, None) }
| CLONE use clone_subst
    { UseClone (loc (), $2, Some $3) }
| NAMESPACE uident list0_decl END
    { Namespace (loc (), false, Some $2, $3) }
| NAMESPACE UNDERSCORE list0_decl END
    { Namespace (loc (), false, None, $3) }
| NAMESPACE IMPORT uident list0_decl END
    { Namespace (loc (), false, Some $3, $4) }
| NAMESPACE IMPORT UNDERSCORE list0_decl END
    { Namespace (loc (), false, None, $4) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
292 293
;

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
294 295 296 297 298 299 300 301
list1_theory:
| theory 
   { [$1] }
| theory list1_theory 
   { $1 :: $2 }
;

theory:
302
| THEORY uident list0_decl END 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
303
   { { pt_loc = loc (); pt_name = $2; pt_decl = $3 } }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
304 305
;

306 307 308 309
type_args:
| /* epsilon */                                             { [] }
| type_var                                                  { [$1] }
| LEFTPAR type_var COMMA list1_type_var_sep_comma RIGHTPAR  { $2 :: $4 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
310 311 312 313
;

typedefn:
| /* epsilon */
314 315 316
    { TDabstract }
| EQUAL primitive_type
    { TDalias $2 }
317 318 319
| EQUAL typecases
    { TDalgebraic $2 }
| EQUAL BAR typecases
320
    { TDalgebraic $3 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
321 322 323 324 325 326 327 328
;

typecases:
| typecase                { [$1] }
| typecase BAR typecases  { $1::$3 }
;

typecase:
329
| uident params { (loc_i 1,$1,$2) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
330 331 332 333 334 335 336 337 338 339 340 341 342
;

inddefn:
| /* epsilon */       { [] }
| EQUAL bar_ indcases { $3 }
;

indcases:
| indcase               { [$1] }
| indcase BAR indcases  { $1::$3 }
;

indcase:
Andrei Paskevich's avatar
Andrei Paskevich committed
343
| uident COLON lexpr { (loc (),$1,$3) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
344 345 346 347
;

primitive_type:
| type_var 
348
   { PPTtyvar $1 }
349
| lqualid
350
   { PPTtyapp ([], $1) }
351
| primitive_type lqualid
352
   { PPTtyapp ([$1], $2) }
353
| LEFTPAR primitive_type COMMA list1_primitive_type_sep_comma RIGHTPAR lqualid
354
   { PPTtyapp ($2 :: $4, $6) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
;

list1_primitive_type_sep_comma:
| primitive_type                                      { [$1] }
| primitive_type COMMA list1_primitive_type_sep_comma { $1 :: $3 }
;

lexpr:
| lexpr ARROW lexpr 
   { infix_pp $1 PPimplies $3 }
| lexpr LRARROW lexpr 
   { infix_pp $1 PPiff $3 }
| lexpr OR lexpr 
   { infix_pp $1 PPor $3 }
| lexpr AND lexpr 
   { infix_pp $1 PPand $3 }
| NOT lexpr 
   { prefix_pp PPnot $2 }
373
| lexpr EQUAL lexpr 
374
   { let id = { id = infix "="; id_loc = loc_i 2 } in
375
     mk_pp (PPapp (Qident id, [$1; $3])) }
376 377
| lexpr OP0 lexpr 
   { let id = { id = infix $2; id_loc = loc_i 2 } in
378
     mk_pp (PPapp (Qident id, [$1; $3])) }
379 380
| lexpr OP1 lexpr 
   { let id = { id = infix $2; id_loc = loc_i 2 } in
381
     mk_pp (PPapp (Qident id, [$1; $3])) }
382 383
| lexpr OP2 lexpr 
   { let id = { id = infix $2; id_loc = loc_i 2 } in
384
     mk_pp (PPapp (Qident id, [$1; $3])) }
385 386
| lexpr OP3 lexpr 
   { let id = { id = infix $2; id_loc = loc_i 2 } in
387
     mk_pp (PPapp (Qident id, [$1; $3])) }
388 389 390 391 392 393 394 395
| any_op lexpr %prec unary_op
   { let id = { id = prefix $1; id_loc = loc_i 2 } in
     mk_pp (PPapp (Qident id, [$2])) }
/*
| lexpr any_op %prec unary_op
   { let id = { id = postfix $2; id_loc = loc_i 2 } in
     mk_pp (PPapp (Qident id, [$1])) }
*/
396
| qualid
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
397
   { mk_pp (PPvar $1) }
398
| qualid LEFTPAR list1_lexpr_sep_comma RIGHTPAR
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
399 400 401
   { mk_pp (PPapp ($1, $3)) }
| IF lexpr THEN lexpr ELSE lexpr %prec prec_if 
   { mk_pp (PPif ($2, $4, $6)) }
402 403 404 405
| FORALL list1_uquant_sep_comma triggers DOT lexpr %prec prec_quant
   { mk_pp (PPquant (PPforall, $2, $3, $5)) }
| EXISTS list1_uquant_sep_comma triggers DOT lexpr %prec prec_quant
   { mk_pp (PPquant (PPexists, $2, $3, $5)) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
406
| INTEGER
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
407
   { mk_pp (PPconst (Term.ConstInt $1)) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
408
| FLOAT
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
409
   { mk_pp (PPconst (Term.ConstReal $1)) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
410 411 412 413 414 415
| TRUE
   { mk_pp PPtrue }
| FALSE
   { mk_pp PPfalse }    
| LEFTPAR lexpr RIGHTPAR
   { $2 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
416 417
| STRING lexpr %prec prec_named
   { mk_pp (PPnamed ($1, $2)) }
418
| LET lident EQUAL lexpr IN lexpr 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
419
   { mk_pp (PPlet ($2, $4, $6)) }
420
| MATCH list1_lexpr_sep_comma WITH bar_ match_cases END
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
421
   { mk_pp (PPmatch ($2, $5)) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
422 423
| lexpr COLON primitive_type
   { mk_pp (PPcast ($1, $3)) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
424 425
;

426 427 428 429 430 431 432
list1_uquant_sep_comma:
| uquant                              { [$1] }
| uquant COMMA list1_uquant_sep_comma { $1::$3 }

uquant:
| list1_lident_sep_comma COLON primitive_type { $1,$3 }

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
433 434 435 436 437 438
match_cases:
| match_case                  { [$1] }
| match_case BAR match_cases  { $1::$3 }
;

match_case:
439
| list1_pat_sep_comma ARROW lexpr { ($1,$3) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
440 441
;

Andrei Paskevich's avatar
Andrei Paskevich committed
442 443 444 445
list1_pat_sep_comma:
| pattern                           { [$1] }
| pattern COMMA list1_pat_sep_comma { $1::$3 }

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
446
pattern:
Andrei Paskevich's avatar
Andrei Paskevich committed
447 448 449 450 451
| UNDERSCORE                                    { mk_pat (PPpwild) }
| lident                                        { mk_pat (PPpvar $1) }
| uqualid                                       { mk_pat (PPpapp ($1, [])) }
| uqualid LEFTPAR list1_pat_sep_comma RIGHTPAR  { mk_pat (PPpapp ($1, $3)) }
| pattern AS lident                             { mk_pat (PPpas ($1,$3)) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
;

triggers:
| /* epsilon */                         { [] }
| LEFTSQ list1_trigger_sep_bar RIGHTSQ  { $2 }
;

list1_trigger_sep_bar:
| trigger                           { [$1] }
| trigger BAR list1_trigger_sep_bar { $1 :: $3 }
;

trigger:
  list1_lexpr_sep_comma { $1 }
;

list1_lexpr_sep_comma:
| lexpr                             { [$1] }
| lexpr COMMA list1_lexpr_sep_comma { $1 :: $3 }
;

type_var:
| QUOTE ident { $2 }
;

list1_type_var_sep_comma:
| type_var                                { [$1] }
| type_var COMMA list1_type_var_sep_comma { $1 :: $3 }
;

bar_:
| /* epsilon */ { () }
| BAR           { () }
;

487 488 489 490 491
list1_lident_sep_comma:
| lident                              { [$1] }
| lident COMMA list1_lident_sep_comma { $1 :: $3 }
;

492
use:
493
| imp_exp tqualid              
494
    { { use_theory = $2; use_as = None; use_imp_exp = $1 } }
495
| imp_exp tqualid AS uident
496 497 498
    { { use_theory = $2; use_as = Some (Some $4); use_imp_exp = $1 } }
| imp_exp tqualid AS UNDERSCORE
    { { use_theory = $2; use_as = Some None; use_imp_exp = $1 } }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
499 500
;

501 502 503 504
imp_exp:
| IMPORT        { Import }
| EXPORT        { Export }
| /* epsilon */ { Nothing }
505 506
;

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
507
clone_subst:
508 509
| /* epsilon */          { [] } 
| WITH list1_comma_subst { $2 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
510 511 512
;

list1_comma_subst:
513 514
| subst                         { [$1] }
| subst COMMA list1_comma_subst { $1 :: $3 } 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
515 516 517
;

subst:
518 519 520 521
| TYPE  qualid EQUAL qualid { CStsym ($2, $4) }
| LOGIC qualid EQUAL qualid { CSlsym ($2, $4) }
| LEMMA qualid              { CSlemma $2 }
| GOAL  qualid              { CSgoal  $2 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
522 523
;