nfa.H

00001 /*
00002  * This file is part of the "Archon" framework.
00003  * (http://files3d.sourceforge.net)
00004  *
00005  * Copyright © 2002 by Kristian Spangsege and Brian Kristiansen.
00006  *
00007  * Permission to use, copy, modify, and distribute this software and
00008  * its documentation under the terms of the GNU General Public License is
00009  * hereby granted. No representations are made about the suitability of
00010  * this software for any purpose. It is provided "as is" without express
00011  * or implied warranty. See the GNU General Public License
00012  * (http://www.gnu.org/copyleft/gpl.html) for more details.
00013  *
00014  * The characters in this file are ISO8859-1 encoded.
00015  *
00016  * The documentation in this file is in "Doxygen" style
00017  * (http://www.doxygen.org).
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; // See "regex.H"
00039 
00046     class NFA
00047     {
00048       friend class DFA;
00049 
00050       struct Edge
00051       {
00052         unsigned begin;
00053         unsigned end; // inclusive
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; // -1 for non-final
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; // State indices
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

Generated on Sun Jul 30 22:55:45 2006 for Archon by  doxygen 1.4.4