annotate lab3/lexer.mll @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 (* lab3/lexer.mll *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2 (* Copyright (c) 2017 J. M. Spivey *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 open Keiko
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 open Parser
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 open Tree
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 open Lexing
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 let make_hash n ps =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 let t = Hashtbl.create n in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 List.iter (fun (k, v) -> Hashtbl.add t k v) ps;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 t
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 (* A little table to recognize keywords *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 let kwtable =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 make_hash 64
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 [ ("begin", BEGIN); ("end", END); ("var", VAR); ("print", PRINT);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 ("if", IF); ("then", THEN); ("else", ELSE); ("while", WHILE);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 ("do", DO); ("proc", PROC); ("return", RETURN); ("newline", NEWLINE);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 ("true", NUMBER 1); ("false", NUMBER 0);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 ("and", MULOP And); ("div", MULOP Div); ("or", ADDOP Or);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 ("not", MONOP Not); ("mod", MULOP Mod) ]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 let lookup s = try Hashtbl.find kwtable s with Not_found -> IDENT s
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 let lineno = ref 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 rule token = parse
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 ['A'-'Z''a'-'z']['A'-'Z''a'-'z''0'-'9''_']* as s
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 { lookup s }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 | ['0'-'9']+ as s { NUMBER (int_of_string s) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 | "=" { RELOP Eq }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 | "+" { ADDOP Plus }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 | "-" { MINUS }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 | "*" { MULOP Times }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 | "<" { RELOP Lt }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 | ">" { RELOP Gt }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 | "<>" { RELOP Neq }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 | "<=" { RELOP Leq }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 | ">=" { RELOP Geq }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 | "(" { LPAR }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 | ")" { RPAR }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 | "," { COMMA }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 | ";" { SEMI }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 | "." { DOT }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 | ":=" { ASSIGN }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 | [' ''\t']+ { token lexbuf }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 | "(*" { comment lexbuf; token lexbuf }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 | '\n' { incr lineno; Source.note_line !lineno lexbuf;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 token lexbuf }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 | _ { BADTOK }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 | eof { EOF }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 and comment = parse
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 "*)" { () }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 | "\n" { incr lineno; Source.note_line !lineno lexbuf;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 comment lexbuf }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 | _ { comment lexbuf }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 | eof { () }