Commit c3ba83ee authored by Nicaudie Charlotte's avatar Nicaudie Charlotte

Added base content for lexer

parent 159315dd
(* Analyseur lexical pour mini-Turtle *)
{
open Lexing
open Parser
exception Lexing_error of string
(*
Table of keywords (only identifying keywords)
*)
let keyword_table =
["init", INIT;
"iter", ITER;
"translation", TRANSLATION;
"rotation", ROTATION;
"or", OR;
"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) keyword_table;
fun s ->
let s = String.lowercase s in (* not sensitive to casing *)
try List.assoc s keyword_table 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 float = ['0'-'9']+'.'['0'-'9']+
let space = [' ' '\t']
rule get_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 }
| ';' { SEMICOLON }
| "(*" { comment lexbuf }
| integer as s { INT (int_of_string s) }
| float as f { FLOAT (float_of_string s) }
| "#" [^ '\n']* eof
| eof { EOF }
| _ as c { raise (Lexing_error ("illegal character: " ^ String.make 1 c)) }
(* Note : comments cannot be interlocked in Turtle *)
and comment = parse
| "*)" { token lexbuf }
| _ { comment lexbuf }
| eof { raise (Lexing_error ("unterminated comment")) }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment