pd_alg_sub.h
Go to the documentation of this file.
1 /** @file pd_alg_sub.h algorithm subfunctions*/
2 
3 /* Pushdown plugin for FAU Discrete Event Systems Library (libfaudes)
4 
5  Copyright (C) 2013/14 Ramon Barakat, Stefan Jacobi, Sven Schneider, Anne-Kathrin Hess
6 
7  */
8 
9 #ifndef FAUDES_PD_ALG_SUB_H
10 #define FAUDES_PD_ALG_SUB_H
11 
12 #include "corefaudes.h"
13 #include "pd_include.h"
14 
15 
16 namespace faudes {
17 
18 //Convenience definition for a vector which contains each transformation from PDA to SPDA by function SPDA
19 typedef std::vector<std::vector<std::pair<Idx,MergeTransition> > > TransformationHistoryVec;
20 
21 /**
22  * Associates each state of the generator with an annotation. Each state's
23  * merge attribute will be set to a MergeStateAnnotation. The MergeStateAnnotation
24  * will contain the annotation.
25  *
26  * (previously called RenQ)
27  *
28  * @param word
29  * word with which the states will be annotated
30  * @param pd
31  * reference to pushdown automaton to be annotated
32  * @return
33  * copy of the parameter automaton with annotated states
34  */
35 PushdownGenerator AnnotatePdStates(const std::string word,
36  const PushdownGenerator& pd);
37 
38 /**
39  * Adds an annotation to each stack symbol of the generator. Each stack symbol will
40  * be copied and added to to generator with the annotation preceding the original
41  * name.
42  *
43  * (previously called RenG)
44  *
45  * @param word
46  * word with which the stack symbols will be annotated
47  * @param pd
48  * reference to pushdown automaton to be annotated
49  *
50  * @return
51  * copy of the parameter automaton with annotated states
52  */
53 PushdownGenerator RenameStackSymbold(const std::string word,
54  const PushdownGenerator& pd);
55 
56 /**
57  * Make generator trim
58  *
59  * This function removes all inaccessible states,
60  * useless transitions and unused stack symbols,
61  * with regard to the top n elements of the stacks.
62  * Useless transitions are those outgoing transitions, that requiring stack-top never fulfills.
63  * The function also removes states, that do not reach a marked state (with regard to the top n elements)
64  * by calling vGenerator::trim after removing foregoing transitions.
65  *
66  * Note: For n = 0 the pushdown generator will call vGenerator::trim,
67  * therefore no useless transitions or unused stack symbols will be removed.
68  *
69  * @param pd
70  * reference to pushdown automaton to be trim
71  * @para n
72  * lookahead
73  * @param debug
74  * Set true to debug
75  *
76  * @return
77  * True if resulting generator contains at least one initial state and at least one marked state.
78  */
79 bool PushdownTrim(PushdownGenerator& rPd, const uint n, bool debug = false);
80 
81 /**
82  * Fill map. For each state entered, which states are reachable (directly or transitively)
83  * with regard to the top n elements of the stacks.
84  *
85  * @param rPd
86  * reference to pushdown generator
87  * @param n
88  * lookahead
89  * @param rMap
90  * reference to resulting map
91  *
92  * @return
93  */
94 void ReachableStateMap(const PushdownGenerator& rPd, std::map<Idx, StateSet>& rMap);
95 
96 /**
97  * (previously called Rpp)
98  * Restricts a pushdown generator to a simple pushdown generator SPDA with transitions that
99  * are either
100  * read (p,a,lambda,lambda,q),
101  * pop (p,lambda,x,lambda,q) or
102  * push (p,lambda,x,yx,q).
103  *
104  * The parameter pTransformHistory is necessary to remember the transformations of each iteration.
105  * To rebuild the former generator see RebuildFromSPDA()
106  *
107  * Note: To rebuild the former generator, the idx's of the states must not have been changed.
108  *
109  * @param pd
110  * the generator, which to convert into SPDA
111  *
112  * @param pTransformHistory
113  * pointer to vector to remember the transformations for each iteration
114  * By default no pointer is needed (NULL)
115  *
116  * @return
117  * simple pushdown generator
118  */
120  TransformationHistoryVec* pTransformHistory = NULL);
121 
122 /**
123  * Input must have passed SPDA() !
124  * Rebuild a pushdown generator which passed SPDA as far as possible.
125  * This mean, if a transition t of the original generator was split into two or more transitions t1...tn of the SPDA,
126  * this transition t only will be rebuild, iff t1...tn still exists.
127  * After rebuilding, inaccessible states will be removed.
128  *
129  * Note: Between the call of SPDA() and RebuildFromSPDA()
130  * the indices of states and events as well as stack symbol names must not have be changed.
131  *
132  * @param rSPDA
133  * reference to the generator which passed SPDA
134  * @param rTransformHistory
135  * reference to vector of each transformation per iteration
136  *
137  * @return
138  * pushdown generator
139  */
141  const TransformationHistoryVec& rTransformHistory);
142 
143 /**
144  * Input must have passed SPDA() !
145  * Compares the given pushdown generator with the given grammar and removes all transition,
146  * which cannot be find in productions of the grammar.
147  * After that, inaccessible states will be removed.
148  *
149  * @param rGr
150  * reference to grammar
151  * @param pd
152  * reference to generator
153  *
154  * @return
155  * remaining generator
156  */
158 
159 /**
160  * Input must have passed SPDA() !
161  * Remodels the generator to prevent double (or multiple) acceptance of the same
162  * input string.
163  *
164  * @param pd
165  * the generator
166  * @return
167  * remodeled generator without double acceptance
168  *
169  * @exception Exception
170  * if a transition is neither read nor pop nor push transition
171  */
173 
174 /**
175  * Input must have passed SPDA() and Nda() !
176  * Transform a simple pushdown generator into a context free grammar.
177  * To reduce the number of to create productions, the generator will be trim
178  * (by calling PushdownGeneratorTrim )
179  *
180  * @param pd
181  * reference to pushdown generator
182  * @param n
183  * the lookahead for calling PushdownGeneratorTrim
184  * @param ignorReducible
185  * by set ignorReducible to false, only productions are generated which are coaccessible.
186  * @param debug
187  * Set true, to print out single steps of the function
188  *
189  * @return
190  * the grammar
191  *
192  * @exception Exception
193  * if a transition is neither read nor pop nor push transition
194  */
195 Grammar Sp2Lr(const PushdownGenerator& rPd, uint n=0, bool ignorReducible = false, bool debug = false);
196 
197 /**
198  * Extract all symbols from the word that are in the symbol set
199  *
200  * @param symbolSet
201  * the symbol set
202  * @param w
203  * the word
204  * @return
205  * set of found symbols
206  */
207 std::set<Nonterminal> Filter(const std::set<Nonterminal>& symbolSet, const GrammarSymbolVector& w);
208 
209 /**
210  * Find all nonterminals that are eliminable in one step, assuming that a certain
211  * set of nonterminals has already been eliminated.
212  *
213  * @param gr
214  * the grammar
215  * @param ntSet
216  * the set of already eliminated nonterminals
217  * @return
218  * eliminable symbols including the already eliminated ones
219  */
220 std::set<Nonterminal> Rnpp1(const Grammar& gr, const std::set<Nonterminal>& ntSet);
221 
222 /**
223  * Find all nonterminals that are eliminable in as many steps as needed, assuming
224  * that a certain set of nonterminals has already been eliminated.
225  *
226  * @param gr
227  * the grammar
228  * @param ntSet
229  * the set of already eliminated nonterminals
230  * @return
231  * eliminable symbols including the already eliminated ones
232  */
233 std::set<Nonterminal> Rnppl(const Grammar& gr, const std::set<Nonterminal>& ntSet);
234 
235 /**
236  * Remove all productions from a grammar that are nonproductive, i. e. those
237  * containing nonterminals that are not eliminable.
238  *
239  * @param gr
240  * reference to grammar
241  * @return
242  * grammar with only productive productions
243  */
244 Grammar Rnpp(const Grammar& gr);
245 
246 /**
247  * Remove all unreachable productions and nonterminals from the grammar
248  *
249  * @param gr
250  * grammar with productions to be removed
251  *
252  * @return
253  * grammar with only reachable productions
254  */
255 Grammar Rup(const Grammar& gr);
256 
257 
258 /**
259  * (previously called Rep2)
260  * Removes transitions popping more than one stack symbol.
261  * Inserts new transitions instead that only pop one stack symbol.
262  *
263  * @param pd
264  * the generator, which must NOT contain lambda pops
265  * @return
266  * generator with altered transitions
267  *
268  * @exception Exception
269  * if a lambda pop transition exists
270  */
272 
273 /**
274  * Getting the number of incoming transitions of a state
275  *
276  * @param trg
277  * idx of target state
278  * @param rPd
279  * reference to pushdown generator
280  *
281  * @return
282  * number of incoming transitions
283  */
285 
286 
287 /**
288  * Merge adjacent transitions until no more mergers are possible.
289  * This will reduce the number of transitions and states.
290  * The marked language does not change.
291  *
292  * Technical detail:
293  * Let qb be the state between transitions t1 and t2.
294  * t1 and t2 will be merged, if
295  * * qb is no marked state
296  * * t1 is the only transition, that leads to qb
297  * * for all transitions t' from qp:
298  * - t' is no self-loop transition
299  * - if t1->Ev != lambda then t'->Ev = lambda
300  * * the from t2 requiring stack-top is pushed by t1
301  *
302  * @param rPd
303  * reference to pushdown generator
304  */
306 
307 /**
308  * Merge transitions from state q with adjacent transitions
309  * without changing the marked language. ( see PossibleAdjacentMerges() )
310  *
311  * @param q
312  * idx of state q
313  * @param rPd
314  * reference to pushdown generator
315  * @param next
316  * reference to set of states to be checked next.
317  * If no transitions were merged, all successor states of q will be added to next.
318  * If transitions were merged, state q will be added to next.
319  * @param done
320  * reference to set of states which already checked
321  * If no transitions was merged, state q will be added to done.
322  * @return
323  * true, if a merger was made. False if not
324  */
325 bool CombinedTransitions(Idx q,PushdownGenerator& rPd, /*std::set<Idx>&*/ StateSet& next,/*std::set<Idx>&*/ StateSet& done);
326 
327 /*
328  * Ascertain on transitions (from state q) adjacent transitions which are possibly mergeable
329  * without changing the marked language.
330  *
331  *Technical details:
332  * Let qb be the state between transitions t1 and t2.
333  * t1 and t2 are possible mergeable if:
334  * * qb is no marked state
335  * * t1 is the only transition, that leads to qb
336  * * for all transitions t' from qp:
337  * - t' is no self-loop transition
338  * - if t1->Ev != lambda then t'->Ev = lambda
339  *
340  * @param q
341  * idx of state q
342  * @param pd
343  * reference to pushdown generator
344  *
345  * @return
346  * set of pairs of possible mergeable transitions
347  */
348 std::set<std::pair <Transition,Transition> > PossibleAdjacentMerges (Idx q, const PushdownGenerator& pd);
349 
350 
351 /**
352  * (previously called Rep0)
353  * Removes all transitions popping lambda.
354  * Expect the generator to be deterministic
355  *
356  * Note:
357  * By the new treatment of Transitions like (p,a,pop,push,q) with a != lambda and pop = push,
358  * there is no need to clear old init state and create a new init state which
359  * derived from pd.InitState()
360  *
361  * @param pd
362  * pushdown automaton
363  * @return
364  * pushdown automaton without lambda popping edges
365  */
367 
368 } // namespace faudes
369 
370 #endif

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