annotate lab2/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 (* lab2/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 lineno = ref 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 let make_hash n ps =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 let t = Hashtbl.create n in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 List.iter (fun (k, v) -> Hashtbl.add t k v) ps;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 t
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 (* A little table to recognize keywords *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 let kwtable =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 make_hash 64
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 [ ("begin", BEGIN); ("do", DO); ("if", IF ); ("else", ELSE);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 ("end", END); ("then", THEN);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 ("var", VAR); ("while", WHILE); ("print", PRINT); ("newline", NEWLINE);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 ("integer", INTEGER); ("boolean", BOOLEAN); ("array", ARRAY);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 ("of", OF); ("true", BOOLCONST 1); ("false", BOOLCONST 0);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 ("and", MULOP And); ("div", MULOP Div); ("or", ADDOP Or);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 ("not", MONOP Not); ("mod", MULOP Mod) ]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 let lookup s =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 try Hashtbl.find kwtable s with Not_found -> IDENT s
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 rule token =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 parse
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 ['A'-'Z''a'-'z']['A'-'Z''a'-'z''0'-'9''_']* as s
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 { lookup s }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 | ['0'-'9']+ as s { NUMBER (int_of_string s) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 | ";" { SEMI }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 | "." { DOT }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 | ":" { COLON }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 | "(" { LPAR }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 | ")" { RPAR }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 | "[" { SUB }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 | "]" { BUS }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 | "," { COMMA }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 | "=" { EQUAL }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 | "+" { ADDOP Plus }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 | "-" { MINUS }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 | "*" { MULOP Times }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 | "<" { RELOP Lt }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 | ">" { RELOP Gt }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 | "<>" { RELOP Neq }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 | "<=" { RELOP Leq }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 | ">=" { RELOP Geq }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 | ":=" { ASSIGN }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 | [' ''\t']+ { token lexbuf }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 | "(*" { comment lexbuf; token lexbuf }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 | "\n" { incr lineno; Source.note_line !lineno lexbuf;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 token lexbuf }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 | _ { BADTOK }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 | eof { EOF }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 and comment = parse
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 "*)" { () }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 | "\n" { incr lineno; Source.note_line !lineno lexbuf;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 comment lexbuf }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 | _ { comment lexbuf }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 | eof { () }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68