pd_gotogenerator.h
Go to the documentation of this file.
1 /** @file pd_gotogenerator.h LR machine */
2 
3 /* Pushdown plugin for FAU Discrete Event Systems Library (libfaudes)
4 
5  Copyright (C) 2013 Stefan Jacobi, Sven Schneider, Anne-Kathrin Hess
6 
7 */
8 
9 
10 #ifndef FAUDES_PD_GOTOGENERATOR_H
11 #define FAUDES_PD_GOTOGENERATOR_H
12 
13 #include "corefaudes.h"
14 #include "pd_gotoattributes.h"
15 #include "pd_parser.h"
16 
17 
18 namespace faudes {
19 
20 /**
21  * Simple generator for parsing with goto tables
22 *
23 *
24 *
25 *
26 * @ingroup PushdownPlugin
27 *
28 * @section Overview
29 * Overview
30 *
31 * @section Contents
32 * Contents
33 */
35 
36 private:
37 
40  std::set<Lr1Configuration> mInitState;
41 
42 public:
43 
44  /**
45  * Setter for transitions
46  *
47  * @param transitions
48  * the transitions to set
49  */
50  void Transitions(const LrmTransitionMap& transitions){ mTransitions = transitions;}
51 
52  /**
53  * Getter for transitions
54  *
55  * @return
56  * the transitions
57  */
58  const LrmTransitionMap& Transitions() const { return mTransitions; }
59 
60  /**
61  * Setter for config sets
62  *
63  * @param states
64  * the transitions to set
65  */
66  void States(const Lr1ConfigurationSetSet& states){ mStates = states; }
67 
68  /**
69  * Getter for states
70  *
71  * @return
72  * the states
73  */
74  const Lr1ConfigurationSetSet& States() const { return mStates; }
75 
76  /**
77  * Setter for initState
78  *
79  * @param initState
80  * the initStates to set
81  */
82  void InitState(const std::set<Lr1Configuration>& initState){ mInitState = initState; }
83 
84  /**
85  * Getter for initState
86  *
87  * @return
88  * the initState
89  */
90  const std::set<Lr1Configuration>& InitState() const { return mInitState; }
91 
92 };
93 //end class GotoMachine
94 
95 /**
96  * Generator for parsing with goto tables
97  *
98  *
99  *
100  * @ingroup PushdownPlugin
101  */
102 
103 template <class GlobalAttr, class StateAttr, class EventAttr, class TransAttr>
104 class FAUDES_API TgotoGenerator : public TaGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr> {
105 
106 public:
107  /**
108  * Constructor
109  */
110  TgotoGenerator(void);
111 
112  /**
113  * Copy constructor
114  *
115  * @param rOtherGen
116  */
117  TgotoGenerator(const TgotoGenerator& rOtherGen);
118 
119  /**
120  * Copy constructor (no attributes)
121  *
122  * @param rOtherGen
123  */
124  TgotoGenerator(const vGenerator& rOtherGen);
125 
126  /**
127  * Assignment operator (uses copy)
128  *
129  * Note: you must reimplement this operator in derived
130  * classes in order to handle internal pointers correctly
131  *
132  * @param rOtherGen
133  * Other generator
134  */
135  virtual TgotoGenerator& operator= (const TgotoGenerator& rOtherGen) {this->Assign(rOtherGen); return *this;};
136 
137  /* Assignment method
138  *
139  * Note: you must reimplement this method in derived
140  * classes in order to handle internal pointers correctly
141  *
142  * @param rOtherGen
143  * Other generator
144  */
145  virtual TgotoGenerator& Assign(const Type& rSource);
146 
147 
148  /**
149  * Construct from file
150  *
151  * @param rFileName
152  * Name of file
153  *
154  * @exception Exception
155  * - file format errors (id 1, 50, 51, 52)
156  */
157  TgotoGenerator(const std::string& rFileName);
158 
159  /**
160  * Construct on heap.
161  * Constructs a TgotoGenerator on heap.
162  *
163  * @return
164  * new Generator
165  */
166  TgotoGenerator* New(void) const;
167 
168  /**
169  * Construct copy on heap.
170  * Constructs a TgotoGenerator on heap.
171  *
172  * @return
173  * new Generator
174  */
175  TgotoGenerator* Copy(void) const;
176 
177  /**
178  * Type test.
179  * Uses C++ dynamic cast to test whether the specified object
180  * casts to a TgotoGenerator.
181  *
182  * @return
183  * TgotoGenerator reference if dynamic cast succeeds, else NULL
184  */
185  virtual const Type* Cast(const Type* pOther) const {
186  return dynamic_cast< const TgotoGenerator* > (pOther);
187  };
188 
189 
190  /**
191  * Construct on stack.
192  * Constructs a TgotoGenerator on stack.
193  *
194  * @return
195  * new Generator
196  */
197  TgotoGenerator NewGotoGen(void) const;
198 
199  /**
200  * Add a transition to generator by indices. States
201  * must already exist!
202  *
203  * @param x1
204  * Predecessor state index
205  * @param symbol
206  * grammar symbol used for the transition
207  * @param x2
208  * Successor state index
209  *
210  * @return
211  * True, if the transition was new the generator
212  *
213  * @exception Exception
214  * - state or event not in generator (id 95)
215  */
216  bool SetTransition(Idx x1, const GrammarSymbolPtr& symbol, Idx x2);
217 
218  /**
219  * Getter for the symbol of a transitions
220  *
221  * @param rTrans
222  * the transition
223  * @return
224  * the symbol
225  */
226  GrammarSymbolPtr Symbol(const Transition& rTrans) const;
227 
228  /**
229  * Add a transition to generator
230  *
231  * @param rTrans
232  * the transition
233  * @param symbol
234  * grammar symbol used for the transitions
235  *
236  * @return
237  * True, if the transition was new in the generator
238  *
239  * @exception Exception
240  * - state or event not in generator (id 95)
241  */
242  bool SetTransition(const Transition& rTrans, const GrammarSymbolPtr& symbol);
243 
244  /**
245  * Setter for the configuration set of a state
246  *
247  * @param index
248  * the index of the state
249  * @param configs
250  * the configuration set
251  *
252  */
253  void ConfigSet(Idx index, const std::set<Lr1Configuration>& configs);
254 
255  /**
256  * Getter for the configuration set of a state
257  *
258  * @param index
259  * the index of the state
260  * @return
261  * the configuration set of the state
262  */
263  std::set<Lr1Configuration> const& ConfigSet(Idx index) const;
264 
265  /**
266  * Get first state with the specified configuration set as attribute
267  *
268  * @param configs
269  * the configuration set
270  * @return
271  * state index if the state was found, else 0
272  */
273  Idx StateIndex(const std::set<Lr1Configuration>& configs) const;
274 
275  /**
276  * Check if generator is valid
277  *
278  * @return
279  * Success
280  */
281  virtual bool Valid(void) const;
282 
283 }; //end class TgotoGenerator
284 
285 /** Convenience typedef for std GotoGenerator */
286 typedef TgotoGenerator<AttributeVoid, AttributeGotoState, AttributeVoid, AttributeGotoTransition>
288 
289 
290 // convenient scope macros
291 #define THISGOTO TgotoGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
292 #define BASEGOTO TaGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
293 #define TEMPGOTO template <class GlobalAttr, class StateAttr, class EventAttr, class TransAttr>
294 
295 // TgotoGenerator(void)
296 TEMPGOTO THISGOTO::TgotoGenerator(void) : BASEGOTO() {
297  // set basic members (cosmetic)
298  FD_DG("GotoGenerator(" << this << ")::GotoGenerator()");
299 }
300 
301 // TgotoGenerator(rOtherGen)
302 TEMPGOTO THISGOTO::TgotoGenerator(const TgotoGenerator& rOtherGen) : BASEGOTO(rOtherGen) {
303  FD_DG("GotoGenerator(" << this << ")::GotoGenerator(rOtherGotoGen)");
304 }
305 
306 // TgotoGenerator(rOtherGen)
307 TEMPGOTO THISGOTO::TgotoGenerator(const vGenerator& rOtherGen) : BASEGOTO(rOtherGen) {
308  // set basic members (cosmetic)
309  FD_DG("GotoGenerator(" << this << ")::GotoGenerator(rOtherGen)");
310 }
311 
312 // TgotoGenerator(rFilename)
313 TEMPGOTO THISGOTO::TgotoGenerator(const std::string& rFileName) : BASEGOTO(rFileName) {
314  FD_DG("GotoGenerator(" << this << ")::GotoGenerator(file)");
315 }
316 
317 
318 // copy from other faudes type
319 TEMPGOTO THISGOTO& THISGOTO::Assign(const Type& rSrc) {
320  FD_DG("TgotoGenerator(" << this << ")::Assign([type] " << &rSrc << ")");
321  // bail out on match
322  if(&rSrc==static_cast<const Type*>(this)) return *this;
323  // pass on to base
324  BASEGOTO::Assign(rSrc);
325  return *this;
326 }
327 
328 // New
329 TEMPGOTO THISGOTO* THISGOTO::New(void) const {
330  // allocate
331  THISGOTO* res = new THISGOTO;
332  // fix base data
333  res->EventSymbolTablep(BASEGOTO::mpEventSymbolTable);
334  res->mStateNamesEnabled=BASEGOTO::mStateNamesEnabled;
335  res->mReindexOnWrite=BASEGOTO::mReindexOnWrite;
336  // fix my data
337  return res;
338 }
339 
340 // Copy
341 TEMPGOTO THISGOTO* THISGOTO::Copy(void) const {
342  // allocate
343  THISGOTO* res = new THISGOTO(*this);
344  // done
345  return res;
346 }
347 
348 // NewTGen
349 TEMPGOTO THISGOTO THISGOTO::NewGotoGen(void) const {
350  // call base (fixes by assignment constructor)
351  THISGOTO res= BASEGOTO::NewAGen();
352  // fix my data
353  return res;
354 }
355 //SetTransition(rTrans, symbol)
356 TEMPGOTO bool THISGOTO::SetTransition(const Transition& rTrans, const GrammarSymbolPtr& symbol){
357  TransAttr attr;
358  attr.Symbol(symbol);
359  return BASEGOTO::SetTransition(rTrans,attr);
360 }
361 
362 // SetTransition(x1, symbol, x2)
363 TEMPGOTO bool THISGOTO::SetTransition(Idx x1, const GrammarSymbolPtr& symbol, Idx x2) {
364  //events dont matter, so just insert a dummy event
365  Idx ev = BASEGOTO::InsEvent("dummy");
366  return SetTransition(Transition(x1,ev,x2), symbol);
367 }
368 //Symbol(rTrans)
369 TEMPGOTO GrammarSymbolPtr THISGOTO::Symbol(const Transition& rTrans) const{
370  return BASEGOTO::pTransRel->Attribute(rTrans).Symbol();
371 }
372 
373 //ConfigSet(index, configs)
374 TEMPGOTO void THISGOTO::ConfigSet(Idx index, const std::set<Lr1Configuration>& configs){
375  BASEGOTO::pStates->Attributep(index)->ConfigSet(configs);
376 }
377 
378 //ConfigSet(index)
379 TEMPGOTO std::set<Lr1Configuration> const& THISGOTO::ConfigSet(Idx index) const{
380  return BASEGOTO::pStates->Attributep(index)->ConfigSet();
381 }
382 
383 //StateIdx(configs)
384 TEMPGOTO Idx THISGOTO::StateIndex(const std::set<Lr1Configuration>& configs) const{
385 
386  StateSet::Iterator stateit;
387  //iterate over all states
388  for(stateit = BASEGOTO::StatesBegin(); stateit != BASEGOTO::StatesEnd(); stateit++){
389 
390  //look for the first state with the correct config set
391  std::set<Lr1Configuration> curretConfigs = BASEGOTO::pStates->Attributep(*stateit)->ConfigSet();
392 
393  //found the set
394  if(!(CompareConfigSet(configs, curretConfigs) || CompareConfigSet(curretConfigs, configs))){
395  return *stateit;
396  }
397  }
398  return 0;
399 }
400 
401 
402 // Valid() TODO checks?
403 TEMPGOTO bool THISGOTO::Valid(void) const {
404  FD_DV("GotoGenerator(" << this << ")::Valid()");
405  //call base
406  if(!BASEGOTO::Valid()) return false;
407  // check my names
408 
409  // check my clockset
410 
411  // check all clocksymboltables
412 
413  return true;
414 }
415 
416 
417 } // namespace faudes
418 
419 
420 #endif
421 

libFAUDES 2.28c --- 2016.09.30 --- c++ api documentaion by doxygen