Keyword table

The number of status transitions generated by ocamllex are limited to at most 32767. If you use too many transitions, for example, too many keywords, ocamllex generates the following error message:

camllex: transition table overflow, automaton is too big

It tells that your lexer definition is too complex. To make the generated automata small, you have to encode using keyword table:

{
  let keyword_table = Hashtbl.create 72
  let _ =
    List.iter (fun (kwd, tok) -> Hashtbl.add keyword_table kwd tok)
              [ ("keyword1", KEYWORD1);
                ("keyword2", KEYWORD2);
		...
              ]
}

rule token = parse
  | ...
  | ['A'-'Z' 'a'-'z'] ['A'-'Z' 'a'-'z' '0'-'9' '_']* as id
               { try
	       		Hashtbl.find keyword_table id
                 with
		 	Not_found -> IDENT id
		}
  | ...

For a complete example, see Toy Language program.

comments powered by Disqus