Finite State Machine (String abab) > Java Program

Theoretical Computer Science

Finite State Machine (String abab) > Java Program


import java.io.*;
class FSMS1
{


public static void main(String args[])throws IOException
{
int n=0,i,f=0;
DataInputStream in=new DataInputStream(System.in);
String fsm;
System.out.println("Enter fsm input");
fsm=in.readLine();
System.out.print("fsm table  ");
System.out.println("\ta\tb");
System.out.println("q0\tq1\tq0");
System.out.println("q1\tq1\tq2");
System.out.println("q2\tq3\tq0");
System.out.println("q3\tq1\tq4");
System.out.println("q4\tq4\tq4");
System.out.println();
System.out.print("State transition diagram of fsm");
char arr[] = fsm.toCharArray();
System.out.print("-->q0");
for(i=0;i<fsm.length();i++)
{
if(n>=4)
{
if(arr[i]=='a')
{
System.out.print("-->q4");
f=1;
}}
if(n>=4)
{
if(arr[i]=='b')
{
System.out.print("-->q4");
f=1;
}}
if(n==3)
{
if(arr[i]=='a')
{
System.out.print("-->q1");
n=1;
}}
if(n==3)
{
if(arr[i]=='b')
{
System.out.print("-->q4");
n=4;
f=1;
}}
if(n==2)
{
if(arr[i]=='a')
{
System.out.print("-->q3");
n=3;
}}
if(n==2)
{
if(arr[i]=='b')
{
System.out.print("-->q0");
n=0;
}}
if(n==1)
{
if(arr[i]=='a')
{
System.out.print("-->q1");
n=1;
}}
if(n==1)
{
if(arr[i]=='b')
{
System.out.print("-->q2");
n=2;
}}
if(n==0)
{
if(arr[i]=='a')
{
System.out.print("-->q1");
n=1;
}}
if(n==0)
{
if(arr[i]=='b')
{
System.out.print("-->q0");
}}}
System.out.println();
if(f==1)
{
System.out.print("String Contains  abab  ");
}
else
{
System.out.print("String Does Not Contain   abab  ");
}}}

/*OUTPUT of fsm:

Enter fsm input:  abbbbabab

transition table for fsm

            a       b
q0      q1      q0
q1      q1      q2
q2      q3      q0
q3      q1      q4
q4      q4      q4

transition diagram of fsm

-->q0-->q1-->q2-->q0-->q0-->q0-->q0-->q1-->q2-->q3-->q4

String Contains  abab
*/

Comments

Popular posts from this blog

Intermediate Code Generation > C Program