Search code examples
c++boostboost-spiritboost-spirit-qi

Unable to make grammar from rule in boost::spirit::qi


I am trying to use the Spirit library for the first time. I am enjoying it so far but I find myself unable to build a grammar from predefined rules, even when trying examples heavily inspired by the documentation!

Here is the heart of my problem :

#include <boost/spirit/home/qi.hpp>
using boost::spirit::qi::ascii::space_type;
using boost::spirit::qi::grammar;
using boost::spirit::qi::phrase_parse;

template<typename P>
bool test_parser(char const* input, P const& p) {
  char const* f(input);
  char const* l(f + strlen(f));
  return parse(f, l, p) && f == l;
}

struct my_grammar : grammar<char const*, space_type> {
  my_grammar() : base_type(r) {
    r = boost::spirit::qi::int_;
  }
  rule<char const*, space_type> r;
} g;
bool b = test_phrase_parser("5", g);

And this is was the compiler says:

error: no matching function for call to ‘test_phrase_parser(const char [6], ph_files_parsing::process_parsing::test_method()::my_grammar&)’

note: candidate is:

note: template bool test_phrase_parser(const char*, const P&)

It all works fine if I replace

bool b = test_phrase_parser("5", g);

by

bool b = test_phrase_parser("5", boost::spirit::qi::int_);

Many thanks in advance to anyone who can help.

(Boost version is 1.48)


Solution

  • Your question turns out to be frequently-asked (at the bottom).

    Since the grammar has a skip-parser, it must be invoked with

     phrase_parse()
    

    instead of just parse(). For example,

    return phrase_parse(f, l, p, boost::spirit::ascii::space) && f == l;