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