(* Lexical analyser for mini-Turtle *) { open Lexing open Parser (* exception signaling a lexical error *) exception Lexing_error of string (* note: consider calling the [Lexing.new_line] function for each new line character '\n' *) let keywords = [ "forward", FORWARD; "penup", PENUP; "pendown", PENDOWN; "turnleft", TURNLEFT; "turnright", TURNRIGHT; "color", COLOR; "black", BLACK; "white", WHITE; "red", RED; "green", GREEN; "blue", BLUE; "repeat", REPEAT; "if", IF; "else", ELSE; "def", DEF; ] let is_keyword = let h = Hashtbl.create 16 in List.iter (fun (s, t) -> Hashtbl.add h s t) keywords; fun s -> try Hashtbl.find h s with Not_found -> VAR s } let blank = [' ' '\t'] let char = ['a'-'z' 'A'-'Z'] let digit = ['0'-'9'] let ident = char (char digit '_')* let integer = digit+ rule token = parse '\n' { new_line lexbuf; token lexbuf } blank+ { token lexbuf } ident as s { is_keyword s } integer as d { INT (int_of_string d) } '(' { LPAR } ')' { RPAR } '{' { LCURLY } '}' { RCURLY } '+' { PLUS } '-' { MINUS } '*' { TIMES } '/' { DIV } ',' { COMMA } "(*" { block_comment lexbuf } "//" { single_comment lexbuf } eof { EOF } _ { assert false (* TODO *) } and single_comment = parse '\n' { Lexing.new_line lexbuf; token lexbuf } eof { EOF } _ { single_comment lexbuf } and block_comment = parse "*)" { token lexbuf } eof { raise (Lexing_error "Unterminated comment") } _ { block_comment lexbuf }
This document was generated using caml2html