source: app/src/lib/tfti.h @ 2f8d6d8

Last change on this file since 2f8d6d8 was 2f8d6d8, checked in by to <to@…>, 15 years ago

Poprawki kodu w C++ (nazwy plikow naglowkowych, using ...).

modified: compiledic/aut2fsa.cc
modified: dgp/grammar.hh
modified: dgp/mgraph.hh
modified: dgp/sgraph.hh
modified: dgp/thesymbols.hh
modified: gue/guess.cc
modified: kor/corlist.cc
modified: lem/lem.cc
modified: lib/symtab.cc
modified: lib/tft.h
modified: lib/tfti.h
modified: lib/ttrans.h
modified: lib/word.cc
modified: lib/word.h

  • Property mode set to 100755
File size: 15.6 KB
Line 
1#ifndef TFTiH
2#define TFTiH
3//---------------------------------------------------------------------------
4#include <fstream>
5#include <math.h>
6#include <iomanip>
7//#include <typeinfo.h>
8
9#include "tft.h"
10//---------------------------------------------------------------------------
11
12
13using namespace std;
14
15
16template<class I, class Ipass, class O, class Opass>
17class TFTi : public TFT<I,Ipass,O,Opass,TTrans_i<I,Ipass,O,Opass> >
18{
19public:
20  TFTi() : TFT<I,Ipass,O,Opass,TTrans_i<I,Ipass,O,Opass> >() {};
21  TFTi(const char* filename)
22  : TFT<I,Ipass,O,Opass,TTrans_i<I,Ipass,O,Opass> >() { load(filename); };
23
24  void read(const char* filename);
25  void read(istream& is=cin);
26  void write(const char* filename);
27  void write(ostream& os=cout);
28  void load(const char* filename);
29  void load(FILE* f=stdin);
30  void save(const char* filename);
31  void save(FILE* f=stdout);
32  void clear();
33  using TFT<I,Ipass,O,Opass,TTrans_i<I,Ipass,O,Opass> >::ttn;
34  using TFT<I,Ipass,O,Opass,TTrans_i<I,Ipass,O,Opass> >::states;
35  using TFT<I,Ipass,O,Opass,TTrans_i<I,Ipass,O,Opass> >::transitions;
36  using TFT<I,Ipass,O,Opass,TTrans_i<I,Ipass,O,Opass> >::itype;
37  using TFT<I,Ipass,O,Opass,TTrans_i<I,Ipass,O,Opass> >::ftTYPELEN;
38  using TFT<I,Ipass,O,Opass,TTrans_i<I,Ipass,O,Opass> >::otype;
39  using TFT<I,Ipass,O,Opass,TTrans_i<I,Ipass,O,Opass> >::tt;
40  using TFT<I,Ipass,O,Opass,TTrans_i<I,Ipass,O,Opass> >::copy_default;
41  using TFT<I,Ipass,O,Opass,TTrans_i<I,Ipass,O,Opass> >::print_mode;
42
43
44//  friend istream& operator>>(istream&, TFTi<I,Ipass,O,Opass>&);
45//  friend ostream& operator<<(ostream&, const TFTi<I,Ipass,O,Opass>&);
46};
47
48//---------------------------------------------------------------------------
49
50template<class I, class Ipass, class O, class Opass>
51void TFTi<I,Ipass,O,Opass>::read(const char* filename)
52{
53  ifstream is(filename);
54  if(!is) { fprintf(stderr,"Failed to open input file."); exit(1); }
55  read(is);
56}
57
58template<class I, class Ipass, class O, class Opass>
59void TFTi<I,Ipass,O,Opass>::read(istream& is)
60{
61  long *si;                             // state-index relation
62  long ci=0;                            // current index
63  char ch;                              // character read;
64  int empty=0;                          // no of states with 0 trans?
65  char intype[FT::ftTYPELEN];
66  char outtype[FT::ftTYPELEN];
67
68  clear();
69
70  is >> states >> transitions >> intype >> outtype;
71
72//  if(strcmp(intype,itype)!=0 ||
73//     strcmp(outtype,otype)!=0 && strcmp(outtype,"void")!=0)
74//    { is.clear(ios::badbit); goto end; };
75
76  while(is.peek()==' ' || is.peek()=='\t') is.get(ch);
77  while(is.peek()!='\n')
78  {
79    char s[20];
80    is >> s;
81    if(strcmp(s,"COPY")==0 && strcmp(intype,outtype)==0) copy_default=true;
82    else if(strcmp(s,"NOCOPY")==0) copy_default=false;
83    else if(strcmp(s,"II")==0) print_mode=FT::II;
84    else if(strcmp(s,"OO")==0) print_mode=FT::OO;
85    else if(strcmp(s,"IOIO")==0) print_mode=FT::IOIO;
86    else if(strcmp(s,"OIOI")==0) print_mode=FT::OIOI;
87    else if(strcmp(s,"IIOO")==0) print_mode=FT::IIOO;
88    else if(strcmp(s,"OIOI")==0) print_mode=FT::OIOI;
89    while(is.peek()==' ' || is.peek()=='\t') is.get(ch);
90  }
91
92  ttn=transitions+2;      // 1 state without trans., 1 additional
93  si=new long[states];
94  tt=new TTrans_i<I,Ipass,O,Opass>[ttn];
95
96  for(long cs=0;cs<states;cs++)
97  {
98    long tc;                             // transition counter
99    si[cs]=ci;
100    long cscheck;
101
102    if(!is) goto end;
103    while(is.peek()==' ' || is.peek()=='\t') is.get(ch);
104    is >> cscheck;
105    if(cs!=cscheck) goto end;
106
107    while(is.peek()==' ' || is.peek()=='\t') is.get(ch);
108
109    is.get(ch);
110    if(!is) goto end;
111    switch(ch)
112    {
113      case '-': tt[ci].final(false); break;
114      case '+': tt[ci].final(true); break;
115      default: goto end;
116    }
117    tc=0, tt[ci].continued(false);
118
119    while(is.peek()==' ' || is.peek()=='\t') is.get(ch);
120    while(is && is.peek()!='\n')
121    {
122      switch(is.peek())
123      {
124        case '~': tt[ci].epsi(true); tt[ci].defi(true); is.get(ch);
125                  break;
126        case '@': tt[ci].epsi(false); tt[ci].defi(true); is.get(ch);
127                  break;
128        default : tt[ci].geti(is);
129      }
130      if(!is) goto end;
131      if(is.peek()=='/')
132      {
133        is.get(ch);
134        switch(is.peek())
135        {
136          case '~': tt[ci].epso(true); tt[ci].defo(true); is.get(ch);
137                    break;
138          case '@': tt[ci].epso(false); tt[ci].defo(true); is.get(ch);
139                    break;
140          default : tt[ci].geto(is);
141        }
142      }
143      else
144      {
145        tt[ci].defo(true);
146        if(copy_default) tt[ci].epso(false); else tt[ci].epso(true);
147      }
148      if(!is) goto end;
149
150      unsigned long transition;
151      is >> transition;
152      tt[ci].next(transition);
153
154      tt[ci].continued(false);
155      tt[ci].empty(false);
156
157      if(tc>0) tt[ci-1].continued(true);
158      tc++,ci++;
159    }
160    if(tc==0)
161    {
162      if(++empty>2) { fprintf(stderr, "Nondeterministic automaton."); exit(1); }
163      tt[ci].empty(true);
164      ci++;
165    }
166    is.get(ch);
167    if(ch!='\n') { is.clear(ios::badbit); goto end; }
168  }
169
170  ttn=transitions+empty;
171  if(ttn!=ci) { is.clear(ios::badbit); goto end; };
172  for(long i=0;i<ttn;i++)
173    tt[i].next(si[tt[i].next()]);
174  delete[] si;
175  sort();
176
177end:
178  if(is.bad()) { fprintf(stderr,"Input error."); exit(1); }
179}
180
181//---------------------------------------------------------------------------
182
183template<class I, class Ipass, class O, class Opass>
184void TFTi<I,Ipass,O,Opass>::write(const char* filename)
185{
186  ofstream os(filename);
187  if(!os) err("Failed to open output file.");
188  write(os);
189}
190
191template<class I, class Ipass, class O, class Opass>
192void TFTi<I,Ipass,O,Opass>::write(ostream& os)
193{
194  os << states << ' ' << transitions << ' ';
195//  os << itype << ' ' << otype << ' ';
196  os << "char void";
197//  os << (copy_default ? "COPY" : "NOCOPY") << ' ';
198//  switch(print_mode)
199//  {
200//    case FT::II  : os << "II"; break;
201//    case FT::OO  : os << "OO"; break;
202//    case FT::IOIO: os << "IOIO"; break;
203//    case FT::OIOI: os << "OIOI"; break;
204//    case FT::IIOO: os << "IIOO"; break;
205//    case FT::OOII: os << "OOII";
206//  }
207  os << '\n';
208
209  long* si=new long[ttn];
210  long cs=0;
211  for(long i=0;i<ttn;i++)
212  {
213    si[i]=cs;
214    if(continued(i)==false) cs++;
215  }
216
217  int statefieldwidth=log10(cs+1);
218
219  bool first=true;
220  for(long i=0;i<ttn;i++)
221  {
222    if(first)
223    {
224      os << setw(statefieldwidth) << si[i] << " ";
225      if(final(i)) os << '+'; else os << '-';
226    }
227
228
229    if(!empty(i))
230    {
231      os << ' ';
232      if(epsi(i))
233        os << FT::ftEPSILON;
234      else
235      if(defi(i))
236        os << FT::ftDEFAULT;
237      else
238        os << input(i);
239
240      if(epso(i))
241      { if(copy_default) os << '/' << FT::ftEPSILON; }
242      else
243      if(defo(i))
244      { if(!copy_default) os << '/' << FT::ftDEFAULT; }
245      else
246      { os << '/' << output(i); }
247
248      if(strcmp(itype,"char")!=0 || strcmp(otype,"char")!=0)
249        os << ' ';
250      os << si[next(i)];
251    }
252    if(continued(i))
253      first=false;
254    else
255    { os << '\n'; first=true; }
256  }
257}
258
259//---------------------------------------------------------------------------
260
261template<class I, class Ipass, class O, class Opass>
262void TFTi<I,Ipass,O,Opass>::load(const char* filename)
263{
264  FILE* f;
265  if(*filename)
266    f=fopen(filename,"rb");
267  else
268    f=stdin;
269  if(!f) { fprintf(stderr, "Cannot open automaton file."); return; }
270  load(f);
271}
272
273template<class I, class Ipass, class O, class Opass>
274void TFTi<I,Ipass,O,Opass>::load(FILE* f)
275{
276
277  clear();
278
279  if(fread(&ttn,sizeof(ttn),1,f)!=1) { fprintf(stderr, "Binary input error."); return;}
280  if(fread(&states,sizeof(states),1,f)!=1) { fprintf(stderr, "Binary input error."); return;}
281  if(fread(&transitions,sizeof(transitions),1,f)!=1) { fprintf(stderr, "Binary input error."); return;}
282  if(fread(itype,sizeof(char),ftTYPELEN,f)!=ftTYPELEN) { fprintf(stderr, "Binary input error."); return;}
283  if(fread(otype,sizeof(char),ftTYPELEN,f)!=ftTYPELEN) { fprintf(stderr, "Binary input error."); return;}
284  if(fread(&copy_default,sizeof(copy_default),1,f)!=1) { fprintf(stderr, "Binary input error."); return;}
285  if(fread(&print_mode,sizeof(print_mode),1,f)!=1) { fprintf(stderr, "Binary input error."); return;}
286  if((tt=new TTrans_i<I,Ipass,O,Opass>[ttn])==NULL) { fprintf(stderr, "Cannot allocate memory for tt."); return;}
287  if(fread(tt,sizeof(TTrans_i<I,Ipass,O,Opass>),ttn,f)!=ttn) { fprintf(stderr, "Binary input error."); return; }
288  fclose(f);
289
290
291}
292
293//---------------------------------------------------------------------------
294
295template<class I, class Ipass, class O, class Opass>
296void TFTi<I,Ipass,O,Opass>::save(const char* filename)
297{
298  FILE* f;
299  if(*filename)
300    f=fopen(filename,"wb");
301  else
302    f=stdout;
303  if(!f) err("Cannot open file.");
304  save(f);
305}
306
307template<class I, class Ipass, class O, class Opass>
308void TFTi<I,Ipass,O,Opass>::save(FILE* f)
309{
310  if(fwrite(&ttn,sizeof(ttn),1,f)!=1) { fprintf(stderr,"Binary output error."); exit(1); }
311  if(fwrite(&states,sizeof(states),1,f)!=1) { fprintf(stderr,"Binary output error."); exit(1); }
312  if(fwrite(&transitions,sizeof(transitions),1,f)!=1) { fprintf(stderr,"Binary output error."); exit(1); }
313  if(fwrite(itype,sizeof(char),ftTYPELEN,f)!=ftTYPELEN) { fprintf(stderr,"Binary output error."); exit(1); }
314  if(fwrite(otype,sizeof(char),ftTYPELEN,f)!=ftTYPELEN) { fprintf(stderr,"Binary output error."); exit(1); }
315  if(fwrite(&copy_default,sizeof(copy_default),1,f)!=1) { fprintf(stderr,"Binary output error."); exit(1); }
316  if(fwrite(&print_mode,sizeof(print_mode),1,f)!=1) { fprintf(stderr,"Binary output error."); exit(1); }
317  if(fwrite(tt,sizeof(TTrans_i<I,Ipass,O,Opass>),ttn,f)!=ttn) { fprintf(stderr,"Binary output error."); exit(1); }
318  fclose(f);
319}
320
321//---------------------------------------------------------------------------
322
323template<class I, class Ipass, class O, class Opass>
324void TFTi<I,Ipass,O,Opass>::clear()
325{
326  if(tt) delete[] tt;
327  ttn=0;
328}
329
330//---------------------------------------------------------------------------
331/*
332template<class I, class Ipass, class O, class Opass>
333istream& operator>>(istream& is, TFTi<I,Ipass,O,Opass>& ft)
334{
335  long *si;                             // state-index relation
336  long ci=0;                            // current index
337  char ch;                              // character read;
338  int empty=0;                          // no of states with 0 trans?
339  char intype[FT::ftTYPELEN];
340  char outtype[FT::ftTYPELEN];
341
342  ft.clear();
343
344  is >> ft.states >> ft.transitions >> intype >> outtype;
345
346  if(strcmp(intype,ft.itype)!=0 ||
347     strcmp(outtype,ft.otype)!=0 && strcmp(outtype,"void")!=0)
348    { is.clear(ios::badbit); return is; };
349
350  while(is.peek()==' ' || is.peek()=='\t') is.get(ch);
351  while(is.peek()!='\n')
352  {
353    char s[20];
354    is >> s;
355    if(strcmp(s,"COPY")==0 && strcmp(intype,outtype)==0) ft.copy_default=true;
356    else if(strcmp(s,"NOCOPY")==0) ft.copy_default=false;
357    else if(strcmp(s,"II")==0) ft.print_mode=FT::II;
358    else if(strcmp(s,"OO")==0) ft.print_mode=FT::OO;
359    else if(strcmp(s,"IOIO")==0) ft.print_mode=FT::IOIO;
360    else if(strcmp(s,"OIOI")==0) ft.print_mode=FT::OIOI;
361    else if(strcmp(s,"IIOO")==0) ft.print_mode=FT::IIOO;
362    else if(strcmp(s,"OIOI")==0) ft.print_mode=FT::OIOI;
363    while(is.peek()==' ' || is.peek()=='\t') is.get(ch);
364  }
365
366  ft.ttn=ft.transitions+2;      // 1 state without trans., 1 additional
367  si=new long[ft.states];
368  ft.tt=new TTrans_i<I,Ipass,O,Opass>[ft.ttn];
369
370  for(long cs=0;cs<ft.states;cs++)
371  {
372    long tc;                             // transition counter
373    si[cs]=ci;
374    do is >> ch; while(ch!='+' && ch!='-');
375    switch(ch)
376    {
377      case '-': ft.tt[ci].final(false); break;
378      case '+': ft.tt[ci].final(true); break;
379      default: return is;
380    }
381    tc=0, ft.tt[ci].continued(false);
382    while((is.get(ch),ch==' '))
383    {
384      if(!is) return is;
385      switch(is.peek())
386      {
387        case '~': ft.tt[ci].epsi(true); ft.tt[ci].defi(true); is.get(ch);
388                  break;
389        case '@': ft.tt[ci].epsi(false); ft.tt[ci].defi(true); is.get(ch);
390                  break;
391        default : ft.tt[ci].geti(is);
392      }
393      if(!is) return is;
394      if(is.peek()=='/')
395      {
396        is.get(ch);
397        switch(is.peek())
398        {
399          case '~': ft.tt[ci].epso(true); ft.tt[ci].defo(true); is.get(ch);
400                    break;
401          case '@': ft.tt[ci].epso(false); ft.tt[ci].defo(true); is.get(ch);
402                    break;
403          default : ft.tt[ci].geto(is);
404        }
405      }
406      else
407      {
408        ft.tt[ci].defo(true);
409        if(ft.copy_default) ft.tt[ci].epso(false); else ft.tt[ci].epso(true);
410      }
411      if(!is) return is;
412
413      unsigned long transition;
414      is >> transition;
415      ft.tt[ci].next(transition);
416
417      ft.tt[ci].continued(false);
418
419      ft.tt[ci].empty(false);
420      if(tc>0) ft.tt[ci-1].continued(true);
421      tc++,ci++;
422    }
423    if(tc==0)
424    {
425      if(++empty>2) err("Nondeterministic automaton.");
426      ft.tt[ci].empty(true);
427      ci++;
428    }
429    if(ch!='\n') { is.clear(ios::badbit); return is; }
430  }
431
432  ft.ttn=ft.transitions+empty;
433  if(ft.ttn!=ci) { is.clear(ios::badbit); return is; };
434  for(long i=0;i<ft.ttn;i++)
435    ft.tt[i].next(si[ft.tt[i].next()]);
436  delete[] si;
437  ft.sort();
438  return is;
439}
440*/
441//---------------------------------------------------------------------------
442/*
443template<class I, class Ipass, class O, class Opass>
444ostream& operator<<(ostream& os, const TFTi<I,Ipass,O,Opass>& ft)
445{
446  os << ft.states << ' ' << ft.transitions << ' '
447     << ft.itype << ' ' << ft.otype << ' ';
448  os << (ft.copy_default ? "COPY" : "NOCOPY") << ' ';
449  switch(ft.print_mode)
450  {
451    case FT::II  : os << "II"; break;
452    case FT::OO  : os << "OO"; break;
453    case FT::IOIO: os << "IOIO"; break;
454    case FT::OIOI: os << "OIOI"; break;
455    case FT::IIOO: os << "IIOO"; break;
456    case FT::OOII: os << "OOII";
457  }
458  os << ' ' << '\n';
459
460  long* si=new long[ft.ttn];
461  long cs=0;
462  for(long i=0;i<ft.ttn;i++)
463  {
464    si[i]=cs;
465    if(ft.continued(i)==false) cs++;
466  }
467
468  bool first=true;
469  for(long i=0;i<ft.ttn;i++)
470  {
471    if(first)
472      if(ft.final(i)) os << '+'; else os << '-';
473
474    if(!ft.empty(i))
475    {
476      os << ' ';
477      if(ft.epsi(i))
478        os << FT::ftEPSILON;
479      else
480      if(ft.defi(i))
481        os << FT::ftDEFAULT;
482      else
483        os << ft.input(i);
484
485      if(ft.epso(i))
486      { if(ft.copy_default) os << '/' << FT::ftEPSILON; }
487      else
488      if(ft.defo(i))
489      { if(!ft.copy_default) os << '/' << FT::ftDEFAULT; }
490      else
491      { os << '/' << ft.output(i); }
492
493      if(strcmp(ft.itype,"char")!=0 || strcmp(ft.otype,"char")!=0)
494
495        os << ' ';
496      os << si[ft.next(i)];
497    }
498    if(ft.continued(i))
499      first=false;
500    else
501    { os << '\n'; first=true; }
502  }
503  return os;
504}
505*/
506//---------------------------------------------------------------------------
507//---------------------------------------------------------------------------
508
509template<class I, class O>
510class TFTiv : public TFTi<I,I,O,O>
511{
512public:
513  TFTiv() : TFTi<I,I,O,O>() {};
514  TFTiv(const char* filename) : TFTi<I,I,O,O>(filename) {};
515};
516
517//---------------------------------------------------------------------------
518
519template<class I, class O>
520class TFTir : public TFTi<I,I&,O,O&>
521{
522public:
523  TFTir() : TFTi<I,I,O,O>() {};
524};
525
526//---------------------------------------------------------------------------
527#endif
Note: See TracBrowser for help on using the repository browser.