Ein Parser für einfache Ausdrücke in C++ (Ein Parser für einfache Ausdrücke in C++), Lektion, Seite 724296
https://www.purl.org/stefan_ram/pub/parser_c++ (Permalink) ist die kanonische URI dieser Seite.
Stefan Ram

Ein Parser für einfache Ausdrücke in C++

#include <iostream>

#include <ostream>

#include <map>

#include <functional>

#include <cmath>

#include <sstream>

#include <cstring>

::std::map< char, ::std::function< double( double, double )>> ex

{ { '^', []( double const x, double const y )->double

{ return ::std::pow( x, y ); }},

{ '*', []( double const x, double const y )->double{ return x * y; }},

{ '/', []( double const x, double const y )->double{ return x / y; }},

{ '+', []( double const x, double const y )->double{ return x + y; }},

{ '-', []( double const x, double const y )->double{ return x - y; }}};

::std::map< char, bool >left_associative

{ { '^', false }, { '*', true }, { '/', true }, { '+', true }, { '-', true }};

template< typename scanner >struct parser

{ scanner s; parser( char const * s ): s{ s } {}

char check( char const * const op )

{ return ::std::strchr( op, static_cast< char >( s.peek() )) ?

static_cast< char >( s.get() ): 0; }

double numeral(){ return s.get() - '0'; }

double prefix(){ int sign = 1;

while( '-' == s.peek() ){ s.get(); sign *= -1; } return sign * numeral(); }

double parse( char const * const op, double( ::parser< scanner >::*next ) () )

{ double result =( this->*next )();

for( char sym; sym = check( op ); )result = ex[ sym ]

( result, left_associative[ sym ]?( this->*next )(): parse( op, next ));

return result; };

double power(){ return parse( "^", &::parser< scanner >::prefix ); }

double product(){ return parse( "*/", &::parser< scanner >::power ); }

double sum(){ return parse( "+-", &::parser< scanner >::product ); }

double start(){ return sum(); }};

int main()

{ using p = ::parser< ::std::stringstream >;

auto test = []( char const * s ){ ::std::cout << p{ s }.start() << '\n'; };

test( "2^2^3/2/2" ); test( "2+2*2^2^-3" ); }

64

4.18102

Seiteninformationen und Impressum   |   Mitteilungsformular  |   "ram@zedat.fu-berlin.de" (ohne die Anführungszeichen) ist die Netzpostadresse von Stefan Ram.   |   Eine Verbindung zur Stefan-Ram-Startseite befindet sich oben auf dieser Seite hinter dem Text "Stefan Ram".)  |   Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram. Schlüsselwörter zu dieser Seite/relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram724296 stefan_ram:724296 Ein Parser für einfache Ausdrücke in C++ Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd724296, slrprddef724296, PbclevtugFgrsnaEnz Erklärung, Beschreibung, Info, Information, Hinweis,

Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram.
https://www.purl.org/stefan_ram/pub/parser_c++