# 简单的DFA实现

Type Reference
Source SLScanner/FiniteAutomaton.cpp
Source SLScanner/DeterministicFiniteAutomaton.cpp
Test SLScanner/TestDeterministicFiniteAutomaton.cpp

## Definition

is the finite set of states in the recognizer, along with an error state

is the finite alphabet used by the recognizer. Typically, is the union of the edge labels in the transition diagram.

is the recognizer’s transition function. It maps each state and each character into some next state. In state si with input character , the takes the transition .

is the designated start state.

is the set of accepting states, . Each state in appears as a double circle in the transition diagram.

## Design

According to the definition of Finite Automaton, I have designed the class FiniteAutomaton in below structure. Actually, starting state is always , so I didn't describe it explicitly in the class. Before building the class structrue, I decided to define some fundamental types for the internal elements of a finite automaton.

Followings are some necessary elements for a finite automaton.

And those values has been defined outside the FiniteAutomaton class.

The FiniteAutomaton class is a base class for DeterministicFiniteAutomaton. And we can only use DFA to match the strings. So the most important part of the scanner is the fundamental DFA.

So I built the following function to simulate the actions of real DFA.

The transite function needs to check if the DFA has finished a match task executed by match function. And one transition means finding an accessible transition from all transitions. If it does not exist in the transitions, the DFA may cause a trap state to exclaim the match is not successful and it can not move to other states unless reboot is executed.

Actually the structure of a DFA is simple and you must manually set all the elements for the DFA by using the following constructor function.

## Test

So I used following test case to check the availability of the DFA.

DeterministicFiniteAutomaton.SimpleTest:

Test Result:

String StateType TestResult
A12345 Accepted PASS
A Accepted PASS
Z3910 Accepted PASS
Z99999 Accepted PASS
Z9999D Trapped PASS
Trapped PASS
3232 Trapped PASS

DeterministicFiniteAutomaton.FloatNumberTest:

The definition of test DFA is omitted, you can reference it in the SLScanner/TestDeterministicFiniteAutomaton.cpp.

String StateType TestResult
0.13423231 Accepted PASS
0.1342E+213.4 Accepted PASS
+13 Accepted PASS
+13e-19 Accepted PASS
.234144 Trapped PASS
.234122.21310012. Trapped PASS
.234E NotAccepted PASS

DeterministicFiniteAutomaton.CamelCaseTest:

The definition of test DFA is omitted, you can reference it in the SLScanner/TestDeterministicFiniteAutomaton.cpp.

String StateType TestResult
HelloWorld Accepted PASS
GoodBoy Accepted PASS
GoodB NotAccepted PASS
GoodB8 Trapped PASS