0% found this document useful (0 votes)
64 views9 pages

Rapport Java

This document contains code for a Java program that implements a deterministic finite automaton (DFA). It defines classes for DFA states (DFAS), transitions between states (DFAT), and the overall automaton (DFA). The main method constructs a sample DFA with four states - initial, zero, one, and accept - and tests whether input strings are accepted by the automaton.

Uploaded by

Imane Ch'atoui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views9 pages

Rapport Java

This document contains code for a Java program that implements a deterministic finite automaton (DFA). It defines classes for DFA states (DFAS), transitions between states (DFAT), and the overall automaton (DFA). The main method constructs a sample DFA with four states - initial, zero, one, and accept - and tests whether input strings are accepted by the automaton.

Uploaded by

Imane Ch'atoui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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