(* Analyseur lexical pour mini-Turtle *) { open Lexing open Parser exception Lexing_error of string (* tables des mots-clés *) let kwd_tbl = ["if", IF; "else", ELSE; "def", DEF; "repeat", REPEAT; "penup", PENUP; "pendown", PENDOWN; "forward", FORWARD; "turnleft", TURNLEFT; "turnright", TURNRIGHT; "color", COLOR; "black", BLACK; "white", WHITE; "red", RED; "green", GREEN; "blue", BLUE; ] let id_or_kwd = let h = Hashtbl.create 17 in List.iter (fun (s,t) -> Hashtbl.add h s t) kwd_tbl; fun s -> let s = String.lowercase_ascii s in (* la casse n'est pas significative *) try Hashtbl.find h s with _ -> IDENT s } let letter = ['a'-'z' 'A'-'Z'] let digit = ['0'-'9'] let ident = letter (letter digit '_')* let integer = ['0'-'9']+ let space = [' ' '\t' '\r'] rule token = parse "//" [^ '\n']* '\n' '\n' { new_line lexbuf; token lexbuf } space+ { token lexbuf } ident as id { id_or_kwd id } '+' { PLUS } '-' { MINUS } '*' { TIMES } '/' { DIV } '(' { LPAREN } ')' { RPAREN } '{' { BEGIN } '}' { END } ',' { COMMA } "(*" { comment lexbuf } integer as s { CST (int_of_string s) } "//" [^ '\n']* eof eof { EOF } _ as c { raise (Lexing_error ("illegal character: " ^ String.make 1 c)) } (* note : les commentaires ne sont pas imbriqués en Turtle *) and comment = parse "*)" { token lexbuf } '\n' { new_line lexbuf; comment lexbuf } _ { comment lexbuf } eof { raise (Lexing_error ("unterminated comment")) }
This document was generated using caml2html