-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstokens.mll
33 lines (28 loc) · 960 Bytes
/
stokens.mll
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{
type token = PLUS | IF | ID of string | NUM of int | EOF
let string_of_token = function
| PLUS -> "PLUS"
| IF -> "IF"
| ID(s) -> Printf.sprintf "ID(%s)" s
| NUM(i) -> Printf.sprintf "NUM(%d)" i
| EOF -> "eof"
let rec iterate scanner =
match scanner () with
| EOF -> ()
| tok -> Printf.printf "%s\n" (string_of_token tok); iterate scanner
}
let letter = ['a'-'z' 'A'-'Z']
let digit = ['0' - '9']
let identifier = letter (letter | digit | '_')*
rule token = parse
| [' ' '\n' '\t'] { token lexbuf } (* ignore whitespace *)
| '+' { PLUS } (* a symbol *)
| "if" { IF } (* a keyword *)
| identifier as id { ID(id) } (* identifiers *)
| digit+ as lit { NUM(int_of_string lit) } (* numeric literals *)
| eof { EOF }
{
let () =
let lexbuf = Lexing.from_channel stdin in
iterate (fun () -> token lexbuf)
}