title: How to parse a mathematical expression with boost::spirit and bind it to a function
body: <p>I would like to define a function taking 2 arguments </p>
  
  <pre><code> double func(double t, double x); 
  </code></pre>
  
  <p>where the actual implementation is read from an external text file.<br>
  For example, specifying in the text file </p>
  
  <pre><code>function = x*t;    
  </code></pre>
  
  <p>the function should implement the multiplication between <code>x</code> and <code>t</code>, so that it could be called at a later stage.
  I'm trying to parse the function using boost::spirit. But I do not know how to actually achieve it.</p>
  
  <p>Below, I created a simple function that implements the multiplication. I bind it to a boost function and I can use it. I also created a simple grammar, which parse the multiplication between two doubles. </p>
  
  <pre><code>#include &lt;boost/config/warning_disable.hpp&gt;
  #include &lt;boost/spirit/include/qi.hpp&gt;
  #include &lt;boost/spirit/include/phoenix.hpp&gt;
  #include "boost/function.hpp"
  #include "boost/bind.hpp"
  #include &lt;boost/spirit/include/qi_symbols.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  namespace qi = boost::spirit::qi;
  namespace ascii=boost::spirit::ascii;
  using boost::spirit::ascii::space;
  using boost::spirit::qi::symbols;
  
  template&lt; typename Iterator &gt;
  struct MyGrammar : public virtual qi::grammar&lt;  Iterator,  ascii::space_type &gt;
  {
      MyGrammar() : MyGrammar::base_type(expression)
      {
          using qi::double_;
          //The expression should take x and t as symbolic expressions
          expression = (double_ &gt;&gt; '*' &gt;&gt; double_)[std::cout &lt;&lt; "Parse multiplication: " &lt;&lt; (qi::_1 * qi::_2)];
       }
  
       qi::rule&lt;Iterator, ascii::space_type&gt; expression;
   };
  
  double func(const double a, const double b)
  {
      return a*b; //This is the operation to perform
  }
  
  int main()
  {
      typedef std::string::const_iterator iterator_Type;
      typedef MyGrammar&lt;iterator_Type&gt; grammar_Type;
  
      grammar_Type calc; 
  
      std::string str = "1.*2."; // This should be changed to x*t
  
      iterator_Type iter = str.begin();
      iterator_Type end = str.end();
      bool r = phrase_parse(iter, end, calc, space);
  
      typedef boost::function &lt; double ( const double t,
                                         const double x) &gt; function_Type;
  
      function_Type multiplication = boost::bind(&amp;func, _1, _2);
  
      std::cout &lt;&lt; "\nResult: " &lt;&lt; multiplication( 2.0, 3.0) &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  
  <p>If I modify the above code setting</p>
  
  <pre><code>std::string str = "x*t";
  </code></pre>
  
  <p>how can I parse such an expression and bind it to the function <code>multiplication</code> such that, if I call <code>multiplication(1.0, 2.0)</code>, it associates  t to 1.0, x to 2.0 and it returns the result of the operation?</p>