pd_gotogenerator.hGo to the documentation of this file.00001 /** @file pd_gotogenerator.h LR machine */ 00002 00003 /* Pushdown plugin for FAU Discrete Event Systems Library (libfaudes) 00004 00005 Copyright (C) 2013 Stefan Jacobi, Sven Schneider, Anne-Kathrin Hess 00006 00007 */ 00008 00009 00010 #ifndef FAUDES_PD_GOTOGENERATOR_H 00011 #define FAUDES_PD_GOTOGENERATOR_H 00012 00013 #include "corefaudes.h" 00014 #include "pd_gotoattributes.h" 00015 #include "pd_parser.h" 00016 00017 00018 namespace faudes { 00019 00020 /** 00021 * Simple generator for parsing with goto tables 00022 * 00023 * 00024 * 00025 * 00026 * @ingroup PushdownPlugin 00027 * 00028 * @section Overview 00029 * Overview 00030 * 00031 * @section Contents 00032 * Contents 00033 */ 00034 class GotoMachine{ 00035 00036 private: 00037 00038 LrmTransitionMap mTransitions; 00039 Lr1ConfigurationSetSet mStates; 00040 std::set<Lr1Configuration> mInitState; 00041 00042 public: 00043 00044 /** 00045 * Setter for transitions 00046 * 00047 * @param transitions 00048 * the transitions to set 00049 */ 00050 void Transitions(const LrmTransitionMap& transitions){ mTransitions = transitions;} 00051 00052 /** 00053 * Getter for transitions 00054 * 00055 * @return 00056 * the transitions 00057 */ 00058 const LrmTransitionMap& Transitions() const { return mTransitions; } 00059 00060 /** 00061 * Setter for config sets 00062 * 00063 * @param states 00064 * the transitions to set 00065 */ 00066 void States(const Lr1ConfigurationSetSet& states){ mStates = states; } 00067 00068 /** 00069 * Getter for states 00070 * 00071 * @return 00072 * the states 00073 */ 00074 const Lr1ConfigurationSetSet& States() const { return mStates; } 00075 00076 /** 00077 * Setter for initState 00078 * 00079 * @param initState 00080 * the initStates to set 00081 */ 00082 void InitState(const std::set<Lr1Configuration>& initState){ mInitState = initState; } 00083 00084 /** 00085 * Getter for initState 00086 * 00087 * @return 00088 * the initState 00089 */ 00090 const std::set<Lr1Configuration>& InitState() const { return mInitState; } 00091 00092 }; 00093 //end class GotoMachine 00094 00095 /** 00096 * Generator for parsing with goto tables 00097 * 00098 * 00099 * 00100 * @ingroup PushdownPlugin 00101 */ 00102 00103 template <class GlobalAttr, class StateAttr, class EventAttr, class TransAttr> 00104 class TgotoGenerator : public TaGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr> { 00105 00106 public: 00107 /** 00108 * Constructor 00109 */ 00110 TgotoGenerator(void); 00111 00112 /** 00113 * Copy constructor 00114 * 00115 * @param rOtherGen 00116 */ 00117 TgotoGenerator(const TgotoGenerator& rOtherGen); 00118 00119 /** 00120 * Copy constructor (no attributes) 00121 * 00122 * @param rOtherGen 00123 */ 00124 TgotoGenerator(const vGenerator& rOtherGen); 00125 00126 /** 00127 * Assignment operator (uses copy) 00128 * Note: you must reimplement this operator in derived 00129 * classes in order to handle internal pointers correctly 00130 * 00131 * @param rOtherGen 00132 * Other generator 00133 */ 00134 virtual TgotoGenerator& operator= (const TgotoGenerator& rOtherGen) {this->Assign(rOtherGen); return *this;}; 00135 00136 /** 00137 * Assignment operator (uses copy) 00138 * 00139 * @param rOtherGen 00140 * Other generator 00141 */ 00142 virtual TgotoGenerator& operator= (const vGenerator& rOtherGen) {this->Assign(rOtherGen); return *this;}; 00143 00144 /** 00145 * Construct from file 00146 * 00147 * @param rFileName 00148 * Name of file 00149 * 00150 * @exception Exception 00151 * - file format errors (id 1, 50, 51, 52) 00152 */ 00153 TgotoGenerator(const std::string& rFileName); 00154 00155 /** 00156 * Construct on heap. 00157 * Constructs a TgotoGenerator on heap. 00158 * 00159 * @return 00160 * new Generator 00161 */ 00162 TgotoGenerator* New(void) const; 00163 00164 /** 00165 * Construct copy on heap. 00166 * Constructs a TgotoGenerator on heap. 00167 * 00168 * @return 00169 * new Generator 00170 */ 00171 TgotoGenerator* Copy(void) const; 00172 00173 /** 00174 * Type test. 00175 * Uses C++ dynamic cast to test whether the specified object 00176 * casts to a TgotoGenerator. 00177 * 00178 * @return 00179 * TgotoGenerator reference if dynamic cast succeeds, else NULL 00180 */ 00181 virtual const Type* Cast(const Type* pOther) const { 00182 return dynamic_cast< const TgotoGenerator* > (pOther); 00183 }; 00184 00185 00186 /** 00187 * Construct on stack. 00188 * Constructs a TgotoGenerator on stack. 00189 * 00190 * @return 00191 * new Generator 00192 */ 00193 TgotoGenerator NewGotoGen(void) const; 00194 00195 /** 00196 * Add a transition to generator by indices. States 00197 * must already exist! 00198 * 00199 * @param x1 00200 * Predecessor state index 00201 * @param symbol 00202 * grammar symbol used for the transition 00203 * @param x2 00204 * Successor state index 00205 * 00206 * @return 00207 * True, if the transition was new the generator 00208 * 00209 * @exception Exception 00210 * - state or event not in generator (id 95) 00211 */ 00212 bool SetTransition(Idx x1, const GrammarSymbolPtr& symbol, Idx x2); 00213 00214 /** 00215 * Getter for the symbol of a transitions 00216 * 00217 * @param rTrans 00218 * the transition 00219 * @return 00220 * the symbol 00221 */ 00222 GrammarSymbolPtr Symbol(const Transition& rTrans) const; 00223 00224 /** 00225 * Add a transition to generator 00226 * 00227 * @param rTrans 00228 * the transition 00229 * @param symbol 00230 * grammar symbol used for the transitions 00231 * 00232 * @return 00233 * True, if the transition was new in the generator 00234 * 00235 * @exception Exception 00236 * - state or event not in generator (id 95) 00237 */ 00238 bool SetTransition(const Transition& rTrans, const GrammarSymbolPtr& symbol); 00239 00240 /** 00241 * Setter for the configuration set of a state 00242 * 00243 * @param index 00244 * the index of the state 00245 * @param configs 00246 * the configuration set 00247 * 00248 */ 00249 void ConfigSet(Idx index, const std::set<Lr1Configuration>& configs); 00250 00251 /** 00252 * Getter for the configuration set of a state 00253 * 00254 * @param index 00255 * the index of the state 00256 * @return 00257 * the configuration set of the state 00258 */ 00259 std::set<Lr1Configuration> const& ConfigSet(Idx index) const; 00260 00261 /** 00262 * Get first state with the specified configuration set as attribute 00263 * 00264 * @param configs 00265 * the configuration set 00266 * @return 00267 * state index if the state was found, else 0 00268 */ 00269 Idx StateIndex(const std::set<Lr1Configuration>& configs) const; 00270 00271 /** 00272 * Check if generator is valid 00273 * 00274 * @return 00275 * Success 00276 */ 00277 virtual bool Valid(void); 00278 00279 }; //end class TgotoGenerator 00280 00281 /** Convenience typedef for std GotoGenerator */ 00282 typedef TgotoGenerator<AttributeVoid, AttributeGotoState, AttributeVoid, AttributeGotoTransition> 00283 GotoGenerator; 00284 00285 00286 // convenient scope macros 00287 #define THISGOTO TgotoGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr> 00288 #define BASEGOTO TaGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr> 00289 #define TEMPGOTO template <class GlobalAttr, class StateAttr, class EventAttr, class TransAttr> 00290 00291 // TgotoGenerator(void) 00292 TEMPGOTO THISGOTO::TgotoGenerator(void) : BASEGOTO() { 00293 // set basic members (cosmetic) 00294 FD_DG("GotoGenerator(" << this << ")::GotoGenerator()"); 00295 } 00296 00297 // TgotoGenerator(rOtherGen) 00298 TEMPGOTO THISGOTO::TgotoGenerator(const TgotoGenerator& rOtherGen) : BASEGOTO(rOtherGen) { 00299 FD_DG("GotoGenerator(" << this << ")::GotoGenerator(rOtherGen)"); 00300 } 00301 00302 // TgotoGenerator(rOtherGen) 00303 TEMPGOTO THISGOTO::TgotoGenerator(const vGenerator& rOtherGen) : BASEGOTO(rOtherGen) { 00304 // set basic members (cosmetic) 00305 FD_DG("GotoGenerator(" << this << ")::GotoGenerator(rOtherGen)"); 00306 } 00307 00308 // TgotoGenerator(rFilename) 00309 TEMPGOTO THISGOTO::TgotoGenerator(const std::string& rFileName) : BASEGOTO(rFileName) { 00310 FD_DG("GotoGenerator(" << this << ")::GotoGenerator(rOtherGen)"); 00311 } 00312 00313 00314 // New 00315 TEMPGOTO THISGOTO* THISGOTO::New(void) const { 00316 // allocate 00317 THISGOTO* res = new THISGOTO; 00318 // fix base data 00319 res->EventSymbolTablep(BASEGOTO::mpEventSymbolTable); 00320 res->mStateNamesEnabled=BASEGOTO::mStateNamesEnabled; 00321 res->mReindexOnWrite=BASEGOTO::mReindexOnWrite; 00322 // fix my data 00323 return res; 00324 } 00325 00326 // Copy 00327 TEMPGOTO THISGOTO* THISGOTO::Copy(void) const { 00328 // allocate 00329 THISGOTO* res = new THISGOTO(*this); 00330 // done 00331 return res; 00332 } 00333 00334 // NewTGen 00335 TEMPGOTO THISGOTO THISGOTO::NewGotoGen(void) const { 00336 // call base (fixes by assignment constructor) 00337 THISGOTO res= BASEGOTO::NewAGen(); 00338 // fix my data 00339 return res; 00340 } 00341 //SetTransition(rTrans, symbol) 00342 TEMPGOTO bool THISGOTO::SetTransition(const Transition& rTrans, const GrammarSymbolPtr& symbol){ 00343 00344 TransAttr attr; 00345 attr.Symbol(symbol); 00346 return BASEGOTO::SetTransition(rTrans,attr); 00347 } 00348 00349 // SetTransition(x1, symbol, x2) 00350 TEMPGOTO bool THISGOTO::SetTransition(Idx x1, const GrammarSymbolPtr& symbol, Idx x2) { 00351 //events dont matter, so just insert a dummy event 00352 Idx ev = BASEGOTO::InsEvent("dummy"); 00353 return SetTransition(Transition(x1,ev,x2), symbol); 00354 } 00355 //Symbol(rTrans) 00356 TEMPGOTO GrammarSymbolPtr THISGOTO::Symbol(const Transition& rTrans) const{ 00357 return BASEGOTO::pTransRel->Attribute(rTrans).Symbol(); 00358 } 00359 00360 //ConfigSet(index, configs) 00361 TEMPGOTO void THISGOTO::ConfigSet(Idx index, const std::set<Lr1Configuration>& configs){ 00362 BASEGOTO::pStates->Attributep(index)->ConfigSet(configs); 00363 } 00364 00365 //ConfigSet(index) 00366 TEMPGOTO std::set<Lr1Configuration> const& THISGOTO::ConfigSet(Idx index) const{ 00367 return BASEGOTO::pStates->Attributep(index)->ConfigSet(); 00368 } 00369 00370 //StateIdx(configs) 00371 TEMPGOTO Idx THISGOTO::StateIndex(const std::set<Lr1Configuration>& configs) const{ 00372 00373 StateSet::Iterator stateit; 00374 //iterate over all states 00375 for(stateit = BASEGOTO::StatesBegin(); stateit != BASEGOTO::StatesEnd(); stateit++){ 00376 00377 //look for the first state with the correct config set 00378 std::set<Lr1Configuration> curretConfigs = BASEGOTO::pStates->Attributep(*stateit)->ConfigSet(); 00379 00380 //found the set 00381 if(!(CompareConfigSet(configs, curretConfigs) || CompareConfigSet(curretConfigs, configs))){ 00382 return *stateit; 00383 } 00384 } 00385 return 0; 00386 } 00387 00388 00389 // Valid() TODO checks? 00390 TEMPGOTO bool THISGOTO::Valid(void) { 00391 FD_DV("GotoGenerator(" << this << ")::Valid()"); 00392 //call base 00393 if(!BASEGOTO::Valid()) return false; 00394 // check my names 00395 00396 // check my clockset 00397 00398 // check all clocksymboltables 00399 00400 return true; 00401 } 00402 00403 00404 } // namespace faudes 00405 00406 00407 #endif 00408 libFAUDES 2.23h --- 2014.04.03 --- c++ api documentaion by doxygen |