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