00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef ARCHON_UTILITIES_NFA_H
00021 #define ARCHON_UTILITIES_NFA_H
00022
00023 #include <string>
00024 #include <vector>
00025 #include <set>
00026 #include <map>
00027 #include <utility>
00028
00029 #include <archon/util/dfa.H>
00030 #include <archon/util/exception.H>
00031
00032 namespace Archon
00033 {
00034 namespace Utilities
00035 {
00036 using namespace std;
00037
00038 class Regex;
00039
00046 class NFA
00047 {
00048 friend class DFA;
00049
00050 struct Edge
00051 {
00052 unsigned begin;
00053 unsigned end;
00054 int targetState;
00055 Edge(unsigned begin, unsigned end, int targetState):
00056 begin(begin), end(end), targetState(targetState) {}
00057 };
00058
00059 struct State
00060 {
00061 int finalValue;
00062 vector<Edge> symbolEdges;
00063 vector<int> epsilonEdges;
00064 State(int finalValue = -1): finalValue(finalValue) {}
00065 };
00066
00067 vector<State> states;
00068 vector<int> startStates;
00069
00070 void add(const NFA &, int routeTo = -1);
00071 bool closedAdd(int state, set<int> &stateSet) const;
00072 void updateDfaEdgeRange(Edge e, map<unsigned, pair<unsigned, set<int> > > &dEdges) const;
00073 int chooseFinalValue(const set<int> &stateSet) const;
00074 void constructDFA(DFA *, vector<set<int> > *, DFA::AnchorInfo *) const;
00075
00076 public:
00086 NFA() {}
00087
00094 NFA(basic_string<unsigned> s, int finalValue = 0);
00095
00102 NFA(unsigned begin, unsigned end, int finalValue = 0);
00103
00110 NFA(const vector<pair<unsigned, unsigned> > &ranges, int finalValue = 0);
00111
00115 NFA(const DFA &);
00116
00121 NFA(const Regex &, int finalValue = 0);
00122
00129 void altern(const NFA &);
00130
00137 void concat(const NFA &);
00138
00146 void repeat(bool includeZero=false, int finalValue=0);
00147
00152 void setFinalValue(int finalValue);
00153
00166 int addStartState(int finalValue = -1);
00167
00180 int addState(int finalValue = -1);
00181
00185 void addEdge(int originalState, int targetState);
00186
00190 void addEdge(int originalState, int targetState, unsigned symbol)
00191 {
00192 addEdge(originalState, targetState, symbol, symbol);
00193 }
00194
00199 void addEdge(int originalState, int targetState,
00200 unsigned begin, unsigned end);
00201
00202 struct Printer
00203 {
00204 virtual string printSymbol(unsigned) const = 0;
00205 virtual ~Printer() {}
00206 };
00207
00208 static const Printer &getDefaultPrinter();
00209
00210 string print(const Printer &printer = getDefaultPrinter(), int width = 0) const;
00211 };
00212 }
00213 }
00214
00215 #endif // ARCHON_UTILITIES_NFA_H