(* mlexer.mll for Scheme *) (* Stephanie Weirich, Adapted from code by Amr Sabry *) { (* Header *) open EoplParser open Format exception Eof exception LexErr let lineno = ref 1 and start = ref 0 let newline lexbuf = incr lineno; start := (Lexing.lexeme_start lexbuf) let print_error (str:string) : unit = (print_string "Error at "; print_int (!lineno); print_string "."; print_int (!start); print_string ":"; print_string str) } let digit = ['0'-'9'] let letter = ['a'-'z' 'A'-'Z'] let id = '_' | (letter (letter | digit | ['-' '_' '!' '?'])*) let number = ['1'-'9'] (digit*) | ['0'] rule token = parse [' ' '\009' '\012']+ { token lexbuf } (* skip blanks *) | [' ' '\009' '\012']*("\r")?"\n" (* Count the number of newlines *) { newline lexbuf; token lexbuf } | ';'[^'\n']*'\n' (* skip comments *) { newline lexbuf; token lexbuf } | "proc" { LAMBDA } | '(' { LPAREN } | ')' { RPAREN } | "let" { LET } | '+' { PLUS } | '*' { TIMES } | '-' { MINUS } | '=' { EQUAL } | "and" { AND } | "or" { OR } | "not" { NOT } | "if" { IF } | "true" { TRUE } | "false" { FALSE } | "in" { IN } | "then" { THEN } | "else" { ELSE } | "," { COMMA } | "letrec" { LETREC } | "set" { SET } | "add1" { ADD1 } | "sub1" { SUB1 } | "zero?" { ZERO } | "<" { LT } | ">" { GT } | id { ID(Lexing.lexeme lexbuf) } | number { NUMBER(int_of_string (Lexing.lexeme lexbuf)) } | eof { raise Eof } | _ { failwith ("Illegal character '" ^ (Lexing.lexeme lexbuf) ^ "'") } { (* Trailer *) }