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