diff options
author | Alex AUVOLAT <alex.auvolat@ens.fr> | 2013-11-10 10:11:16 +0100 |
---|---|---|
committer | Alex AUVOLAT <alex.auvolat@ens.fr> | 2013-11-10 10:11:16 +0100 |
commit | d9fab442401005b49b9221b9d897501fef9a4d8d (patch) | |
tree | ff9fa0535f91d8d160c6e7360f256664d1b4169e /src/lexer.mll | |
parent | 8f1093f0e00f9b1df7ce343a879303fd56a95d08 (diff) | |
download | LPC-Projet-d9fab442401005b49b9221b9d897501fef9a4d8d.tar.gz LPC-Projet-d9fab442401005b49b9221b9d897501fef9a4d8d.zip |
Nothing interesting to see yet.
Diffstat (limited to 'src/lexer.mll')
-rw-r--r-- | src/lexer.mll | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/lexer.mll b/src/lexer.mll new file mode 100644 index 0000000..f2f47ef --- /dev/null +++ b/src/lexer.mll @@ -0,0 +1,115 @@ + +(* + Analysateur lexicographiquep pour maxi-C++ +*) + +{ + open Lexing + open Parser + + exception Lexing_error of string + exception End_of_file + + let keywordz_l = [ + "class", CLASS; + "else", ELSE; + "false", FALSE; + "for", FOR; + "if", IF; + "int", INT; + "new", NEW; + "NULL", NULL; + "public", PUBLIC; + "return", RETURN; + "this", THIS; + "true", TRUE; + "virtual", VIRTUAL; + "void", VOID; + "while", WHILE; + ] + + let id_or_kwd = + let h = Hashtbl.create 20 in + List.iter (fun (s, t) -> Hashtbl.add h s t) keywordz_l; + fun s -> + try Hashtbl.find h s with _ -> + if Sset.mem (!type_names) s + then TIDENT s + else IDENT s +} + +let digit = ['0'-'9'] +let alpha = ['a'-'z' 'A'-'Z'] +let ident = ('_' | alpha) ('_' | alpha | digit)* +let octal = ['0'-'7'] +let hexa = ['0'-'9' 'a'-'f' 'A'-'F'] + +rule token = parse + | ['\n' ' ' '\t']+ { token lexbuf } + | ident as id { id_or_kwd id } + | "//" { short_comment lexbuf; token lexbuf } + | "/*" { long_comment lexbuf; token lexbuf } + | "#include <iostream>" { INCLUDE_IOSTREAM } + | "0x" (hexa+ as n) { INTVAL(int_of_string("0x" ^ n)) } + | ['1'-'9'] digit* as n { INTVAL(int_of_string(n)) } + | '0' (octal+ as n) { INTVAL(int_of_string("0o" ^ n)) } + | "0" { INTVAL(0) } + | digit ('_' | alpha | digit)+ + { raise (Lexing_error "Missing separators") } + | "\"" { STRVAL(strval "" lexbuf) } + | "=" { ASSIGN } + | "||" { LOR } + | "&&" { LAND } + | "==" { EQ } + | "!=" { NE } + | "<" { LT } + | "<=" { LE } + | ">" { GT } + | ">=" { GE } + | "+" { PLUS } + | "-" { MINUS } + | "*" { TIMES } + | "/" { DIV } + | "%" { MOD } + | "!" { NOT } + | "++" { INCR } + | "--" { DECR } + | "&" { REF } + | "(" { LPAREN } + | ")" { RPAREN } + | "->" { RARROW } + | "." { DOT } + | ";" { SEMICOLON } + | "::" { DOUBLECOLON } + | "<<" { LFLOW } + | "{" { LBRACE } + | "}" { RBRACE } + | eof { raise End_of_file } + | _ as c + { raise + (Lexing_error + ("illegal character: " ^ String.make 1 c)) } +and strval s = parse + | "\"" { s } + | "\\\\" { strval (s ^ "\\") lexbuf } + | "\\\"" { strval (s ^ "\"") lexbuf } + | "\\n" { strval (s ^ "\n") lexbuf } + | "\\t" { strval (s ^ "\t") lexbuf } + | "\\x" (hexa hexa as x) + { strval (s ^ + (String.make 1 (char_of_int (int_of_string("0x" ^ x))))) + lexbuf } + | "\\" + { raise (Lexing_error "Invalid escape sequence") } + | '\n' { raise (Lexing_error "Invalid character (newline) in string litteral.") } + | _ as c { strval (s ^ (String.make 1 c)) lexbuf } + | eof { raise (Lexing_error "Unfinished string") } +and short_comment = parse + | '\n' {} + | _ { short_comment lexbuf } + | eof {} +and long_comment = parse + | "*/" {} + | _ { long_comment lexbuf } + | eof { raise (Lexing_error "Unclosed comment") } + |