TP-automates/TP1/lexerJava.mll
2023-06-21 19:58:18 +02:00

96 lines
3.3 KiB
OCaml
Executable file

{
open TokenJava
(* open String *)
(* open Str *)
exception LexicalError
}
(* Macro-definitions *)
let minuscule = ['a'-'z']
let javaLetter = minuscule | '_'
let majuscule = ['A'-'Z']
let chiffre = ['0'-'9']
let hexa = chiffre | ['a'-'f'] | ['A'-'F']
let octa = ['0'-'7']
let alphabet = minuscule | majuscule
let alphanum = alphabet | chiffre | '_'
let commentaireBloc = "/*" [^'*']* ('*' [^'/'] [^'*']*)* "*/"
let commentaireLigne = "//" [^'\n']* '\n'
(* Analyseur lexical : expression reguliere { action CaML } *)
rule lexer = parse
(* Espace, tabulation, passage a ligne, etc : consommes par l'analyse lexicale *)
| ['\n' '\t' ' ']+ { lexer lexbuf }
(* Commentaires consommés par l'analyse lexicale *)
| commentaireBloc { lexer lexbuf }
| commentaireLigne { lexer lexbuf }
(* Structures de blocs *)
| "(" { PAROUV }
| ")" { PARFER }
| "[" { CROOUV }
| "]" { CROFER }
| "{" { ACCOUV }
| "}" { ACCFER }
(* Separateurs *)
| "," { VIRG }
| ";" { PTVIRG }
(* Operateurs booleens *)
| "||" { OPOU }
| "&&" { OPET }
| "!" { OPNON }
(* Operateurs comparaisons *)
| "==" { OPEG }
| "!=" { OPNONEG }
| "<=" { OPSUPEG }
| "<" { OPSUP }
| ">=" { OPINFEG }
| ">" { OPINF }
(* Operateurs arithmetiques *)
| "+" { OPPLUS }
| "-" { OPMOINS }
| "*" { OPMULT }
| "/" { OPDIV }
| "%" { OPMOD }
| "." { OPPT }
| "=" { ASSIGN }
| "new" { NOUVEAU }
(* Mots cles : types *)
| "bool" { BOOL }
| "char" { CHAR }
| "float" { FLOAT }
| "int" { INT }
| "String" { STRING }
| "void" { VOID }
(* Mots cles : instructions *)
| "while" { TANTQUE }
| "if" { SI }
| "else" { SINON }
| "return" { RETOUR }
(* Mots cles : constantes *)
| "true" { (BOOLEEN true) }
| "false" { (BOOLEEN false) }
| "null" { VIDE }
(* Nombres entiers : *)
| ( '0' | (['1' - '9'](chiffre | '_')*) ) as texte { (ENTIER (int_of_string texte)) }
| ( '0'('b'|'B') (chiffre | '_')* ) as texte { (BINAIRE (int_of_string texte)) }
| ( '0'('x'|'X') (hexa | '_')* ) as texte { (HEXA (int_of_string texte)) }
| ( '0'* (octa | '_')* ) as texte { (OCTA (int_of_string ("0o" ^ texte))) }
(* Nombres flottants : *)
| ['+''-']? ( chiffre+ | chiffre+ '.' chiffre+ | '.' chiffre+ | chiffre+ '.' ) (['e''E'] ['+''-']? chiffre+ )? as texte { (FLOTTANT (float_of_string texte)) }
(* Caracteres : *)
| "'" _ "'" as texte { CARACTERE texte.[1] }
| "'" '\\' _* "'" as texte { print_endline texte; CARACTERE (char_of_int (int_of_string (String.sub texte 2 ((String.length texte) - 3)))) }
(* Chaines de caracteres : A COMPLETER *)
| '"' _* '"' as texte { CHAINE texte }
(* Identificateurs *)
| majuscule alphanum* as texte { TYPEIDENT texte }
| javaLetter alphanum* as texte { IDENT texte }
| eof { FIN }
| _ { raise LexicalError }
{
}