(* 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 s in (* la casse n'est pas significative *)
      try List.assoc s kwd_tbl with _ -> IDENT s

  let newline lexbuf =
    let pos = lexbuf.lex_curr_p in
    lexbuf.lex_curr_p <-
      { pos with pos_lnum = pos.pos_lnum + 1; pos_bol = pos.pos_cnum }

}

let letter = ['a'-'z' 'A'-'Z']
let digit = ['0'-'9']
let ident = letter (letter | digit | '_')*
let integer = ['0'-'9']+
let space = [' ' '\t']

rule token = parse
  | "//" [^ '\n']* '\n'
  | '\n'    { newline 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 }
  | _       { comment lexbuf }
  | eof     { raise (Lexing_error ("unterminated comment")) }



This document was generated using caml2html