pd_grammar.h

Go to the documentation of this file.
00001 /** @file pd_merge.h  grammar 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_GRAMMAR_H
00012 #define FAUDES_PD_GRAMMAR_H
00013 
00014 #include "corefaudes.h"
00015 #include "pd_pdgenerator.h"
00016 #include <tr1/memory>
00017 
00018 namespace faudes {
00019 
00020 /**
00021 * Grammar Symbol
00022 *
00023 * 
00024 *
00025 * @ingroup PushdownPlugin
00026 * 
00027 * @section Overview
00028 *  Overview
00029 * 
00030 * @section Contents
00031 *  Contents
00032 */
00033 class GrammarSymbol{
00034   
00035   public:
00036   
00037   /**
00038    * To String function
00039    */
00040   virtual std::string Str() const = 0;
00041   
00042   /**
00043    * < operator
00044    * @param other
00045    *    cmp
00046    * @return 
00047    *    true if this < cmp, else false
00048    */
00049    virtual bool operator<(const GrammarSymbol& other) const = 0;
00050    
00051    /**
00052    * == operator
00053    * @param other
00054    *    cmp
00055    * @return 
00056    *    true if this == cmp, else false
00057    */
00058    bool operator==(const GrammarSymbol& other) const;
00059    
00060    /**
00061    * != operator
00062    * @param other
00063    *    cmp
00064    * @return 
00065    *    true if this != cmp, else false
00066    */
00067    bool operator!=(const GrammarSymbol& other) const;
00068    
00069    /**
00070     * Clone function. Allocates memory!
00071     * 
00072     * @return
00073     *   pointer to cloned object
00074     */
00075    virtual GrammarSymbol* Clone() const = 0;
00076   
00077 };//class GrammarSymbol
00078 
00079 /**
00080 * Terminal
00081 *
00082 * 
00083 *
00084 * @ingroup PushdownPlugin
00085 * 
00086 * @section Overview
00087 *  Overview
00088 * 
00089 * @section Contents
00090 *  Contents
00091 */
00092 class Terminal : public GrammarSymbol{
00093 
00094   public:
00095   
00096   /** Symbol representing the terminals */
00097   //std::string mSymbol;
00098   /** Event representing the terminal */
00099   Idx mEvent;
00100   
00101   
00102   /**
00103    * Constructor
00104    * 
00105    * @param event
00106    *    the event
00107    */
00108   Terminal(const Idx event) : mEvent(event){};
00109   
00110   /**
00111    * Getter for event
00112    * 
00113    * @return
00114    *    the event
00115    */
00116   Idx Event() const{return mEvent;}
00117   
00118   /**
00119    * Determine if the event is lambda
00120    * 
00121    * @return
00122    *    true if it is lambda, else false
00123    */
00124   bool IsLambda() const;
00125   
00126   /**
00127    * To String function
00128    */
00129   std::string Str() const;
00130   
00131   /**
00132    * < operator
00133    * @param other
00134    *    cmp
00135    * @return 
00136    *    true if this < cmp, else false
00137    */
00138    bool operator<(const GrammarSymbol& other) const;
00139    
00140    /**
00141     * Clone function. Allocates memory!
00142     * 
00143     * @return
00144     *   pointer to cloned Terminal
00145     */
00146    Terminal* Clone() const;
00147    
00148 };//class Terminal
00149 
00150 /**
00151  * Nonterminals are constructed from two states and a
00152  * stack symbol or one state and a stack symbol
00153 *
00154 * 
00155 *
00156 * @ingroup PushdownPlugin
00157 * 
00158 * @section Overview
00159 *  Overview
00160 * 
00161 * @section Contents
00162 *  Contents
00163 */
00164 class Nonterminal : public GrammarSymbol{
00165   
00166   public: 
00167   
00168   /** The start state*/
00169   Idx mStartState;
00170   /** The stack symbols*/
00171   std::vector<Idx> mOnStack;
00172    /** The end state */
00173   Idx mEndState;
00174   
00175   
00176   /**
00177    * Constructor for NonterminalEnd
00178    */
00179   Nonterminal(const Idx state, const std::vector<Idx> symbols):mStartState(state), mOnStack(symbols), mEndState(0){}
00180   
00181   /**
00182    * Constructor for NonterminalMid
00183    */
00184   Nonterminal(const Idx state1, const std::vector<Idx> symbols, const Idx state2) : mStartState(state1), mOnStack(symbols), mEndState(state2){}
00185   
00186   /**
00187    * Getter for startState
00188    * 
00189    * @return
00190    *    Idx of the startState
00191    */
00192   Idx StartState() const{return mStartState;}
00193   
00194   /**
00195    * Getter for onStack
00196    * 
00197    * @return
00198    *    indices of the stack symbols
00199    */
00200   const std::vector<Idx>& OnStack() const {return mOnStack;}
00201   
00202   /**
00203    * Getter for endState
00204    * 
00205    * @return
00206    *    Idx of the endState
00207    */
00208   Idx EndState() const {return mEndState;}
00209   
00210   /**
00211    * To String function
00212    */
00213   virtual std::string Str() const;
00214   
00215   /**
00216    * < operator
00217    * @param other
00218    *    cmp
00219    * @return 
00220    *    true if this < cmp, else false
00221    */
00222    bool operator<(const GrammarSymbol& other) const;
00223    
00224    /**
00225     * Clone function. Allocates memory!
00226     * 
00227     * @return
00228     *   pointer to cloned nonterminal
00229     */
00230    Nonterminal* Clone() const;
00231 
00232 };//class Nonterminal
00233 
00234 //convenience definition for a grammar symbol pointer that manages its own memory
00235 typedef std::tr1::shared_ptr<GrammarSymbol> GrammarSymbolPtr;
00236 typedef std::tr1::shared_ptr<const GrammarSymbol> ConstGrammarSymbolPtr;
00237 
00238 //convenience definition for a nonterminal pointer that manages its own memory
00239 typedef std::tr1::shared_ptr<Nonterminal> NonterminalPtr;
00240 typedef std::tr1::shared_ptr<const Nonterminal> ConstNonterminalPtr;
00241 
00242 //convenience definition for a terminal pointer that manages its own memory
00243 typedef std::tr1::shared_ptr<Terminal> TerminalPtr;
00244 typedef std::tr1::shared_ptr<const Terminal> ConstTerminalPtr;
00245 
00246 //convencience definition for a vector of grammar symbols
00247 typedef std::vector<GrammarSymbolPtr> GrammarSymbolVector;
00248 
00249 //comparator for two grammar symbol vectors
00250 bool CompareGsVector(const GrammarSymbolVector& lhs, const GrammarSymbolVector& rhs);
00251 
00252 struct GsVectorComparator {
00253   bool operator() (const GrammarSymbolVector& lhs, const GrammarSymbolVector& rhs) const{
00254     return CompareGsVector(lhs,rhs);
00255   }
00256 };
00257 
00258 //equals for two grammar symbol vectors
00259 bool EqualsGsVector(const GrammarSymbolVector& lhs, const GrammarSymbolVector& rhs);
00260 
00261 //comparator for two grammar symbols
00262 bool CompareGs(const GrammarSymbolPtr& lhs, const GrammarSymbolPtr& rhs);
00263 
00264 struct GsComparator {
00265   bool operator() (const GrammarSymbolPtr& lhs, const GrammarSymbolPtr& rhs) const{
00266     return CompareGs(lhs,rhs);
00267   }
00268 };
00269 
00270 //convencience definition for a set of grammar symbols
00271 typedef std::set<GrammarSymbolPtr, GsComparator> GrammarSymbolSet;
00272 
00273 //convencience definition for a set of vectors of grammar symbols (a set of words)
00274 typedef std::set<GrammarSymbolVector,GsVectorComparator> GrammarSymbolWordSet;
00275 
00276 typedef std::map<GrammarSymbolVector, std::set<Terminal>, GsVectorComparator> GrammarSymbolWordMap;
00277 
00278 /**
00279 * Test if a given set of words contains a specific word
00280 * 
00281 * @param set
00282 *      the set to be searched
00283 * @param word
00284 *      the word to be searched for
00285 * @return
00286 *      true, if the set contains the word, else false
00287 */
00288 bool ContainsWord(const GrammarSymbolWordSet& set, const GrammarSymbolVector& word);
00289 
00290 /**
00291 * Grammar Production
00292 *
00293 * 
00294 *
00295 * @ingroup PushdownPlugin
00296 * 
00297 * @section Overview
00298 *  Overview
00299 * 
00300 * @section Contents
00301 *  Contents
00302 */
00303 class GrammarProduction{
00304   
00305   public:
00306   
00307   /** Nonterminal left hand side */
00308   Nonterminal mLhs;
00309   /** Right hand side, can contain both Terminals and Nonterminals */
00310   GrammarSymbolVector mRhs;
00311   
00312   /**
00313    * Constructor
00314    */
00315   GrammarProduction(const Nonterminal& lhs, const GrammarSymbolVector& rhs) : mLhs(lhs), mRhs(rhs){};
00316   
00317   /**
00318    * Getter for mLhs
00319    * 
00320    * @return
00321    *    mLhs
00322    */
00323   Nonterminal const&  Lhs() const {return mLhs;}
00324   
00325   /**
00326    * Getter for mRhs
00327    * 
00328    * @return
00329    *    mRhs
00330    */
00331   GrammarSymbolVector const& Rhs() const {return mRhs;}
00332   
00333   /**
00334    * To String function
00335    */
00336   std::string Str() const;
00337   
00338     /**
00339    * < operator
00340    * @param other
00341    *    cmp
00342    * @return 
00343    *    true if this < cmp, else false
00344    */
00345    bool operator<(const GrammarProduction& other) const;
00346   
00347 };//class GrammarProduction
00348 
00349 /**
00350 * Grammar
00351 *
00352 * 
00353 *
00354 * @ingroup PushdownPlugin
00355 * 
00356 * @section Overview
00357 *  Overview
00358 * 
00359 * @section Contents
00360 *  Contents
00361 */
00362 class Grammar{
00363 
00364   public:
00365   
00366   /** the Terminals */
00367   std::set<Terminal> mTerminals;
00368   /** the Nonterminals */
00369   std::set<Nonterminal> mNonterminals;
00370   /** the Start Symbol */
00371   Nonterminal mStartSymbol;
00372   /** the Productions */
00373   std::set<GrammarProduction> mGrammarProductions;
00374   
00375   /**
00376    * Constructor
00377    */
00378   Grammar():mStartSymbol(Nonterminal(0,std::vector<Idx>(),0)) {}
00379   
00380   /**
00381    * Constructor
00382    */
00383   Grammar(const Nonterminal& startSymbol):mStartSymbol(startSymbol){}
00384   
00385   
00386   /**
00387    * set the grammar's start symbol and add it to the set of nonterminals
00388    * 
00389    * @param s
00390    *    start symbol to set
00391    * @return 
00392    *    true, if nonterminal did not exist in grammar, else false
00393    */
00394   bool SetStartSymbol(const Nonterminal& s);
00395   
00396   /**
00397    * add Terminal
00398    * 
00399    * @param t
00400    *    terminal to add
00401    * @return 
00402    *    true, if successful (terminal did not exist in grammar), else 
00403    * false (terminal did already exist in grammar)
00404    */
00405   bool InsTerminal(const Terminal& t);
00406   
00407   /**
00408    * add Terminals
00409    * 
00410    * @param t
00411    *    terminals to add
00412    */
00413   void InsTerminals(const std::set<Terminal>& t);
00414   
00415    /**
00416    * add Nonterminal
00417    * 
00418    * @param nt
00419    *    nonterminal to add
00420    * @return 
00421    *    true, if successful (nonterminal did not exist in grammar), else 
00422    * false (nonterminal did already exist in grammar)
00423    */
00424   bool InsNonterminal(const Nonterminal& nt);
00425   
00426   /**
00427    * add Nonterminals
00428    * 
00429    * @param nt
00430    *    nonterminals to add
00431    */
00432   void InsNonterminals(const std::set<Nonterminal>& nt);
00433   
00434   /**
00435    * Add a grammar production to the grammar. All grammar symbols used must exist
00436    * in the grammar.
00437    * 
00438    * @param gp
00439    *    grammar production to add
00440    * @return 
00441    *    true, if successful (grammar production did not exist in grammar), else 
00442    * false (grammar production did already exist in grammar)
00443    */
00444   bool InsGrammarProduction(const GrammarProduction& gp);
00445   
00446   /**
00447    * Add grammar productions to the grammar. All grammar symbols used must exist
00448    * in the grammar.
00449    * 
00450    * @param gp
00451    *    grammar productions to add
00452    */
00453   void InsGrammarProductions(const std::set<GrammarProduction>& gp);
00454   
00455   /**
00456    * Getter for mTerminals
00457    * 
00458    * @return
00459    *    the terminals
00460    */
00461   const std::set<Terminal>& Terminals() const { return mTerminals;}
00462   
00463   /**
00464    * Getter for mNonterminals
00465    * 
00466    * @return
00467    *    the nonterminals
00468    */
00469   const std::set<Nonterminal>& Nonterminals() const { return mNonterminals;}
00470   
00471   /**
00472    * Getter for mStartSymbol
00473    * 
00474    * @return
00475    *    the start symbol
00476    */
00477   const Nonterminal& StartSymbol() const { return mStartSymbol;}
00478   
00479   /**
00480    * Getter for mGrammarProductions
00481    * 
00482    * @return
00483    *    the grammar productions
00484    */
00485   const std::set<GrammarProduction>& GrammarProductions() const { return mGrammarProductions;}
00486   
00487   /**
00488    * Iterator to the beginning of terminals
00489    * 
00490    * @return
00491    *    iterator
00492    */
00493   std::set<Terminal>::const_iterator TerminalsBegin() const;
00494   
00495   /**
00496    * Iterator to the end of terminals
00497    * 
00498    * @return
00499    *    iterator
00500    */
00501   std::set<Terminal>::const_iterator TerminalsEnd() const;
00502   
00503   /**
00504    * Iterator to the beginning of nonterminals
00505    * 
00506    * @return
00507    *    iterator
00508    */
00509   std::set<Nonterminal>::const_iterator NonterminalsBegin() const;
00510   
00511   /**
00512    * Iterator to the end of nonterminals
00513    * 
00514    * @return
00515    *    iterator
00516    */
00517   std::set<Nonterminal>::const_iterator NonterminalsEnd() const;
00518   
00519   /**
00520    * Iterator to the beginning of grammar productions
00521    * 
00522    * @return
00523    *    iterator
00524    */
00525   std::set<GrammarProduction>::const_iterator GrammarProductionsBegin() const;
00526   
00527   /**
00528    * Iterator to the end of grammar productions
00529    * 
00530    * @return
00531    *    iterator
00532    */
00533   std::set<GrammarProduction>::const_iterator GrammarProductionsEnd() const;
00534   
00535   /**
00536    * To String function for Terminals
00537    */
00538   std::string StrTerminals() const;
00539   
00540   /**
00541    * To String function for Nonterminals
00542    */
00543   std::string StrNonterminals() const;
00544   
00545   /**
00546    * To String function for start symbol
00547    */
00548   std::string StrStartSymbol() const;
00549   
00550   /**
00551    * To String function for Grammar Productions
00552    */
00553   std::string StrGrammarProductions() const;
00554   
00555    /**
00556    * To String function
00557    */
00558   std::string Str() const;
00559 };//class Grammar
00560 
00561 } // namespace faudes
00562 
00563 #endif

libFAUDES 2.23h --- 2014.04.03 --- c++ api documentaion by doxygen