ocamllex syntax error
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
I get the following laconic error message :
$ ocamllex huong.ml
File "huong.mll", line 2, character 2: syntax error.
Here is my complete file :
{
let create_hashtable size init =
let tbl = Hashtbl.create size in
List.iter (fun (key, data) -> Hashtbl.add tbl key data) init;
tbl
type token =
| OP of char
| INT of int
| FLOAT of float
| CHAR of char
| PUNCTUATION_MARK of char
| RESERVED of string
| UNRESERVED of string
let reserved_words=
[
"AUTO_INCREMENT";
"CHARSET";
"COLLATE";
"CREATE";
"DEFAULT";
"ENGINE";
"INSERT";
"INTO";
"KEY";
"NOT";
"NULL";
"PRIMARY";
"SET";
"SQL_MODE";
"time_zone";
"VALUES"
]
let punctuation_marks=
[
';';
'\"';
'\'';
'=';
'`';
',';
'?';
':';
'(';
')';
'{';
'}';
]
let keyword_list =
(List.map (fun c->(String.make 1 c,PUNCTUATION_MARK c)) reserved_words )
@(List.map (fun x->(x,RESERVED x)) reserved_words )
let keyword_table =
create_hashtable (List.length keyword_list) keyword_list
let accu=ref([])
let memorize x=(accu:=x::(!accu))
}
let digit = ['0'-'9']
let id = ['a'-'z' 'A'-'Z' '0'-'9']*
rule toy_lang = parse
| digit+ as inum
{ let num = int_of_string inum in
let tok=INT num in
memorize tok; tok
}
| digit+ '.' digit* as fnum
{ let num = float_of_string fnum in
let tok=FLOAT num in
memorize tok; tok
}
| id as word
{ try
let token = Hashtbl.find keyword_table word in
memorize token; token
with Not_found ->
let token=UNRESERVED word in
memorize token; token
}
| '+'
| '-'
| '*'
| '/' as op
{ let tok=OP op in
memorize tok; tok
}
| [' ' '\t' '\n'] (* eat up whitespace *)
()
| _ as c
{
let tok=CHAR c in
memorize tok; tok
}
| eof
{ raise End_of_file }
{
let rec parse lexbuf =
let token = toy_lang lexbuf in
(* do nothing in this example *)
parse lexbuf
}