-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.mll
69 lines (57 loc) · 1.73 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
(* ***************************************************************************)
(* ********************* LEXER **********************)
(* ***************************************************************************)
{
open Lexing
open Parser
open Printf
exception Lexing_error of string
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 letter = ['a' - 'z' 'A' - 'Z']
let digit = ['0' - '9']
let integer = ['1'-'9'] digit+
let ident = [ 'a' - 'z' 'A' - 'Z' '_'] ['a' - 'z' 'A' - 'Z' '0' - '9' '_']*
let space = [' ' '\t']
let digit_octal = ['0'-'7']
let digit_hexa = ['0'-'9' 'a'-'f' 'A'-'F']
let character = ([' ' - '~'] # ['\\' '\'' '\"'])
let coeff = (((['0'-'9']+ | ['0'-'9']+ '.' ['0'-'9']*) | (['0'-'9']* '.' ['0'-'9']+)) ((['e' 'E'] ['+' '-'] ['0'-'9']+) | (['e' 'E'] ['0'-'9']+)))
| ((['0'-'9']+ '.' ['0'-'9']*) | (['0'-'9']* '.' ['0'-'9']+))
| (['1'-'9']['0'-'9']*)
rule token = parse
| '\n' {newline lexbuf;token lexbuf}
| "PARAMS" {PARAMS}
| "INIT" {INIT}
| "EQN" {EQN}
| "UNSCALABLE" {UNSCALABLE}
| "square" {SQRT}
| space+ {token lexbuf}
| '#'| "//" {comment lexbuf}
| integer as c {CONST(c)}
| digit as c {CONST(String.make 1 c)}
| coeff as c {CONST(c)}
| ident as name {ID(name)}
| '-' {MINUS}
| '^' {POW}
| '/' {DIV}
| '*' {TIMES}
| '(' {LPAR}
| ')' {RPAR}
| '+' {PLUS}
| '=' {EQUAL}
| "<->" {REV}
| "->" {IREV}
| "<=>" {NMAREV}
| "=>" {NMAIREV}
| ';' {SEMI}
| eof {EOF}
| _ as c {raise (Lexing_error ("illegal character: " ^
(String.make 1 c )^ "\n"))}
and comment = parse
|'\n' {newline lexbuf; token lexbuf}
| eof {EOF}
| _ {comment lexbuf}