annotate lab1/lexer.mll @ 1:b5139af1a420 tip basis

Fixed permissions on compile scripts
author Mike Spivey <mike@cs.ox.ac.uk>
date Fri, 13 Oct 2017 17:27:58 +0100
parents bfdcc3820b32
children
rev   line source
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 (* lab1/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 Lexing
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 open Tree
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 open Keiko
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 open Parser
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 (* |lineno| -- line number for use in error messages *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 let lineno = ref 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 (* |make_hash| -- create hash table from list of pairs *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 let make_hash n ps =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 let t = Hashtbl.create n in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 List.iter (fun (k, v) -> Hashtbl.add t k v) ps;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 t
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 (* |kwtable| -- a little table to recognize keywords *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 let kwtable =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 make_hash 64
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 [ ("begin", BEGIN); ("do", DO); ("if", IF ); ("else", ELSE);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 ("end", END); ("then", THEN); ("while", WHILE); ("print", PRINT);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 ("newline", NEWLINE); ("and", MULOP And); ("div", MULOP Div);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 ("or", ADDOP Or); ("not", MONOP Not); ("mod", MULOP Mod);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 ("true", NUMBER 1); ("false", NUMBER 0) ]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 (* |idtable| -- table of all identifiers seen so far *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 let idtable = Hashtbl.create 64
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 (* |lookup| -- convert string to keyword or identifier *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 let lookup s =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 try Hashtbl.find kwtable s with
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 Not_found ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 Hashtbl.replace idtable s ();
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 IDENT s
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 (* |get_vars| -- get list of identifiers in the program *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 let get_vars () =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 Hashtbl.fold (fun k () ks -> k::ks) idtable []
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 rule token =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 parse
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 ['A'-'Z''a'-'z']['A'-'Z''a'-'z''0'-'9''_']* as s
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 { lookup s }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 | ['0'-'9']+ as s { NUMBER (int_of_string s) }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 | ";" { SEMI }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 | "." { DOT }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 | ":" { COLON }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 | "(" { LPAR }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 | ")" { RPAR }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 | "," { COMMA }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 | "=" { RELOP Eq }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 | "+" { ADDOP Plus }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 | "-" { MINUS }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 | "*" { MULOP Times }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 | "<" { RELOP Lt }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 | ">" { RELOP Gt }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 | "<>" { RELOP Neq }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 | "<=" { RELOP Leq }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 | ">=" { RELOP Geq }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 | ":=" { ASSIGN }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 | [' ''\t']+ { token lexbuf }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 | "(*" { comment lexbuf; token lexbuf }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 | "\n" { incr lineno; Source.note_line !lineno lexbuf;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 token lexbuf }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 | _ { BADTOK }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 | eof { EOF }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 and comment =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 parse
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 "*)" { () }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 | "\n" { incr lineno; Source.note_line !lineno lexbuf;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 comment lexbuf }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76 | _ { comment lexbuf }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 | eof { () }