-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.mll
More file actions
60 lines (53 loc) · 1.95 KB
/
lexer.mll
File metadata and controls
60 lines (53 loc) · 1.95 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
{
open Lexing
open Parser
exception Error of char
}
let alpha = ['a'-'z' 'A'-'Z']
let num = ['0'-'9']
let identifier = alpha (alpha | num | '-' | '_')*
rule token = parse
| eof { Lend }
| [ ' ' '\t' ] { token lexbuf }
| '\n' { Lexing.new_line lexbuf; token lexbuf }
| '#' { comment lexbuf }
| "return" { Lreturn }
| '-' { Lsub }
| num+ as n { Lint (int_of_string n )}
| '*' { Lmul }
| ';' { Lsc }
| '=' { Lassign}
| '+' { Ladd }
| '/' { Ldiv }
| "void" {Lvoid}
| '(' {Lparo}
| ')' {Lparf}
| '{' {Lacoo}
| '}' {Lacof}
| "==" {Legal}
| "int" {Ldecl (Int_t(Int_t,false))}
| "bool" {Ldecl (Bool_t(Bool_t,false))}
| "True" {Lbool (true)}
| "False" {Lbool (false)}
| "if" {Lcond}
| "else" {Lelse}
| ">" {Lsup}
| "<" {Linf}
| "for" {Lfor}
| "while" {Lwhile}
| "prints" {Lprints}
| "geti" {Lgeti}
| "puti" {Lputi}
| '"' { Lstring ( String.of_seq (List.to_seq (string_ lexbuf)) ) }
| identifier+ as var { Lvar (var) }
| _ as c {raise (Error c) }
and string_ = parse
| '"' { [] } (* on a fini de lire la chaine, on renvoie la liste vide, dans tous les autres cas on construit une liste des caractères dans la chaine : celui lu suivi par la suite de la chaîne *)
| "\\n" { '\n' :: (string_ lexbuf) } (* on rencontre un '\' suivi d'un 'n' donc on met un retour à la ligne dans la chaîne *)
| "\\t" { '\t' :: (string_ lexbuf) } (* idem pour "\t" *)
| "\\\\" { '\\' :: (string_ lexbuf) } (* idem pour "\\" *)
| _ as c { c :: (string_ lexbuf) } (* pour tous les autres caractères pas de traitement particulier ils se valent eux-mêmes *)
and comment = parse
| eof { Lend }
| '\n' { Lexing.new_line lexbuf; token lexbuf }
| _ { comment lexbuf }