Java – finite state machine program

My task is to create a small program that can read the definition of FSM from the input, read some strings from the input, and determine whether FSM accepts these strings according to the definition I need to write it in C, C or Java I've searched the Internet for ideas on how to get started, but the best I can find is the Wikipedia article on automata based programming The C example provided seems to use an enumeration list to define the state. If the state is difficult, it doesn't matter to encode it in advance Similarly, I need to be able to actually read the number of States and the definition of each state Any suggestions are appreciated

@H_ 301_ 8@

Update: I can make the alphabet smaller (for example, {a, B}) and adopt other conventions. The start state is always state 0 I am allowed to impose reasonable limits on the number of countries, such as no more than 10@ H_ 301_ 8@

Question summary: @ h_ 301_ 8@

>How do I implement the FSA@ H_ 301_ 8@

Solution

First, get a list of all States (n) and a list of all symbols (m) Then there are two methods, interpretation or code generation:

@H_ 301_ 8@

>Interpretation Create an nxm matrix, in which each element of the matrix is filled with the corresponding target status number. If not, fill in - 1 Then you just need an initial state variable and start processing the input If you reach - 1, you fail If you run out of input symbols and do not enter the success state, it will fail Otherwise you will succeed. > Code generation Print out a program in C or your favorite compiler language It should have an integer state variable initialized to the start state It should have a for loop on the input character, including the switch of the state variable Each state should have a case, and in each case, there is a switch statement on the current character to change the state variable@ H_ 301_ 8@

If you want something faster than 2, it will definitely fail you (!), Get rid of state variables and use goto: -) if you're overwhelmed, you can comfort yourself that this is what the compiler does@ H_ 301_ 8@

Note: if you identify loops in the state diagram and print out the corresponding while and if statements instead of goto, you can change f to A. @ H_ 301_ 8@

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>