Main Page | Class List | File List | Class Members | File Members

MachineGrammar.h

Go to the documentation of this file.
00001 
00005 //
00006 // Date:      20 August   2005   
00007 //
00008 // Copyright (c) Sergey Satskiy 2003 - 2005
00009 //               <sergesatsky@yahoo.com>
00010 //
00011 // Permission to copy, use, modify, sell and distribute this software 
00012 // is granted provided this copyright notice appears in all copies. 
00013 // This software is provided "as is" without express or implied
00014 // warranty, and with no claim as to its suitability for any purpose.
00015 //
00016 
00017 
00018 #ifndef MACHINEGRAMMAR_H
00019 #define MACHINEGRAMMAR_H
00020 
00021 
00022 #include <string>
00023 #include <vector>
00024 using namespace std;
00025 
00026 #include <boost/spirit/core.hpp>
00027 #include <boost/spirit/utility.hpp>
00028 #include <boost/spirit/symbols.hpp>
00029 using namespace boost;
00030 using namespace boost::spirit;
00031 
00032 
00033 
00035 struct SaveStartState
00036 {
00040     SaveStartState( string &  name ) : Name( name )
00041     {}
00042 
00047     void operator()( const char* const&  Start, const char* const& End ) const
00048     {
00049         Name = string( Start, End - Start );
00050     }  
00051 
00052     private:
00053         string &        Name;       // Reference to the start state
00054 };
00055 
00056 
00057 
00058 
00060 struct SStartStateGrammar: public grammar< SStartStateGrammar >
00061 {
00062     string &        Name;       // Reference to the start name
00063     
00067     SStartStateGrammar( string &  name ) : Name( name )
00068     {}
00069 
00071     template < typename  ScannerT >
00072     struct definition
00073     {
00074         definition( SStartStateGrammar const &  Self )
00075         {
00076             Identifier 
00077                 =   lexeme_d
00078                     [
00079                         (
00080                             ( alpha_p | '_' | '$' )
00081                             >> * ( alnum_p | '_' | '$' )
00082                         )
00083                     ]
00084                 ;
00085             
00086             StartStateLine
00087                 =   str_p( "start" ) >> (Identifier[ SaveStartState( Self.Name ) ])
00088                 ;
00089         }
00090 
00091         rule< ScannerT >    Identifier;
00092         rule< ScannerT >    StartStateLine;
00093 
00094 
00095         rule< ScannerT > const &  start( void ) const
00096         {
00097             return StartStateLine;
00098         }
00099     };
00100 };
00101 
00102 
00103 
00105 struct SaveState
00106 {
00110     SaveState( SStatesListProxy< string > &   states ) : States( states )
00111     {}
00112 
00117     void operator()( const char* const&  Start, const char* const& End ) const
00118     {
00119         States << string( Start, End - Start );
00120     }  
00121 
00122     private:
00123         SStatesListProxy< string > &      States;
00124 };
00125 
00126 
00127 
00129 struct SStatesGrammar: public grammar< SStatesGrammar >
00130 {
00131     SStatesListProxy< string > &      States;   // Reference to the states list
00132     
00136     SStatesGrammar( SStatesListProxy< string > &  states ) : States( states )
00137     {}
00138 
00140     template < typename  ScannerT >
00141     struct definition
00142     {
00143         definition( SStatesGrammar const &  Self )
00144         {
00145             Identifier 
00146                 =   lexeme_d
00147                     [
00148                         (
00149                             ( alpha_p | '_' | '$' )
00150                             >> * ( alnum_p | '_' | '$' )
00151                         )
00152                     ]
00153                 ;
00154             
00155             StatesLine
00156                 =   str_p( "states" ) >> Identifier[ SaveState( Self.States ) ] 
00157                                       >> * ( ',' >> Identifier[ SaveState( Self.States ) ] )
00158                 ;
00159         }
00160 
00161         rule< ScannerT >    Identifier;
00162         rule< ScannerT >    StatesLine;
00163 
00164 
00165         rule< ScannerT > const &  start( void ) const
00166         {
00167             return StatesLine;
00168         }
00169     };
00170 };
00171 
00172 
00173 
00174 
00176 struct SaveID
00177 {
00181     SaveID( vector< string > &   states ) : States( states )
00182     {}
00183 
00188     void operator()( const char* const&  Start, const char* const& End ) const
00189     {
00190         States.push_back( string( Start, End - Start ) );
00191     }  
00192 
00193     private:
00194         vector< string > &          States;
00195 };
00196 
00197 
00199 struct SEventsGrammar: public grammar< SEventsGrammar >
00200 {
00201     vector< string > &          Transitions;    // Reference to the IDs' list
00202     
00206     SEventsGrammar( vector< string > &  transitions ) : Transitions( transitions )
00207     {}
00208 
00210     template < typename  ScannerT >
00211     struct definition
00212     {
00213         definition( SEventsGrammar const &  Self )
00214         {
00215             Identifier 
00216                 =   lexeme_d
00217                     [
00218                         (
00219                             ( alpha_p | '_' | '$' )
00220                             >> * ( alnum_p | '_' | '$' )
00221                         )
00222                     ]
00223                 ;
00224             
00225             EventLine
00226                 =   str_p( "event" ) >> '(' >> Identifier[ SaveID( Self.Transitions ) ] >> ')' >>
00227                     Identifier[ SaveID( Self.Transitions ) ] >> 
00228                     * ( ',' >> Identifier[ SaveID( Self.Transitions ) ] )
00229                 ;
00230         }
00231 
00232         rule< ScannerT >    Identifier;
00233         rule< ScannerT >    EventLine;
00234 
00235 
00236         rule< ScannerT > const &  start( void ) const
00237         {
00238             return EventLine;
00239         }
00240     };
00241 };
00242 
00243 #endif
00244             

Generated on Mon Aug 22 04:58:11 2005 for Example of an FSM which reads description from a file by  doxygen 1.4.4