-
Notifications
You must be signed in to change notification settings - Fork 1
/
lexer.mll
executable file
·42 lines (38 loc) · 977 Bytes
/
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
{
open Parser
open Printf
exception Eof
exception Err
let incline lexbuf =
let pos = lexbuf.Lexing.lex_curr_p in
lexbuf.Lexing.lex_curr_p <- { pos with
Lexing.pos_lnum = pos.Lexing.pos_lnum + 1;
Lexing.pos_bol = pos.Lexing.pos_cnum;
}
}
let digit = ['0'-'9']
let id = ['a'-'z'] ['a'-'z' '0'-'9']*
let ws = [' ' '\t']
rule token = parse
| ws { token lexbuf }
| '\n' { incline lexbuf; token lexbuf }
| "+" { PLUS }
| "/" { DIVIDE }
| "-" { MINUS }
| "*" { MULTIPLY }
| "(" { LPAREN }
| ")" { RPAREN }
| "." { DOT }
| "=" { EQUALS }
| "lambda" { LAMBDA }
| "let" { LET }
| "in" { IN }
| id as v { VAR(v) }
| digit+ as n { INT(int_of_string n) }
| eof { EOF }
| _ as c {
let pos = lexbuf.Lexing.lex_curr_p in
printf "Error at line %d\n" pos.Lexing.pos_lnum;
printf "Unrecognized character: [%c]\n" c;
exit 1
}