00001 #ifndef FSM_H
00002 #define FSM_H
00003
00004
00005 #include <iostream>
00006 #include <string>
00007 #include <set>
00008 #include <map>
00009 #include <stdexcept>
00010
00011 #include "ref.h"
00012
00013
00015
00024 class Fsm {
00025 public:
00027
00034 typedef string State;
00036
00039 typedef set<State> STATES;
00041
00048 typedef string Symbol;
00050 typedef set<Symbol> SYMBOLS;
00052
00065 typedef map<pair<State,Symbol>,STATES > TRANSITIONS;
00066
00068
00071 static const Symbol& epsilon();
00072
00074
00078 static ref<Fsm> create(const State& initial_state);
00080 virtual ~Fsm();
00081
00083
00086 Fsm& add_state(const State& s);
00088
00091 Fsm& add_symbol(const Symbol& a);
00093
00095 Fsm& add_final_state(const State& s);
00097
00103 Fsm& add_transition(const State& q1,const Symbol& a, const State& q2);
00104
00106 const State& initial_state() const { return initial_state_; }
00108 const STATES& final_states() const { return final_states_; }
00110 const STATES& states() const { return states_; }
00111
00113 bool is_final(const State& s) const;
00114
00116
00125 const STATES& delta(const State&, const Symbol&) const;
00127
00132 const State& ddelta(const State&, const Symbol&) const
00133 throw(runtime_error);
00134
00136
00139 bool is_deterministic() const;
00140
00142 TRANSITIONS::const_iterator begin() const { return transitions_.begin(); }
00144 TRANSITIONS::const_iterator end() const { return transitions_.end(); }
00145
00147 STATES::const_iterator sbegin() const { return states_.begin(); }
00149 STATES::const_iterator send() const { return states_.end(); }
00150
00152 STATES::const_iterator fbegin() const { return final_states_.begin(); }
00154 STATES::const_iterator fend() const { return final_states_.end(); }
00155
00157 SYMBOLS::const_iterator abegin() const { return alphabet_.begin(); }
00159 SYMBOLS::const_iterator aend() const { return alphabet_.end(); }
00160
00162 friend ostream& operator<<(ostream& os, const Fsm& fsm);
00163
00164 protected:
00166
00174 Fsm(const State& initial_state);
00175
00176 private:
00178 State initial_state_;
00180 STATES final_states_;
00182 STATES states_;
00184 SYMBOLS alphabet_;
00185
00187
00192 TRANSITIONS transitions_;
00193
00194 };
00195
00197 ostream&
00198 operator<<(ostream&, const Fsm::STATES& Q);
00199 #endif