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

libFAUDES 2.26g --- 2015.08.17 --- c++ api documentaion by doxygen