build.h

00001 /*
00002  * ASTL - the Automaton Standard Template Library.
00003  * C++ generic components for Finite State Automata handling.
00004  * Copyright (C) 2000-2003 Vincent Le Maout (vincent.lemaout@chello.fr).
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #ifndef ASTL_BUILD_H
00023 #define ASTL_BUILD_H
00024 
00025 #include <astl.h>
00026 #include <iostream>
00027 #include <map>
00028 
00029 using namespace std;
00030 
00031 namespace astl {
00032 
00033 #if 0
00034 void skip_comments(istream &in)
00035 {
00036   char comments;
00037   if (!in.eof()) {
00038     in >> comments;
00039     if (comments == '#') {
00040       string bogus;
00041       getline(in, bogus);
00042       skip_comments(in);
00043     }
00044     else in.unget();
00045   }
00046 }
00047 #endif
00048  
00049 template <typename State> 
00050 int next_state(istream &in, State &i)
00051 {
00052   //  skip_comments(in);
00053   if (in.eof()) return -1;
00054   in >> i;
00055   return 0;
00056 }
00057 
00058 template <typename Char>
00059 int next_letter(istream &in, Char &c)
00060 {
00061   //  skip_comments(in);
00062   if (in.eof()) return -1;
00063   in >> c;
00064   return 0;
00065 }
00066 
00067 template <typename State, typename Char>
00068 int next_transition(istream &in, State &q, Char &a, State &p)
00069 {
00070   if (next_state(in, q) == -1) return -1;
00071   if (next_letter(in, a) == -1) return -1;
00072   if (next_state(in, p) == -1) return -1;
00073   return 0;
00074 } 
00075 
00076 template <typename FA>
00077 typename FA::state_type build(FA &A, istream &in)
00078 {
00079   map<int, safe<typename FA::state_type, FA::null_state> > m;
00080   typename FA::state_type i = FA::null_state;
00081   int q, p;
00082   typename FA::char_type c;
00083 
00084   if (next_transition(in, q, c, p) != -1) {
00085     m[q] = A.new_state();
00086     A.final(m[q]) = q < 0;
00087     safe<typename FA::state_type, FA::null_state> &pp = m[p];
00088     if (pp == FA::null_state) {
00089       pp = A.new_state();
00090       A.final(pp) = p < 0;
00091     }
00092     A.set_trans(m[q], c, pp);
00093     i = m[q];
00094   }
00095 
00096   while (next_transition(in, q, c, p) != -1) {
00097     safe<typename FA::state_type, FA::null_state> &qq = m[q];
00098     safe<typename FA::state_type, FA::null_state> &pp = m[p];
00099     if (qq == FA::null_state) {
00100       qq = A.new_state();
00101       A.final(qq) = q < 0;
00102     }
00103     if (pp == FA::null_state) {
00104       pp = A.new_state();
00105       A.final(pp) = p < 0;
00106     }
00107     A.set_trans(qq, c, pp);
00108   }
00109 
00110   return i;
00111 }
00112     
00113 } // namespace astl
00114 
00115 #endif

Generated on Sun Mar 8 02:41:30 2009 for ASTL by  doxygen 1.5.7.1