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