-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathlexer.mll
More file actions
82 lines (77 loc) · 2.26 KB
/
lexer.mll
File metadata and controls
82 lines (77 loc) · 2.26 KB
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
82
{
open Lexing
open Parser
exception Error of string
(** [newline lexbuf] updates the current line number [pos_lnum] of the [lexbuf]
and the number of characters from the beginning of the file to the beginning
of the current line [pos_bol].
*)
let newline 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 }
}
let digit = ['0'-'9']
let integer = digit+
let space = [' ' '\t' '\r']
let letter = ['a'-'z''A'-'Z''_']
let ident = letter (digit | letter)*
rule get_token = parse
| "//" [^ '\n']* '\n'
| '\n' { newline lexbuf; get_token lexbuf }
| space+ { get_token lexbuf }
| "/*" { comment lexbuf }
| '+' { PLUS }
| '-' { MINUS }
| '*' { TIMES }
| "&&" { AND }
| "<" { LT }
| '(' { LPAREN }
| ')' { RPAREN }
| '[' { LBRACKET }
| ']' { RBRACKET }
| '{' { LBRACE }
| '}' { RBRACE }
| '.' { DOT }
| ';' { SEMICOLON }
| '=' { ASSIGN }
| "," { COMMA }
| "true" { BOOL_CONST true }
| "false" { BOOL_CONST false }
| "int" { INTEGER }
| "boolean" { BOOLEAN }
| "!" { NOT }
| "," { COMMA }
| "class" { CLASS }
| "public" { PUBLIC }
| "static" { STATIC }
| "void" { VOID }
| "main" { MAIN }
| "return" { RETURN }
| "String" { STRING }
| "extends" { EXTENDS }
| "new" { NEW }
| "this" { THIS }
| "length" { LENGTH }
| "System.out.println" { SYSO }
| "if" { IF }
| "else" { ELSE }
| "while" { WHILE }
| integer as i
{
try
(** We want two's complement 32 bits integers.
On a 64 bits architecture, Ocaml integers are 63 bits wide. *)
INT_CONST (Int32.of_string i)
with Failure _ ->
raise (Error "Invalid integer constant")
}
| ident as id { IDENT (Location.make (lexeme_start_p lexbuf) (lexeme_end_p lexbuf) id) }
| "//" [^ '\n']* eof
| eof { EOF }
| _ as c { raise (Error ("Illegal character: " ^ String.make 1 c)) }
and comment = parse
| "*/" { get_token lexbuf }
| '\n' { newline lexbuf; comment lexbuf }
| _ { comment lexbuf }
| eof { raise (Error ("Unterminated comment")) }