# 简单的NFA实现

Type Reference
Source SLScanner/NondeterministicFiniteAutomaton.cpp
Test SLScanner/TestNondeterministicFiniteAutomaton.cpp

## Difference to DFA

Nondeterministic FA: an FA that allows transitions on the empty string, , and states that have multiple transitions on the same character.

## Derived from DFA class

To simulate NFA, I extended class DeterministicFiniteAutomaton to the new NFA class named NoneterministicFiniteAutomaton. The differences between DFA and NFA caused different structure for class. However, we need to transform the NFA to the DFA to emulate the real FA.

In NFA, transitions can be -transitions, which means one state can moved to different states when a same alphabet or nothing inputed. So I created those objects to prepare for describe a NFA which can be used for construct the equivalent DFA.

We can notice that there is a new type ETransitionSet. I think the type StateSet can not describe the -transitions well and we don't need to tell the computer which alphabet is inputed, so I defined it in the following way.

We can easily iterate over the -trasitions started from some one state and stably support the construction for DFA. And the construction function for this class has been defind in the following way.

## Necessary Functions

There are two function to construct the new available DFA named eClosure and delta.

eClosure function will return a closures of states reachable by -transitions from states in the inputed state set. And delta function will returns .

## NFA to DFA

Transforming NFA to DFA needs the method called the subset construction, which constructs new states by map state sets to states and it is realized by the construct function.

As you see, by including the state set in the -closure of results from fiding all states moved from the previous state set recursively, we can construct a new DFA which equals with the NFA. That means we should search through all of -path of -transitions to get the whole graph to finish the task and we seems to use a depth-first search to get every states constructed. Finally we mapped each states and transformed the state set and the alphabet set into the base DFA class.

When the base DFA class has been constructed, the DFA can be available to match strings.

## Test

The test cases seem like those we used to test DFA, but I manually set some parameters, such like nETransitions to describe all -transitions.

And the test result are omitted, you can try to test it by building the module TESTScanner, see SLScanner/TestNondeterministicFiniteAutomaton.cpp.