-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.mll
71 lines (67 loc) · 2.15 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(*
B Interpreter
*)
{
open Parser
exception Eof
exception LexicalError
let verbose1 s = (* (print_string s; print_newline(); s) *) s
let verbose2 s = (* (print_string s; print_newline()) *) ()
let comment_depth = ref 0
let keyword_tbl = Hashtbl.create 31
let _ = List.iter (fun (keyword, tok) -> Hashtbl.add keyword_tbl keyword tok)
[("unit", UNIT);
("true", TRUE);
("false", FALSE);
("not", NOT);
("if", IF);
("then",THEN);
("else",ELSE);
("let", LET);
("in", IN);
("end", END);
("proc", PROC);
("while", WHILE);
("do" , DO);
("read" , READ);
("write", WRITE)
]
}
let blank = [' ' '\n' '\t' '\r']+
let id = ['a'-'z' 'A'-'Z']['a'-'z' 'A'-'Z' '\'' '0'-'9' '_']*
let number = ['0'-'9']+
rule start =
parse blank { start lexbuf }
| "(*" { comment_depth :=1;
comment lexbuf;
start lexbuf }
| number { NUM (int_of_string (verbose1 (Lexing.lexeme lexbuf))) }
| id { let id = verbose1 (Lexing.lexeme lexbuf)
in try Hashtbl.find keyword_tbl id
with _ -> ID id
}
| "+" {verbose2 "+"; PLUS}
| "-" {verbose2 "-";MINUS}
| "*" { verbose2 "*"; STAR}
| "/" { verbose2 "/"; SLASH}
| "=" {verbose2 "="; EQUAL}
| "<" { verbose2 "<"; LB}
| ">" { verbose2 ">"; RB}
| "]" { verbose2 "]"; RBLOCK}
| "[" { verbose2 "["; LBLOCK}
| ":=" {verbose2 ":="; COLONEQ}
| ";" { verbose2 ";"; SEMICOLON}
| "," { verbose2 ","; COMMA}
| "." { verbose2 "."; PERIOD}
| "(" { verbose2 "("; LP}
| ")" { verbose2 ")"; RP}
| "{" { verbose2 "{"; LC}
| "}" { verbose2 "}"; RC}
| eof { verbose2 "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}