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

rules.cpp


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

Sample program implementing grammar expressions to parse fractional numbers.

// sample code: rules.cpp
// demonstrates EBNF grammar rules

#include "cttl/cttl.h"

using namespace cttl;

// EBNF grammar for fractional number can be written as: 
//
//  <start>  := '-'* <fract>
//  <fract>  := <digits> ('.' <digits>)*
//  <digits> := ( '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' )+
//
// With cttl, these rules can be written as C++ functions:
//

size_t digits( const_edge<>& universe_ )
{
    return entity( isdigit ).match( universe_ );
}

size_t fract( const_edge<>& universe_ )
{
    return (
        rule( digits )
        +
        ( '.' + rule( digits ) ) * 1

    ).match( universe_ );
}

size_t start( const_edge<>& universe_ )
{
    return ( *symbol( '-' ) + rule( fract ) ).match( universe_ );
}

int main()
{
    input<> inp( "123.45" );
    const_edge<> U( new_edge( inp ) );
    size_t P = rule( start ).match( U );
    assert( P == 0 );
    assert( U.length() == 0 );
    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:44:56 2006 for Common Text Transformation Library by  doxygen 1.3.9.1