Your Ad Here

Tuesday, March 31, 2009

Generalized FA

import java.io.*;

class fa
{
public static void main(String args[])
throws IOException
{
String str;
char ch,ch1;
char ipsy[] = new char[5];
int len,temp=0,no,st1,cst,fst=0,temp1=0,isy=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
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:-");
isy = Integer.parseInt(br.readLine());
//System.out.println(isy);
int z=0;
do
{
System.out.println("Enter the input Symbol " + (z+1) +" : ");
ch1 = (char) br.read();
ch = (char) br.read();
ch = (char) br.read();
z++;
}while(z!=(isy));
System.out.print("Enter a String:- ");
str=br.readLine();
len= str.length();
int st[][] = new int [st1][2];
for(int x = 0 ; x < st1 ; x++)
{
for(int y = 0 ; y < isy ; y++)
{
System.out.print("Enter the state which we reach when going through " + ipsy[y] + " from state " + (x+1) + ":-");
temp = Integer.parseInt(br.readLine());
st[x][y]=temp;
}
}
cst=1;
for(int x = 0 ; x < len ; x++)
{
temp=cst;
ch = str.charAt(x);
for(int y = 0 ; y < isy ; y++)
{
if (ch==ipsy[y])
{
if (st[cst-1][y]!=0)
{
cst=st[cst-1][y];
}
}
}
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");
}
}
}1