pd_parser.h
Go to the documentation of this file.
1 /** @file pd_parser.h parser 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_PARSER_H
12 #define FAUDES_PD_PARSER_H
13 
14 #include "corefaudes.h"
15 #include "pd_grammar.h"
16 
17 namespace faudes {
18 
19 /**
20 * Lr1 Configuration
21 *
22 *
23 *
24 * @ingroup PushdownPlugin
25 *
26 * @section Overview
27 * Overview
28 *
29 * @section Contents
30 * Contents
31 */
32 
34 
35  public:
36 
41 
42  /**
43  * Constructor TODO make sure that vectors are not empty
44  *
45  * @param lhs
46  * lefthand side nonterminal
47  * @param beforeDot
48  * before dot grammar symbols
49  * @param afterDot
50  * after dot grammar symbols
51  * @param lookahead
52  * lookahead terminal
53  */
54  Lr1Configuration (const Nonterminal& lhs, const GrammarSymbolVector& beforeDot, const GrammarSymbolVector& afterDot, const Terminal& lookahead);
55 
56  /**
57  * Getter for mLhs
58  *
59  * @return
60  * mLhs
61  */
62  Nonterminal const& Lhs() const { return mLhs; }
63 
64  /**
65  * Getter for mBeforeDot
66  *
67  * @return
68  * mBeforeDot
69  */
70  GrammarSymbolVector const& BeforeDot() const { return mBeforeDot; }
71 
72  /**
73  * Getter for mAfterDot
74  *
75  * @return
76  * mAfterDot
77  */
78  GrammarSymbolVector const& AfterDot() const { return mAfterDot; }
79 
80  /**
81  * Getter for mLookahead
82  *
83  * @return
84  * mLookahead
85  */
86  Terminal const& Lookahead() const { return mLookahead; }
87 
88  /**
89  * < operator
90  * @param other
91  * cmp
92  * @return
93  * true if this < cmp, else false
94  */
95  bool operator<(const Lr1Configuration& other) const;
96 
97  /**
98  * == operator
99  * @param other
100  * cmp
101  * @return
102  * true if this == cmp, else false
103  */
104  bool operator==(const Lr1Configuration& other) const;
105 
106  /**
107  * != operator
108  * @param other
109  * cmp
110  * @return
111  * true if this != cmp, else false
112  */
113  bool operator!=(const Lr1Configuration& other) const;
114 
115 
116  /**
117  * To String function
118  */
119  std::string Str() const;
120 
121 };//class Lr1Configuration
122 
123 //comparator for two sets of Lr1Configurations
124 extern FAUDES_API bool CompareConfigSet(const std::set<Lr1Configuration>& lhs, const std::set<Lr1Configuration>& rhs);
125 
127  bool operator() (const std::set<Lr1Configuration>& lhs, const std::set<Lr1Configuration>& rhs) const{
128  return CompareConfigSet(lhs,rhs);
129  }
130 };
131 
132 //convencience definition for a set of sets of Lr1Configurations
133 typedef std::set<std::set<Lr1Configuration>, ConfigSetComparator> Lr1ConfigurationSetSet;
134 
135 //convencience definition for a pair of a set of Lr1Configurations and a GrammarSymbolPtr
136 typedef std::pair<std::set<Lr1Configuration>, GrammarSymbolPtr> ConfigSetGsPair;
137 
138 //convencience definition for a vector of sets of Lr1Configurations
139 typedef std::vector<std::set<Lr1Configuration> > ConfigSetVector;
140 
141 //comparator for two pairs of configuration set and grammar symbol pointer
142 bool CompareConfigGsPair(const ConfigSetGsPair& lhs, const ConfigSetGsPair& rhs);
143 
145  bool operator() (const ConfigSetGsPair& lhs, const ConfigSetGsPair& rhs) const{
146  return CompareConfigGsPair(lhs,rhs);
147  }
148 };
149 
150 //convencience definition for a map of ConfigSetGsPairs to sets of Lr1Configurations
151 typedef std::map<ConfigSetGsPair, std::set<Lr1Configuration>, ConfigGsPairComparator> LrmTransitionMap;
152 
153 //convenience definition for a set of ConfigSetGsPairs
154 typedef std::set<ConfigSetGsPair, ConfigGsPairComparator> ConfigSetGsPairSet;
155 
156 /**
157  * To string function for configuration sets.
158  *
159  * @param configs
160  * the configuration set to turn into a string
161  * @return
162  * the configuration set as a string
163  */
164 extern FAUDES_API std::string ConfigSetToStr(const std::set<Lr1Configuration>& configs);
165 
166 /**
167  * To string function for configuration set sets.
168  *
169  * @param configSetSet
170  * the configuration set sets to turn into a string
171  * @return
172  * the configuration set sets as a string
173  */
174 extern FAUDES_API std::string ConfigSetSetToStr(const Lr1ConfigurationSetSet configSetSet);
175 
176 /**
177  * To string function for a transition map.
178  *
179  * @param transitionMap
180  * the transition map to turn into a string
181  * @return
182  * the transition map as a string
183  */
184 extern FAUDES_API std::string TransitionMapToStr(const LrmTransitionMap& transitionMap);
185 
186 /**
187 *
188 * Lr1ParserActionElement
189 *
190 *
191 * @ingroup PushdownPlugin
192 *
193 * @section Overview
194 * Overview
195 *
196 * @section Contents
197 * Contents
198 */
200 
201  public:
202 
203  std::vector<Idx> mStateStack;
205 
206  /**
207  * Constructor
208  *
209  * @param stateStack
210  * stack of states
211  * @param terminal
212  * terminal
213  */
214  Lr1ParserActionElement(const std::vector<Idx>& stateStack, const Terminal& terminal) : mStateStack(stateStack), mNextTerminal(terminal) {}
215 
216  /**
217  * Getter for mStateStack
218  *
219  * @return
220  * mStateStack
221  */
222  std::vector<Idx> const& StateStack() const { return mStateStack; }
223 
224  /**
225  * Getter for mTerminal
226  *
227  * @return
228  * mTerminal
229  */
230  Terminal const& NextTerminal() const { return mNextTerminal; }
231 
232  /**
233  * < operator
234  * @param other
235  * cmp
236  * @return
237  * true if this < cmp, else false
238  */
239  bool operator<(const Lr1ParserActionElement& other) const;
240 
241  /**
242  * To String function
243  */
244  std::string Str() const;
245 
246 };//class Lr1ParserActionElement
247 
248 /**
249 *
250 * Lr1ParserAction
251 *
252 *
253 * @ingroup PushdownPlugin
254 *
255 * @section Overview
256 * Overview
257 *
258 * @section Contents
259 * Contents
260 */
262 
263  public:
264 
268  bool mIsReduce;
269 
270  /**
271  * Constructor for shift actions
272  *
273  * @param lhs
274  * lhs
275  * @param rhs
276  * rhs
277  */
278  Lr1ParserAction(const Lr1ParserActionElement& lhs, const Lr1ParserActionElement& rhs): mLhs(lhs), mRhs(rhs), mProduction(GrammarProduction(Nonterminal(0,std::vector<Idx>()), GrammarSymbolVector())), mIsReduce(false) {}
279 
280  /**
281  * Constructor for reduce actions
282  *
283  * @param lhs
284  * lhs
285  * @param rhs
286  * rhs
287  * @param production
288  * grammar production that causes this rule
289  */
290  Lr1ParserAction(const Lr1ParserActionElement& lhs, const Lr1ParserActionElement& rhs, const GrammarProduction production) : mLhs(lhs), mRhs(rhs), mProduction(production), mIsReduce(true) {}
291 
292  /**
293  * Getter for mLhs
294  *
295  * @return
296  * mLhs
297  */
298  Lr1ParserActionElement const& Lhs() const { return mLhs; }
299 
300  /**
301  * Getter for mRhs
302  *
303  * @return
304  * mRhs
305  */
306  Lr1ParserActionElement const& Rhs() const { return mRhs; }
307 
308  /**
309  * < operator
310  * @param other
311  * cmp
312  * @return
313  * true if this < cmp, else false
314  */
315  bool operator<(const Lr1ParserAction& other) const;
316 
317  /**
318  * To String function
319  */
320  std::string Str() const;
321 
322 };//class Lr1ParserAction
323 
324 /**
325 *
326 * Lr1Parser
327 *
328 *
329 * @ingroup PushdownPlugin
330 *
331 * @section Overview
332 * Overview
333 *
334 * @section Contents
335 * Contents
336 */
338  public:
339 
341 // Idx mFinalState;
342  std::set<Idx> mFinalStates;
343  std::set<Idx> mNonterminals;
344  std::set<Terminal> mTerminals;
345  std::set<Lr1ParserAction> mActions;
347 
348  /**
349  * default constructor
350  */
351  Lr1Parser(): mAugSymbol(Terminal(0)) {};
352 
353  /**
354  * Getter for mStartState
355  *
356  * @return
357  * mStartState
358  */
359  Idx StartState() const { return mStartState; }
360 
361  /**
362  * Getter for mFinalState
363  *
364  * @return
365  * mFinalState
366  */
367  // Idx FinalState() const { return mFinalState; }
368  std::set<Idx> const& FinalStates() const { return mFinalStates;}
369 
370  void ClrFinalStates(){mFinalStates.clear();} //TODO
371 
372  /**
373  * Getter for mNonterminals
374  *
375  * @return
376  * mNonterminals
377  */
378  std::set<Idx> const& Nonterminals() const { return mNonterminals; }
379 
380  /**
381  * Getter for mTerminals
382  *
383  * @return
384  * mTerminals
385  */
386  std::set<Terminal> const& Terminals() const { return mTerminals; }
387 
388  /**
389  * Getter for mActions
390  *
391  * @return
392  * mActions
393  */
394  std::set<Lr1ParserAction> const& Actions() const { return mActions; }
395 
396  /**
397  * Getter for mAugSymbol
398  *
399  * @return
400  * mAugSymbol
401  */
402  Terminal AugSymbol() const { return mAugSymbol; }
403 
404  /**
405  * set the parser's start state and add it to the set of nonterminals
406  *
407  * @param start
408  * start state to set
409  * @return
410  * true, if nonterminal did not exist in parser, else false
411  */
412  bool SetStartState(Idx start);
413 
414  /**
415  * set the parser's final state and add it to the set of nonterminals
416  *
417  * @param final
418  * final state to set
419  * @return
420  * true, if nonterminal did not exist in parser, else false
421  */
422  bool InsFinalState(Idx final);
423 
424  /**
425  * Add a new nonterminal to the parser.
426  *
427  * @param nt
428  * the nonterminal to add
429  * @return
430  * true if the nonterminal did not exist and was successfully added,
431  * else false
432  */
433  bool InsNonterminal(Idx nt);
434 
435  /**
436  * TODO description
437  *
438  * @param t
439  * aug symbol
440  */
441  void SetAugSymbol(Terminal t);
442 
443  /**
444  * Add a new terminal to the parser.
445  *
446  * @param t
447  * the terminal to add
448  * @return
449  * true if the terminal did not exist and was successfully added,
450  * else false
451  */
452  bool InsTerminal(const Terminal& t);
453 
454  /**
455  * Add a new Lr1ParserAction to the parser
456  *
457  * @param action
458  * the action to add
459  * @return
460  * true if the action did not exist and was successfully added, else false
461  */
462  bool InsAction(const Lr1ParserAction& action);
463 
464  /**
465  * To String function for all nonterminals
466  */
467  std::string StrFinalStates() const;
468 
469  /**
470  * To String function for all nonterminals
471  */
472  std::string StrNonterminals() const;
473 
474  /**
475  * To String function for all terminals
476  */
477  std::string StrTerminals() const;
478 
479  /**
480  * To String function for actions
481  */
482  std::string StrActions(std::string separator) const;
483 
484  /**
485  * To String function
486  */
487  std::string Str() const;
488 
489 };//class Lr1Parser
490 
491 } // namespace faudes
492 
493 #endif

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