Guest User

Untitled

a guest
Jul 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. #include <boost/config/warning_disable.hpp>
  5. #include <boost/spirit/include/qi.hpp>
  6.  
  7. namespace test {
  8.  
  9. namespace qi = boost::spirit::qi;
  10. namespace ascii = boost::spirit::ascii;
  11. namespace alphabet = boost::spirit::ascii;
  12.  
  13. template <typename Iterator, typename Skipper>
  14. struct Parser: qi::grammar<Iterator, std::string(), Skipper> {
  15. Parser()
  16. :Parser::base_type(areaPart_)
  17. {
  18. using qi::lexeme;
  19. using alphabet::char_;
  20.  
  21. areaPart_ = lexeme[+(char_ - ' ') >> *(+char_(" ") >> +(char_ - ' '))]; // preserve inner spaces
  22. }
  23.  
  24. qi::rule<Iterator, std::string(), Skipper> areaPart_;
  25. };
  26.  
  27. } // namespace test
  28.  
  29. int main(void) {
  30. using namespace test;
  31. typedef std::string::const_iterator InputIter;
  32. typedef boost::spirit::ascii::space_type SpaceSkipper;
  33. SpaceSkipper space;
  34. typedef Parser<InputIter, SpaceSkipper> CfgParser;
  35.  
  36. CfgParser g;
  37.  
  38. std::string areaPartStr = " Some words ";
  39. std::string areaPart;
  40.  
  41. InputIter iter = areaPartStr.begin();
  42. InputIter end = areaPartStr.end();
  43. bool r= phrase_parse(iter, end, g, space, areaPart);
  44.  
  45.  
  46. if (r) {
  47. std::cout << "Area name: \"" << areaPart << '"' << std::endl;
  48. std::cout << "\n-------------------------\n";
  49. std::cout << "\n Stopped at \"" << std::string(iter, end) << "\"\n";
  50. } else {
  51. std::cout << "-------------------------\n";
  52. std::cout << "Parsing failed\n";
  53. }
  54. return 0;
  55. }
Add Comment
Please, Sign In to add comment