8

I understand that any language worth its salt can encode a Finite state machine. My question is the opposite, is it possible to convert an arbitrary piece of code ( say in C ) to a functionally equivalent Finite State Machine ?

for example if I have a piece of code with a couple of llvm basic blocks (loops, branches etc), can I replace all of them with a giant state machine, which essentially does the same ?

Jehandad
  • 191

3 Answers3

13

The Finite State Machine has less computational power than a Turing machine. That is, the Turing Machine can do things that the FSM cannot. This is true because the FSM is limited in memory by the number of (finite) states.

The following diagram illustrates the relationship between a Finite State Machine and a Turing Machine. As you can see, the Turing Machine is built on top of the other layers, one of which is the Finite State Machine.

Could you write a large enough state machine to encompass any arbitrary piece of code? Possibly. But you won't have a generalized Turing Machine, so there's no guarantee that it's going to work every time.

Automata Theory Venn Diagram

Further Reading

Turing Machine
Finite State Machine

Robert Harvey
  • 200,592
7

Yes, of course it can.

See Bohm & Jacopini's 1966 paper (Yes, fifty years ago), "Flow diagrams, turing machines and languages with only two formation rules" for the gory details.

They showed that any program, even a mess of GOTO-spaghetti, can be converted into a WHILE-loop around a CASE statement, that together implement a finite-state machine.

This was a critical result on the road to Structured Programming. It proved, among other things, that GOTO statements were not necessary.

5

No. Consider a piece of code that reads in a string of 0's and recognizes if the string's length is a perfect square (1, 4, 9, 16, 25 ...). Is there a finite state machine that can do this? No, since the set of all such strings is not a regular set (hint: use pumping lemma) and a finite state machine can only recognize a regular set. This example is from page 57 of the book Introduction to automata theory, languages, and computation (1st edition) by Hopcroft and Ullman (available at https://archive.org/details/HopcroftUllman_cinderellabook). Additionally, chapters two and three of this book provide a good introduction to finite state machines, regular sets, and the pumping lemma. There are many other examples of non-regular sets on the Internet. Search for non-regular languages.

Chris Dupilka
  • 51
  • 1
  • 3