这就是我目前的mll文件,它运行得很好。
{ type token = EOF | Word of string }
rule token = parse
| eof { EOF }
| ['a'-'z' 'A'-'Z']+ as word {Word(word)}
| _ { token lexbuf }
{
let lexbuf = Lexing.from_channel stdin in
let wordlist =
let rec next l =
match token lexbuf with
EOF -> l
| Word(s) -> next (s::l)
in next []
in
List.iter print_endline wordlist
}我做ocamllex a.mll,然后做ocamlc -o a a.ml。运行./a < a.mll将打印出mll文件中存在的所有字符串,这正是我所期望的。
但是,如果我在module StringMap = Map.Make(String)调用之前添加List.iter,则会得到一个语法错误.
File "a.mll", line 17, characters 4-10:,第17行是module的行,4-10是module这个词.
我不明白为什么添加这一行会给我一个语法错误.如果我在toplevel中输入相同的代码,它就会工作得很好。
发布于 2017-10-25 22:01:47
我假设ocamllex生成的代码在函数中结束。不能在函数中声明全局样式的模块。
但是,您可以像这样声明一个本地模块:
let module StringMap = Map.Make(String) in ...示例:
# let fgm () =
module StringMap = Map.Make(String)
StringMap.cardinal StringMap.empty;;
Error: Syntax error
# let flm () =
let module StringMap = Map.Make(String) in
StringMap.cardinal StringMap.empty;;
val flm : unit -> int = <fun>
# flm ();;
- : int = 0https://stackoverflow.com/questions/46942644
复制相似问题