Orxonox  0.0.5 Codename: Arcturus
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
orxonox::ExprParser Class Reference

Parser for expressions like "3 * cos(5 + 4) / a" where a is a predeclared variable. More...

#include </home/jenkins/workspace/orxonox_doxygen_trunk/src/libraries/util/ExprParser.h>

Public Types

enum  binary_operator {
  b_plus, b_minus, mal, durch,
  modulo, hoch, undef, oder,
  und, gleich, b_nicht, kleiner,
  groesser, ungleich, kleinergleich, groessergleich
}
 
enum  unary_operator { u_plus, u_minus, u_nicht }
 

Public Member Functions

 ExprParser ()
 
const std::string & getRemains ()
 
float getResult ()
 
bool getSuccess ()
 
void parse (const std::string &str)
 
void setVariable (const std::string &varname, float value)
 

Private Member Functions

float parse_argument ()
 
binary_operator parse_binary_operator ()
 
float parse_expr_1 ()
 
float parse_expr_2 ()
 
float parse_expr_3 ()
 
float parse_expr_4 ()
 
float parse_expr_5 ()
 
float parse_expr_6 ()
 
float parse_expr_7 ()
 
float parse_expr_8 ()
 
float parse_last_argument ()
 
unary_operator parse_unary_operator ()
 
void parse_word (char *str)
 

Private Attributes

bool failed_
 
binary_operator op
 
const char * reading_stream
 
std::string remains_
 
float result_
 
std::map< std::string, float > variables_
 

Detailed Description

Parser for expressions like "3 * cos(5 + 4) / a" where a is a predeclared variable.

Usage
Using it is rather simple:
std::string str("3 + 4");
expr.parse(str);
if (expr.getSuccess())
{
if (!expr.getRemains().empty())
{
orxout(user_warning) << "Expression could not be parsed to the end! Remains: '" << expr.getRemains() << '\'' << endl;
}
float result = expr.getResult();
}
else
orxout(user_error) << "Cannot calculate expression: Parse error." << endl;
getRemains() returns the expression after what could be parsed. For instance "2*3 text" will return "text" as remains.
The implementation of this class is really old and sort of a trial for
a first C++ class by Reto. That explains why it looks more like C than
C++... Also some of the variable names are in German. <br>
Explaining how it works exactly is probably not possible anymore, but it
is based on recursively parsing the expression character by character.
That much I can remember.
Functions, operators and variables supported
  • Variables:
    • e
    • pi
  • Functions:
    • sin, asin, sinh, asinh
    • cos, acos, cosh, acosh
    • tan, atan, tanh, atanh
    • int, floor, ceil, abs, sign
    • pow, sqrt, exp, ln, log
    • mod, div
    • min, max
    • radians, degrees
  • Operators:
    • +, -, ! (unary)
    • +, -, *, /, %, ^, |, &, !, <, >, !=, <=, >=, =
Note
Operators may not be very consistent with C++ rules, but using the class for plus and minus should be perfectly ok.

Member Enumeration Documentation

Enumerator
b_plus 
b_minus 
mal 
durch 
modulo 
hoch 
undef 
oder 
und 
gleich 
b_nicht 
kleiner 
groesser 
ungleich 
kleinergleich 
groessergleich 
Enumerator
u_plus 
u_minus 
u_nicht 

Constructor & Destructor Documentation

orxonox::ExprParser::ExprParser ( )

Member Function Documentation

const std::string& orxonox::ExprParser::getRemains ( )
inline
float orxonox::ExprParser::getResult ( )
inline
bool orxonox::ExprParser::getSuccess ( )
inline
void orxonox::ExprParser::parse ( const std::string &  str)
float orxonox::ExprParser::parse_argument ( )
private
ExprParser::binary_operator orxonox::ExprParser::parse_binary_operator ( )
private
float orxonox::ExprParser::parse_expr_1 ( )
private
float orxonox::ExprParser::parse_expr_2 ( )
private
float orxonox::ExprParser::parse_expr_3 ( )
private
float orxonox::ExprParser::parse_expr_4 ( )
private
float orxonox::ExprParser::parse_expr_5 ( )
private
float orxonox::ExprParser::parse_expr_6 ( )
private
float orxonox::ExprParser::parse_expr_7 ( )
private
float orxonox::ExprParser::parse_expr_8 ( )
private
float orxonox::ExprParser::parse_last_argument ( )
private
ExprParser::unary_operator orxonox::ExprParser::parse_unary_operator ( )
private
void orxonox::ExprParser::parse_word ( char *  str)
private
void orxonox::ExprParser::setVariable ( const std::string &  varname,
float  value 
)

Member Data Documentation

bool orxonox::ExprParser::failed_
private
binary_operator orxonox::ExprParser::op
private
const char* orxonox::ExprParser::reading_stream
private
std::string orxonox::ExprParser::remains_
private
float orxonox::ExprParser::result_
private
std::map<std::string, float> orxonox::ExprParser::variables_
private

The documentation for this class was generated from the following files: