Using Iterator Parsing with Boost :: Spirit Grammars

When I try to use the iterator form for parsing for Spirit grammar, I get an argument that passes the conversion error from the type of the iterator to const char *. How to fix it?

There are some limitations. I use an iterator adapter on large inputs, so it is not possible for me to convert it to a C style string.

Here is a sample code demonstrating the problem:

#include <boost/spirit/core.hpp>
#include <boost/spirit/iterator/file_iterator.hpp>
#include <vector>
#include <string>
using std;
using boost::spirit;
struct ex : public grammar<route_grammar> {
  template <typename ScannerT> struct defintion {
    definition(ex const& self) {
      expression = real_p; 
    }
    rule<ScannerT> expression;
    rule<ScannerT> const& start() const { return expression; }
};

int main() {
  file_iterator<char> first;
  file_iterator<char> last = first.make_end();
  ex ex_p;
  parse_info<file_iterator<char> > info = parse(first, last, ex_p, space_p);
  return 0;
}

This code breaks into: error: cannot convert const boost::spirit::file_iterator<char_t, boost::spirit::fileiter_impl::mmap_file_iterator<char_t> >to const char*when passing arguments

+3
source share
4 answers

, , . ( MSV++ 7.1):

#include <boost/spirit/core.hpp>
#include <vector>
#include <string>
using namespace std;
using namespace boost::spirit;
struct ex : public grammar<ex> {
template <typename ScannerT> 
struct definition {
    definition(ex const& self)
    {
    expression = real_p; 
    }
    rule<ScannerT> expression;
    rule<ScannerT> const& start() const { return expression; }
};
};

int main() {
vector<char> v;
v.push_back('3'); v.push_back('.'); v.push_back('2');
ex ex_p;
parse_info<vector<char>::iterator> info = parse(v.begin(), v.end(), ex_p, space_p);
return 0;
}
+3

char *, , :

&v.front() // v.begin()
&v.back() + 1 // v.end()

, :

vector<char> v;
v.push_back("3.2");
0

. , .

, char * I.

0

, . - :

struct my_action {
  template <class ParamType>
  void operator()(ParamType const & parameter) const {
    // deal with parameter
  }
};

, , :

real_p[my_action()]

, , - :

struct binary_action {
  template <class ParamType>
  void operator()(ParamType const & param1, ParamType const & param2) const {
    // deal with either parameter
  }
};

(*char_p)[binary_action()]
0
source

Source: https://habr.com/ru/post/1702934/


All Articles