Write Java code to implement a FSM machine that recognizes the language for the alphabet {a,b,c} consisting of all strings that contain two consecutive c's and end with b.
Your FSM program should include the following three static methods (Java) or functions (C):
a. int nextState(int state, char symbol)
A state-transition function that returns the next state based on the
current state and an input symbol. This function should also return
-1 when an invalid input character is detected.
State transition behavior can be stored in a 2-dimensional array
where the first dimension represents the current state and the
second dimension represents the next input character. The entries
for the array are the resulting “next state.”
For example, a FSM that has 4 states and recognizes 3 valid input
characters would be fully characterized by a 4 X 3 state transition
array.
b. boolean accept(int state)
A output function that returns true if the input state is an acceptance
state and false otherwise.
Acceptance states can be stored in a Boolean one dimensional array
where the single dimension represents the current state. The entries
for the array are either true or false, depending on whether the
corresponding state is an acceptance state.
c. boolean validString(String word)
A processing function that returns true if the input string is in the
language of the FSM. A string is in the language if it moves the
system from the start state to an acceptance state.
This function should read each of the characters in the input string
in order, and invoke the nextState() method on each iteration.
After every character in the input string has been checked, the
accept(int state) method can be called to determine if the
final resulting state is an acceptance state.
Turn in source code for your main program and functions, plus sample output for the following input strings:
a. aaccbbabc
b. cacbcabbc
c. bcbcccbbb
TestFSM.java
// Test Finite State Machine Class
public class TestFSM
{
public static void main(String[] args)
{
/*
// L1
String A = "ab";
int[][] ST = {{1,4},
{0,2},
{3,5},
{2,5},
{2,5},
{5,5}};
int[] AS = {0,0,1,0,0,0};
*/
/*
// L2
String A = "01";
int[][] ST = {{1,6},
{2,5},
{3,4},
{8,8},
{8,8},
{3,8},
{7,8},
{3,8},
{8,8}};
int[] AS =
{0,1,1,1,1,0,0,0,0};
*/
/*
// L3
String A = "xyz";
int[][] ST = {{1,0,4},
{1,2,0},
{3,0,4},
{3,3,3},
{0,5,4},
{1,0,6},
{6,6,6}};
int[] AS = {0,0,0,1,0,0,1};
*/
// L4
String A = "pqr";
int[][] ST = {{4,0,4},
{4,2,1},
{4,0,3},
{3,3,3},
{5,0,1},
{5,0,1}};
int[] AS = {0,0,0,1,0,1};
String inString;
boolean accept1 = false;
FSM FSM1 = new FSM(A, ST, AS);
if(args.length >= 1)
{
// Input string is command line
parameter
inString = args[0];
accept1 =
FSM1.validString(inString);
System.out.println("\n String: " +
inString
+ "
Accept? " + accept1);
}
} // end main
} // end class
FSM.java
// Finite State Machine Class
public class FSM
{
// Instance variables
public String alphabet;
public int stateTrans[][];
public int acceptState[];
private int cstate;
// Constructor function
public FSM(String A, int[][] ST, int[] AS)
{
int NSYMBOLS = A.length();
int NSTATES = AS.length;
// Alphabet
alphabet = "" + A;
// State transition table
stateTrans = new int[NSTATES][NSYMBOLS];
for(int r = 0; r < NSTATES; r++)
for(int c = 0; c < NSYMBOLS;
c++)
stateTrans[r][c] =
ST[r][c];
// Accept states
acceptState = new int[NSTATES];
for(int r = 0; r < NSTATES; r++)
acceptState[r] = AS[r];
// Start state
cstate = 0;
}
// Methods
public int getState()
{
return cstate;
}
public void setState(int state)
{
cstate = state;
return;
}
public int nextState(char symbol)
{
int nstate = -1;
int col = alphabet.indexOf(symbol);
if(col >= 0)
nstate =
stateTrans[cstate][col];
return nstate;
}
public boolean accept(int state)
{
if(state < 0)
return false;
return (acceptState[state] != 0);
}
public boolean validString(String word)
{
cstate = 0;
for(int k = 0; k < word.length(); k++){
cstate =
nextState(word.charAt(k));
if(cstate < 0)
return false;
}
return accept(cstate);
}
} // end class
Write Java code to implement a FSM machine that recognizes the language for the alphabet {a,b,c} ...
(1) Write a regular expression for the language. (2) Define a finite state machine (FSM) that recognizes words in the language (input alphabet, states, start state, state transition table, and accept states). Include a state digraph for the FSM. A: For alphabet {p,q,r}, all strings that contain the substring rqr or end with pp.
In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a series of characters separated by a single space - The second line contains the number of states as an integer k 2 1; states will be numbered 0,1,..., k -1. The start state is always state O The third line contains a series...
In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a series of characters separated by a single space - The second line contains the number of states as an integer k 2 1; states will be numbered 0,1,..., k -1. The start state is always state O The third line contains a series...
IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...
In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...
Write in C language 5. [8] Write a function with prototype » void string_copy(const char source[], char destination[], int n); This function copies string source to string destination. Parameter n represents the size of array destination. If the latter array is not sufficiently large to hold the whole source string then only the prefix of the string which has room in the latter array should be copied. Note that after copying, the null character should also be included to mark...
write program in C language. To get more practice working with files, you will write several functions that involve operations on files. In particular, implement the following functions 1. Write a function that, given a file path/name as a string opens the file and returns its entire contents as a single string. Any endline characters should be preserved char *getFileContents (const char filePath); 2. Write a function that, given a file path/name as a string opens the file and returns...
using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings separated by a space. The first string will consist of the following sets of characters: +, *, $, and {N} which is optional. The plus (+) character represents a single alphabetic character, the ($) character represents a number between 1-9, and the asterisk (*) represents a sequence of the same character of length 3 unless it is followed by {N} which represents how many...
please implement this function by C language Write a string compare function which returns 1 if the strings match for n characters starting at offset m, O if the strings don't match. You must check if m is within the length of both s and t. int submatch(char* s, char* t, int n, int m) Write a string compare function which returns 1 if the strings match for n characters starting at offset m, O if the strings don't match....
C++ preffered please, thank you. Write a code in a high-level language (C++, C# or Java or ??) for a Deterministic Pushdown automaton which will accept the string a^nb^n where sigma = a, b. Create an input file with the following strings aaabbb, ababab, aababb, abbaab and give your output showing what is accepted and what is not accepted.