Voila je débute en Ocaml et j'ai eu envie de coder une application pour tracer les fonctions mathématiques, j'ai donc commencé par coder un analyseur lexical, et étant nouveau j'ai beaucoup de mal a trouver le bogue qui c'est glissé dans mon code j'ai du mal a utiliser ocamlDebug . voila donc mon Code si une ame charitable voulait bien m'aider sa serait gentil.
donc Merci d'avance pour votre aide et bonne journée
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 open Str;; open Printf;; type achaine = {chaine:string; mutable courant:int; taille:int};; type token = TEOS of unit (**lexeme représentant la fin de l'expression*) |TInt of int (**lexeme representant un entier *) |TFloat of float (**lexeme representant un flotant *) |TVar of string (**lexeme représentant une variable *) |TOp_Bin of string (**lexeme représentant un opérateur binaire *) |TOp_Unr of string (**lexeme représentant un opérateur unaire *) |TFonction of string ;; (**lexeme représentant une fonction de base *) (* avance : achaine -> unit*) let avance a = a.courant<-a.courant + 1;; (* avance : achaine -> unit*) let avancen a n = a.courant<-a.courant + n;; (* extraitVar : char -> string *) let extraitVar x = String.make 1 x;; (* regex permetant de tester si le début d'une chaine de caractere contient un * fonction connue de l'analyseur *) let foncRegex = Str.regexp "\\(exp\\|ln\\|log\\|sin\\|cos\\|tan\\|cotan\\|arcsin\\|arccos\\|arctan\\|arccotan\\|ch\\|sh\\|th\\|coth\\|argch\\|argsh\\|argth\\|argcoth\\|sqrt\\)" ;; (* Regex Permetant de tester si le début d'une chaine de caractère contient un * Float *) let floatRegex = Str.regexp "[0-9]+\\.[0-9]+" ;; (* Regex permetant de tester si le début d'une cahine de caractere contient un * entier*) let intRegex = Str.regexp "[0-9]+";; (* Vérifier d'abord les Floats avant les Int*) exception ParseError;; exception FParseError;; (*ilexer achaine->token list*) let rec ilexer s = let fExtraction = (* test succesivement les différentes regex et renvoie le lexeme qui * convient selon la situation en avançant le point courant de la * chaine*) let extraction c = match c with (* Fonction qui a pour vocation d'extraire les opérateur binaires, * Unaires et les variables *) 'x' | 'y' -> avance s; TVar (extraitVar c) |'+' | '-' | '*' | '^' | '/' -> avance s; TOp_Bin (String.make 1 c) |'(' | ')' -> avance s; TOp_Unr (String.make 1 c) | _ -> raise ParseError in if ( Str.string_match foncRegex s.chaine s.courant ) = true then let a = (Str.matched_string (s.chaine)) in avancen s (String.length a) ; TFonction a else if ( Str.string_match floatRegex s.chaine s.courant ) = true then let a = (Str.matched_string (s.chaine)) in avancen s (String.length a); TFloat (float_of_string a) else if ( Str.string_match intRegex s.chaine s.courant ) = true then let a = (Str.matched_string (s.chaine)) in avancen s (String.length a); TInt (int_of_string a) else try let b = String.get s.chaine s.courant in extraction b with ParseError -> failwith "Erreur format" in if s.courant < s.taille then let a = fExtraction in a :: (ilexer s) else [TEOS ()];; (* lexer : string -> token list *) let lexer s = let cl={chaine = s; courant = 0; taille = String.length s } in ilexer cl;; (*printlistlexer : token list -> unit ()*)
Partager