parse_env.ml 5.87 KB
Newer Older
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
(*-----------------------------------------------------------------------
** Copyright (C) 2001 - Verimag.
** This file may only be copied under the terms of the GNU Library General
** Public License 
**-----------------------------------------------------------------------
**
** File: parse_env.ml
** Main author: jahier@imag.fr
**
** Implements a .env parser.
*)

open Formula

  
type read_arc = Arc of node * arc_info * node

type read_automata = Automata of 
  node             (* Initial node *)
  * vnt list  (* Input var list *)
  * vnt list  (* Output var list *)
  * vnt list  (* Local var list *)
  * read_arc list  (* Transition list *)

(* Keywords of the automata format *)
let lexer = Genlex.make_lexer ["automata"; "["; "]"; "("; ")"; ","; ":"; ";";
			       "arc"; "eps"; "pre";
			       "And"; "Or"; "Not"; "true"; "false";
			       "="; ">"; ">=";
			       "+"; "-"; "*"; "/"; "mod"; "%"]

type aut_token = Genlex.token Stream.t

let rec (parse_automata: aut_token -> read_automata) = 
  fun tok -> 
    match tok with parser  
	[< 'Genlex.Kwd "automata" ;
	   'Genlex.Kwd "(" ; 
	   'Genlex.Int node_id ; 'Genlex.Kwd "," ;
	   'Genlex.Kwd "[" ; li = parse_list_var ; 'Genlex.Kwd "]" ; 
	   'Genlex.Kwd "," ;
	   'Genlex.Kwd "[" ; lo = parse_list_var ; 'Genlex.Kwd "]" ; 
	   'Genlex.Kwd "," ;
	   'Genlex.Kwd "[" ; ll = parse_list_var ; 'Genlex.Kwd "]" ; 
	   'Genlex.Kwd "," ;
	   'Genlex.Kwd "[" ; la = parse_list_arc ; 'Genlex.Kwd "]" ;
	   'Genlex.Kwd ")" >] 
	-> Automata(node_id, li, lo, ll, la)
	  
and (parse_list_var: aut_token -> vnt list) = 
  fun tok -> 
    match tok with parser  
	[< vnt = parse_var ; tail = parse_list_var_tail >]  
	-> vnt :: tail  
      | [< >] -> [] (* empty list *)
and (parse_list_var_tail: aut_token -> vnt list) = 
  fun tok -> 
    match tok with parser 
      | [< 'Genlex.Kwd ";" ; vnt = parse_var ; 
	   tail = parse_list_var_tail >]
	-> vnt :: tail  
      | [< >] -> [] (* end of the list *)
and (parse_var: aut_token -> vnt) = 
  fun tok -> 
    match tok with parser  
	[< 'Genlex.Kwd "("; 'Genlex.String var; 'Genlex.Kwd ","; 
	   'Genlex.String typ ; 'Genlex.Kwd ")" >]
	-> (var, typ)

and (parse_list_arc: aut_token -> read_arc list) = 
  fun tok -> 
    match tok with parser 
	[< arc = parse_arc ; tail = parse_list_arc_tail >]
	-> arc :: tail  
      | [< >] -> [] (* empty list *)
and (parse_list_arc_tail: aut_token -> read_arc list) = 
  fun tok -> 
    match tok with parser
	[< 'Genlex.Kwd ";";  arc = parse_arc ; tail = parse_list_arc_tail >]
	-> arc :: tail   
      | [< >] -> [] (* end of the list *)
and (parse_arc: aut_token -> read_arc) = 
  fun tok -> 
    match tok with parser  
	[< 'Genlex.Kwd "arc"; 'Genlex.Kwd "("; 'Genlex.Int node_from ; 
	   'Genlex.Kwd ","; arc_info = parse_arc_info ; 
	   'Genlex.Kwd "," ; 'Genlex.Int node_to ; 'Genlex.Kwd ")";  >]
	-> Arc(node_from, arc_info, node_to)
and (parse_arc_info: aut_token -> arc_info) = 
  fun tok -> 
    match tok with parser  
	[< 'Genlex.Int weigth ; 'Genlex.Kwd ":"; expr = parse_formula_eps >] 
	-> (weigth, expr)

and (parse_formula_eps: aut_token -> formula_eps) = 
  fun tok -> 
    match tok with parser   
	[< 'Genlex.Kwd "eps" >] -> Eps
      | [< f = parse_formula >] -> Form(f)
	  
and (parse_formula: aut_token -> formula) = 
  fun tok -> 
    match tok with parser  
	[< 'Genlex.Kwd "Not"; f1 = parse_formula ; 
	   f = parse_more_formula (Not(f1)) >] -> f  
      | [< 'Genlex.Kwd "("; f1 = parse_formula; 'Genlex.Kwd ")" ; 
	   f = parse_more_formula f1 >] -> f 
      | [< 'Genlex.Kwd "pre" ; 'Genlex.Kwd "("; f = parse_formula ; 
	   'Genlex.Kwd ")" >] -> Pre_f(f)
      | [< 'Genlex.Kwd "true" ; f = parse_more_formula True >] -> f
      | [< 'Genlex.Kwd "false" ; f = parse_more_formula False >] -> f
      | [< 'Genlex.Ident b ; f = parse_more_formula (Bool_id(b)) >] -> f
      | [< e1 = parse_expr; f1 = parse_expr_right e1 ; 
	   f = parse_more_formula f1 >] -> f

and (parse_more_formula: formula -> aut_token -> formula) = 
  fun f1 tok -> 
    match tok with parser  
	[< 'Genlex.Kwd "Or";  f2 = parse_formula >] -> Or(f1, f2)
      | [< 'Genlex.Kwd "And"; f2 = parse_formula >] -> And(f1, f2) 
      | [< >] -> f1
and (parse_expr_right : expr -> aut_token -> formula) = 
  fun e1 tok -> 
    match tok with parser
	[< 'Genlex.Kwd "=";  e2 = parse_expr >] -> Eq(e1, e2) 
      | [< 'Genlex.Kwd ">";  e2 = parse_expr >] -> G(e1, e2) 
      | [< 'Genlex.Kwd ">="; e2 = parse_expr >] -> Ge(e1, e2)
 

(* 
** The following is copy-paste-adapted from sec 1.8 of the ocaml ref man 
** untitled ``pretty printing and parsing''.
*)
and (parse_expr: aut_token -> expr) = 
  fun tok -> 
    match tok with parser  
	[< e1 = parse_mult; e = parse_more_adds e1 >] -> e
and (parse_more_adds: expr -> aut_token -> expr) = 
  fun e1 tok -> 
    match tok with parser  
	[< 'Genlex.Kwd "+"; e2 = parse_mult; 
	   e = parse_more_adds (Sum(e1, e2)) >] -> e
      | [< 'Genlex.Kwd "-"; e2 = parse_mult; 
	   e = parse_more_adds (Diff(e1, e2)) >] -> e
      | [<  >] -> e1
and (parse_mult: aut_token -> expr) = 
  fun tok -> 
    match tok with parser  
	[< e1 = parse_simple; e = parse_more_mults e1 >] -> e
and (parse_more_mults: expr -> aut_token -> expr) = 
  fun e1 tok -> 
    match tok with parser  
	[< 'Genlex.Kwd "*";   e2 = parse_simple; 
	   e = parse_more_mults (Prod(e1, e2)) >] -> e
      | [< 'Genlex.Kwd "/";   e2 = parse_simple; 
	   e = parse_more_mults (Quot(e1, e2)) >] -> e
      | [< 'Genlex.Kwd "mod"; e2 = parse_simple; 
	   e = parse_more_mults (Mod(e1, e2)) >] -> e
      | [< 'Genlex.Kwd "%";   e2 = parse_simple; 
	   e = parse_more_mults (Div(e1, e2)) >] -> e
      | [<  >] -> e1
and (parse_simple: aut_token -> expr) = 
  fun tok -> 
    match tok with parser  
	[< 'Genlex.Ident s >] -> Var(s)
      | [< 'Genlex.Int i >]   -> Int_id(i)
      | [< 'Genlex.Float f >] -> Float_id(f)
      | [< 'Genlex.Kwd "("; e = parse_expr; 'Genlex.Kwd ")" >] -> e
      | [< 'Genlex.Kwd "pre" ;  e = parse_expr  >] -> Pre_e(e)