CTTL on
SourceForge |
Download
Latest |
Documentation
Index |
Library
News |
CVS
Repository |
Other
Links |
Demonstrates cttl::edge_replace
function object class. The program replaces words (substrings) inside input text.
// sample code: edge_functors.cpp // demonstrates cttl::edge_replace class. //#define NDEBUG // must appear before assert.h is included to stop assertions from being compiled //#define CTTL_TRACE_EVERYTHING #include <iostream> #include "cttl/cttl.h" #include "cttl/edge_functors.h" using namespace cttl; struct parser { std::vector< edge<> > word_edges; // semantic actions: size_t accumulate_words( const_edge<>& edge_ ) { // remember each word that was found: word_edges.push_back( new_edge( edge_ ) ); return edge_.second.offset(); } }; template< typename ParserT > struct lexer : public ParserT { size_t start( const_edge< policy_space<> >& universe_ ) { return ( +( isalpha & rule( *this, &ParserT::accumulate_words ) ) ).match( universe_ ) ; } }; int main(int argc, char* argv[]) { if ( argc == 1 ) { std::cout << "Usage: on command the line, enter some words to process, for example," << std::endl << '\t' << argv[ 0 ] << " abc def ghi" << std::endl ; return 1; } // construct input string from the command line arguments: input<> inp( &argv[ 1 ], ' ' ); // construct universe to be parsed: const_edge< policy_space<> > universe( new_edge( inp ) ); // construct the parser: lexer< parser > word_parser; // count words: if ( word_parser.start( universe ) != std::string::npos ) { std::cout << "Input: " << inp.text() << std::endl; // Replace words with word marker: std::for_each( word_parser.word_edges.begin(), word_parser.word_edges.end(), edge_replace<>( "<WORD/>" ) ); std::cout << "Output: " << inp.text() << std::endl; } else { std::cout << "*** parser failed ***" << std::endl; return 1; } return 0; }
Input: one two three Output: <WORD/> <WORD/> <WORD/>
Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.