00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef NFAFACTORY_H
00019 #define NFAFACTORY_H
00020
00021
00022 #include "nfa.h"
00027 class StringToNFAFactory
00028 {
00029 public:
00030 StringToNFAFactory() {}
00031 ~StringToNFAFactory() {}
00032
00033 template <class InType>
00034 static NFA<InType>* makeNFA( const InType* array, int size, int token )
00035 {
00036 NFA<InType>* tempNFA = new NFA<InType>(token);
00037
00038 for(int i = 0; i <= size; ++i)
00039 tempNFA->addState();
00040
00041 for(int i = 0; i < size; ++i)
00042 (*tempNFA)[i]->addEdge( array[i], i+1 );
00043
00044 (*tempNFA)[size]->setGoalState();
00045
00046 return tempNFA;
00047 }
00048 };
00049
00050 class StringWithWildToNFAFactory
00051 {
00052 public:
00053 StringWithWildToNFAFactory() {}
00054 ~StringWithWildToNFAFactory() {}
00055
00056 static NFA<char>* makeNFA( const char* array, int size, int token )
00057 {
00058 NFA<char>* tempNFA = new NFA<char>(token);
00059
00060
00061 unsigned states = 0;
00062 FAEdge<char>* edgePtr = NULL;
00063
00064 bool charMode = false;
00065
00066 for( int i = 0; i < size; ++i )
00067 {
00068 if( array[i] == '"' )
00069 {
00070 charMode ? charMode = false : charMode = true;
00071 }
00072
00073 if( !charMode )
00074 {
00075 if( array[i] == '*' )
00076 {
00077
00078 --states;
00079 }
00080 else
00081 if( array[i] == '+' )
00082 {
00083 if( edgePtr )
00084 edgePtr->connectTo( (*tempNFA)[states] );
00085
00086 edgePtr = (*tempNFA)[states]->addEdge( array[i-1] );
00087 }
00088 else
00089 {
00090 if( edgePtr )
00091 edgePtr->connectTo( (*tempNFA)[states] );
00092
00093 edgePtr = (*tempNFA)[states]->addEdge(array[i]);
00094 ++states;
00095 }
00096 }
00097 else
00098 {
00099 if( edgePtr )
00100 edgePtr->connectTo( (*tempNFA)[states] );
00101
00102 edgePtr = (*tempNFA)[states]->addEdge(array[i]);
00103 ++states;
00104 }
00105
00106 };
00107
00108 edgePtr->connectTo( (*tempNFA)[states] );
00109
00110 (*tempNFA)[states]->setGoalState();
00111
00112 return tempNFA;
00113 }
00114 };
00115
00116 #endif