import java.io.*; class Defautomate_det { int Etat; int Table[][]; int Final[]; int MaxF; int SupEtat; public String Vocab = "abcdefghijklmnopqrstuvwxyz"; Defautomate_det(int N) { Etat = 0; Table = new int[N][26]; Final = new int[N]; SupEtat = N; MaxF = 0; for(int I = 0; I < N; ++I) for(int J = 0; J < 26; ++J) Table[I][J] = -1; } boolean Ecriture_Transition(int Q,int Symbole,int QR) { if ((Q < 0) || (Q >= SupEtat) || (Symbole < 0) || (Symbole >= 26) || (QR < 0) || (QR >= SupEtat) || (Table[Q][Symbole] >= 0)) return false; Table[Q][Symbole] = QR; return true; } boolean Ecriture_Finale(int F) { if (F >= SupEtat) return false; Final[MaxF++] = F; return true; } boolean Transition(int V) { int c = Vocab.indexOf(V); if ((c < 0) || (c >= 26) || (Etat == -1) || (Table[Etat][c] < 0)) { Etat = -1; return false; } Etat = Table[Etat][c]; return true; } boolean Resultat() { for(int I = 0; I < MaxF; ++I) if (Final[I] == Etat) return true; return(false); } void Depart() { Etat = 0; return; } } class automatedet { public static void main(String args[]) { try { if (args.length != 1) { System.out.println("Pas de parametre d'appel: 0) { try { v = Integer.parseInt(entree.substring(p,f)); } catch (NumberFormatException e) { System.out.println("Definition d'un etat final incorrecte:"); return; } if (!A.Ecriture_Finale(v)) { System.out.println("Definition de l'etat final incorrecte: " + v); return; } p = f + 1; } try { v = Integer.parseInt(entree.substring(p)); } catch (NumberFormatException e) { System.out.println("Definition du dernier etat final incorrecte:"); return; } if (!A.Ecriture_Finale(v)) { System.out.println("Definition de l'etat final incorrecte: " + v); return; } if((entree = d.readLine()) == null) { System.out.println("fichier de definition incomplet"); return; } if (!entree.equals("Fonction de transition:")) { System.out.println("La definition de l'automate doit comprendre:"); System.out.println("Fonction de transition:"); return; } while((entree = d.readLine()) != null) { p = entree.indexOf('(') + 1; f = entree.indexOf(','); try { v = Integer.parseInt(entree.substring(p,f)); } catch (NumberFormatException e) { System.out.println("Definition de l'etat d'une transition incorrecte: " + entree.substring(p,f)); return; } p = f + 1; f = entree.indexOf(')'); String prov = entree.substring(p,f); int c; if ((prov.length() != 1) || ((c = A.Vocab.indexOf(prov)) < 0)) { System.out.println("Definition du caractere d'une transition incorrecte: " + prov); return; } p = entree.indexOf("->") + 2; int r; try { r = Integer.parseInt(entree.substring(p)); } catch (NumberFormatException e) { System.out.println("Definition de l'etat resultant d'une transition incorrecte: " + entree.substring(p)); return; } if (!A.Ecriture_Transition(v,c,r)) { System.out.println("Definition d'une transition incorrecte ou dupliquee: " + entree); return; } } while(true) { System.out.print("Entrez un mot (ligne vide pour arreter): "); try { v = System.in.read(); } catch (IOException e) { System.out.println("Erreur de lecture"); return; } if (v == '\n') break; A.Depart(); Boolean Ok = True; do { if (Ok) Ok = A.Transition(v); try { v = System.in.read(); } catch (IOException e) { System.out.println("Erreur de lecture"); return; } } while(v != '\n'); System.out.println("La chaine appartient au langage: " + (Ok && A.Resultat())); } return; } catch (FileNotFoundException e) { return ; } catch (IOException e) { return ; } catch (NullPointerException e) { return ; } } }