10 #pragma warning( disable : 4786)
90 const char * pWordStart_;
104 symbols_ [
"pi"] = 3.1415926535897932385;
105 symbols_ [
"e"] = 2.7182818284590452354;
114 const double Evaluate (
const std::string & program);
126 const TokenType GetToken (
const bool ignoreSign =
false);
127 const double CommaList (
const bool get);
128 const double Expression (
const bool get);
129 const double Comparison (
const bool get);
130 const double AddSubtract (
const bool get);
131 const double Term (
const bool get);
132 const double Primary (
const bool get);
134 inline void CheckToken (
const TokenType wanted)
138 std::ostringstream s;
139 s <<
"'" <<
static_cast <char> (wanted) <<
"' expected.";
140 throw std::runtime_error (s.str ());
Mathematical expression parser and evaluator.
double & operator[](const std::string &key)
Access or create a symbol (variable) by name.
std::map< std::string, double > symbols_
Symbol table mapping variable names to values. Can be accessed directly.
TokenType
Token types used by the expression lexer.
@ LHPAREN
Left parenthesis.
@ NE
Not equal comparison (!=)
@ RHPAREN
Right parenthesis.
@ NOT
Logical NOT operator.
@ EQ
Equal comparison (==)
@ ASSIGN_DIV
Compound assignment (/=)
@ DIVIDE
Division operator.
@ MULTIPLY
Multiplication operator.
@ GE
Greater than or equal (>=)
@ NAME
Identifier or variable name.
@ LT
Less than comparison.
@ ASSIGN_MUL
Compound assignment (*=)
@ ASSIGN_SUB
Compound assignment (-=)
@ LE
Less than or equal (<=)
@ NONE
No token (error state)
@ GT
Greater than comparison.
@ MINUS
Subtraction operator.
@ ASSIGN_ADD
Compound assignment (+=)
@ ASSIGN
Assignment operator.
const double Evaluate()
Evaluate the stored expression using current symbol values.
Parser(const std::string &program)
Construct a parser with the given expression.