Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

edge_functors.cpp


SourceForge.net Logo     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;
}
This program displays the following output:
Input: one two three
Output: <WORD/> <WORD/> <WORD/>



Copyright © 1997-2006 Igor Kholodov mailto:cttl@users.sourceforge.net.

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.


Generated on Thu Nov 2 17:44:55 2006 for Common Text Transformation Library by  doxygen 1.3.9.1