pd_parser.hGo to the documentation of this file.00001 /** @file pd_parser.h parser data structure*/ 00002 00003 00004 /* Pushdown plugin for FAU Discrete Event Systems Library (libfaudes) 00005 00006 Copyright (C) 2013 Stefan Jacobi, Sven Schneider, Anne-Kathrin Hess 00007 00008 */ 00009 00010 00011 #ifndef FAUDES_PD_PARSER_H 00012 #define FAUDES_PD_PARSER_H 00013 00014 #include "corefaudes.h" 00015 #include "pd_grammar.h" 00016 00017 namespace faudes { 00018 00019 /** 00020 * Lr1 Configuration 00021 * 00022 * 00023 * 00024 * @ingroup PushdownPlugin 00025 * 00026 * @section Overview 00027 * Overview 00028 * 00029 * @section Contents 00030 * Contents 00031 */ 00032 00033 class Lr1Configuration{ 00034 00035 public: 00036 00037 Nonterminal mLhs; 00038 GrammarSymbolVector mBeforeDot; 00039 GrammarSymbolVector mAfterDot; 00040 Terminal mLookahead; 00041 00042 /** 00043 * Constructor TODO make sure that vectors are not empty 00044 * 00045 * @param lhs 00046 * lefthand side nonterminal 00047 * @param beforeDot 00048 * before dot grammar symbols 00049 * @param afterDot 00050 * after dot grammar symbols 00051 * @param lookahead 00052 * lookahead terminal 00053 */ 00054 Lr1Configuration (const Nonterminal& lhs, const GrammarSymbolVector& beforeDot, const GrammarSymbolVector& afterDot, const Terminal& lookahead); 00055 00056 /** 00057 * Getter for mLhs 00058 * 00059 * @return 00060 * mLhs 00061 */ 00062 Nonterminal const& Lhs() const { return mLhs; } 00063 00064 /** 00065 * Getter for mBeforeDot 00066 * 00067 * @return 00068 * mBeforeDot 00069 */ 00070 GrammarSymbolVector const& BeforeDot() const { return mBeforeDot; } 00071 00072 /** 00073 * Getter for mAfterDot 00074 * 00075 * @return 00076 * mAfterDot 00077 */ 00078 GrammarSymbolVector const& AfterDot() const { return mAfterDot; } 00079 00080 /** 00081 * Getter for mLookahead 00082 * 00083 * @return 00084 * mLookahead 00085 */ 00086 Terminal const& Lookahead() const { return mLookahead; } 00087 00088 /** 00089 * < operator 00090 * @param other 00091 * cmp 00092 * @return 00093 * true if this < cmp, else false 00094 */ 00095 bool operator<(const Lr1Configuration& other) const; 00096 00097 /** 00098 * == operator 00099 * @param other 00100 * cmp 00101 * @return 00102 * true if this == cmp, else false 00103 */ 00104 bool operator==(const Lr1Configuration& other) const; 00105 00106 /** 00107 * != operator 00108 * @param other 00109 * cmp 00110 * @return 00111 * true if this != cmp, else false 00112 */ 00113 bool operator!=(const Lr1Configuration& other) const; 00114 00115 00116 /** 00117 * To String function 00118 */ 00119 std::string Str() const; 00120 00121 };//class Lr1Configuration 00122 00123 //comparator for two sets of Lr1Configurations 00124 bool CompareConfigSet(const std::set<Lr1Configuration>& lhs, const std::set<Lr1Configuration>& rhs); 00125 00126 struct ConfigSetComparator { 00127 bool operator() (const std::set<Lr1Configuration>& lhs, const std::set<Lr1Configuration>& rhs) const{ 00128 return CompareConfigSet(lhs,rhs); 00129 } 00130 }; 00131 00132 //convencience definition for a set of sets of Lr1Configurations 00133 typedef std::set<std::set<Lr1Configuration>, ConfigSetComparator> Lr1ConfigurationSetSet; 00134 00135 //convencience definition for a pair of a set of Lr1Configurations and a GrammarSymbolPtr 00136 typedef std::pair<std::set<Lr1Configuration>, GrammarSymbolPtr> ConfigSetGsPair; 00137 00138 //convencience definition for a vector of sets of Lr1Configurations 00139 typedef std::vector<std::set<Lr1Configuration> > ConfigSetVector; 00140 00141 //comparator for two pairs of configuration set and grammar symbol pointer 00142 bool CompareConfigGsPair(const ConfigSetGsPair& lhs, const ConfigSetGsPair& rhs); 00143 00144 struct ConfigGsPairComparator { 00145 bool operator() (const ConfigSetGsPair& lhs, const ConfigSetGsPair& rhs) const{ 00146 return CompareConfigGsPair(lhs,rhs); 00147 } 00148 }; 00149 00150 //convencience definition for a map of ConfigSetGsPairs to sets of Lr1Configurations 00151 typedef std::map<ConfigSetGsPair, std::set<Lr1Configuration>, ConfigGsPairComparator> LrmTransitionMap; 00152 00153 //convenience definition for a set of ConfigSetGsPairs 00154 typedef std::set<ConfigSetGsPair, ConfigGsPairComparator> ConfigSetGsPairSet; 00155 00156 /** 00157 * To string function for configuration sets. 00158 * 00159 * @param configs 00160 * the configuration set to turn into a string 00161 * @return 00162 * the configuration set as a string 00163 */ 00164 std::string ConfigSetToStr(const std::set<Lr1Configuration>& configs); 00165 00166 /** 00167 * To string function for configuration set sets. 00168 * 00169 * @param configSetSet 00170 * the configuration set sets to turn into a string 00171 * @return 00172 * the configuration set sets as a string 00173 */ 00174 std::string ConfigSetSetToStr(const Lr1ConfigurationSetSet configSetSet); 00175 00176 /** 00177 * To string function for a transition map. 00178 * 00179 * @param transitionMap 00180 * the transition map to turn into a string 00181 * @return 00182 * the transition map as a string 00183 */ 00184 std::string TransitionMapToStr(const LrmTransitionMap& transitionMap); 00185 00186 /** 00187 * 00188 * Lr1ParserActionElement 00189 * 00190 * 00191 * @ingroup PushdownPlugin 00192 * 00193 * @section Overview 00194 * Overview 00195 * 00196 * @section Contents 00197 * Contents 00198 */ 00199 class Lr1ParserActionElement{ 00200 00201 public: 00202 00203 std::vector<Idx> mStateStack; 00204 Terminal mNextTerminal; 00205 00206 /** 00207 * Constructor 00208 * 00209 * @param stateStack 00210 * stack of states 00211 * @param terminal 00212 * terminal 00213 */ 00214 Lr1ParserActionElement(const std::vector<Idx>& stateStack, const Terminal& terminal) : mStateStack(stateStack), mNextTerminal(terminal) {} 00215 00216 /** 00217 * Getter for mStateStack 00218 * 00219 * @return 00220 * mStateStack 00221 */ 00222 std::vector<Idx> const& StateStack() const { return mStateStack; } 00223 00224 /** 00225 * Getter for mTerminal 00226 * 00227 * @return 00228 * mTerminal 00229 */ 00230 Terminal const& NextTerminal() const { return mNextTerminal; } 00231 00232 /** 00233 * < operator 00234 * @param other 00235 * cmp 00236 * @return 00237 * true if this < cmp, else false 00238 */ 00239 bool operator<(const Lr1ParserActionElement& other) const; 00240 00241 /** 00242 * To String function 00243 */ 00244 std::string Str() const; 00245 00246 };//class Lr1ParserActionElement 00247 00248 /** 00249 * 00250 * Lr1ParserAction 00251 * 00252 * 00253 * @ingroup PushdownPlugin 00254 * 00255 * @section Overview 00256 * Overview 00257 * 00258 * @section Contents 00259 * Contents 00260 */ 00261 class Lr1ParserAction{ 00262 00263 public: 00264 00265 Lr1ParserActionElement mLhs; 00266 Lr1ParserActionElement mRhs; 00267 GrammarProduction mProduction; 00268 bool mIsReduce; 00269 00270 /** 00271 * Constructor for shift actions 00272 * 00273 * @param lhs 00274 * lhs 00275 * @param rhs 00276 * rhs 00277 */ 00278 Lr1ParserAction(const Lr1ParserActionElement& lhs, const Lr1ParserActionElement& rhs): mLhs(lhs), mRhs(rhs), mProduction(GrammarProduction(Nonterminal(0,std::vector<Idx>()), GrammarSymbolVector())), mIsReduce(false) {} 00279 00280 /** 00281 * Constructor for reduce actions 00282 * 00283 * @param lhs 00284 * lhs 00285 * @param rhs 00286 * rhs 00287 * @param production 00288 * grammar production that causes this rule 00289 */ 00290 Lr1ParserAction(const Lr1ParserActionElement& lhs, const Lr1ParserActionElement& rhs, const GrammarProduction production) : mLhs(lhs), mRhs(rhs), mProduction(production), mIsReduce(true) {} 00291 00292 /** 00293 * Getter for mLhs 00294 * 00295 * @return 00296 * mLhs 00297 */ 00298 Lr1ParserActionElement const& Lhs() const { return mLhs; } 00299 00300 /** 00301 * Getter for mRhs 00302 * 00303 * @return 00304 * mRhs 00305 */ 00306 Lr1ParserActionElement const& Rhs() const { return mRhs; } 00307 00308 /** 00309 * < operator 00310 * @param other 00311 * cmp 00312 * @return 00313 * true if this < cmp, else false 00314 */ 00315 bool operator<(const Lr1ParserAction& other) const; 00316 00317 /** 00318 * To String function 00319 */ 00320 std::string Str() const; 00321 00322 };//class Lr1ParserAction 00323 00324 /** 00325 * 00326 * Lr1Parser 00327 * 00328 * 00329 * @ingroup PushdownPlugin 00330 * 00331 * @section Overview 00332 * Overview 00333 * 00334 * @section Contents 00335 * Contents 00336 */ 00337 class Lr1Parser{ 00338 public: 00339 00340 Idx mStartState; 00341 Idx mFinalState; 00342 std::set<Idx> mNonterminals; 00343 std::set<Terminal> mTerminals; 00344 std::set<Lr1ParserAction> mActions; 00345 Terminal mAugSymbol; 00346 00347 /** 00348 * default constructor 00349 */ 00350 Lr1Parser(): mAugSymbol(Terminal(0)) {}; 00351 00352 /** 00353 * Getter for mStartState 00354 * 00355 * @return 00356 * mStartState 00357 */ 00358 Idx StartState() const { return mStartState; } 00359 00360 /** 00361 * Getter for mFinalState 00362 * 00363 * @return 00364 * mFinalState 00365 */ 00366 Idx FinalState() const { return mFinalState; } 00367 00368 /** 00369 * Getter for mNonterminals 00370 * 00371 * @return 00372 * mNonterminals 00373 */ 00374 std::set<Idx> const& Nonterminals() const { return mNonterminals; } 00375 00376 /** 00377 * Getter for mTerminals 00378 * 00379 * @return 00380 * mTerminals 00381 */ 00382 std::set<Terminal> const& Terminals() const { return mTerminals; } 00383 00384 /** 00385 * Getter for mActions 00386 * 00387 * @return 00388 * mActions 00389 */ 00390 std::set<Lr1ParserAction> const& Actions() const { return mActions; } 00391 00392 /** 00393 * Getter for mAugSymbol 00394 * 00395 * @return 00396 * mAugSymbol 00397 */ 00398 Terminal AugSymbol() const { return mAugSymbol; } 00399 00400 /** 00401 * set the parser's start state and add it to the set of nonterminals 00402 * 00403 * @param start 00404 * start state to set 00405 * @return 00406 * true, if nonterminal did not exist in parser, else false 00407 */ 00408 bool SetStartState(Idx start); 00409 00410 /** 00411 * set the parser's final state and add it to the set of nonterminals 00412 * 00413 * @param final 00414 * final state to set 00415 * @return 00416 * true, if nonterminal did not exist in parser, else false 00417 */ 00418 bool SetFinalState(Idx final); 00419 00420 /** 00421 * Add a new nonterminal to the parser. 00422 * 00423 * @param nt 00424 * the nonterminal to add 00425 * @return 00426 * true if the nonterminal did not exist and was successfully added, 00427 * else false 00428 */ 00429 bool InsNonterminal(Idx nt); 00430 00431 /** 00432 * TODO description 00433 * 00434 * @param t 00435 * aug symbol 00436 */ 00437 void SetAugSymbol(Terminal t); 00438 00439 /** 00440 * Add a new terminal to the parser. 00441 * 00442 * @param t 00443 * the terminal to add 00444 * @return 00445 * true if the terminal did not exist and was successfully added, 00446 * else false 00447 */ 00448 bool InsTerminal(const Terminal& t); 00449 00450 /** 00451 * Add a new Lr1ParserAction to the parser 00452 * 00453 * @param action 00454 * the action to add 00455 * @return 00456 * true if the action did not exist and was successfully added, else false 00457 */ 00458 bool InsAction(const Lr1ParserAction& action); 00459 00460 /** 00461 * To String function for all nonterminals 00462 */ 00463 std::string StrNonterminals() const; 00464 00465 /** 00466 * To String function for all terminals 00467 */ 00468 std::string StrTerminals() const; 00469 00470 /** 00471 * To String function for actions 00472 */ 00473 std::string StrActions(std::string separator) const; 00474 00475 /** 00476 * To String function 00477 */ 00478 std::string Str() const; 00479 00480 };//class Lr1Parser 00481 00482 } // namespace faudes 00483 00484 #endif libFAUDES 2.23h --- 2014.04.03 --- c++ api documentaion by doxygen |