fork(6) download
  1. #define BOOST_SPIRIT_USE_PHOENIX_V3
  2.  
  3. #include <boost/spirit/include/qi.hpp>
  4. #include <boost/spirit/include/lex_lexertl.hpp>
  5. #include <boost/spirit/include/phoenix.hpp>
  6. #include <boost/phoenix/function/adapt_callable.hpp>
  7.  
  8. using namespace boost::spirit;
  9. using namespace boost::spirit::ascii;
  10.  
  11. enum tokenids { IDANY = lex::min_token_id + 10 };
  12.  
  13. template <typename Lexer>
  14. struct word_count_tokens : lex::lexer<Lexer> {
  15. word_count_tokens() {
  16. this->self.add_pattern("WORD", "[^ \t\n]+");
  17. word = "{WORD}";
  18. this->self.add(word)('\n')(".", IDANY);
  19. }
  20. lex::token_def<std::string> word;
  21. };
  22.  
  23. struct SA {
  24. template<typename> struct result { typedef void type; };
  25. template<typename P>
  26. void operator()(P pair) const {
  27. std::cout << pair.begin()->id() << '\n';
  28. }
  29. };
  30.  
  31. BOOST_PHOENIX_ADAPT_CALLABLE(sa, SA, 1);
  32.  
  33. template <typename Iterator>
  34. struct grammar : qi::grammar<Iterator> {
  35. template <typename TokenDef>
  36. grammar(TokenDef const& tok) : grammar::base_type(start) {
  37. start = *( raw[tok.word] [sa(qi::_1)]
  38. | raw[lit('\n')] [sa(qi::_1)]
  39. | raw[qi::token(IDANY)] [sa(qi::_1)] );
  40. }
  41. qi::rule<Iterator> start;
  42. };
  43.  
  44. int main(int argc, char* argv[])
  45. {
  46. typedef lex::lexertl::token<char const*, boost::mpl::vector<std::string> > token_type;
  47. typedef lex::lexertl::lexer<token_type> lexer_type;
  48.  
  49. typedef word_count_tokens<lexer_type>::iterator_type iterator_type;
  50.  
  51. word_count_tokens<lexer_type> word_count; // Our lexer
  52. grammar<iterator_type> g (word_count); // Our parser
  53.  
  54. std::string str("why did the hickup cross the broad?");
  55. char const* first = str.c_str(), * last = first + str.size();
  56. lex::tokenize_and_parse(first, last, word_count, g);
  57. }
  58.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty