This repository has been archived by the owner on Jun 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.ml
276 lines (232 loc) · 9.03 KB
/
code.ml
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
(* Décommenter la commande directory selon votre verison d'ocaml *)
(* #directory "Module/ocaml-4.02.3/";; *)
(* #directory "Module/ocaml-4.05.0/";; *)
#directory "Module/ocaml-4.08.1/";;
(* #directory "Module/ocaml-4.10.0/";; *)
(* #directory "Module/ocaml-4.11.1/";; *)
(* ----------------------------------- Analyse lexicale ----------------------------------- *)
(* Chargement du module Expression_scanner pour l'analyse lexicale *)
#load "expression_scanner.cmo";;
open Expression_scanner;;
(* Pour vérifier si le module est bien chargé *)
#show Expression_scanner;;
(* Test des fonctions du module *)
let _ : token = Add;; (* Expression_scanner.token = Add *)
let _ : token = Variable('x');; (* Expression_scanner.token = Variable 'x' *)
let _ : token = Number(2);; (* Expression_scanner.token = Number 2 *)
string_to_token_list "34 56 2 + x * -;";; (* Expression_scanner.token list = [Number 34; Number 56; Number 2; Add; Variable 'x'; Multiply; Subtract; End] *)
(* --------------------- Analyse syntaxique et construction de l'arbre --------------------- *)
(* Chargement du module Stack pour manipuler des piles *)
open Stack;;
(* Création du type des arbres de syntaxe abstraite *)
type operator = | Plus | Minus | Mult | Div;;
type tree =
| Var of char
| Cst of int
| Unary of tree
| Binary of operator * tree * tree;;
(* Conversion d'un token en operateur *)
let operator_of_token token =
match token with
| Add -> Plus
| Subtract -> Minus
| Multiply -> Mult
| Divide -> Div
| _ -> failwith "le token n'est pas convertible en opérateur";;
(* Analyse d'une expression de Lukasiewicz *)
let parse token_list =
let stack : tree t = create() in
let rec parse_aux(token_l : token list): tree =
match token_l with
| [] -> failwith "l'expression de Lukasiewicz est mal formée"
| End::[] -> pop stack
| hd::tl ->
match hd with
| Add | Subtract | Multiply | Divide ->
let tree = Binary(operator_of_token hd, pop stack, pop stack) in
push tree stack;
parse_aux tl
| Minus ->
let tree = Unary(pop stack) in
push tree stack;
parse_aux tl
| Variable(v) ->
push (Var(v)) stack;
parse_aux tl
| Number(n) ->
push (Cst(n)) stack;
parse_aux tl
| _ -> failwith "l'expression de Lukasiewicz est mal formée"
in parse_aux token_list
;;
(* Test d'expressions correctes *)
parse (string_to_token_list "34 56 2 + x * -;");;
parse (string_to_token_list "x 3 + 5 7 + + 3 4 * 1 3 + / /;");;
parse (string_to_token_list "34;");;
(* Test d'expressions incorrectes *)
parse (string_to_token_list "34 56 2 + x * -");;
parse (string_to_token_list "34 56 2 ; + x * -");;
parse (string_to_token_list "34 56 2 + + x * -");;
parse (string_to_token_list "");;
parse (string_to_token_list ";");;
parse (string_to_token_list "34");;
(* ------------------------------ Simplification sur l'arbre ------------------------------ *)
(* Evaluation d'une sous-expression de constantes entières *)
let evaluate operator n1 n2 =
match operator with
| Plus -> Cst(n1 + n2)
| Minus -> Cst(n1 - n2)
| Mult -> Cst(n1 * n2)
| Div ->
if n2 <> 0
then Cst(n1 / n2)
else Binary(operator, Cst(n1), Cst(n2))
;;
(* Test d'évaluation d'une sous-expression d'entiers *)
evaluate Plus 5 9;;
evaluate Div 8 2;;
evaluate Div 8 0;;
(* Evaluation d'une sous-expression de variables *)
let eval_sub_expr operator x y =
if x = y
then
match operator with
| Minus -> Cst(0)
| Div -> Cst(1)
| _ -> Binary(operator, x, y)
else
match (operator, x, y) with
| (Mult, Cst(1), z)
| (Mult, z, Cst(1))
| (Plus, Cst(0), z)
| (Plus, z, Cst(0)) -> z
| (Mult, Cst(0), _)
| (Mult, _, Cst(0)) -> Cst(0)
| _ -> Binary(operator, x, y)
;;
(* Test d'évaluation d'une sous-expression de variables *)
eval_sub_expr Minus (Var('x')) (Var('x'));;
eval_sub_expr Plus (Cst(0)) (Var('x'));;
eval_sub_expr Div (Var('x')) (Var('y'));;
eval_sub_expr Mult(Cst(0)) (Var('x'));;
(* Evaluation d'une sous-expression *)
let rec simplification tree =
match tree with
| Binary(operator, x, y) -> (
match (simplification x, simplification y) with
| (Cst(n1), Cst(n2)) -> evaluate operator n1 n2
| (simpl_x, simpl_y) -> eval_sub_expr operator simpl_x simpl_y)
| _ -> tree
;;
(* Test de l'evaluation d'une sous-expression *)
simplification (Binary(Mult, Cst(3), Cst(4)));;
simplification (Binary(Mult, Cst(1), Var('x')));;
simplification (Binary(Plus, Var('y'), Cst(0)));;
simplification (Binary(Div, Var('x'), Var('x')));;
simplification (Unary(Var('x')));;
simplification (parse (string_to_token_list "34 56 2 + x * -;"));;
simplification (parse (string_to_token_list "x 3 + 5 7 + + 3 4 * 1 3 + / /;"));;
(* ------------------------------ Affichage des expressions ------------------------------ *)
(* Ouvre le module Printf pour effectuer un affichage sur un terminal *)
open Printf;;
#show Printf;;
(* Test d'un printf en Ocaml *)
let _ = printf "Hello World !\n";;
let test_s = "Hello World !";;
let _ = printf "%s\n" test_s;;
(* Conversion un operateur en string *)
let string_of_operator op =
match op with
| Plus -> " + "
| Minus -> " - "
| Mult -> " * "
| Div -> " / "
;;
(* Test de la conversion d'un operateur en string *)
string_of_operator Plus;;
string_of_operator Minus;;
string_of_operator Mult;;
string_of_operator Div;;
(* Ouverture du module Char pour utiliser la fonction 'escaped' *)
open Char;;
(* Conversion d'un arbre en string sans simplifiaction des parenthèses *)
let string_of_tree tree =
let rec string_of_tree_aux tree first =
match tree with
| Cst(n) -> string_of_int n
| Var(x) -> escaped x
| Unary(exp) -> "(-" ^ (string_of_tree_aux exp false) ^ ")"
| Binary(op, x, y) ->
if first
then (string_of_tree_aux x false) ^ (string_of_operator op) ^ (string_of_tree_aux y false)
else "(" ^ (string_of_tree_aux x false) ^ (string_of_operator op) ^ (string_of_tree_aux y false) ^ ")"
in string_of_tree_aux tree true
;;
(* Conversion d'un arbre en string avec simplifiaction des parenthèses *)
let rec string_of_tree_final tree =
match tree with
| Cst(n) -> string_of_int n
| Var(x) -> escaped x
| Unary(exp) -> (
match exp with
| Cst(_) | Var(_) -> "(-" ^ (string_of_tree_final exp) ^ ")"
| _ -> "(-(" ^ (string_of_tree_final exp) ^ "))")
| Binary(op, x, y) ->
match op with
| Plus | Minus -> (string_of_tree_final x) ^ (string_of_operator op) ^ (string_of_tree_final y)
| _ ->
match (x, y) with
| (Binary(Plus,_,_), _) | (Binary(Minus,_,_), _) -> "(" ^ (string_of_tree_final x) ^ ")" ^ (string_of_operator op) ^ (string_of_tree_final y)
| (_, Binary(Plus,_,_)) | (_, Binary(Minus,_,_)) -> (string_of_tree_final x) ^ (string_of_operator op) ^ "(" ^ (string_of_tree_final y) ^ ")"
| _ -> (string_of_tree_final x) ^ (string_of_operator op) ^ (string_of_tree_final y)
;;
(* Test de la conversion en string *)
string_of_tree (parse (string_to_token_list "34 56 2 + x * -;"));;
string_of_tree_final (simplification (parse (string_to_token_list "34 56 2 + x * -;")));;
string_of_tree (parse (string_to_token_list "x 3 + 5 7 + + 3 4 * 1 3 + / /;"));;
string_of_tree_final (simplification (parse (string_to_token_list "x 3 + 5 7 + + 3 4 * 1 3 + / /;")));;
string_of_tree_final (simplification (parse (string_to_token_list "a b * c * e f + *;")));;
(* Affichage sur le terminal de l'expression sans simplifiaction des parenthèses *)
let print_exp tree =
printf "%s\n" (string_of_tree tree)
;;
(* Affichage sur le terminal de l'expression avec simplifiaction des parenthèses *)
let print_exp_final tree =
printf "%s\n" (string_of_tree_final tree)
;;
(* Test de l'affichage *)
print_exp (parse (string_to_token_list "34 56 2 + x * -;"));;
print_exp_final (simplification (parse (string_to_token_list "34 56 2 + x * -;")));;
print_exp (parse (string_to_token_list "x 3 + 5 7 + + 3 4 * 1 3 + / /;"));;
print_exp_final (simplification (parse (string_to_token_list "x 3 + 5 7 + + 3 4 * 1 3 + / /;")));;
(* ----------------------------------- Programme final ----------------------------------- *)
(* Ouvre le module Printf pour effectuer un affichage sur un terminal *)
open List;;
#show List;;
(* Fonction principale *)
let main input =
let input_list = string_to_token_list input in
let (temp_list, token_list) =
fold_right (fun elem (sub_list, list) ->
if elem = End && sub_list <> []
then ([End], sub_list::list)
else (elem::sub_list, list))
input_list ([], []) in
let num_exp = ref 1 in
map (fun elem ->
let tree = parse elem in
printf "Expression numéro %d avant simplification :\n" !num_exp;
print_exp tree;
let simpl_tree = simplification tree in
printf "Expression numéro %d après simplification :\n" !num_exp;
print_exp_final simpl_tree;
printf "\n";
num_exp := !num_exp + 1;)
(temp_list::token_list)
;;
(* Test de la fonction principale *)
main "34 56 2 + x * -;
x 3 + 5 7 + + 3 4 * 1 3 + / /;
a b * c * e f + *;
13 2 5 * 1 0 / - +;"
;;