This repository has been archived by the owner on Dec 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexer.mll
executable file
·56 lines (53 loc) · 1.58 KB
/
lexer.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
{
open Parser
exception Eof
exception LexicalError
let comment_depth = ref 0
let keyword_tbl = Hashtbl.create 31
let _ = List.iter (fun (keyword, tok) -> Hashtbl.add keyword_tbl keyword tok)
[
("iszero", ISZERO);
("if", IF);
("then",THEN);
("else",ELSE);
("let",LET);
("in",IN);
("letrec",LETREC);
("read",READ);
("proc",PROC);
("true", TRUE);
("false", FALSE);
]
}
let blank = [' ' '\n' '\t' '\r']+
let id = ['a'-'z' 'A'-'Z']['a'-'z' 'A'-'Z' '0'-'9' '_']*
let digit = ['0'-'9']+
rule start =
parse blank { start lexbuf }
| "/*" { comment_depth :=1; comment lexbuf; start lexbuf }
| digit { NUM (int_of_string (Lexing.lexeme lexbuf)) }
| id { let id = Lexing.lexeme lexbuf
in try Hashtbl.find keyword_tbl id
with _ -> ID id
}
| "," { COMMA }
| ";" { SEMICOLON }
| "+" { PLUS }
| "-" { MINUS }
| "*" { STAR }
| "/" { SLASH }
| "=" { EQUAL }
| "<=" { LE }
| ">=" { GE }
| "<" { LT }
| ">" { GT }
| "(" { LPAREN }
| ")" { RPAREN }
| eof { EOF}
| _ { raise LexicalError }
and comment = parse
"(*" {comment_depth := !comment_depth+1; comment lexbuf}
| "*)" {comment_depth := !comment_depth-1;
if !comment_depth > 0 then comment lexbuf }
| eof {raise Eof}
| _ {comment lexbuf}