source: src/lib/ttrans.h @ abd28d1

Last change on this file since abd28d1 was 5f4d9c3, checked in by Maciej Prill <mprill@…>, 13 years ago

Rewritten the build system, added lem UTF-8 version.

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