pd_parser.cppGo to the documentation of this file.00001 /** @file pd_parser.cpp 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 #include "pd_parser.h" 00011 00012 namespace faudes { 00013 00014 00015 /******************************* 00016 * 00017 * Implementation of Lr1Configuration 00018 * 00019 */ 00020 00021 Lr1Configuration::Lr1Configuration (const Nonterminal& lhs, const GrammarSymbolVector& beforeDot, const GrammarSymbolVector& afterDot, const Terminal& lookahead) : mLhs(lhs), mBeforeDot(beforeDot), mAfterDot(afterDot), mLookahead(lookahead){ 00022 if(beforeDot.empty()) { 00023 std::stringstream errstr; 00024 errstr << "mBeforeDot must not be empty." <<std::endl; 00025 throw Exception("Lr1Configuration::Lr1Configuration", errstr.str(), 1001); 00026 } 00027 if(afterDot.empty()) { 00028 std::stringstream errstr; 00029 errstr << "mAfterDot must not be empty." <<std::endl; 00030 throw Exception("Lr1Configuration::Lr1Configuration", errstr.str(), 1001); 00031 } 00032 } 00033 00034 bool Lr1Configuration::operator<(const Lr1Configuration& other) const{ 00035 //compare Lhs 00036 if(mLhs < other.Lhs()) return true; 00037 if(other.Lhs() < mLhs) return false; 00038 00039 //compare beforeDot 00040 if(CompareGsVector(mBeforeDot,other.BeforeDot())) return true; 00041 if(CompareGsVector(other.BeforeDot(),mBeforeDot)) return false; 00042 00043 //compare afterDot 00044 if(CompareGsVector(mAfterDot,other.AfterDot())) return true; 00045 if(CompareGsVector(other.AfterDot(),mAfterDot)) return false; 00046 00047 //compare lookahead 00048 if(mLookahead < other.Lookahead()) return true; 00049 00050 return false; 00051 } 00052 00053 bool Lr1Configuration::operator==(const Lr1Configuration& other) const{ 00054 return !((*this < other) || (other < *this)); 00055 } 00056 00057 bool Lr1Configuration::operator!=(const Lr1Configuration& other) const{ 00058 return ((*this < other) || (other < *this)); 00059 } 00060 00061 std::string Lr1Configuration::Str() const{ 00062 00063 std::stringstream s; 00064 GrammarSymbolVector::const_iterator gsvit; 00065 00066 s << mLhs.Str() << " → "; 00067 for(gsvit = mBeforeDot.begin(); gsvit != mBeforeDot.end(); gsvit++){ 00068 s << (*gsvit)->Str(); 00069 } 00070 s << " • "; 00071 for(gsvit = mAfterDot.begin(); gsvit != mAfterDot.end(); gsvit++){ 00072 s << (*gsvit)->Str(); 00073 } 00074 s << ", " << mLookahead.Str(); 00075 00076 00077 return s.str(); 00078 } 00079 00080 /******************************* 00081 * 00082 * Implementation of CompareConfigSet 00083 * 00084 */ 00085 bool CompareConfigSet(const std::set<Lr1Configuration>& lhs, const std::set<Lr1Configuration>& rhs){ 00086 if (lhs.size() < rhs.size()) return true; //also makes sure that it2 will not go out of bounds 00087 if (rhs.size() < lhs.size()) return false; 00088 00089 std::set<Lr1Configuration>::const_iterator it1, it2; 00090 it2 = rhs.begin(); 00091 for(it1 = lhs.begin(); it1 != lhs.end(); it1++){ 00092 if(*it1 < *it2) return true; 00093 if(*it2 < *it1) return false; 00094 it2++; 00095 } 00096 return false; 00097 } 00098 00099 /******************************* 00100 * 00101 * Implementation of CompareConfigGsPair 00102 * 00103 */ 00104 bool CompareConfigGsPair(const ConfigSetGsPair& lhs, const ConfigSetGsPair& rhs){ 00105 if(*lhs.second < *rhs.second) return true; 00106 if(*rhs.second < *lhs.second) return false; 00107 if(CompareConfigSet(lhs.first,rhs.first)) return true; 00108 return false; 00109 } 00110 00111 /******************************* 00112 * 00113 * Implementation of ConfigSetToStr 00114 * 00115 */ 00116 std::string ConfigSetToStr(const std::set<Lr1Configuration>& configs){ 00117 std::stringstream s; 00118 std::set<Lr1Configuration>::const_iterator configit; 00119 for(configit = configs.begin(); configit != configs.end(); configit++){ 00120 if(configit != configs.begin()){ 00121 s << ", "; 00122 } 00123 s << "(" << configit->Str() <<")"; 00124 } 00125 return s.str(); 00126 } 00127 00128 /******************************* 00129 * 00130 * Implementation of ConfigSetSetToStr 00131 * 00132 */ 00133 std::string ConfigSetSetToStr(const Lr1ConfigurationSetSet configSetSet){ 00134 std::stringstream s; 00135 Lr1ConfigurationSetSet::const_iterator setit; 00136 int i = 0; 00137 for(setit = configSetSet.begin(); setit != configSetSet.end(); setit++){ 00138 s << i << ":\n"; 00139 s << " " << ConfigSetToStr(*setit) << "\n"; 00140 i++; 00141 } 00142 return s.str(); 00143 } 00144 /******************************* 00145 * 00146 * Implementation of TransitionMapToStr 00147 * 00148 */ 00149 std::string TransitionMapToStr(const LrmTransitionMap& transitionMap){ 00150 std::stringstream s; 00151 LrmTransitionMap::const_iterator transmapit; 00152 std::set<Lr1Configuration>::const_iterator configit; 00153 for(transmapit = transitionMap.begin(); transmapit != transitionMap.end(); transmapit++){ 00154 s << "CONFIGURATION SET " << ConfigSetToStr(transmapit->first.first) << "\n"; 00155 s << " with symbol " << transmapit->first.second->Str() << ":\n"; 00156 s << " leads to " << ConfigSetToStr(transmapit->second) << "\n"; 00157 } 00158 return s.str(); 00159 } 00160 00161 /******************************* 00162 * 00163 * Implementation of Lr1ParserActionElement 00164 * 00165 */ 00166 bool Lr1ParserActionElement::operator<(const Lr1ParserActionElement& other) const{ 00167 00168 //compare nextTerminal 00169 if(mNextTerminal < other.NextTerminal()) return true; 00170 if(other.NextTerminal() < mNextTerminal) return false; 00171 00172 //compare stateStack 00173 if (mStateStack.size() < other.StateStack().size()) return true; //also makes sure that it2 will not go out of bounds 00174 if (other.StateStack().size() < mStateStack.size()) return false; 00175 00176 std::vector<Idx>::const_iterator it1, it2; 00177 it2 = other.StateStack().begin(); 00178 for(it1 = mStateStack.begin(); it1 != mStateStack.end(); it1++){ 00179 if(*it1 < *it2) return true; 00180 if(*it2 < *it1) return false; 00181 it2++; 00182 } 00183 return false; 00184 } 00185 00186 std::string Lr1ParserActionElement::Str() const{ 00187 std::stringstream s; 00188 std::vector<Idx>::const_iterator it; 00189 for(it = mStateStack.begin(); it != mStateStack.end(); it++){ 00190 if (it == mStateStack.begin()) 00191 s << "(" << *it; 00192 else 00193 s << ", " << *it; 00194 } 00195 s << " | " << mNextTerminal.Str() << ")"; 00196 return s.str(); 00197 } 00198 00199 /******************************* 00200 * 00201 * Implementation of Lr1ParserAction 00202 * 00203 */ 00204 00205 bool Lr1ParserAction::operator<(const Lr1ParserAction& other) const{ 00206 if (mLhs < other.Lhs()) return true; 00207 if (other.Lhs() < mLhs) return false; 00208 if (mRhs < other.Rhs()) return true; 00209 return false; 00210 } 00211 00212 std::string Lr1ParserAction::Str() const{ 00213 std::stringstream s; 00214 s << mLhs.Str() << " -> " << mRhs.Str(); 00215 if(mIsReduce){ 00216 s << " [" << mProduction.Str() << "]"; 00217 } 00218 return s.str(); 00219 } 00220 00221 /******************************* 00222 * 00223 * Implementation of Lr1Parser 00224 * 00225 */ 00226 00227 bool Lr1Parser::SetStartState(Idx start){ 00228 mStartState = start; 00229 return mNonterminals.insert(start).second; 00230 } 00231 00232 bool Lr1Parser::SetFinalState(Idx final){ 00233 mFinalState = final; 00234 return mNonterminals.insert(final).second; 00235 } 00236 00237 bool Lr1Parser::InsNonterminal(Idx nt){ 00238 return mNonterminals.insert(nt).second; 00239 } 00240 00241 bool Lr1Parser::InsTerminal(const Terminal& t){ 00242 return mTerminals.insert(t).second; 00243 } 00244 00245 bool Lr1Parser::InsAction(const Lr1ParserAction& action){ 00246 return mActions.insert(action).second; 00247 } 00248 00249 void Lr1Parser::SetAugSymbol(Terminal t){ 00250 mAugSymbol = t; 00251 } 00252 00253 00254 std::string Lr1Parser::StrNonterminals() const{ 00255 std::string s = ""; 00256 std::set<Idx>::const_iterator it; 00257 for(it = mNonterminals.begin(); it != mNonterminals.end(); it++){ 00258 if (it == mNonterminals.begin()) 00259 s += static_cast<std::ostringstream*>( &(std::ostringstream() << *it) )->str(); 00260 else 00261 s += ", " + static_cast<std::ostringstream*>( &(std::ostringstream() << *it) )->str(); 00262 } 00263 return s; 00264 } 00265 00266 std::string Lr1Parser::StrTerminals() const{ 00267 std::string s = ""; 00268 std::set<Terminal>::const_iterator it; 00269 for(it = mTerminals.begin(); it != mTerminals.end(); it++){ 00270 if (it == mTerminals.begin()) 00271 s += it->Str(); 00272 else 00273 s += ", " + it->Str(); 00274 } 00275 return s; 00276 } 00277 00278 std::string Lr1Parser::StrActions(std::string separator) const{ 00279 std::string s = ""; 00280 std::set<Lr1ParserAction>::const_iterator it; 00281 for(it = mActions.begin(); it != mActions.end(); it++){ 00282 if (it == mActions.begin()) 00283 s += it->Str(); 00284 else 00285 s += separator + it->Str(); 00286 } 00287 return s; 00288 } 00289 00290 std::string Lr1Parser::Str() const{ 00291 std::stringstream s; 00292 s << "start state: " << mStartState << std::endl; 00293 s << "final state: " << mFinalState << std::endl; 00294 s << "augment symbol: " << mAugSymbol.Str() << std::endl; 00295 s << "nonterminals: " << StrNonterminals() << std::endl; 00296 s << "terminals: " << StrTerminals() << std::endl; 00297 s << "actions: " << StrActions("\n") << std::endl; 00298 return s.str(); 00299 } 00300 00301 } // namespace faudes 00302 libFAUDES 2.23h --- 2014.04.03 --- c++ api documentaion by doxygen |