pd_parser.cpp
Go to the documentation of this file.
1 /** @file pd_parser.cpp 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 #include "pd_parser.h"
11 
12 namespace faudes {
13 
14 
15 /*******************************
16 *
17 * Implementation of Lr1Configuration
18 *
19 */
20 
21 Lr1Configuration::Lr1Configuration (const Nonterminal& lhs, const GrammarSymbolVector& beforeDot, const GrammarSymbolVector& afterDot, const Terminal& lookahead) : mLhs(lhs), mBeforeDot(beforeDot), mAfterDot(afterDot), mLookahead(lookahead){
22  if(beforeDot.empty()) {
23  std::stringstream errstr;
24  errstr << "mBeforeDot must not be empty." <<std::endl;
25  throw Exception("Lr1Configuration::Lr1Configuration", errstr.str(), 1001);
26  }
27  if(afterDot.empty()) {
28  std::stringstream errstr;
29  errstr << "mAfterDot must not be empty." <<std::endl;
30  throw Exception("Lr1Configuration::Lr1Configuration", errstr.str(), 1001);
31  }
32 }
33 
35  //compare Lhs
36  if(mLhs < other.Lhs()) return true;
37  if(other.Lhs() < mLhs) return false;
38 
39  //compare beforeDot
40  if(CompareGsVector(mBeforeDot,other.BeforeDot())) return true;
41  if(CompareGsVector(other.BeforeDot(),mBeforeDot)) return false;
42 
43  //compare afterDot
44  if(CompareGsVector(mAfterDot,other.AfterDot())) return true;
45  if(CompareGsVector(other.AfterDot(),mAfterDot)) return false;
46 
47  //compare lookahead
48  if(mLookahead < other.Lookahead()) return true;
49 
50  return false;
51 }
52 
54  return !((*this < other) || (other < *this));
55 }
56 
58  return ((*this < other) || (other < *this));
59 }
60 
61 std::string Lr1Configuration::Str() const{
62 
63  std::stringstream s;
64  GrammarSymbolVector::const_iterator gsvit;
65 
66  s << mLhs.Str() << " → ";
67  for(gsvit = mBeforeDot.begin(); gsvit != mBeforeDot.end(); gsvit++){
68  s << (*gsvit)->Str();
69  }
70  s << " • ";
71  for(gsvit = mAfterDot.begin(); gsvit != mAfterDot.end(); gsvit++){
72  s << (*gsvit)->Str();
73  }
74  s << ", " << mLookahead.Str();
75 
76 
77  return s.str();
78 }
79 
80 /*******************************
81 *
82 * Implementation of CompareConfigSet
83 *
84 */
85 bool CompareConfigSet(const std::set<Lr1Configuration>& lhs, const std::set<Lr1Configuration>& rhs){
86  if (lhs.size() < rhs.size()) return true; //also makes sure that it2 will not go out of bounds
87  if (rhs.size() < lhs.size()) return false;
88 
89  std::set<Lr1Configuration>::const_iterator it1, it2;
90  it2 = rhs.begin();
91  for(it1 = lhs.begin(); it1 != lhs.end(); it1++){
92  if(*it1 < *it2) return true;
93  if(*it2 < *it1) return false;
94  it2++;
95  }
96  return false;
97 }
98 
99 /*******************************
100 *
101 * Implementation of CompareConfigGsPair
102 *
103 */
105  if(*lhs.second < *rhs.second) return true;
106  if(*rhs.second < *lhs.second) return false;
107  if(CompareConfigSet(lhs.first,rhs.first)) return true;
108  return false;
109 }
110 
111 /*******************************
112 *
113 * Implementation of ConfigSetToStr
114 *
115 */
116 std::string ConfigSetToStr(const std::set<Lr1Configuration>& configs){
117  std::stringstream s;
118  std::set<Lr1Configuration>::const_iterator configit;
119  for(configit = configs.begin(); configit != configs.end(); configit++){
120  if(configit != configs.begin()){
121  s << ", ";
122  }
123  s << "(" << configit->Str() <<")";
124  }
125  return s.str();
126 }
127 
128 /*******************************
129 *
130 * Implementation of ConfigSetSetToStr
131 *
132 */
133 std::string ConfigSetSetToStr(const Lr1ConfigurationSetSet configSetSet){
134  std::stringstream s;
135  Lr1ConfigurationSetSet::const_iterator setit;
136  int i = 0;
137  for(setit = configSetSet.begin(); setit != configSetSet.end(); setit++){
138  s << i << ":\n";
139  s << " " << ConfigSetToStr(*setit) << "\n";
140  i++;
141  }
142  return s.str();
143 }
144 /*******************************
145 *
146 * Implementation of TransitionMapToStr
147 *
148 */
149 std::string TransitionMapToStr(const LrmTransitionMap& transitionMap){
150  std::stringstream s;
151  LrmTransitionMap::const_iterator transmapit;
152  std::set<Lr1Configuration>::const_iterator configit;
153  for(transmapit = transitionMap.begin(); transmapit != transitionMap.end(); transmapit++){
154  s << "CONFIGURATION SET " << ConfigSetToStr(transmapit->first.first) << "\n";
155  s << " with symbol " << transmapit->first.second->Str() << ":\n";
156  s << " leads to " << ConfigSetToStr(transmapit->second) << "\n";
157  }
158  return s.str();
159 }
160 
161 /*******************************
162 *
163 * Implementation of Lr1ParserActionElement
164 *
165 */
167 
168  //compare nextTerminal
169  if(mNextTerminal < other.NextTerminal()) return true;
170  if(other.NextTerminal() < mNextTerminal) return false;
171 
172  //compare stateStack
173  if (mStateStack.size() < other.StateStack().size()) return true; //also makes sure that it2 will not go out of bounds
174  if (other.StateStack().size() < mStateStack.size()) return false;
175 
176  std::vector<Idx>::const_iterator it1, it2;
177  it2 = other.StateStack().begin();
178  for(it1 = mStateStack.begin(); it1 != mStateStack.end(); it1++){
179  if(*it1 < *it2) return true;
180  if(*it2 < *it1) return false;
181  it2++;
182  }
183  return false;
184 }
185 
186 std::string Lr1ParserActionElement::Str() const{
187  std::stringstream s;
188  std::vector<Idx>::const_iterator it;
189  for(it = mStateStack.begin(); it != mStateStack.end(); it++){
190  if (it == mStateStack.begin())
191  s << "(" << *it;
192  else
193  s << ", " << *it;
194  }
195  s << " | " << mNextTerminal.Str() << ")";
196  return s.str();
197 }
198 
199 /*******************************
200 *
201 * Implementation of Lr1ParserAction
202 *
203 */
204 
206  if (mLhs < other.Lhs()) return true;
207  if (other.Lhs() < mLhs) return false;
208  if (mRhs < other.Rhs()) return true;
209  return false;
210 }
211 
212 std::string Lr1ParserAction::Str() const{
213  std::stringstream s;
214  s << mLhs.Str() << " -> " << mRhs.Str();
215  if(mIsReduce){
216  s << " [" << mProduction.Str() << "]";
217  }
218  return s.str();
219 }
220 
221 /*******************************
222 *
223 * Implementation of Lr1Parser
224 *
225 */
226 
228  mStartState = start;
229  return mNonterminals.insert(start).second;
230 }
231 
233  // mFinalState = final;
234  mFinalStates.insert(final);
235  return mNonterminals.insert(final).second;
236 }
237 
239  return mNonterminals.insert(nt).second;
240 }
241 
243  return mTerminals.insert(t).second;
244 }
245 
247  return mActions.insert(action).second;
248 }
249 
251  mAugSymbol = t;
252 }
253 
254 
255 std::string Lr1Parser::StrFinalStates() const{
256  std::string s = "";
257  std::set<Idx>::const_iterator it;
258  for(it = mFinalStates.begin(); it != mFinalStates.end(); it++){
259  if (it == mFinalStates.begin())
260  s += static_cast<std::ostringstream*>( &(std::ostringstream() << *it) )->str();
261  else
262  s += ", " + static_cast<std::ostringstream*>( &(std::ostringstream() << *it) )->str();
263  }
264  return s;
265 }
266 
267 std::string Lr1Parser::StrNonterminals() const{
268  std::string s = "";
269  std::set<Idx>::const_iterator it;
270  for(it = mNonterminals.begin(); it != mNonterminals.end(); it++){
271  if (it == mNonterminals.begin())
272  s += static_cast<std::ostringstream*>( &(std::ostringstream() << *it) )->str();
273  else
274  s += ", " + static_cast<std::ostringstream*>( &(std::ostringstream() << *it) )->str();
275  }
276  return s;
277 }
278 
279 std::string Lr1Parser::StrTerminals() const{
280  std::string s = "";
281  std::set<Terminal>::const_iterator it;
282  for(it = mTerminals.begin(); it != mTerminals.end(); it++){
283  if (it == mTerminals.begin())
284  s += it->Str();
285  else
286  s += ", " + it->Str();
287  }
288  return s;
289 }
290 
291 std::string Lr1Parser::StrActions(std::string separator) const{
292  std::string s = "";
293  std::set<Lr1ParserAction>::const_iterator it;
294  for(it = mActions.begin(); it != mActions.end(); it++){
295  if (it == mActions.begin())
296  s += it->Str();
297  else
298  s += separator + it->Str();
299  }
300  return s;
301 }
302 
303 std::string Lr1Parser::Str() const{
304  std::stringstream s;
305  s << "************************" << std::endl;
306  s << " nonterminals: " << Nonterminals().size() << std::endl;
307  s << " terminals: " << Terminals().size() << std::endl;
308  s << " actions: " << Actions().size() << std::endl;
309  s << " final states: " << FinalStates().size() << std::endl;
310  s << "************************" << std::endl << std::endl;
311 
312  s << "start state: " << mStartState << std::endl;
313  // s << "final state: " << mFinalState << std::endl;
314  s << "final state: " << StrFinalStates() << std::endl;
315  s << "augment symbol: " << mAugSymbol.Str() << std::endl;
316  s << "nonterminals: " << StrNonterminals() << std::endl;
317  s << "terminals: " << StrTerminals() << std::endl;
318  s << "actions: \n" << StrActions("\n") << std::endl;
319  return s.str();
320 }
321 
322 } // namespace faudes
323 

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