How does one specify types of functions for lexers in ocamlex?

Why is this complaining about the syntax? Can’t I specify the type in a lexer? The error is at this line:

and get_str accumulator:string = parse

error msg:

❯ ocamllex bin.mll
File "bin.mll", line 85, character 24: illegal character :.

Appendix

all the code:

rule token = parse
  | [' ' '\t'] { token lexbuf }  (* skip over whitespace *)
  | ['\n']     { token lexbuf }  (* skip over whitespace *)
  | eof        { EOF          }

  | "~"     { NEG }
  | "-"     { MINUS  }
  | "*"     { TIMES  }
  | "/"     { DIV  }
  | "+."    { DPLUS  }
  | "-."    { DMINUS  }
  | "*."    { DTIMES  }
  | "/."    { DDIV  }
  | "^"     { CARAT  }
  | "<"     { LT  }
  | "<="    { LEQ  }
  | ">="    { GEQ  }
  | "="     { EQUALS  }
  | "<>"    { NEQ }
  | "|"     { PIPE  }
  | "->"    { ARROW  }
  | "::"    { DCOLON  }
  | ";"     { SEMI }
  | ";;"    { DSEMI }
  | "@"     { AT  }
  | "[]"    { NIL }
  | "let"   { LET  }
  | "and"   { AND}
  | "end"   { END}
  | "in"    { IN  }
  | "if"    { IF  }
  | "then"  { THEN  }
  | "else"  { ELSE  }
  | "mod"   { MOD  }
  | "try"   { TRY }
  | "with"  { WITH }
  | "not"   { NOT  }
  | "&&"    { LOGICALAND}
  | "||"    { LOGICALOR}
  | "["     { LBRAC  }
  | "()"    { UNIT }
  | "("     { LPAREN  }
  | ")"     { RPAREN  }
  | ","     { COMMA  }
  | "_"     { UNDERSCORE }
  | "true"  { TRUE }
  | "false" { FALSE }
  | "()"    { UNIT }

  | numeric+ as s         { INT (int_of_string s) }
  | ("0x"(hexadec)+) as s { INT (int_of_string s) }
  | ((numeric+)'.'(numeric*)('e'(numeric)+)?) as s       { FLOAT (float_of_string s) }
(* your rules go here *)

  | "+" { PLUS }
  | ">" { GT }
  | "]" { RBRAC }
  | "rec" { REC }
  | "fun" { FUN }
  | "raise" { RAISE }

  | "0b" ['0' - '1']+ as s { INT (int_of_string s)  }

  | lowercase (alphanum | "\'" | "_")* as s { IDENT s }

  (* | "\"" as c { let s=(String.make 1 c) in get_str s lexbuf } *)
  | "\"" as c { let s=(String.make 1 c) in get_str s lexbuf }
and get_str accumulator:string = parse
  | "\"" as c { let s=(String.make 1 c) in STRING (accumulator^s) }
  (* | "\"" as s { STRING (accumulator^s) } *)
  (* | "\"" as s { STRING (s) } *)
  | "\\\"" as s { get_str (accumulator^s) lexbuf }
  | (alphanum|whitespace)* as s { get_str (acculuator^s) }

An ocamllex input file is not a file of OCaml code. It contains OCaml code inside the actions { ... }, but is otherwise a specialized language of its own. There’s apparently no provision in its syntax for specifying the type of an argument.

Granted, the specification in the manual doesn’t make this completely clear. Arguments are designated arg1 and so on but aren’t further defined. I would guess they must be simple identifiers.

1 Like