I am trying to read a mathematical function which depends on the symbol t
with boost::spirit.
In the example below, I'm trying evaluate the function "tan(t)"
in t=1.2
.
Instead of having
Exit: 1, value = 2.5721
I get
Exit: 1, value = 1.2
I understand that when I try to read the function "tan(t)"
, instead of computing the tangent of t
, the value of t
is assigned to the first letter in the word tan
. Is it possible to circumvent this behavior, without changing the symbol t
? Moreover, shouldn't the parse fail?
#include <string>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/qi_symbols.hpp>
#include <boost/phoenix/stl/cmath.hpp>
namespace qi = boost::spirit::qi;
namespace ascii=boost::spirit::ascii;
using boost::spirit::ascii::space;
using boost::spirit::qi::symbols;
template< typename Iterator >
struct Grammar : public qi::grammar< Iterator, double(), ascii::space_type >
{
Grammar() : Grammar::base_type(expression)
{
using qi::double_;
using qi::_val;
using qi::_1;
expression = double_ [_val = _1]
| symbol [_val = _1]
| function [_val = _1]
| group [_val = _1];
function = qi::lit("tan") >> group [_val = boost::phoenix::tan(_1)];
group = '(' >> expression [_val = _1] >> ')' ;
}
qi::rule<Iterator, double(), ascii::space_type> expression, function, group;
qi::symbols<char, double > symbol;
};
int main()
{
typedef std::string::iterator iterator;
Grammar<iterator> grammar;
std::string function = "tan(t)"; //it would work if function = "tan(x)"
grammar.symbol.add("t",1.2); // and add("x",1.2)
double value;
bool r = qi::phrase_parse(function.begin(), function.end(), grammar, space, value);
std::cout << "Exit: " << r << ", value = " << value << std::endl;
return 0;
}