You are on page 1of 9

COMPILATION : MINI-

PROJET
By: imane chatoui
Chaymae azizi

prof : Khalid Elfahssi

Le programme en java

package automate;
///BY IMANE CHATOUI , CHAYMAE AZIZI
import java.util.Scanner;
import static java.lang.System.exit;

class DFAT{
DFAS origin_state;
DFAS destination_state;
char trigger_value;

public DFAT(char trigger_value, DFAS origin_state,DFAS


destination_state) {
this.destination_state=destination_state;
this.origin_state=origin_state;
this.trigger_value=trigger_value;
}
}

class DFAS {
boolean accept_state;
DFAT[] transitions;

public DFAS(boolean accept_state) {


this.accept_state=accept_state;
this.transitions= new DFAT[0];

public void setTransition(String str, DFAS des) {

DFAT[] tmp = new DFAT[transitions.length + str.length()];


System.arraycopy(transitions, 0, tmp, 0, transitions.length);

for (int i = 0; i < str.length(); i++) {


tmp[transitions.length + i] = new DFAT(str.charAt(i), this,
des);
}

this.transitions = tmp;
}
}

class DFA {
DFAS[] states;
DFAS current_state;
DFAS initial_state;
public DFA(DFAS initial_state) {
this.initial_state= initial_state;
this.current_state= initial_state;
this.states= new DFAS[0];
}

public void set_state_to_automata(DFAS state) {


DFAS[] tmp = new DFAS[states.length + 1];
System.arraycopy(states, 0, tmp, 0, states.length);

tmp[states.length] = state; // Add new state to the final of


the array

this.states = tmp;

public void init_automata() {

if(this.initial_state == null) {
System.out.println("Error: Initial state of automata not set");
exit(0);
}

this.current_state = this.initial_state;

public boolean update_automata(char ch) {


int j;
DFAS state = this.current_state;

for(j=0;j<state.transitions.length; j++) {
if(state.transitions[j].trigger_value == ch) {
this.current_state = state.transitions[j].destination_state;
return true;
}
}
return false;
}

public boolean belongs_to_language(String string) {


if(string.length() == 0) {
System.out.println("empty strung send to analyse in belongs
to langage");
return false;
}

init_automata();

for(int i = 0; i < string.length(); i++) {


if(update_automata(string.charAt(i)) == false) {
return false;
}
}

return this.current_state.accept_state; /// nous retern true

public class testeglobale {


public static void main(String[] args) {

DFAS initial_state = new DFAS(false);


DFAS zero_state = new DFAS(false);
DFAS one_state = new DFAS(false);
DFAS accept_state = new DFAS(true);

initial_state.setTransition("a-", initial_state);
initial_state.setTransition("0", zero_state);
initial_state.setTransition("1", one_state);
zero_state.setTransition("+", accept_state);
zero_state.setTransition("1", one_state);
one_state.setTransition("0", zero_state);
one_state.setTransition("%", accept_state);

DFA automata = new DFA(initial_state);

automata.set_state_to_automata(accept_state);
automata.set_state_to_automata(zero_state);
automata.set_state_to_automata(one_state);
automata.set_state_to_automata(initial_state);

Scanner sc=new Scanner(System.in);


System.out.println("entrer une string");
String s=sc.nextLine();

if(automata.belongs_to_language(s))
{
System.out.println("Le mot est accepte dans ce langage
");
}
else
{
System.out.println("Le mot n'est pas accepte dans ce
langage");
}

}
Resultats de teste :

You might also like