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

lambda grammars


SourceForge.net Logo     CTTL on    
    SourceForge    
    Download    
    Latest    
    Documentation    
    Index    
    Library    
    News    
    CVS    
    Repository    
   Other    
   Links    

lambda grammar

stack, kleene star, epsilon parser

std::stack< std::string > str_stack;

size_t grammar( edge<>& universe_ )
{
    return
        (
            *(
                symbol( isalpha )
                &
                *(
                    scalar( &str_stack ) = scalar( &universe_ )
                )
            )
        ).match( universe_ );
}

node text insertion

// insert digit names in a binary number, e.g. "01" -> "0zero1one"
#define CTTL_TRACE_EVERYTHING

#include "cttl/cttl.h"
#include "lambda/lambda.h"

using namespace cttl;

struct digit_parser {
    std::vector< std::string > vect_digit_names;
    lambda< char >::scalar sdigit;

    digit_parser()
        : vect_digit_names( 2 )
    {
        vect_digit_names[ 0 ] = "zero";
        vect_digit_names[ 1 ] = "one";
    }

    std::string get_digit_name( char digit_ ) const
    {
        assert( digit_ == 0 || digit_ == 1 );
        return vect_digit_names[ digit_ ];
    }

    size_t grammar( edge<>& edge_ )
    {
        return (
            *(
                first( isdigit )
                +
                *(
                    // get digit:
                    sdigit = scalar( edge_.first )[ -1 ] - '0',
                    // insert digit name:
                    scalar( edge_.first )
                    += // += insert after, -= before
                    *scalar( CTTL_MEMBER_ACTION(
                            *this,
                            std::mem_fun( &digit_parser::get_digit_name ),
                            sdigit.top()
                    ))
                )
            )
        ).match( edge_ );
    }

};

int main(int argc, char* argv[])
{
    if ( argc == 1 ) {
        std::cout
            << "usage: specify binary number, for example:"
            << std::endl
            << argv[ 0 ] << "01101"
            ;
        return 1;
    }

    input<> inp( argv[ 1 ] );
    assert( inp.length() );
    edge<> universe( new_edge< std::string >( inp ) );

    digit_parser parser;
    if ( parser.grammar( universe ) != std::string::npos ) {
        std::cout << inp.text();
        return 0;
    }

    std::cout << "*** parser failed ***" << std::endl;
    return 1;
}

grammar production in lambda expression

#define CTTL_TRACE_EVERYTHING

#include "cttl/cttl.h"
#include "lambda/lambda.h"

using namespace cttl;

template< typename UniverseT >
size_t grammar( UniverseT const& edge_ )
{
    return edge_.second.offset();
}

int main(int argc, char* argv[])
{
    lambda< size_t >::scalar sresult;
    input<> inp( "abc" );
    typedef const_edge<> UniverseT;
    UniverseT universe( new_edge( inp ) );

    // invoke grammar production using CTTL lambda expression:
    (
        // call function template with one constant argument:
        sresult = *scalar(
            CTTL_STATIC_ACTION(
                std::ptr_fun( &grammar< UniverseT > ),
                universe
            )
        ),
        CTTL_LAMBDA_ASSERT( sresult != std::string::npos ),
        CTTL_LAMBDA_ASSERT( sresult == size_t( 3 ) )

    ).evaluate();


    // invoke grammar production using CTTL function adaptor:
    size_t result = 
        (
            CTTL_STATIC_RULE( &grammar< UniverseT > )
        ).match( universe );

    assert( result == size_t( 3 ) );

    return 0;
}

closure compared to CTTL function adaptor call

#define CTTL_TRACE_EVERYTHING

#include "cttl/cttl.h"
#include "lambda/lambda.h"

using namespace cttl;

template< typename UniverseT >
size_t grammar( UniverseT& edge_ )
{
    return ( entity( isalpha ) ).match( edge_ );
}

int main(int argc, char* argv[])
{
    lambda< size_t >::scalar sresult;
    input<> inp( "abc" );
    typedef const_edge<> UniverseT;
    UniverseT universe( new_edge( inp ) );

    universe.push();

    // invoke grammar production using CTTL lambda expression:
    (
        // call function template with one mutable argument:
        sresult = *scalar(
            CTTL_STATIC_ACTION(
                std::ptr_fun( &grammar< UniverseT > ),
                &universe
            )
        ),
        CTTL_LAMBDA_ASSERT( sresult != std::string::npos )

    ).evaluate();

    universe.pop();

    // invoke grammar production using CTTL function adaptor:
    size_t result = 
        (
            CTTL_STATIC_RULE( &grammar< UniverseT > )
        ).match( universe );

    assert( result != std::string::npos );

    return 0;
}



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:48:53 2006 for CTTL Lambda Expression by  doxygen 1.3.9.1