pd_grammar.h
Go to the documentation of this file.
1 /** @file pd_merge.h grammar data structure */
2 
3 
4 /* Pushdown plugin for FAU Discrete Event Systems Library (libfaudes)
5 
6  Copyright (C) 2013 Stefan Jacobi, Sven Schneider, Anne-Kathrin Hess
7 
8 */
9 
10 
11 #ifndef FAUDES_PD_GRAMMAR_H
12 #define FAUDES_PD_GRAMMAR_H
13 
14 #include "corefaudes.h"
15 #include "pd_basics.h"
16 #include "pd_pdgenerator.h"
17 
18 #ifdef FAUDES_PD_TR1
19 #include <tr1/memory>
20 namespace std {
21 using std::tr1::shared_ptr;
22 using std::tr1::dynamic_pointer_cast;
23 }
24 #else
25 #include <memory>
26 #endif
27 
28 namespace faudes {
29 
30 /**
31 * Grammar Symbol
32 *
33 *
34 *
35 * @ingroup PushdownPlugin
36 *
37 * @section Overview
38 * Overview
39 *
40 * @section Contents
41 * Contents
42 */
44 
45  public:
46 
47  /**
48  * To String function
49  */
50  virtual std::string Str() const = 0;
51 
52  /**
53  * < operator
54  * @param other
55  * cmp
56  * @return
57  * true if this < cmp, else false
58  */
59  virtual bool operator<(const GrammarSymbol& other) const = 0;
60 
61  /**
62  * == operator
63  * @param other
64  * cmp
65  * @return
66  * true if this == cmp, else false
67  */
68  bool operator==(const GrammarSymbol& other) const;
69 
70  /**
71  * != operator
72  * @param other
73  * cmp
74  * @return
75  * true if this != cmp, else false
76  */
77  bool operator!=(const GrammarSymbol& other) const;
78 
79  /**
80  * Clone function. Allocates memory!
81  *
82  * @return
83  * pointer to cloned object
84  */
85  virtual GrammarSymbol* Clone() const = 0;
86 
87 };//class GrammarSymbol
88 
89 /**
90 * Terminal
91 *
92 *
93 *
94 * @ingroup PushdownPlugin
95 *
96 * @section Overview
97 * Overview
98 *
99 * @section Contents
100 * Contents
101 */
103 
104  public:
105 
106  /** Symbol representing the terminals */
107  //std::string mSymbol;
108  /** Event representing the terminal */
110 
111 
112  /**
113  * Constructor
114  *
115  * @param event
116  * the event
117  */
118  Terminal(const Idx event) : mEvent(event){};
119 
120  /**
121  * Getter for event
122  *
123  * @return
124  * the event
125  */
126  Idx Event() const{return mEvent;}
127 
128  /**
129  * Determine if the event is lambda
130  *
131  * @return
132  * true if it is lambda, else false
133  */
134  bool IsLambda() const;
135 
136  /**
137  * To String function
138  */
139  std::string Str() const;
140 
141  /**
142  * < operator
143  * @param other
144  * cmp
145  * @return
146  * true if this < cmp, else false
147  */
148  bool operator<(const GrammarSymbol& other) const;
149 
150  /**
151  * Clone function. Allocates memory!
152  *
153  * @return
154  * pointer to cloned Terminal
155  */
156  Terminal* Clone() const;
157 
158 };//class Terminal
159 
160 /**
161  * Nonterminals are constructed from two states and a
162  * stack symbol or one state and a stack symbol
163 *
164 *
165 *
166 * @ingroup PushdownPlugin
167 *
168 * @section Overview
169 * Overview
170 *
171 * @section Contents
172 * Contents
173 */
175 
176  public:
177 
178  /** The start state*/
180  /** The stack symbols*/
181  std::vector<Idx> mOnStack;
182  /** The end state */
184 
185 
186  /**
187  * Constructor for NonterminalEnd
188  */
189  Nonterminal(const Idx state, const std::vector<Idx> symbols):mStartState(state), mOnStack(symbols), mEndState(0){}
190 
191  /**
192  * Constructor for NonterminalMid
193  */
194  Nonterminal(const Idx state1, const std::vector<Idx> symbols, const Idx state2) : mStartState(state1), mOnStack(symbols), mEndState(state2){}
195 
196  /**
197  * Getter for startState
198  *
199  * @return
200  * Idx of the startState
201  */
202  Idx StartState() const{return mStartState;}
203 
204  /**
205  * Getter for onStack
206  *
207  * @return
208  * indices of the stack symbols
209  */
210  const std::vector<Idx>& OnStack() const {return mOnStack;}
211 
212  /**
213  * Getter for endState
214  *
215  * @return
216  * Idx of the endState
217  */
218  Idx EndState() const {return mEndState;}
219 
220  /**
221  * To String function
222  */
223  virtual std::string Str() const;
224 
225  /**
226  * < operator
227  * @param other
228  * cmp
229  * @return
230  * true if this < cmp, else false
231  */
232  bool operator<(const GrammarSymbol& other) const;
233 
234  /**
235  * Clone function. Allocates memory!
236  *
237  * @return
238  * pointer to cloned nonterminal
239  */
240  Nonterminal* Clone() const;
241 
242 };//class Nonterminal
243 
244 //convenience definition for a grammar symbol pointer that manages its own memory
245 typedef std::shared_ptr<GrammarSymbol> GrammarSymbolPtr;
246 typedef std::shared_ptr<const GrammarSymbol> ConstGrammarSymbolPtr;
247 
248 //convenience definition for a nonterminal pointer that manages its own memory
249 typedef std::shared_ptr<Nonterminal> NonterminalPtr;
250 typedef std::shared_ptr<const Nonterminal> ConstNonterminalPtr;
251 
252 //convenience definition for a terminal pointer that manages its own memory
253 typedef std::shared_ptr<Terminal> TerminalPtr;
254 typedef std::shared_ptr<const Terminal> ConstTerminalPtr;
255 
256 //convencience definition for a vector of grammar symbols
257 typedef std::vector<GrammarSymbolPtr> GrammarSymbolVector;
258 
259 //comparator for two grammar symbol vectors
260 extern FAUDES_API bool CompareGsVector(const GrammarSymbolVector& lhs, const GrammarSymbolVector& rhs);
261 
263  bool operator() (const GrammarSymbolVector& lhs, const GrammarSymbolVector& rhs) const{
264  return CompareGsVector(lhs,rhs);
265  }
266 };
267 
268 //equals for two grammar symbol vectors
269 extern FAUDES_API bool EqualsGsVector(const GrammarSymbolVector& lhs, const GrammarSymbolVector& rhs);
270 
271 //comparator for two grammar symbols
272 extern FAUDES_API bool CompareGs(const GrammarSymbolPtr& lhs, const GrammarSymbolPtr& rhs);
273 
274 struct GsComparator {
275  bool operator() (const GrammarSymbolPtr& lhs, const GrammarSymbolPtr& rhs) const{
276  return CompareGs(lhs,rhs);
277  }
278 };
279 
280 //convencience definition for a set of grammar symbols
281 typedef std::set<GrammarSymbolPtr, GsComparator> GrammarSymbolSet;
282 
283 //convencience definition for a set of vectors of grammar symbols (a set of words)
284 typedef std::set<GrammarSymbolVector,GsVectorComparator> GrammarSymbolWordSet;
285 
286 typedef std::map<GrammarSymbolVector, std::set<Terminal>, GsVectorComparator> GrammarSymbolWordMap;
287 
288 /**
289 * Test if a given set of words contains a specific word
290 *
291 * @param set
292 * the set to be searched
293 * @param word
294 * the word to be searched for
295 * @return
296 * true, if the set contains the word, else false
297 */
298 extern FAUDES_API bool ContainsWord(const GrammarSymbolWordSet& set, const GrammarSymbolVector& word);
299 
300 /**
301 * Grammar Production
302 *
303 *
304 *
305 * @ingroup PushdownPlugin
306 *
307 * @section Overview
308 * Overview
309 *
310 * @section Contents
311 * Contents
312 */
314 
315  public:
316 
317  /** Nonterminal left hand side */
319  /** Right hand side, can contain both Terminals and Nonterminals */
321 
322  /**
323  * Constructor
324  */
325  GrammarProduction(const Nonterminal& lhs, const GrammarSymbolVector& rhs) : mLhs(lhs), mRhs(rhs){};
326 
327  /**
328  * Getter for mLhs
329  *
330  * @return
331  * mLhs
332  */
333  Nonterminal const& Lhs() const {return mLhs;}
334 
335  /**
336  * Getter for mRhs
337  *
338  * @return
339  * mRhs
340  */
341  GrammarSymbolVector const& Rhs() const {return mRhs;}
342 
343  /**
344  * To String function
345  */
346  std::string Str() const;
347 
348  /**
349  * < operator
350  * @param other
351  * cmp
352  * @return
353  * true if this < cmp, else false
354  */
355  bool operator<(const GrammarProduction& other) const;
356 
357 };//class GrammarProduction
358 
359 /**
360 * Grammar
361 *
362 *
363 *
364 * @ingroup PushdownPlugin
365 *
366 * @section Overview
367 * Overview
368 *
369 * @section Contents
370 * Contents
371 */
373 
374  public:
375 
376  /** the Terminals */
377  std::set<Terminal> mTerminals;
378  /** the Nonterminals */
379  std::set<Nonterminal> mNonterminals;
380  /** the Start Symbol */
382  /** the Productions */
383  std::set<GrammarProduction> mGrammarProductions;
384 
385  /**
386  * Constructor
387  */
388  Grammar():mStartSymbol(Nonterminal(0,std::vector<Idx>(),0)) {}
389 
390  /**
391  * Constructor
392  */
393  Grammar(const Nonterminal& startSymbol):mStartSymbol(startSymbol){}
394 
395 
396  /**
397  * set the grammar's start symbol and add it to the set of nonterminals
398  *
399  * @param s
400  * start symbol to set
401  * @return
402  * true, if nonterminal did not exist in grammar, else false
403  */
404  bool SetStartSymbol(const Nonterminal& s);
405 
406  /**
407  * add Terminal
408  *
409  * @param t
410  * terminal to add
411  * @return
412  * true, if successful (terminal did not exist in grammar), else
413  * false (terminal did already exist in grammar)
414  */
415  bool InsTerminal(const Terminal& t);
416 
417  /**
418  * add Terminals
419  *
420  * @param t
421  * terminals to add
422  */
423  void InsTerminals(const std::set<Terminal>& t);
424 
425  /**
426  * add Nonterminal
427  *
428  * @param nt
429  * nonterminal to add
430  * @return
431  * true, if successful (nonterminal did not exist in grammar), else
432  * false (nonterminal did already exist in grammar)
433  */
434  bool InsNonterminal(const Nonterminal& nt);
435 
436  /**
437  * add Nonterminals
438  *
439  * @param nt
440  * nonterminals to add
441  */
442  void InsNonterminals(const std::set<Nonterminal>& nt);
443 
444  /**
445  * Add a grammar production to the grammar. All grammar symbols used must exist
446  * in the grammar.
447  *
448  * @param gp
449  * grammar production to add
450  * @return
451  * true, if successful (grammar production did not exist in grammar), else
452  * false (grammar production did already exist in grammar)
453  */
454  bool InsGrammarProduction(const GrammarProduction& gp);
455 
456  /**
457  * Add grammar productions to the grammar. All grammar symbols used must exist
458  * in the grammar.
459  *
460  * @param gp
461  * grammar productions to add
462  */
463  void InsGrammarProductions(const std::set<GrammarProduction>& gp);
464 
465  /**
466  * Getter for mTerminals
467  *
468  * @return
469  * the terminals
470  */
471  const std::set<Terminal>& Terminals() const { return mTerminals;}
472 
473  /**
474  * Getter for mNonterminals
475  *
476  * @return
477  * the nonterminals
478  */
479  const std::set<Nonterminal>& Nonterminals() const { return mNonterminals;}
480 
481  /**
482  * Getter for mStartSymbol
483  *
484  * @return
485  * the start symbol
486  */
487  const Nonterminal& StartSymbol() const { return mStartSymbol;}
488 
489  /**
490  * Getter for mGrammarProductions
491  *
492  * @return
493  * the grammar productions
494  */
495  const std::set<GrammarProduction>& GrammarProductions() const { return mGrammarProductions;}
496 
497  /**
498  * Iterator to the beginning of terminals
499  *
500  * @return
501  * iterator
502  */
503  std::set<Terminal>::const_iterator TerminalsBegin() const;
504 
505  /**
506  * Iterator to the end of terminals
507  *
508  * @return
509  * iterator
510  */
511  std::set<Terminal>::const_iterator TerminalsEnd() const;
512 
513  /**
514  * Iterator to the beginning of nonterminals
515  *
516  * @return
517  * iterator
518  */
519  std::set<Nonterminal>::const_iterator NonterminalsBegin() const;
520 
521  /**
522  * Iterator to the end of nonterminals
523  *
524  * @return
525  * iterator
526  */
527  std::set<Nonterminal>::const_iterator NonterminalsEnd() const;
528 
529  /**
530  * Iterator to the beginning of grammar productions
531  *
532  * @return
533  * iterator
534  */
535  std::set<GrammarProduction>::const_iterator GrammarProductionsBegin() const;
536 
537  /**
538  * Iterator to the end of grammar productions
539  *
540  * @return
541  * iterator
542  */
543  std::set<GrammarProduction>::const_iterator GrammarProductionsEnd() const;
544 
545  /**
546  * To String function for Terminals
547  */
548  std::string StrTerminals() const;
549 
550  /**
551  * To String function for Nonterminals
552  */
553  std::string StrNonterminals() const;
554 
555  /**
556  * To String function for start symbol
557  */
558  std::string StrStartSymbol() const;
559 
560  /**
561  * To String function for Grammar Productions
562  */
563  std::string StrGrammarProductions() const;
564 
565  /**
566  * To String function
567  */
568  std::string Str() const;
569 
570 
571 };//class Grammar
572 
573 } // namespace faudes
574 
575 #endif

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