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; }
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.