-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.mll
81 lines (78 loc) · 1.79 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
72
73
74
75
76
77
78
79
80
81
{
open Parser
exception Eof
exception LexicalError
let comment_depth = ref 0
let keyword_table = Hashtbl.create 31
let _ = List.iter (
fun (keyword, token) -> Hashtbl.add keyword_table keyword token
) [ ("true", TRUE)
; ("TRUE", TRUE)
; ("false", FALSE)
; ("FALSE", FALSE)
; ("int", INT)
; ("not", NOT)
; ("NOT", NOT)
; ("if", IF)
; ("IF", IF)
; ("then", THEN)
; ("THEN", THEN)
; ("else", ELSE)
; ("ELSE", ELSE)
; ("return", RETURN)
; ("and", AND_S)
; ("for", FOR)
; ("FOR", FOR)
; ("exist", EXIST)
; ("EXIST", EXIST)
; ("forall", FORALL)
; ("FORALL", FORALL)
; ("PAR", PAR)
; ("par", PAR)
; ("T", TOTAL)
; ("NONE", NONE)
; ("None", NONE)
]
}
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_table id with _ -> ID id
}
| "@" { AT }
| "+" { PLUS }
| "-" {MINUS}
| "/" { SLASH }
| "|" { BAR }
| "(" { LPAREN }
| ")" { RPAREN }
| "{" { LCURLY }
| "}" { RCURLY }
| "[" { LBLOCK }
| "]" { RBLOCK }
| "->" { IMPLY }
| "==" {EQEQ}
| "!=" {NOTEQ}
| "<" { LT }
| "<=" { LE }
| ">" { GT }
| ">=" { GE }
| "=" { EQUAL }
| ":=" { COLEQ }
| ":" { COLON }
| ";" { SEMI }
| "," { COMMA }
| "." { DOT }
| 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 }