Ocaml抽象语法树 - 语法错误

时间:2013-08-10 07:40:45

标签: ocaml abstract-syntax-tree

我仍然是OCaml的新手,并为一种小语言编写了扫描仪,解析器和抽象语法树(带有漂亮的打印机)。我的AST用于编译正常,但现在它给我一个语法错误,我无法弄清楚为什么。

以下是我收到错误的代码部分:

 1    type op = Add | Sub | Mult | Div | Equal | Neq | Less | Leq | Greater | Geq
 2        | And | Or | Not
 3        
 4        
 5   type primitive = Int | Bool | String
 6   type objtype = Room | Thing | GameMainDef
 7
 8   type program = odecl list
 9
10   type odecl = {
11   object_name : string;
12   obj_type : objtype;
13   attrib : (string * expr) list;   
14   funcdecl : f_decl list;
15   }
16  
17  type expr =
18     Literal of int
19   | BoolLiteral of bool
20   | StringLiteral of string   
21   | Id of string
22   | Unaryop of op * expr
23   | Binop of expr * op * expr
24   | Assign of string * expr
25   | Call of string * expr list
26   | Noexpr
27
28  type stmt =
29     Block of stmt list
30   | Expr of expr
31   | If of expr * stmt * stmt
32   | For of expr * expr * expr * stmt
33   | While of expr * stmt
34  
35   type fdecl = {
36     fname : string;
37     locals : (primitive * string) list;
38     body : stmt list;
39    }
40
41    let rec string_of_type t = match t with
42     Bool -> "bool"
43   | Int -> "int"
44   | String -> "string"
45   | Room -> "Room"
46   | Thing -> "Thing"
47   | MainGameDef -> "MainGameDef"

还有大约100行代码(漂亮的打印机)。

我收到以下语法错误:

文件“Ast.mli”,第41行,字符0-3: 错误:语法错误

如果有人有任何建议,我们将非常感激。谢谢! :)

1 个答案:

答案 0 :(得分:4)

根据您更新的帖子,您正在编译mli文件。 Mli是一个接口文件,您无法编写函数/值定义。您收到语法错误,因为let在接口中无效。