//You have to enter the state table for this program.....
import java.io.*;
class genfa
{
public static void main(String args[])
throws IOException
{
String str;
char ch;
int len,temp=0,no,st1,cst,fst=0,temp1=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a String:- ");
str=br.readLine();
len= str.length();
System.out.print("Enter The number of States:-");
st1 = Integer.parseInt(br.readLine());
System.out.print("Enter The number of final State:-");
fst = Integer.parseInt(br.readLine());
//System.out.print("Enter The number of Input Symbols:-");
//st = Integer.parseInt(br.readLine());
int st[][] = new int [st1][2];
for(int x=0 ; x < st1 ; x++)
{
System.out.print("Enter the state which we reach when going through 'a' from state " + (x+1) + ":-");
temp = Integer.parseInt(br.readLine());
st[x][0]=temp;
System.out.print("Enter the state which we reach when going through 'b' from state " + (x+1) + ":-");
temp = Integer.parseInt(br.readLine());
st[x][1]=temp;
}
cst=1;
for(int x=0 ; x < len ; x++)
{
temp=cst;
ch = str.charAt(x);
if (ch=='a')
{
if (st[cst-1][0]!=0)
{
cst=st[cst-1][0];
}
}
else if(ch=='b')
{
if (st[cst-1][1]!=0)
{
cst=st[cst-1][1];
}
}
System.out.println("Moving from state S" + temp + " to S" + cst );
}
if (cst==fst)
{
System.out.println("The String is acceptable");
}
else
{
System.out.println("The String is not acceptable");
}
}
}
Tuesday, March 24, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment