Sie sind auf Seite 1von 5

# /**

2 *
3 * @author Erfan
4 */
5 public class DFA1 {
6
7 //REGULAR EXPRESSION = ab(a+b)*
8 //deklarasi STATE dalam bentuk variabel
9 private final int A=0;
10 private final int B=1;
11 private final int C=2;
12 private final int D=3;
13 private final int E=4;
14 private final int F=5;
15 private final int G=6;
16 private final int H=7;
17
18 //definisi is_final untuk setiap state yang ada
19 private boolean
final_state[]={false,true,false,true,true,true,false,true};
20
21 //Variable penyimpan uji string
22 private String Uji;
23
24 //Variabel kursor State
25 private int state;
26
27 public DFA1(String input_string){
28 Uji=input_string;
29 //menghilangkan spasi pada string input
30 Uji=Uji.trim();
31 //membuat char input menjadi alphabet kecil
32 Uji=Uji.toLowerCase();
33 this.State_Awal();
34 this.Proses_Uji();
35 }
36 private void State_Awal(){
37 state=0;
38 }
39
40 //function untuk menguji state pada diagram DFA
41 private int DFA(int x, char c){
42 switch(x){
43 case A: switch(c){
44 case 'a': return B;
45 case 'b': return C;
46 }
47 case B: switch(c){
48 case 'a': return D;
49 case 'b': return E;
50 }
51 case C: switch(c){
52 case 'a': return C;
53 case 'b': return C;
54 }
55 case D: switch(c){
56 case 'a': return C;
57 case 'b': return E;
58 }
59 case E: switch(c){
60 case 'a': return F;
61 case 'b': return B;
62 }
63 case F:switch(c){
64 case 'a': return G;
65 case 'b': return H;
66 }
67 case G:switch(c){
68 case 'a': return G;
69 case 'b': return B;
70 }
71 case H:switch(c){
72 case 'a':return F;
73 case 'b':return H;
74 }
75 }
76 return 0;
77 }
78
79 private void Proses_Uji(){
80
81 for (int i=0;i < Uji.length();i++){
82 char c=Uji.charAt(i);
83 state=DFA(state,c);
84 }
85
86 }
87 public boolean hasil_akhir(){
88 return final_state[state];
89 }
90 }

## public int[][] Transition = new int[20][20];

public int alphaset, stateset;
public ArrayList < Character > alphabet;
public Set < Integer > finalstates;

## public void builtdfa() throws Exception {

System.out.println("please enter the no. of alphabets in the alphabet set
of DFA");
//System.out.println(alphaset);
System.out.println("please enter the no. of states in the set of DFA");

simultaneosly");

## alphabet = new ArrayList < Character > ();

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

}
//System.out.println(alphabet);

## for (int s = 0; s < stateset; s++) {

System.out.println("enter the row entries of the transition table");
for (int r = 0; r < alphaset; r++) {
Transition[s][r] = int1;
}
}

## finalstates = new HashSet < Integer > ();

System.out.println("please enter the final states of the DFA\n");
System.out.println("when you are done of giving the inputs enter -2 to
stop feeding final states");

## while (int2 != -2) {

}
System.out.println(finalstates);
DfaTest();
}

## public void DfaTest() throws Exception {

int state;
System.out.println("enter the starting state");

## System.out.println("give input to the DFA\n");

System.out.println("end your input with # character\n");
int index = 0;

## while (input.charAt(index) != '#') {

char char1 = input.charAt(index);
int index1 = alphabet.indexOf(char1);
state = Transition[state][index1];
index++;
}
System.out.println("\nfinal state after giving the input is " + state);
if (finalstates.contains(state))
System.out.print("your input string is accepted by the DFA");
else
System.out.println("the input string is rejected by the DFA");
}
public static void main(String args[]) throws Exception {
Dfasimulator raw = new Dfasimulator();
raw.builtdfa();
//System.out.println(raw.alphaset);
}
}

## public class StringDFA{

public static class dfa {
private static final int A = 0;
private static final int B = 1;
private static final int C = 2;
private int state;

## private int gambar(int x, char c){

switch(x){
case A: switch(c){
case '0': return B;
case '1': return A;
}
case B: switch(c){
case '0': return B;
case '1': return C;
}
case C: switch(c){
case '0': return C;
case '1': return C;
}
default: return C;
}
}

state = A;
}

## public void proses(String input){

for (int i=0; i
char c = input.charAt(i);
state = gambar(state, c);
}
}
public boolean akhir(){
return state == C;
}
}

## public static void main (String[] args){

dfa test = new dfa();
Scanner scan = new Scanner(System.in);
System.out.println ("********************************");
System.out.println ("* Program Mesin DFA *");
System.out.println ("* Dengan RE : 1*00*1(0+1)* *");
System.out.println ("* *");
System.out.println ("********************************");
System.out.print ("Input String : ");
String x = scan.nextLine();

while (x!=null){
test.awal();
test.proses(x);
if (test.akhir())
System.out.println ("String "+x+" DITERIMA");
else
System.out.println ("String "+x+" DITOLAK");
System.out.print ("\nInput String : ");
x = scan.nextLine();
}
}
}