Questions tagged [fsm]

Acronym for Finite State Machine.

Finite state machine, finite state automata, or state machine, is used in computer science or logic theory to represent a finite number of states and the transitions between states.

Finite state machines are commonly used in parsing and matching strings, so it accepts certain types of strings (such as those representing an integer), and a language (set of strings) is regular if and only if it can be represented as a finite state machine.

An example of a finite state machine implementation in pseudocode, accepting all decimal integers:

state = 0;
digits = "152341264"; // Some sequence of decimal digits
for (k = 0; k < len(digits); k++) {
    switch (state) {
    case 0: // Initial state
        if (digits[k] is a decimal digit)
            state = 1;
        else
            state = 2;
        break;
    case 1: // Digit found, also an accepting state
        if (digits[k] is a decimal digit)
            state = 1;
        else
            state = 2;
        break;
    case 2: // Dead state
        break;
    }
}
FSM accepts the string digits if it finishes at state 1.

Finite state machines represent all the regular languages, or Type 3 languages, which are the lowest in the Chomsky hierarchy, below the context-free (Type 2) languages, which is below the context-sensitive (Type 1) languages, which is below the recursively enumerable (Type 0) languages.

Wikipedia page

The tag is also known like on stackoverflow.

533 questions
96
votes
6 answers

Is a Markov chain the same as a finite state machine?

Is a finite state machine just an implementation of a Markov chain? What are the differences between the two?
Carson
  • 17,073
  • 19
  • 66
  • 87
64
votes
8 answers

How to implement a FSM - Finite State Machine in Java

I have something to do for work and I need your help. We want to implement a FSM - Finite State Machine, to identify char sequence(like: A, B, C, A, C), and tell if it accepted. We think to implement three classes: State, Event and Machine. The…
Ofir A.
  • 3,112
  • 11
  • 57
  • 83
50
votes
12 answers

Python state-machine design

Related to this Stack Overflow question (C state-machine design), could you Stack Overflow folks share your Python state-machine design techniques with me (and the community)? At the moment, I am going for an engine based on the following: class…
jldupont
  • 93,734
  • 56
  • 203
  • 318
34
votes
9 answers

Java enum-based state machine (FSM): Passing in events

I'm using several enum-based state machines in my Android application. While these work very well, what I am looking for is a suggestion for how to elegantly receive events, typically from registered callbacks or from eventbus messages, into the…
Trevor
  • 10,903
  • 5
  • 61
  • 84
29
votes
8 answers

Does C# include finite state machines?

I've recently read about the boost::statechart library (finite state machines) and I loved the concept. Does C# have a similar mechanism ? Or can it be implemented using a specific design pattern?
Maciek
  • 19,435
  • 18
  • 63
  • 87
28
votes
10 answers

Graphical Finite State Machine Editor

I am looking for a sophisticated graphical FSM editor that can export a model in a well-documented output format, like SCXML or similar. Can anybody recommend me a tool?
Roland
  • 436
  • 1
  • 5
  • 11
27
votes
4 answers

implementing a state machine using the "yield" keyword

Is it feasible to use the yield keyword to implement a simple state machine as shown here. To me it looks like the C# compiler has done the hard work for you as it internally implements a state machine to make the yield statement work. Can you…
Matt Warren
  • 10,279
  • 7
  • 48
  • 63
23
votes
6 answers

Short example of regular expression converted to a state machine?

In the Stack Overflow podcast #36 (https://blog.stackoverflow.com/2009/01/podcast-36/), this opinion was expressed: Once you understand how easy it is to set up a state machine, you’ll never try to use a regular expression inappropriately ever…
slothbear
  • 2,016
  • 3
  • 21
  • 38
22
votes
3 answers

How do you even give an (openFST-made) FST input? Where does the output go?

Before I start, note that I'm using the linux shell (via using subprocess.call() from Python), and I am using openFST. I've been sifting through documents and questions about openFST, but I cannot seem to find an answer to this question: how does…
Sterling
  • 235
  • 3
  • 7
19
votes
5 answers

Finite State Machine (FSM) and Android's Java

I'm willing to develop a soccer game for Android. Because the complexity of the AI, i really think i need to design it using a FSM (Finite State Machine) and not with a monster switch. Googling around i found some FSM written in Java, but nothing…
Luigi
  • 201
  • 1
  • 2
  • 7
19
votes
2 answers

FSM vs become/unbecome in Akka

Akka provides two somewhat overlapping ways to manage actor states, Finite State Machines and unbecome/become. What are their respective benefits/drawbacks? When should one of them be chosen over the other?
Tobias Furuholm
  • 4,727
  • 4
  • 30
  • 39
17
votes
7 answers

Why is {a^nb^n | n >= 0} not regular?

In a CS course I'm taking there is an example of a language that is not regular: {a^nb^n | n >= 0} I can understand that it is not regular since no Finite State Automaton/Machine can be written that validates and accepts this input since it lacks a…
Christophe Herreman
  • 15,895
  • 9
  • 58
  • 86
16
votes
4 answers

How to represent a simple finite state machine in Ocaml?

I have written some state machine in C++ and Java but never in a functional language like Ocaml Problem is I don't know if I can just adapt code from the object languages versions, since in Ocaml records and variants are more powerful than class;…
codablank1
  • 6,055
  • 5
  • 19
  • 29
16
votes
13 answers

How to determine if a regex is orthogonal to another regex?

I guess my question is best explained with an (simplified) example. Regex 1: ^\d+_[a-z]+$ Regex 2: ^\d*$ Regex 1 will never match a string where regex 2 matches. So let's say that regex 1 is orthogonal to regex 2. As many people asked what I meant…
Benedikt Waldvogel
  • 12,406
  • 8
  • 49
  • 61
15
votes
4 answers

How to get all constants of a type in Go

Here is an example: package main type State int const ( Created State = iota Modified Deleted ) func main() { // Some code here where I need the list // of all available constants of this type. } The use case for this is to…
IamLearning
  • 165
  • 1
  • 6
1
2 3
35 36