import java.util.Stack; class Pile { private Stack stack; public Pile() { this.stack = new Stack<>(); } public void empiler(char c) { stack.push(c); } public char depiler() { return stack.pop(); } public char sommet() { return stack.peek(); } public boolean estVide() { return stack.isEmpty(); } } public class AutomateAPile { private static final String ETAT_INITIAL = "q0"; private static final String ETAT_FINAL = "qF"; private static final char SYMBOLE_A = 'a'; private static final char SYMBOLE_B = 'b'; private static final char SYMBOLE_C = 'c'; private static String etatCourant; private static Pile pile; public static void main(String[] args) { pile = new Pile(); etatCourant = ETAT_INITIAL; // Exemple d'utilisation du programme principal String mot = "aabbbccc"; boolean motReconnu = reconnaîtreMot(mot); if (motReconnu) { System.out.println("Le mot \"" + mot + "\" appartient au langage L."); } else { System.out.println("Le mot \"" + mot + "\" n'appartient pas au langage L."); } } private static boolean reconnaîtreMot(String mot) { int i = 0; while (i < mot.length()) { char symbole = mot.charAt(i); if (!traiterSymbole(symbole)) { return false; } i++; } return etatCourant.equals(ETAT_FINAL); } private static boolean traiterSymbole(char symbole) { switch (symbole) { case SYMBOLE_A: // Traitement pour le symbole 'a' if (etatCourant.equals("q0")) { etatCourant = "q1"; } else if (etatCourant.equals("q1")) { etatCourant = "q1"; } else if (etatCourant.equals("q2")) { etatCourant = "q2"; } else { return false; } break; case SYMBOLE_B: // Traitement pour le symbole 'b' if (etatCourant.equals("q1")) { etatCourant = "q2"; } else if (etatCourant.equals("q2")) { etatCourant = "q2"; } else { return false; } break; case SYMBOLE_C: // Traitement pour le symbole 'c' if (etatCourant.equals("q2") && !pile.estVide()) { pile.depiler(); etatCourant = "q2"; } else { return false; } break; default: return false; } return true; } }