-
Notifications
You must be signed in to change notification settings - Fork 1
/
cish_lex.mll
64 lines (58 loc) · 1.21 KB
/
cish_lex.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
(* header section *)
{
open Cish_parse
open Lexing
let incr_lineno lexbuf =
let pos = lexbuf.lex_curr_p in
lexbuf.lex_curr_p <- { pos with
pos_lnum = pos.pos_lnum + 1;
pos_bol = pos.pos_cnum;
}
}
(* definition section *)
let cr='\013'
let nl='\010'
let eol=(cr nl|nl|cr)
let ws=('\012'|'\t'|' ')*
let digit=['0'-'9']
let id = ['A'-'Z''a'-'z''_']['a'-'z''A'-'Z''0'-'9''_']*
(* rules section *)
rule lexer = parse
| eol { incr_lineno lexbuf; lexer lexbuf }
| ws+ { lexer lexbuf }
| ';' { SEMI }
| '{' { LBRACE }
| '}' { RBRACE }
| '(' { LPAREN }
| ')' { RPAREN }
| "==" { EQEQ }
| "=" { EQ }
| "!=" { NEQ }
| "<=" { LTE }
| ">=" { GTE }
| '<' { LT }
| '>' { GT }
| '+' { PLUS }
| '-' { MINUS }
| '*' { TIMES }
| '/' { DIV }
| "&&" { AND }
| "||" { OR }
| '!' { BANG }
| ',' { COMMA }
| "return" { RETURN }
| "if" { IF }
| "else" { ELSE }
| "while" { WHILE }
| "for" { FOR }
| "let" { LET }
| "malloc" { MALLOC }
| id { ID(Lexing.lexeme lexbuf) }
| digit+ { INT(int_of_string(Lexing.lexeme lexbuf)) }
| "/*" { comment lexbuf } (* comment start *)
| eof { EOF }
and comment = parse
| eol { incr_lineno lexbuf; comment lexbuf }
| "*/" { lexer lexbuf } (* comment end *)
| _ { comment lexbuf }
(* trailer section *)