I'm trying to write a OCamllex parser that constructs a string map of words from a list. However, I receive an "Unbounded module" error when I attempt to open the StringMap module in the header:

{
  open StringMap
  type token = EOF | Word of string
}
(* other code *)

The same error occurs when I don't explicitly open the module, and when I try to create a map within the trailer:

module StringMap = Map.Make (String)

All the OCaml tutorials suggest I'm using the correct syntax; so perhaps I'm misusing OCamllex(?) I admit, the scope the header, rules, and trailer are not clear to me. I've searched for solution in documentation, but tutorials targeted toward OCamllex are scarce. Can anyone tell deduce what I'm doing wrong? Does OCamllex allow the StringMap module to be used?

有帮助吗?

解决方案

The problem is that no StringMap module exists: you have to generate one with a functor application such as module StringMap = Map.Make (String). Place this in the header, not the trailer. (The trailer code will be placed at the end of the generated file, and thus bindings established in it cannot be seen from within your lexer code.)

If you like you may also open the module:

open module StringMap = Map.Make (String)

Opening modules is considered somewhat poor style, though. In particular avoid opening modules such as applications of Map and Set, as their definitions of compare will shadow the usual one from Pervasives and generate confusion.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top