parse_env.ml 22.1 KB
Newer Older
1
(*pp camlp4o *)
2
(*-----------------------------------------------------------------------
3
** Copyright (C) 2001, 2002 - Verimag.
4
5
6
7
8
** This file may only be copied under the terms of the GNU Library General
** Public License 
**-----------------------------------------------------------------------
**
** File: parse_env.ml
9
** Author: jahier@imag.fr
10
11
12
13
*)

open Formula

14
let debug_parsing = true
15
16
17
  
type read_arc = Arc of node * arc_info * node

18
19
type label_ce = (string * Control.expr) 

20
type read_automata = Automata of 
21
  node        (* Initial node *)
22
23
24
  * vnt list  (* Input var list *)
  * vnt list  (* Output var list *)
  * vnt list  (* Local var list *)
25
  * vnt list  (* pre var list *)
26
  * label_ce list  (* Definition of labels representing control expressions *)
27
28
29
  * read_arc list  (* Transition list *)

(* Keywords of the automata format *)
30
let lexer = Genlex.make_lexer ["("; ")"; ","; ";"; ".";
31
			       "&&"; "||"; "#"; "!"; "true"; "false"; 
32
			       "IfThenElseNum";"IfThenElse";
33
			       "="; ">"; ">="; "<"; "<="; 
34
			       "+"; "-"; "*"; "/"; "%"]
35
36
37

type aut_token = Genlex.token Stream.t

38
39
40
41
let print_genlex_token = 
  fun tok -> 
    let _ =
      match tok with
42
43
44
	  Genlex.Kwd(str)    -> print_string (str ^ " \t(Kwd)")
	| Genlex.Ident(str)  -> print_string (str ^ " \t(Ident)")
	| Genlex.Int(i)      -> print_int i; print_string " \t(Int)"
45
	| Genlex.Float(f)    -> Util.my_print_float f; print_string " \t(Float)"
46
47
	| Genlex.String(str) -> print_string (str ^ " \t(String)")
	| Genlex.Char(c)     -> print_char c ; print_string " \t(Char)"
48
    in
49
      print_string "\n\t"
50

51
52
53
let print_err_msg tok_list func =
  if debug_parsing then
    begin
54
55
      print_string ("* Parse error in " ^ func ^ ". ");
      print_string ("The next 10 tokens are:\n\t");
56
      List.iter (print_genlex_token) tok_list ;
57
      print_string ("\n");
58
59
60
61
      flush stdout
    end 
  else ()
  
62
63
	

64
65

(** Parsing lists *)
66
67
68
let rec
  (parse_list: (aut_token -> 'a) -> aut_token -> 'a list) = 
  fun parse tok -> 
69
70
    let tok_list = Stream.npeek 10 tok in
      try
71
72
73
74
75
76
77
78
79
80
81
82
	match tok_list with  
	  | (Genlex.Kwd ";")::(Genlex.Kwd ",")::_ ->  Stream.junk tok; [] (* empty list *)   
	  | (Genlex.Kwd ";")::(Genlex.Kwd ".")::_ ->  Stream.junk tok; [] (* empty list *)   
	  | (Genlex.Kwd "," )::_ -> [] (* empty list *)   
	  | (Genlex.Kwd "." )::_ -> [] (* empty list *) 
	  | (Genlex.Kwd "%" )::_ -> [] (* empty list *)  
	  | (Genlex.Kwd ")" )::_ -> [] (* empty list *)  
	  | _ -> (
	      match tok with parser
		| [< vnt = parse ; tail = (parse_list_var_tail (parse)) >]
		  -> vnt :: tail  
	    )
83
	with e ->
84
		print_err_msg tok_list "parse_list";
85
	  raise e
86
87
and (parse_list_var_tail: (aut_token -> 'a) -> aut_token -> 'a list) = 
  fun parse tok -> 
88
89
    let tok_list = Stream.npeek 10 tok in
      try
90
91
92
93
94
95
96
97
98
99
100
101
	match tok_list with 
	  | (Genlex.Kwd ";")::(Genlex.Kwd ",")::_ ->  Stream.junk tok; [] (* end of the list *)
	  | (Genlex.Kwd ";")::(Genlex.Kwd ".")::_ ->  Stream.junk tok; [] (* end of the list *)
	  | (Genlex.Kwd "," )::_ -> [] (* end of the list *)  
	  | (Genlex.Kwd "." )::_ -> [] (* end of the list *)  
	  | (Genlex.Kwd "%" )::_ -> [] (* end of the list *)  
	  | (Genlex.Kwd ")" )::_ -> [] (* end of the list *)  
	  | _ -> (
	      match tok with parser
		  [< 'Genlex.Kwd ";" ;  tail = (parse_list_var_tail2 (parse)) >]
		  -> tail  
	    )
102
103
104
	with e ->
	  print_err_msg tok_list "parse_list_var_tail";
	  raise e
105

106
107
and (parse_list_var_tail2: (aut_token -> 'a) -> aut_token -> 'a list) = 
  fun parse tok -> 
108
    (* This function is introduced to allow lists to be ended by a `;'... *)
109
110
111
    let tok_list = Stream.npeek 10 tok in
      try
	match tok with parser 
112
	  | [< 'Genlex.Kwd ";"; _ = (parse_list_var_tail3 (parse)) >] -> [] 
113
	  | [< 'Genlex.Kwd "," >] -> [] 
114
	  | [< 'Genlex.Kwd "." >] -> [] 
115
116
117
118
119
	  | [< a = parse ; tail = (parse_list_var_tail (parse)) >]
	    -> a :: tail  
	with e ->
	  print_err_msg tok_list "parse_list_var_tail2";
	  raise e
120
121
122
123
124
125
126
127
128
129
130
and (parse_list_var_tail3: (aut_token -> 'a) -> aut_token -> 'a list) = 
  fun parse tok -> 
    (* This function is also introduced to allow lists to be ended by a `;'... *)
    let tok_list = Stream.npeek 10 tok in
      try
	match tok with parser 
	  | [< 'Genlex.Kwd "." >] -> [] 
	  | [< 'Genlex.Kwd "," >] -> [] 
	with e ->
	  print_err_msg tok_list "parse_list_var_tail3";
	  raise e
131

132

133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
(** Parsing pragmas *)

type pragma = string * string

let rec (parse_pragma: aut_token -> pragma) =
  fun tok -> 
    let tok_list = Stream.npeek 10 tok in
      try
	match tok with parser  
	    [< 'Genlex.String label ; 'Genlex.Ident ":"; 
	       'Genlex.String pragma  >] -> (label, pragma)
	with e ->
	  print_err_msg tok_list "parse_pragma" ;
	  raise e
and
  (parse_pragma_list: aut_token -> pragma list) =
  fun tok -> 
    let tok_list = Stream.npeek 10 tok in
      try
	( match (Stream.npeek 1 tok) with 
	      [Genlex.Kwd "%"] -> 
		( match tok with parser  
155
156
157
158
159
		      [< 'Genlex.Kwd "%" ; 
			 pl = (parse_list (parse_pragma));
			 'Genlex.Kwd "%" 
		      >] 
		      -> pl
160
161
162
163
164
165
		)
	    | _ -> []
	)
      with e ->
	print_err_msg tok_list "parse_pragma_list" ;
	raise e
166

167
168
let rec (parse_automata: aut_token -> read_automata) = 
  fun tok -> 
169
    let tok_list = Stream.npeek 20 tok in
170
171
      try
	match tok with parser  
172
173
	    [< 'Genlex.Ident "inputs"; 'Genlex.Kwd "="; li = parse_list_var ; 
	       'Genlex.Kwd ",";
174
	       'Genlex.Ident "outputs"; 'Genlex.Kwd "="; lo = parse_list_genvar ;
175
	       'Genlex.Kwd ",";
176
	       'Genlex.Ident "locals"; 'Genlex.Kwd "="; ll = parse_list_genvar ;
177
	       'Genlex.Kwd ",";
178
	       'Genlex.Ident "pre"; 'Genlex.Kwd "="; lpre = parse_list_prevar ;
179
180
181
	       'Genlex.Kwd ",";
	       'Genlex.Ident "ctrl_expr"; 'Genlex.Kwd "="; llabel_ce = parse_list_label_ce ;
	       'Genlex.Kwd ",";
182
	       'Genlex.Ident "start_node"; 'Genlex.Kwd "="; 'Genlex.Int node_id ; 
183
	       'Genlex.Kwd "," ;
184
185
186
187
	       'Genlex.Ident "arcs_nb"; 'Genlex.Kwd "="; 'Genlex.Int arcs_nb ; 
	       'Genlex.Kwd "," ;
	       'Genlex.Ident "nodes_nb"; 'Genlex.Kwd "="; 'Genlex.Int nodes_nb ; 
	       'Genlex.Kwd "," ;
188
	       'Genlex.Ident "arcs"; 'Genlex.Kwd "="; la = parse_list_arc 
189
190
		      (List.append li (List.append lo (List.append ll lpre))) ;
	       'Genlex.Kwd ".";
191
	    >]
192
            -> Automata(node_id, li, lo, ll, lpre, llabel_ce, la)
193
194
195
196
	with e ->
	  print_err_msg tok_list "parse_automata";
	  raise e
	    
197

198
199
200
201
202
203
204
205
206
207
208
209
210
and (parse_list_var: aut_token -> vnt list) = 
  fun tok -> 
    let tok_list = Stream.npeek 10 tok in
      try
	parse_list (parse_var) tok
      with e ->
	print_err_msg tok_list "parse_list_var" ;
	raise e
and (parse_var: aut_token -> vnt) = 
  fun tok -> 
    let tok_list = Stream.npeek 10 tok in
      try
	match tok with parser  
211
212
	    [< 'Genlex.Kwd "("; 
	       'Genlex.Ident var; 
213
	       prag_var = parse_pragma_list ; 
214
215
216
	       'Genlex.Kwd ",";  
	       'Genlex.Ident typ ; 
	       'Genlex.Kwd ")" >]
217
218
219
	    -> 
	      ( match typ with
		  "bool" -> (var, BoolT)
220
221
222
223
		| "float" -> (var, FloatT(-.max_float /. 2., max_float /. 2.))
		| "int" -> (var, IntT(min_int / 2, max_int / 2))
		    (* We divide by 2 so that domains are always smaller 
		       than max_int resp max_float *)
224
225
226
227
228
229
		| str -> failwith ("*** Bad type in .env: " ^ str )
	      )
	with e ->
	  print_err_msg tok_list "parse_var" ;
	  raise e

230
231
232
233
234
235
236
237
238
239
240
241
242
and (parse_list_genvar: aut_token -> vnt list) = 
  fun tok -> 
    let tok_list = Stream.npeek 10 tok in
      try
	parse_list (parse_genvar) tok
      with e ->
	print_err_msg tok_list "parse_list_genvar" ;
	raise e
and (parse_genvar: aut_token -> vnt) = 
  fun tok -> 
    let tok_list = Stream.npeek 10 tok in
      try
	match tok with parser  
243
244
	    [< 'Genlex.Kwd "("; 'Genlex.Ident var; pl = parse_pragma_list ;
	       'Genlex.Kwd ","; 'Genlex.Ident typ ;
245
246
247
248
249
250
251
252
253
254
255
256
257
	       vnt = parse_type var typ >]
	    -> vnt
	with e ->
	  print_err_msg tok_list "parse_genvar" ;
	  raise e
and (parse_type:  string -> string -> aut_token -> vnt) =
  fun var typ tok -> 
    let tok_list = Stream.npeek 10 tok in
      try
	match tok with parser  
	    [< 'Genlex.Kwd ")" >] -> 
	      ( match typ with
		    "bool" -> (var, BoolT)
258
259
		  | "float" -> (var, FloatT(-.max_float /. 2., max_float /. 2.))
		  | "int" -> (var, IntT(min_int / 2, max_int / 2))
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
		  | str -> failwith ("*** Bad type in .env: " ^ str )
	      )
	  | [< 'Genlex.Kwd ","; vnt = parse_type_more var typ >] -> vnt
	with e ->
	  print_err_msg tok_list "parse_type" ;
	  raise e
	    
and (parse_type_more:  string -> string -> aut_token -> vnt) =
  fun var typ tok -> 
    let tok_list = Stream.npeek 10 tok in
      try
	match tok with parser  
	    [< 'Genlex.Int min; 'Genlex.Kwd ","; 'Genlex.Int max;  'Genlex.Kwd ")" >] -> 
	      ( match typ with
		    "bool" -> failwith ("*** int expected in .env" )
		  | "float" -> failwith ("*** int expected in .env " )
		  | "int" -> (var, IntT(min, max))
		  | str -> failwith ("*** Bad type in .env: " ^ str )
	      )
	  | [< 'Genlex.Float min; 'Genlex.Kwd ","; 'Genlex.Float max;  'Genlex.Kwd ")" >] -> 
	      ( match typ with
		    "bool" -> failwith ("*** float expected in .env " )
		  | "float" -> (var, FloatT(min, max))
		  | "int" -> failwith ("*** float expected in .env " )
		  | str -> failwith ("*** Bad type in .env: " ^ str )
	      )
	with e ->
	  print_err_msg tok_list "parse_type_more" ;
	  raise e
	    

291
and (parse_list_prevar: aut_token -> vnt list) = 
292
  fun tok -> 
293
294
    let tok_list = Stream.npeek 10 tok in
      try
295
	parse_list (parse_prevar) tok
296
      with e ->
297
	print_err_msg tok_list "parse_list_prevar" ;
298
	raise e
299
and (parse_prevar: aut_token -> vnt) = 
300
  fun tok -> 
301
302
303
    let tok_list = Stream.npeek 10 tok in
      try
	match tok with parser  
304
305
306
307
	    [< 'Genlex.Kwd "("; 'Genlex.Ident "pre"; 'Genlex.Kwd "("; 
	       'Genlex.Int i ; 'Genlex.Kwd "," ; 'Genlex.Ident var;  
	       'Genlex.Kwd ")"; pl = parse_pragma_list ; 
	       'Genlex.Kwd ","; 'Genlex.Ident typ ; 'Genlex.Kwd ")" >]
308
	    -> 
309
	      let pre_var = ("_pre" ^ (string_of_int i) ^ var) in
310
	      ( match typ with
311
		  "bool" -> (pre_var, BoolT)
312
313
		| "float" -> (pre_var, FloatT(-.max_float /. 2., max_float /. 2.))
		| "int" -> (pre_var, IntT(min_int / 2, max_int / 2))
314
315
		| str -> failwith ("*** Bad type in .env: " ^ str )
	      )
316
	with e ->
317
	  print_err_msg tok_list "parse_prevar" ;
318
	  raise e
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
347
348
349
350
351
352
353
and (parse_list_label_ce: aut_token -> label_ce list) =
  fun tok -> 
    let tok_list = Stream.npeek 10 tok in
      try
	parse_list (parse_ce) tok
      with e ->
	print_err_msg tok_list "parse_list_label_ce" ;
	raise e
and (parse_ce: aut_token -> label_ce) = 
  fun tok -> 
    let tok_list = Stream.npeek 10 tok in
      try
	match tok with parser  
	    [< 'Genlex.Kwd "(" ; 
	       'Genlex.Ident label ;	 
	       'Genlex.Kwd "," ; 
	       expr = parse_list_ctrl_expr ; 
	       'Genlex.Kwd ")" 
	    >]
	    -> 
	      (label, expr)
	with e ->
	  print_err_msg tok_list "parse_ce" ;
	  raise e
and (parse_list_ctrl_expr : aut_token -> Control.expr) =
  fun tok -> 
    let tok_list = Stream.npeek 10 tok in
    let ce_list =
      try
	parse_list (parse_ctrl_expr) tok
      with e ->
	print_err_msg tok_list "parse_list_ctrl_expr" ;
	raise e
    in
354
355
356
357
358
359
      if ce_list = [] then (fun x -> x)
      else
	List.fold_left 
	  (fun ce1 ce2 -> (fun st -> ce2 (ce1 st)))
	  (List.hd ce_list) 
	  (List.tl ce_list)
360
361
362
363
364
365
366
367
and (parse_ctrl_expr: aut_token -> Control.expr) = 
  fun tok -> 
    let tok_list = Stream.npeek 10 tok in
      try
	match tok with parser  
	    [< 'Genlex.Ident "Set" ; 'Genlex.Ident varname ; 'Genlex.Int i 
	    >] ->
	      (Control.set varname i)
368
369
370
371
	  | [< 'Genlex.Ident "Set_between" ; 'Genlex.Ident varname ;
	       'Genlex.Int i ; 'Genlex.Int min ; 'Genlex.Int max 
	    >] -> 
	      (Control.set_between varname i min max)
372
373
374
375
	  | [< 'Genlex.Ident "Draw_between" ; 'Genlex.Ident varname ;
	       'Genlex.Int min ; 'Genlex.Int max 
	    >] -> 
	      (Control.draw_between varname min max)
376
377
	  | [< 'Genlex.Ident "Draw_gauss" ; 'Genlex.Ident varname ; 
	       m = parse_float ; dev = parse_float 
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
	    >] -> 
	      (Control.draw_gauss varname m dev)
	  | [< 'Genlex.Ident "Dec" ; 'Genlex.Ident varname 
	    >] -> 
	      (Control.dec varname)
	  | [< 'Genlex.Kwd "IfThenElse" ; test = parse_test_expr ; 
	       'Genlex.Kwd "(" ;  
	       cel_then = parse_list_ctrl_expr ; 
	       'Genlex.Kwd ")" ;  
	       'Genlex.Kwd "(" ;  
	       cel_else = parse_list_ctrl_expr ; 
	       'Genlex.Kwd ")" 
	    >] ->  
	      Control.ite test cel_then cel_else
	with e ->
	  print_err_msg tok_list "parse_ctrl_expr" ;
	  raise e

396
397
398
399
400
401
402
403
404
405
406
407
and (parse_float: aut_token -> float) =
  fun tok -> 
    let tok_list = Stream.npeek 10 tok in
      try
	match tok with parser  
	    [< 'Genlex.Float f >] -> f
	  | [< 'Genlex.Int i >] -> (float_of_int i)
	with e ->
	  print_err_msg tok_list "parse_float" ;
	  raise e


408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
and (parse_int_or_var: aut_token -> Control.number) =
  fun tok -> 
    let tok_list = Stream.npeek 10 tok in
      try
	match tok with parser  
	    [< 'Genlex.Int i >] -> Control.IntExpr(i)
	  | [< 'Genlex.Ident id >] -> Control.VarExpr(id)
	with e ->
	  print_err_msg tok_list "parse_int_or_var" ;
	  raise e
	    
and (parse_test_expr: aut_token -> Control.test) =
  fun tok -> 
    let tok_list = Stream.npeek 10 tok in
      try
	match tok with parser
	    [< n1 = parse_int_or_var ; 'Genlex.Kwd op ; n2 = parse_int_or_var>] 
	    -> 
	      match op with
		| "<"  -> Control.GtExpr(n2, n1)
		| "<=" -> Control.GeExpr(n2, n1)
		| ">"  -> Control.GtExpr(n1, n2)
		| ">=" -> Control.GeExpr(n1, n2)
		| "="  -> Control.EqExpr(n1, n2)
		| _ -> assert false
	with e ->
	  print_err_msg tok_list "parse_test_expr" ;
	  raise e
	    

438
439
and (parse_list_arc: vnt list -> aut_token -> read_arc list) = 
  fun vars tok -> 
440
441
    let tok_list = Stream.npeek 10 tok in
      try
442
	parse_list (parse_arc vars) tok
443
444
445
      with e ->
	print_err_msg tok_list "parse_list_arc" ;
	raise e
446
447
and (parse_arc: vnt list -> aut_token -> read_arc) = 
  fun vars tok -> 
448
449
450
    let tok_list = Stream.npeek 10 tok in
      try
	match tok with parser  
451
452
453
454
455
456
	    [< 'Genlex.Ident "From"; 
	       'Genlex.Int node_from ; 
	       'Genlex.Ident "To"; 
	       'Genlex.Int node_to ; 
	       'Genlex.Ident "With"; 
	       arc_info = parse_arc_info vars
457
	    >]
458
459
460
461
	    -> Arc(node_from, arc_info, node_to)
	with e ->
	  print_err_msg tok_list "parse_arc" ;
	  raise e
462
463
and (parse_arc_info: vnt list -> aut_token -> arc_info) = 
  fun vars tok -> 
464
465
466
    let tok_list = Stream.npeek 10 tok in
      try
	match tok with parser  
467
468
469
470
	    [< weight = (parse_weight vars) ; 
	       'Genlex.Ident ":"; 
	       expr = parse_formula_eps vars ; 
	       pc = parse_post_cond_id
471
	    >] 
472
	    -> (weight, expr, pc)
473
474
475
	with e ->
	  print_err_msg tok_list "parse_arc_info" ;
	  raise e
476
477
478
and (parse_post_cond_id: aut_token -> string) =
  fun tok -> 
     let tok_list = Stream.npeek 10 tok in
479
      try
480
481
482
483
484
485
486
487
	match (Stream.npeek 2 tok) with
	    [ Genlex.Ident ":"; Genlex.Ident label ] -> 
	      Stream.junk tok;
	      Stream.junk tok;
	      label
	  | _ -> "identity"
	with e ->
	  print_err_msg tok_list "parse_post_cond_id" ;
488
	  raise e
489
and (parse_weight: vnt list -> aut_token -> weight) = 
490
491
492
493
  fun vars tok -> 
    let tok_list = Stream.npeek 10 tok in
      try
	match tok with parser  
494
495
496
497
498
499
500
	    [< 'Genlex.Int w >] -> Wint(w)
	  | [< 'Genlex.Kwd "!" ; 
	       'Genlex.Ident lbl >] 
	    -> Wident_not_sig(lbl)
	  | [< 'Genlex.Ident lbl >] -> Wident(lbl)
 	with e ->
	  print_err_msg tok_list "parse_weight" ;
501
502
503
	  raise e


504
505
and (parse_formula_eps: vnt list -> aut_token -> formula_eps) = 
  fun vars tok -> 
506
507
508
    let tok_list = Stream.npeek 10 tok in
      try
	match tok with parser   
509
510
	  | [< 'Genlex.Ident "eps" >] -> Eps
	  | [< 'Genlex.Ident "("; 'Genlex.Ident "eps"; 'Genlex.Ident ")" >] -> Eps
511
	  | [< f = parse_formula vars >] -> Form(f)
512
513
514
515
	with e ->
	  print_err_msg tok_list "parse_formula_eps" ;
	  raise e
	    
516
517
and (parse_formula: vnt list -> aut_token -> formula) = 
  fun vars tok -> 
518
519
520
    let tok_list = Stream.npeek 10 tok in
      try
	match tok with parser  
521
522
523
	    [< 'Genlex.Kwd "!"; pl = parse_pragma_list ;
	       f1 = parse_formula vars; 
	       f = parse_more_formula (Not(f1)) vars 
524
525
526
	    >] -> f  
	  | 
	    [< 'Genlex.Kwd "IfThenElse";  pl = parse_pragma_list ;
527
528
	       f1 = parse_formula vars; 
	       f2 = parse_formula vars; 
529
530
531
532
533
	       f3 = parse_formula vars           
	    >] 
	    -> IteB(f1, f2, f3)
	  | 
	    [< 'Genlex.Kwd "("; 
534
	       f1 = parse_formula vars ; 'Genlex.Kwd ")" ; 
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
	       f = parse_more_formula f1 vars    
	    >] 
	    -> f 
	  | 
	    [< 'Genlex.Kwd "true" ; pl = parse_pragma_list ;
	       f = parse_more_formula True vars  
	    >] 
	    -> f
	  | 
	    [< 'Genlex.Kwd "false" ; pl = parse_pragma_list ;
	       f = parse_more_formula False vars 
	    >] 
	    -> f
	  | 
	    (* 
	       for pre and ident, the grammer is not ll(1) because
	       it migth be either expr or formule ; hence I need
	       to look to the next token to know if it is a formula
	       (namely a bool ident) or an expr.
	    *)
	    [< 'Genlex.Ident "pre"; 'Genlex.Kwd "("; 'Genlex.Int i ; 
556
557
	       'Genlex.Kwd "," ; 'Genlex.Ident id; 'Genlex.Kwd ")"; 
	       pl = parse_pragma_list ;  
558
559
560
561
562
563
	       f = parse_expr_or_bool_ident 
		     ("_pre" ^ (string_of_int i) ^ id) vars
	    >] 
	    -> f
	  | 
	    [< 'Genlex.Ident id ; 
564
	       pl = parse_pragma_list ; 
565
	       f1 = parse_expr_or_bool_ident id vars; 
566
567
568
569
570
571
572
573
	       f = parse_more_formula f1 vars    
	    >] 
	    -> f
	  | 
	    [< e1 = parse_expr vars ; 
	       f = parse_expr_right e1 vars 
	    >] 
	    -> f
574
575
576
	with e ->
	  print_err_msg tok_list "parse_formula" ;
	  raise e
577

578
579
580
581
582

(* 
   When an ident is encountered, we don't know yet whether it is a
   boolean or a numerical var because the grammar is not ll(1). 
*)
583
584
and (parse_expr_or_bool_ident: string -> vnt list -> aut_token -> formula) = 
  fun id vars tok -> 
585
    let tok_list = Stream.npeek 10 tok in
586
    let (_, vt) = List.find (fun (vn,vt) -> vn = id) vars in
587
    let some_var =
588
      match vt with
589
590
591
	  BoolT -> None
	| IntT(_,_) -> Some(Ivar(id))
	| FloatT(_,_) -> Some(Fvar(id))
592
    in
593
594
595
596
597
598
599
600
601
602
603
      ( match some_var with
	    None -> 
	      ( try
		 match tok with parser
		      [< f = parse_more_formula (Bvar(id)) vars >] -> f 
		 with e ->
		   print_err_msg tok_list "parse_expr_or_bool_ident" ;
		   raise e
	      )
	  | Some(var) -> 
	      ( try
604
605
606
607
608
609
610
		  match tok with parser
		      [< 
			e1 = parse_more_mults var vars; 
			e  = parse_more_adds e1 vars;
			f  = parse_expr_right e vars
		      >] 
		      -> f
611
612
613
614
615
	      with e ->
		print_err_msg tok_list "parse_expr_or_bool_ident" ;
		raise e
	      )
      )
616

617
618
and (parse_more_formula: formula -> vnt list -> aut_token -> formula) = 
  fun f1 vars tok -> 
619
620
621
    let tok_list = Stream.npeek 10 tok in
      try
	match tok with parser  
622
623
624
	    [< 'Genlex.Kwd "||"; pl = parse_pragma_list ; 
	       f2 = parse_formula vars >] -> Or(f1, f2)
	  | [< 'Genlex.Kwd "&&"; pl = parse_pragma_list ; 
625
626
627
	       f2 = parse_formula vars >] -> And(f1, f2)
	  | [< 'Genlex.Kwd "#"; pl = parse_pragma_list ; 
	       f2 = parse_formula vars >] -> And(Or(f1, f2), Not(And(f1, f2))) (* xor *)
628
629
	  | [< 'Genlex.Kwd "="; pl = parse_pragma_list ; 
	       f2 = parse_formula vars >] -> Or(And(f1, f2), And(Not(f1), Not(f2))) 
630
631
632
633
	  | [< >] -> f1
	with e ->
	  print_err_msg tok_list "parse_more_formula" ;
	  raise e
634
635
and (parse_expr_right : expr -> vnt list -> aut_token -> formula) = 
  fun e1 vars tok -> 
636
637
638
    let tok_list = Stream.npeek 10 tok in
      try
	match tok with parser
639
640
641
642
643
	    [< 'Genlex.Kwd "=";  pl = parse_pragma_list ; e2 = parse_expr vars >] -> Eq(e1, e2) 
	  | [< 'Genlex.Kwd ">";  pl = parse_pragma_list ; e2 = parse_expr vars >] -> Sup(e1, e2) 
	  | [< 'Genlex.Kwd ">="; pl = parse_pragma_list ; e2 = parse_expr vars >] -> SupEq(e1, e2)
	  | [< 'Genlex.Kwd "<";  pl = parse_pragma_list ; e2 = parse_expr vars >] -> Inf(e1, e2) 
	  | [< 'Genlex.Kwd "<="; pl = parse_pragma_list ; e2 = parse_expr vars >] -> InfEq(e1, e2)
644
645
646
647
	with e ->
	  print_err_msg tok_list "parse_expr_rigth" ;
	  raise e
	    
648
649

(* 
650
651
652
 ** The following is copy-paste-adapted from sec 1.8 of the ocaml ref man 
 ** untitled ``pretty printing and parsing''.
 *)
653
654
and (parse_expr: vnt list -> aut_token -> expr) = 
  fun vars tok -> 
655
    match tok with parser  
656
657
658
	[< e1 = parse_mult vars; e = parse_more_adds e1 vars >] -> e
and (parse_more_adds: expr -> vnt list -> aut_token -> expr) = 
  fun e1 vars tok -> 
659
    match tok with parser  
660
661
	[< 'Genlex.Kwd "+"; 
	   pl = parse_pragma_list ;
662
	   e2 = parse_mult vars; 
663
664
665
666
667
	   e = parse_more_adds (Sum(e1, e2)) vars 
	>] 
	-> e
      | [< 'Genlex.Kwd "-"; 
	   pl = parse_pragma_list ;
668
	   e2 = parse_mult vars; 
669
670
671
672
673
	   e = parse_more_adds (Diff(e1, e2)) vars 
	>] 
	-> e
      | [< >] 
	-> e1
674
675
and (parse_mult: vnt list -> aut_token -> expr) = 
  fun vars tok -> 
676
    match tok with parser  
677
678
679
	[< e1 = parse_simple vars; e = parse_more_mults e1 vars >] -> e
and (parse_more_mults: expr -> vnt list -> aut_token -> expr) = 
  fun e1 vars tok -> 
680
    match tok with parser  
681
682
	[< 'Genlex.Kwd "*"; 
	   pl = parse_pragma_list ;  
683
	   e2 = parse_simple vars; 
684
685
686
687
688
	   e = parse_more_mults (Prod(e1, e2)) vars 
	>] 
	-> e
      | [< 'Genlex.Kwd "/"; 
	   pl = parse_pragma_list ;  
689
	   e2 = parse_simple vars; 
690
691
692
693
694
	   e = parse_more_mults (Quot(e1, e2)) vars 
	>] 
	-> e
      | [< 'Genlex.Ident "mod"; 
	   pl = parse_pragma_list ; 
695
	   e2 = parse_simple vars; 
696
697
698
699
700
	   e = parse_more_mults (Mod(e1, e2)) vars 
	>] 
	-> e
      | [<  >] 
	-> e1
701
702
and (parse_simple: vnt list -> aut_token -> expr) = 
  fun vars tok -> 
703
    match tok with parser  
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
      
      | [< 'Genlex.Ident "pre"; 'Genlex.Kwd "(";  
	   'Genlex.Int i ; 'Genlex.Kwd "," ; 'Genlex.Ident id;   
	   'Genlex.Kwd ")"; pl = parse_pragma_list  >] -> 
	  let s = ("_pre" ^ (string_of_int i) ^ id) in 
	  let (_, vt) = List.find (fun (vn,vt) -> vn = s) vars in 
	  let var = 
	    match vt with 
		BoolT -> assert false  
	      | IntT(_,_) -> Ivar(s) 
	      | FloatT(_,_) -> Fvar(s) 
	  in 
	    var 

      | [< 'Genlex.Ident s ; pl = parse_pragma_list >] -> 
719
720
721
	  let (_, vt) = List.find (fun (vn,vt) -> vn = s) vars in
	  let var =
	    match vt with
722
		BoolT -> assert false 
723
724
725
726
	      | IntT(_,_) -> Ivar(s)
	      | FloatT(_,_) -> Fvar(s)
	  in
	    var
727
728
  
      | [< 'Genlex.Int i;   pl = parse_pragma_list >] -> Ival(i)
729
      | [< 'Genlex.Float f; pl = parse_pragma_list >] -> Fval(f)
730
      | [< 'Genlex.Kwd "IfThenElseNum";  pl = parse_pragma_list ; 
731
732
	   f1 = parse_formula vars; 
	   e2 = parse_expr vars; 
733
734
	   e3 = parse_expr vars >] -> Ite(f1, e2, e3) 
      | [< 'Genlex.Kwd "("; e = parse_expr vars; 'Genlex.Kwd ")" >] -> e
735