我编写了一个程序来评估 OCaml 中的逆波兰符号,但是当我添加异常的 gestion 时遇到了一些麻烦。
这是我的代码:
open Stack
type lexeme =
| Nombre of float
| Op_binaire of (float -> float -> float)
| Op_unaire of (float -> float)
exception Syntax_Error
let evalue lst =
let pile = create () in
let rec aux = function
| [] -> let rep = pop pile in
try
pop pile;
raise Syntax_Error with
Empty -> rep
| Nombre a :: q -> push a pile; aux q
| Op_unaire f :: q ->
let a = pop pile in
push (f a) pile;
aux q
| Op_binaire f :: q ->
let a = pop pile in
let b = pop pile in
push (f a b) pile;
aux q
in
try
aux lst
with
| Empty -> raise Syntax_Error
控制台返回错误信息:
Line 9, characters 12-14:
9 | | (Nombre a)::q -> push a pile ; aux q
^^
Error: This variant pattern is expected to have type exn
The constructor :: does not belong to type exn
有人能帮助我吗 ?我认为问题在于 OCaml 的语法特异性,因为我在 Caml Light 上看到了一个类似的程序,它运行良好。