Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing A Nondeterminisic Finite Automaton(NFA)

I'm trying to a develop a simulation that executes a non deterministic finite automaton in Java. The first command line argument is a text file that defines the machine. The second argument is an input string. If it accepts the string, it prints to standard output "accept" followed by a list of accept states it can end in. If it rejects, it outputs "reject" followed by a list of all possible end states.

For example, the text:

state   1   start
state   2
state   7   accept
transition  1   0   1
transition  1   1   1
transition  1   0   2
transition  2   0   2
transition  2   0   1
transition  2   1   1
transition  2   0   7
transition  7   0   7
transition  7   1   7

which looks like:

which looks like:

with an input string of 10 would output

reject 1 2

and with an input string of 000 would output

accept 7

I need advice on what data structures to use. I thought about using hashmaps and sets but I'm not sure.

like image 983
donth77 Avatar asked Sep 13 '14 23:09

donth77


People also ask

What is the concept of nondeterministic finite automaton NFA?

NFA stands for non-deterministic finite automata. It is easy to construct an NFA when compared to DFA for a given regular language. The finite automata are called NFA when there exist many paths for specific input from the current state to the next state. Each NFA can be translated into DFA but every NFA is Non DFA.

How do you implement an NFA?

An nfa can be implemented by means of a recursive search from the start state for a path (directed by the symbols of the input string) to a final state. One problem with this implementation is that it could get into an infinite loop if there is a cycle of l transitions.

Why are nondeterministic finite automata NFA necessary in compiler development?

It's important because NFAs may be wont to reduce the complexity of the mathematical work required to determine many important properties within the theory of computation. For instance, it's much easier to prove the closure properties of standard languages using NFAs than DFAs.


2 Answers

I think you should transform your automaton into a deterministic one and then do the obvious.

There are three common ways of implementing a finite state machine in software:

  • Represent the transition function as a table (2D array) and upon each character read, look up the next state in it.
  • Use nested switch statements for the current state and input character to explicitly define the next state in code.
  • Use the State Pattern: Represent each state as a class with a method returning the next state, given an input character.

Since you'll need to build your automaton at run-time, the last two options don't really apply, so you should go with the lookup table. If your alphabet is small, you can write down all transitions. If the alphabet is huge, this might waste a lot of space and you can think about table compression which used to be an active field of research.

For the Downvoters: You cannot write a deterministic program that “executes” a non-deterministic automaton. However, by a rather fundamental theorem of theoretical computer science, you can transform any non-deterministic finite state automaton into a deterministic one by a fully automated procedure, that is, a deterministic algorithm that can easily be implemented in software. Every computer science student should have done this procedure at least once using pencil and paper. If you do so, you'll quickly see how to keep track of which states of the original automaton went into which states of the deterministic one. Then, simply simulate the latter one, see in what state it ends up and output the states of the original non-deterministic automaton that constitute it.

like image 109
5gon12eder Avatar answered Oct 11 '22 22:10

5gon12eder


NFA means you can have set of states at a time. So to represent current state of NFA use Set interface.

Set interface guarantees that there won't be any duplicates of state in . This is important as NFA has more than one state at a time. If you use any other data set this gurrentee is not there. In case of NFA chance of having duplicate state in each transition is exponential. But set of state is always finite. Set interface guarantees that your current set will be filled with duplicates.

For space and performance you can use Enumset as Enumset use bit vectors to store state internally.

Algorithm:

initialise to start state

Process string from right to left starting from index 0;

for character at update the using the state transition;

If for any of this transition leads to final state which means that string is accepted.

like image 28
user2875775 Avatar answered Oct 11 '22 22:10

user2875775