[25ae32e] | 1 | #ifndef _TTransi_h |
---|
| 2 | #define _TTransi_h |
---|
| 3 | //--------------------------------------------------------------------------- |
---|
[2f8d6d8] | 4 | #include <iostream> |
---|
[25ae32e] | 5 | //--------------------------------------------------------------------------- |
---|
| 6 | |
---|
[a6e708f] | 7 | |
---|
| 8 | using namespace std; |
---|
| 9 | |
---|
[25ae32e] | 10 | //! The template for a transition with input and output symbols stored internally. |
---|
| 11 | /*! |
---|
| 12 | A state is identified with the set of its outgoing transitions. |
---|
| 13 | The state index is the index of the first transition for it. |
---|
| 14 | A state with no outgoing transition is represented as an empty transition. |
---|
| 15 | */ |
---|
| 16 | template<class I, class Ipass, class O, class Opass> |
---|
| 17 | class TTrans_i |
---|
| 18 | { |
---|
| 19 | public: |
---|
| 20 | //private: |
---|
| 21 | //! Input symbol |
---|
| 22 | I i; |
---|
| 23 | //! Output symbol |
---|
| 24 | O o; |
---|
| 25 | |
---|
| 26 | public: |
---|
| 27 | |
---|
| 28 | //! state is final |
---|
| 29 | static const unsigned char BITf=0x01; |
---|
| 30 | //! transition list is continued |
---|
| 31 | static const unsigned char BITc=0x02; |
---|
| 32 | //! no transition |
---|
| 33 | static const unsigned char BITe=0x04; |
---|
| 34 | //! epsilon input |
---|
| 35 | static const unsigned char BITepsi=0x08; |
---|
| 36 | //! default input |
---|
| 37 | static const unsigned char BITdefi=0x10; |
---|
| 38 | //! epsilon output |
---|
| 39 | static const unsigned char BITepso=0x20; |
---|
| 40 | //! default output |
---|
| 41 | static const unsigned char BITdefo=0x40; |
---|
| 42 | |
---|
| 43 | //! Flags |
---|
| 44 | unsigned char flags; |
---|
| 45 | |
---|
| 46 | //! The index of the next state |
---|
| 47 | long nxt; |
---|
| 48 | |
---|
| 49 | //! Input symbol. |
---|
| 50 | //! \return The input symbol of the transition. |
---|
| 51 | Ipass in() const { return i; } |
---|
| 52 | |
---|
| 53 | //! Output symbol. |
---|
| 54 | //! \return The output symbol of the transition. |
---|
| 55 | Opass out() const { return o; } |
---|
| 56 | |
---|
| 57 | //! Set the input symbol. |
---|
| 58 | //! \param in input symbol |
---|
| 59 | void in(Ipass in) { i=in; } |
---|
| 60 | |
---|
| 61 | //! Set the output symbol. |
---|
| 62 | //! \param out output symbol |
---|
| 63 | void out(Opass out) { o=out; } |
---|
| 64 | |
---|
| 65 | //! remark Is this needed? |
---|
| 66 | I& iref() { return i; } |
---|
| 67 | |
---|
| 68 | //! remark Is this needed? |
---|
| 69 | O& oref() { return o; } |
---|
| 70 | |
---|
| 71 | //! Test whether an input symbol is accepted. |
---|
| 72 | //! \remark Simplified. Should rely on a test function provided by the user. |
---|
| 73 | bool accepts(Ipass in) { return defi() || in==i; } |
---|
| 74 | |
---|
| 75 | //! Next state. |
---|
| 76 | //! \return Destination state of the transition. |
---|
| 77 | long next() const { return nxt; }; |
---|
| 78 | |
---|
| 79 | //! Set the next state. |
---|
| 80 | //! \param t destination state of the transition |
---|
| 81 | void next(long t) { nxt=t; }; |
---|
| 82 | |
---|
| 83 | //! Is the state final? |
---|
| 84 | //! \return \c true if the state is final, false otherwise. |
---|
| 85 | bool final() const { return flags&BITf; }; |
---|
| 86 | |
---|
| 87 | //! Set the \b final flag. |
---|
| 88 | //! \param b \c true if the state is final, \c false otherwise. |
---|
| 89 | void final(bool b) { if(b) flags|=BITf; else flags&=~BITf; }; |
---|
| 90 | |
---|
| 91 | //! Is the transition list continued? |
---|
| 92 | //! \return \c true if the transition is not the last transition for the state, |
---|
| 93 | //! \c false otherwise. |
---|
| 94 | bool continued() const { return flags&BITc; }; |
---|
| 95 | |
---|
| 96 | //! Set the \b continuation flag. |
---|
| 97 | //! \param b \c true if the transition is not the last one for the state, \c false otherwise. |
---|
| 98 | void continued(bool b) { if(b) flags|=BITc; else flags&=~BITc; }; |
---|
| 99 | |
---|
| 100 | //! Is the transition empty? |
---|
| 101 | //! \return \c true if the transition is empty (represents a state with no outgoing transitions), |
---|
| 102 | //! \c false otherwise. |
---|
| 103 | bool empty() const { return flags&BITe; }; |
---|
| 104 | |
---|
| 105 | //! Set the \b empty flag. |
---|
| 106 | //! \param b \c true if the transition is empty, \c false otherwise. |
---|
| 107 | void empty(bool b) { if(b) flags|=BITe; else flags&=~BITe; }; |
---|
| 108 | |
---|
| 109 | bool epsi() const { return flags&BITepsi; }; |
---|
| 110 | void epsi(bool b) { if(b) flags|=BITepsi; else flags&=~BITepsi; }; |
---|
| 111 | |
---|
| 112 | bool defi() const { return flags&BITdefi; }; |
---|
| 113 | void defi(bool b) { if(b) flags|=BITdefi; else flags&=~BITdefi; }; |
---|
| 114 | |
---|
| 115 | bool epso() const { return flags&BITepso; }; |
---|
| 116 | void epso(bool b) { if(b) flags|=BITepso; else flags&=~BITepso; }; |
---|
| 117 | |
---|
| 118 | bool defo() const { return flags&BITdefo; }; |
---|
| 119 | void defo(bool b) { if(b) flags|=BITdefo; else flags&=~BITdefo; }; |
---|
| 120 | |
---|
| 121 | void geti(istream&); |
---|
| 122 | void geto(istream&); |
---|
| 123 | |
---|
| 124 | // friend ostream& operator<<(ostream& os, const TTrans_i<I,Ipass,O,Opass>& t); |
---|
| 125 | |
---|
| 126 | }; |
---|
| 127 | |
---|
| 128 | //--------------------------------------------------------------------------- |
---|
| 129 | |
---|
| 130 | template<char> |
---|
| 131 | void getsym(istream& is, char& c) |
---|
| 132 | { |
---|
| 133 | is >> c; |
---|
| 134 | if(c=='\\') |
---|
| 135 | { |
---|
| 136 | is.get(c); |
---|
| 137 | switch(c) |
---|
| 138 | { |
---|
| 139 | case 'n':c='\n';break; |
---|
| 140 | case 't':c='\t';break; |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | template<class T> |
---|
| 146 | void getsym(istream& is, T& s) |
---|
| 147 | { is >> s; } |
---|
| 148 | |
---|
| 149 | //--------------------------------------------------------------------------- |
---|
| 150 | |
---|
| 151 | template<class I, class Ipass, class O, class Opass> |
---|
| 152 | void TTrans_i<I,Ipass,O,Opass>::geti(istream& is) |
---|
| 153 | { getsym<I>(is,iref()); }; |
---|
| 154 | |
---|
| 155 | template<class I, class Ipass, class O, class Opass> |
---|
| 156 | void TTrans_i<I,Ipass,O,Opass>::geto(istream& is) |
---|
| 157 | { getsym<I>(is,oref()); }; |
---|
| 158 | |
---|
| 159 | //--------------------------------------------------------------------------- |
---|
| 160 | /* |
---|
| 161 | template<class I, class Ipass, class O, class Opass> |
---|
| 162 | ostream& operator<<(ostream& os, const TTrans_i<I,Ipass,O,Opass>& t) |
---|
| 163 | { |
---|
| 164 | os << (t.final() ? '+' : '-'); |
---|
| 165 | os << ' '; |
---|
| 166 | |
---|
| 167 | if(!t.empty()) |
---|
| 168 | { |
---|
| 169 | if(t.defi()) |
---|
| 170 | os << (t.epsi() ? '~' : '@'); |
---|
| 171 | else |
---|
| 172 | switch(t.in()) |
---|
| 173 | { |
---|
| 174 | case ' ': os << "\\ "; break; |
---|
| 175 | case '\n': os << "\\n"; break; |
---|
| 176 | case '\t': os << "\\t"; break; |
---|
| 177 | default: os << t.in(); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | os << '/'; |
---|
| 181 | |
---|
| 182 | if(t.defo()) |
---|
| 183 | os << (t.epso() ? '~' : '@'); |
---|
| 184 | else |
---|
| 185 | switch(t.out()) |
---|
| 186 | { |
---|
| 187 | case ' ': os << "\\ "; break; |
---|
| 188 | case '\n': os << "\\n"; break; |
---|
| 189 | case '\t': os << "\\t"; break; |
---|
| 190 | default: os << t.out(); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | os << ' ' << t.next(); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | os << '\n'; |
---|
| 197 | |
---|
| 198 | if(!t.continued()) |
---|
| 199 | os << '\n'; |
---|
| 200 | |
---|
| 201 | return os; |
---|
| 202 | } |
---|
| 203 | */ |
---|
| 204 | |
---|
| 205 | //--------------------------------------------------------------------------- |
---|
| 206 | #endif |
---|
| 207 | |
---|