Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
type token =
End
| TInt of int
| TPlus
| TMoins
| TMul
| TPow
| Lpar
| Rpar
type stream = { pos : int; buf : string }
let stream_of_string buf = { pos = 0; buf = buf }
exception Lexing_error of stream
let next_token s =
if s.pos = String.length s.buf then (End, s)
else
let next_stream size_token =
{ pos = s.pos + size_token; buf = s.buf } in
match s.buf.[s.pos] with
'0' .. '9' ->
let rec read_int acc p =
if p < String.length s.buf && s.buf.[p] >= '0' && s.buf.[p] <= '9' then
read_int (acc * 10 + Char.code s.buf.[p] - Char.code '0') (p + 1)
else
(TInt acc, next_stream (p - s.pos))
in
read_int 0 s.pos
| '+' -> TPlus, next_stream 1
| '-' -> TMoins, next_stream 1
| '*' -> TMul, next_stream 1
| '^' -> TPow, next_stream 1
| '(' -> Lpar, next_stream 1
| ')' -> Rpar, next_stream 1
| _ -> raise (Lexing_error s)
let rec tokenize : stream -> token list =
fun s ->
match next_token s with
End, _ -> [End]
| t, s' -> t :: tokenize s'
type expr =
Int of int
| Plus of expr * expr
| Moins of expr * expr
| Mul of expr * expr
| Pow of expr * expr
| Neg of expr
exception Syntax_error of stream
let syntax_error s = raise (Syntax_error (s))
(*
let _ = expr_of_string "24-5+3"
let _ = expr_of_string "24*5+3"
let _ = expr_of_string "24*5+3*4"
let _ = expr_of_string "1*2-3"
let _ = expr_of_string "1+2*3"
let e = expr_of_string "1+2-3-4-5-6-7"
let s = stream_of_string "24*5+3";;
let x, s = next_token s;;
let s = string_of_expr e
*)
(*
S -> T end
T -> F | T + F | T - F
F -> N | F * N
N -> P | -P
P -> A | P ^ A
A -> int | ( T )
*)
let rec p_expr s =
let t, s1 = p_terme s in
match next_token s1 with
End, _ -> t
| _, s' -> syntax_error s'
and p_terme s =
let f, s1 = p_facteur s in
p_reste_terme f s1
and p_reste_terme t s =
match next_token s with
| TPlus, s2 ->
let f, s3 = p_facteur s2 in
p_reste_terme (Plus (t, f)) s3
| TMoins, s2 ->
let f, s3 = p_facteur s2 in
p_reste_terme (Moins (t, f)) s3
| _ -> t, s
and p_facteur s =
let n, s1 = p_neg s in
p_reste_facteur n s1
and p_reste_facteur f s =
match next_token s with
TMul, s1 ->
let n, s2 = p_neg s1 in
p_reste_facteur (Mul (f, n)) s2
| _ -> f, s
and p_neg s =
match next_token s with
TMoins, s1 ->
let p, s2 = p_pow s1 in
(Neg p), s2
| _ -> p_pow s
and p_pow s =
let n, s1 = p_atome s in
p_reste_pow n s1
and p_reste_pow p s =
match next_token s with
TPow, s1 ->
let a, s2 = p_atome s1 in
p_reste_pow (Pow (p, a)) s2
| _ -> p, s
and p_atome s =
match next_token s with
TInt i, s1 -> Int i, s1
| Lpar, s1 ->
let t, s2 = p_terme s1 in
(match next_token s2 with
Rpar, s3 -> t, s3
| _ -> syntax_error s2
)
| _ -> syntax_error s
let expr_of_string s = p_expr (stream_of_string s)
let string_of_expr_naif e =
let rec soen e =
match e with
Int i -> string_of_int i
| Plus (e1, e2) -> "(" ^ soen e1 ^ ")+(" ^ soen e2 ^ ")"
| Mul (e1, e2) -> "(" ^ soen e1 ^ ")*(" ^ soen e2 ^ ")"
| Moins (e1, e2) -> "(" ^ soen e1 ^ ")-(" ^ soen e2 ^ ")"
| Pow (e1, e2) -> "(" ^ soen e1 ^ ")^(" ^ soen e2 ^ ")"
| Neg e -> "-(" ^ soen e ^ ")"
in
soen e
let e = string_of_expr_naif (expr_of_string "1+2-3-4");;
let e = string_of_expr_naif (expr_of_string "1-2^3^(-4)");;
(*
S -> T end
T -> F | T + T | T - F
F -> N | F * F
N -> P | -P
P -> A | P ^ A
A -> int | ( T )
*)
let string_of_expr e =
let rec string_of_term e =
match e with
Plus (e1,e2) -> string_of_term e1 ^"+" ^ string_of_term e2
| Moins (e1,e2) -> string_of_term e1 ^"-" ^ string_of_fact e2
| _ -> string_of_fact e
and
string_of_fact e =
match e with
Mul (e1,e2) -> string_of_fact e1 ^ "*" ^ string_of_fact e2
| _ -> string_of_neg e
and
string_of_neg e =
match e with
Neg e' -> "-" ^ string_of_pow e'
| _ -> string_of_pow e
and
string_of_pow e =
match e with
Pow (e1,e2) -> string_of_pow e1 ^ "^" ^ string_of_atom e2
| _ -> string_of_atom e
and
string_of_atom e =
match e with
Int i -> string_of_int i
| _ -> "(" ^ string_of_term e ^ ")"
in
string_of_term e
;;
let s= expr_of_string "-(1+-2)";;
let _ = string_of_expr (s);;
let s1 = expr_of_string "1^2-3-4";;
let _ = string_of_expr (s1);;
let s2 = expr_of_string "1^2*3";;
let _ = string_of_expr (s2);;
let s3 = expr_of_string "(2*3)^2";;
let _ = string_of_expr (s3);;