cmoulikus
cmoulikus

Reputation: 25

unable to decipher compile error when using boost.spirit.parser

I am trying to learn to parse using boost.spirit parser. I am using Windows 8.1 with VisualStudio 2015. I have installed and successfuly compiled the test program from the boost.spirit installation document so my installation of boost seems fine.

I have been following the tutorial on the boost.org on using the paser. I created the following code to parse a double:

  #include <boost/config/warning_disable.hpp>
  #include <boost/spirit/include/qi.hpp>
  #include <boost/spirit/include/phoenix_core.hpp>
  #include <boost/spirit/include/phoenix_operator.hpp>
  #include <boost/spirit/include/phoenix_object.hpp>

  namespace qi = boost::spirit::qi;
  namespace ascii = boost::spirit::ascii;

  template <typename Iterator>
  bool myParser(Iterator first, Iterator last) {
      using qi::double_;
      qi::rule<Iterator, double(), ascii::space_type> myrule;
      myrule %= double_;
      bool r = parse(first, last, myrule, ascii::space);
      return r;
   }
  int main() {
      std::string dstr = std::string("2.1");
      bool r = myParser(dstr.begin(), dstr.end());
      return 0;
     }

When I compile I get the following error message from rule.hpp line 304:

'bool boost::function4<R,T0,T1,T2,T3>::operator ()(T0,T1,T2,T3) const': cannot convert argument 4 from 'const boost::spirit::unused_type' to 'const boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space,boost::spirit::char_encoding::ascii>> '   

Any help will be greatly appreciated. Thanks

Upvotes: 1

Views: 75

Answers (1)

sehe
sehe

Reputation: 393799

As is mentioned by jv_ in the link, you are using a skipper, but not calling the phrase_parse API which would accept a skipper. So, the parse call tries to bind the ascii::space parser to the first exposed attribute (which is a double).

That assignment fails.

In all likelihood you don't want a skipper for this simple grammar, and I'd write:

#include <boost/spirit/include/qi.hpp>

template <typename Iterator> bool myParser(Iterator first, Iterator last) {
    namespace qi = boost::spirit::qi;
    return qi::parse(first, last, qi::double_ >> qi::eoi);
}

int main() {
    std::string const dstr("2.1");
    bool r = myParser(dstr.begin(), dstr.end());
    return r?0:1;
}

Note the qi::eol which checks that all of the input was consumed.

Upvotes: 2

Related Questions