source: _old/app/src/lib/ttrans.h @ 1e121f4

Last change on this file since 1e121f4 was 1e121f4, checked in by Adam Kędziora <s301614@…>, 14 years ago

Replacing old implementation with working implementation

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