vent
vent

Reputation: 39

[Ocaml]: Build lexer.mll

I use Ocaml by installing OcamlWinPlus and its component Emacs. In my source I have two file main.ml and lexer.mll with following code:

main.ml:

open Format

let print data =
  List.iter (fun l -> printf "%s@." (String.concat " " l)) data
let () = 
  match List.tl (Array.to_list Sys.argv) with
  | [filename] -> 
      let ch = open_in filename in
      let data = Lexer.lex [] (Lexing.from_channel ch) in
      close_in ch;
      print data
  | _ -> eprintf "killer <file>@."; exit 1

lexer.mll

rule lex xs = parse
  | [' '  '\r' '\t']     { lex xs lexbuf }
  | '\n'                 { List.rev xs :: lex [] lexbuf }
  | ['0'-'9']+ as x      { lex (x :: xs) lexbuf }
  | eof                  { [] }

there is a trouble when I run Ocaml in emacs:

Error: Unbound module Lexer

Please help me fix it

Upvotes: 0

Views: 1015

Answers (1)

camlspotter
camlspotter

Reputation: 9040

You failed to describe what you tried here... I guess that you did not compile Lexer before compiling main.ml, though it depends on Lexer. What you should do is:

  • Convert lexer.mll to lexer.ml with ocamllex: ocamllex lexer.mll
  • Compile lexer.ml: ocamlc lexer.ml
  • Then compile main.ml

To automate this process, you should use some build tool such as make, ocamlbuild, omake, etc. In any case, you should read a good OCaml introductory book to learn how to use OCaml and its tools with multi source files.

Upvotes: 3

Related Questions