#include #include #include #include namespace lex = boost::spirit::lex; enum class token { word, eol, char_ }; template struct word_count_tokens: lex::lexer { word_count_tokens() { this->self.add ("[^ \t\n]+", token::word) ("\n" , token::eol) ("." , token::char_); } }; struct counter { using result_type = bool; template bool operator()(Token const &t, int &c, int w, int &l) { switch(t.id()) { case token::word: ++w; c += t.value().size(); break; case token::eol: ++l; ++c; break; case token::char_: ++c; break; } return true; } }; int main(int argc, char *argv[]) { int c{0}, w{0}, l{0}; const std::string text{"test test\nline test"}; const char * first = text.data(), * last = text.data() + text.size(); word_count_tokens > word_count_functor; bool r = lex::tokenize(first, last, word_count_functor, boost::bind(counter(), _1, boost::ref(c), boost::ref(w), boost::ref(l))); std::cout << "result: " << r << std::endl; std::cout << c << '\t' << w << '\t' << l << std::endl; return 0; }