I want to parse a floating point number in a text file and insert it in a symbol table; the parser and the symbol table are provided by spirit::qi.
Here is my code:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <stdint.h>
#include <iostream>
#include <string>
template<typename VTYPE>
struct VTable : boost::spirit::qi::symbols<char, VTYPE> {
VTable() {} // empty
};
int main() {
using boost::spirit::qi::_1;
using boost::spirit::qi::eps;
using boost::spirit::qi::rule;
using boost::spirit::qi::ascii::space;
using boost::spirit::qi::space_type;
using boost::spirit::qi::real_parser;
using boost::spirit::qi::int_parser;
using boost::spirit::qi::strict_real_policies;
VTable<double> floatDecs;
floatDecs.add("hallo", 15.26)("duDa", 18.5);
const std::string some = "some";
rule<std::string::iterator, double> value = real_parser<double, strict_real_policies<double>>() [ boost::phoenix::bind(&VTable<double>::add, floatDecs, boost::phoenix::cref(some), _1) ];
std::cout << boost::spirit::qi::phrase_parse(test.begin(), test.end(), value, space);
return 0;
}
The problem in here lies in boost::phoenix::bind, but I do not know, how to fix that (I am fairly new to this library).
Here’s a working version. After, I’ll list what was wrong with yours.
First, the variable you want to use as an argument to add() needs to be fixed.
Next, you need to fix the syntax in your rule template parameters:
Note the brackets, to make the double a functor declaration rather than a vanilla double, for lazy evaluation.
Next, you need to use the syntax above for your semantic action.
Honestly I am not sure why this works as I am not a phoenix expert, but the link in ‘sehe’s’ comment above provided the answer. Also, you were barking up the wrong tree with
cref, there’s no indication fromadd()‘s declaration that a reference is what’s required.valis what you need to turn thesomevariable into a phoenix-happy lazy argument.Finally, you just need to supply some reasonable test data.
The code above should work for you now.