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 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 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 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 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  */
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 */
337 class Lr1Parser{
338  public:
339 
342  std::set<Idx> mNonterminals;
343  std::set<Terminal> mTerminals;
344  std::set<Lr1ParserAction> mActions;
346 
347  /**
348  * default constructor
349  */
351 
352  /**
353  * Getter for mStartState
354  *
355  * @return
356  * mStartState
357  */
358  Idx StartState() const { return mStartState; }
359 
360  /**
361  * Getter for mFinalState
362  *
363  * @return
364  * mFinalState
365  */
366  Idx FinalState() const { return mFinalState; }
367 
368  /**
369  * Getter for mNonterminals
370  *
371  * @return
372  * mNonterminals
373  */
374  std::set<Idx> const& Nonterminals() const { return mNonterminals; }
375 
376  /**
377  * Getter for mTerminals
378  *
379  * @return
380  * mTerminals
381  */
382  std::set<Terminal> const& Terminals() const { return mTerminals; }
383 
384  /**
385  * Getter for mActions
386  *
387  * @return
388  * mActions
389  */
390  std::set<Lr1ParserAction> const& Actions() const { return mActions; }
391 
392  /**
393  * Getter for mAugSymbol
394  *
395  * @return
396  * mAugSymbol
397  */
398  Terminal AugSymbol() const { return mAugSymbol; }
399 
400  /**
401  * set the parser's start state and add it to the set of nonterminals
402  *
403  * @param start
404  * start state to set
405  * @return
406  * true, if nonterminal did not exist in parser, else false
407  */
408  bool SetStartState(Idx start);
409 
410  /**
411  * set the parser's final state and add it to the set of nonterminals
412  *
413  * @param final
414  * final state to set
415  * @return
416  * true, if nonterminal did not exist in parser, else false
417  */
418  bool SetFinalState(Idx final);
419 
420  /**
421  * Add a new nonterminal to the parser.
422  *
423  * @param nt
424  * the nonterminal to add
425  * @return
426  * true if the nonterminal did not exist and was successfully added,
427  * else false
428  */
429  bool InsNonterminal(Idx nt);
430 
431  /**
432  * TODO description
433  *
434  * @param t
435  * aug symbol
436  */
437  void SetAugSymbol(Terminal t);
438 
439  /**
440  * Add a new terminal to the parser.
441  *
442  * @param t
443  * the terminal to add
444  * @return
445  * true if the terminal did not exist and was successfully added,
446  * else false
447  */
448  bool InsTerminal(const Terminal& t);
449 
450  /**
451  * Add a new Lr1ParserAction to the parser
452  *
453  * @param action
454  * the action to add
455  * @return
456  * true if the action did not exist and was successfully added, else false
457  */
458  bool InsAction(const Lr1ParserAction& action);
459 
460  /**
461  * To String function for all nonterminals
462  */
463  std::string StrNonterminals() const;
464 
465  /**
466  * To String function for all terminals
467  */
468  std::string StrTerminals() const;
469 
470  /**
471  * To String function for actions
472  */
473  std::string StrActions(std::string separator) const;
474 
475  /**
476  * To String function
477  */
478  std::string Str() const;
479 
480 };//class Lr1Parser
481 
482 } // namespace faudes
483 
484 #endif

libFAUDES 2.24g --- 2014.09.15 --- c++ api documentaion by doxygen