00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_EXP_PARSER_HXX
00019 # define VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_EXP_PARSER_HXX
00020 # include <map>
00021 # include <queue>
00022 # include <set>
00023 # include <vaucanson/algebra/implementation/series/krat_exp_parser.hh>
00024 # include <vaucanson/algebra/implementation/series/krat_exp_proxy.hh>
00025 # include <vaucanson/algebra/concept/monoid_base.hh>
00026
00027
00028 namespace yy
00029 {
00030 struct token_queue;
00031
00032
00033
00034
00035 struct krat_exp_parser
00036 {
00037 krat_exp_parser();
00038 ~krat_exp_parser();
00039 void insert_word(vcsn::algebra::krat_exp_virtual* rexp);
00040 void insert_weight(vcsn::algebra::semiring_virtual* sem);
00041 void insert_zero(vcsn::algebra::krat_exp_virtual* rexp);
00042 void insert_token(int i, std::string* str);
00043 int parse(vcsn::algebra::krat_exp_virtual& rexp, std::string& error);
00044
00045
00046 token_queue* tok_q_;
00047 };
00048 }
00049
00050 namespace vcsn
00051 {
00052 namespace algebra
00053 {
00054
00055 template <class S, class T>
00056 struct Lexer
00057 {
00058
00059 typedef typename Element<S, T>::monoid_elt_t monoid_elt_t;
00060 typedef typename Element<S, T>::semiring_elt_t semiring_elt_t;
00061 typedef typename Element<S, T>::set_t::shared_series_rep_t
00062 shared_series_rep_t;
00063
00064 Lexer(const std::string& from,
00065 Element<S, T>& e,
00066 yy::krat_exp_parser& parser,
00067 bool lex_trace,
00068 std::string& error) :
00069 from_(from),
00070 e_(e),
00071 parser_(parser),
00072 lex_trace_(lex_trace),
00073 close_weight_("}"),
00074
00075 token_tab_(7),
00076 error_(error)
00077 {
00078
00079 const shared_series_rep_t& rep = e.structure().representation();
00080
00081 token_tab_[0] = rep->open_par;
00082 token_tab_[1] = rep->close_par;
00083 token_tab_[2] = rep->plus;
00084 token_tab_[3] = rep->times;
00085 token_tab_[4] = rep->star;
00086 token_tab_[5] = rep->zero;
00087 token_tab_[6] = rep->open_weight;
00088 close_weight_ = rep->close_weight;
00089
00090
00091 token_tab_.insert(token_tab_.end(), rep->spaces.begin(), rep->spaces.end());
00092
00093 std::string::const_iterator sit;
00094 semiring_elt_t ww(e_.structure().semiring());
00095 sit = close_weight_.begin();
00096 if (parse_weight(ww, close_weight_, sit))
00097 error_ += "Warning : the token '" + close_weight_ +
00098 + "' is already defined as a weight.\n";
00099 sit = token_tab_[7].begin();
00100 if (parse_weight(ww, token_tab_[7], sit))
00101 error_ += "Warning : the token '" + token_tab_[7]
00102 + "' is already defined as a weight.\n";
00103 for (unsigned i = 0; i < token_tab_.size(); i++)
00104 {
00105 monoid_elt_t w(e_.structure().monoid());
00106 if (parse_word(w, token_tab_[i]).first)
00107 error_ += "Warning : the token '" + token_tab_[i]
00108 + "' is already defined as a word.\n";
00109 }
00110
00111 }
00112
00113 bool
00114 lex()
00115 {
00116 size_t size = from_.size();
00117 size_t it = 0;
00118 while (it < size)
00119 {
00120
00121 monoid_elt_t w(e_.structure().monoid());
00122 std::pair<bool, int> res = parse_word(w, from_.substr(it));
00123 if (res.second > 0)
00124 {
00125 insert_word(w);
00126
00127 if (res.first)
00128 {
00129 assertion(it + res.second == size);
00130 break;
00131 }
00132 it += res.second;
00133 }
00134
00135
00136 size_t i;
00137 for (i = 0; i < token_tab_.size(); i++)
00138 {
00139 if (!from_.compare(it, token_tab_[i].size(), token_tab_[i]))
00140 {
00141 if (i == 6)
00142 {
00143 if (!insert_weight(it))
00144 return false;
00145 }
00146 else
00147 {
00148 if (i < 6)
00149 insert_token(i);
00150 it += token_tab_[i].size();
00151 }
00152 break;
00153 }
00154 }
00155 if (i >= token_tab_.size())
00156 {
00157 error_ += "Lexer error, unrecognized characters: "
00158 + from_.substr(it) + "\n";
00159 return false;
00160 }
00161 }
00162 return true;
00163 }
00164
00165 private:
00166 void
00167 insert_word(monoid_elt_t& w)
00168 {
00169 Element<S, T> ww = (w.value().empty() ?
00170 identity_as<T>::of(e_.structure()) :
00171 Element<S, T>(e_.structure(), w.value()));
00172
00173 krat_exp_proxy<S, T>* rexp = new krat_exp_proxy<S, T>(ww);
00174 parser_.insert_word(rexp);
00175 }
00176
00177 bool
00178 insert_weight(size_t& it)
00179 {
00180 it += token_tab_[7].size();
00181 size_t bg = it;
00182 size_t size = from_.size();
00183 unsigned cpt = 1;
00184 for (; it < size; it++)
00185 {
00186 if (!from_.compare(it, token_tab_[7].size(), token_tab_[7]))
00187 cpt++;
00188 else
00189 if (!from_.compare(it, close_weight_.size(), close_weight_))
00190 {
00191 if (cpt == 1)
00192 {
00193 semiring_elt_t w(e_.structure().semiring());
00194 std::string s = from_.substr(bg, it - bg);
00195 std::string::const_iterator sit = s.begin();
00196 if (parse_weight(w, s, sit))
00197 {
00198 semiring_proxy<S, T>* sem = new semiring_proxy<S, T>(w);
00199 parser_.insert_weight(sem);
00200 }
00201 else
00202 {
00203 error_ += "Lexer error : " + s + " is not a weight\n";
00204 return false;
00205 }
00206 it += close_weight_.size();
00207 return true;
00208 }
00209 else
00210 cpt--;
00211 }
00212 }
00213 error_ += "Lexer error : Expected " + close_weight_
00214 + "instead of END\n";
00215 return false;
00216 }
00217
00218 void
00219 insert_token(int i)
00220 {
00221 if (i == 5)
00222 {
00223 Element<S, T> w = zero_as<T>::of(e_.structure());
00224 krat_exp_proxy<S, T>* rexp = new krat_exp_proxy<S, T>(w);
00225 parser_.insert_zero(rexp);
00226 }
00227 else
00228 {
00229 std::string* str = new std::string(token_tab_[i]);
00230 parser_.insert_token(i, str);
00231 }
00232 }
00233
00234 const std::string& from_;
00235 Element<S, T>& e_;
00236 yy::krat_exp_parser& parser_;
00237 bool lex_trace_;
00238 std::string close_weight_;
00239 std::vector<std::string> token_tab_;
00240 std::string& error_;
00241 };
00242
00243 template <class S, class T>
00244 std::pair<bool, std::string>
00245 parse(const std::string& from,
00246 Element<S, T>& exp,
00247 bool lex_trace = false,
00248 bool parse_trace = false)
00249 {
00250 parse_trace = parse_trace;
00251 std::string error;
00252 yy::krat_exp_parser parser;
00253 Lexer<S, T> lex(from, exp, parser, lex_trace, error);
00254 if (!lex.lex())
00255 return std::make_pair(true, error);
00256 krat_exp_proxy<S, T> rexp(exp);
00257 if (parser.parse(rexp, error))
00258 return std::make_pair(true, error);
00259 exp = rexp.self;
00260 return std::make_pair(false, error);
00261 }
00262
00263 }
00264
00265 }
00266
00267 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_EXP_PARSER_HXX