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