00001 00002 // Contents: Definitions of Alphabet related classes 00003 // 00004 // Principal Author: Alexei Miasnikov 00005 // Copyright (C) 2005 00006 // 00007 // Status: in progress 00008 // 00009 // Revision History: 00010 // 00011 00012 #ifndef _ALPHABET_H_ 00013 #define _ALPHABET_H_ 00014 00015 #include <iostream> 00016 #include <vector> 00017 #include <string> 00018 //#include "Word.h" 00019 #include "Parser.h" 00020 #include <stdlib.h> 00021 #include <sstream> 00022 00023 using namespace std; 00024 00025 class Word; 00026 00027 /********************************************************** 00028 * 00029 * ALPHABET INTERFACE 00030 * 00031 ***********************************************************/ 00032 00034 class Alphabet 00035 { 00036 public: 00038 virtual int getNum( const string& letter )const = 0; 00039 00041 virtual string getLetter( int index )const = 0; 00042 00044 void printWord( ostream& out, const Word& w )const; 00045 00047 Word readWord( istream& in )const; 00048 00050 void printVector( ostream& out, const vector<Word>& v )const; 00051 00053 vector<Word> readVector( istream& in )const; 00054 }; 00055 00056 /********************************************************** 00057 * 00058 * FINITE ALPHABET 00059 * 00060 ***********************************************************/ 00061 00063 00067 class FiniteAlphabet : public Alphabet 00068 { 00069 public: 00071 FiniteAlphabet(): theLetters( 0 ) { } 00073 00077 FiniteAlphabet(int r): theLetters( r ) 00078 { 00079 for (int i=0;i<r;i++){ 00080 stringstream ss; 00081 ss << "x" << i+1 << flush; 00082 theLetters[i] = ss.str(); 00083 } 00084 } 00086 00090 FiniteAlphabet(const vector<string>& letters): theLetters( letters ) { } 00091 00093 int size()const { return theLetters.size(); } 00094 00096 00101 int getNum( const string& letter ) const; 00102 00104 00108 string getLetter( int index ) const; 00109 00111 00114 const vector<string>& getLetters( ) const; 00115 00117 friend ostream& operator << ( ostream& out, const FiniteAlphabet& a ){ 00118 if (a.theLetters.size() == 0) 00119 out << "{ }" << flush; 00120 else { 00121 out << "{ "; 00122 for ( int i=0;i<a.theLetters.size()-1;i++) 00123 out << a.theLetters[i] << ", "; 00124 out << a.theLetters[a.theLetters.size()-1] << " }" << flush; 00125 } 00126 return out; 00127 } 00128 00130 friend istream& operator >> ( istream& in, FiniteAlphabet& a ){ 00131 AParser ap( in ); 00132 ap.parse(); 00133 00134 a = ap.getAlphabet(); 00135 return in; 00136 } 00137 00138 private: 00139 00140 friend class AlphabetFlexLexer; 00141 00142 void addGenerator( const string& g) { theLetters.push_back( g ); } 00143 vector<string> theLetters; 00144 }; 00145 00146 00147 /********************************************************** 00148 * 00149 * INFINITE ALPHABET 00150 * 00151 ***********************************************************/ 00153 class InfiniteAlphabet : public Alphabet 00154 { 00155 public: 00157 00162 InfiniteAlphabet(string pref = string("x")): thePrefix( pref ) { } 00163 00165 00169 int getNum( const string& letter ) const; 00170 00171 00173 00177 string getLetter( int index ) const; 00178 00180 static InfiniteAlphabet defaultAlphabet; 00181 00183 friend ostream& operator << ( ostream& out, const InfiniteAlphabet& a ){ 00184 out << "{ "; 00185 for ( int i=1;i<4;i++) 00186 out << a.thePrefix << i << ", "; 00187 out << "... }" << flush; 00188 00189 return out; 00190 } 00191 00192 private: 00193 string thePrefix; 00194 }; 00195 00196 00197 void readFPPresentation( istream& in ); 00198 00199 00200 #endif 00201 00202