-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlexer.mll
More file actions
46 lines (39 loc) · 1.02 KB
/
lexer.mll
File metadata and controls
46 lines (39 loc) · 1.02 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
{
open Parser
exception Lexing_error of string
let kwds = [
"while", WHILE;
"if", IF;
"else", ELSE;
"print", PRINT
]
let id_or_kwd =
let h = Hashtbl.create 4 in
List.iter (fun (s, t) -> Hashtbl.add h s t) kwds;
fun x -> try Hashtbl.find h x with Not_found -> IDENT x
}
let digit = ['0' - '9']
let number = digit+
let char = ['a' - 'z']
let ident = char+ (char | '_')*
let space = ' ' | '\t'
let newline = '\n'
rule token = parse
| newline { Lexing.new_line lexbuf; token lexbuf }
| space+ { token lexbuf }
| ident as s { id_or_kwd s }
| number as n { NUM (int_of_string n) }
| '+' { BADD }
| '-' { BSUB }
| '*' { BMUL }
| '/' { BDIV }
| '%' { BMOD }
| '{' { LCURLY }
| '}' { RCURLY }
| '(' { LPAR }
| ')' { RPAR }
| ';' { SEMICOLON }
| ":=" { ASSIGN }
| eof { EOF }
| _ as c { raise (Lexing_error
("Ilegal char: " ^ String.make 1 c)) }