Advertisement
Guest User

Untitled

a guest
Oct 16th, 2014
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. /*=============================================================================
  2.     Copyright (c) 2002-2010 Joel de Guzman
  3.  
  4.     Distributed under the Boost Software License, Version 1.0. (See accompanying
  5.     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. ///////////////////////////////////////////////////////////////////////////////
  8. //
  9. //  This sample demontrates a parser for a comma separated list of numbers.
  10. //  No actions.
  11. //
  12. //  [ JDG May 10, 2002 ]    spirit1
  13. //  [ JDG March 24, 2007 ]  spirit2
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16.  
  17. #include <boost/config/warning_disable.hpp>
  18. #include <boost/spirit/include/qi.hpp>
  19.  
  20. #include <iostream>
  21. #include <string>
  22. #include <vector>
  23.  
  24. namespace client
  25. {
  26.     namespace qi = boost::spirit::qi;
  27.     namespace ascii = boost::spirit::ascii;
  28.  
  29.     auto parser = qi::double_ >> *(',' >> qi::double_);
  30.  
  31.     ///////////////////////////////////////////////////////////////////////////
  32.     //  Our number list parser
  33.     ///////////////////////////////////////////////////////////////////////////
  34.     //[tutorial_numlist1
  35.     template <typename Iterator>
  36.     bool parse_numbers(Iterator first, Iterator last)
  37.     {
  38.         using qi::double_;
  39.         using qi::phrase_parse;
  40.         using ascii::space;
  41.  
  42.         bool r = phrase_parse(
  43.             first,                          /*< start iterator >*/
  44.             last,                           /*< end iterator >*/
  45.             parser,
  46.             space                           /*< the skip-parser >*/
  47.         );
  48.         if (first != last) // fail if we did not get a full match
  49.             return false;
  50.         return r;
  51.     }
  52.     //]
  53. }
  54.  
  55. ////////////////////////////////////////////////////////////////////////////
  56. //  Main program
  57. ////////////////////////////////////////////////////////////////////////////
  58. int
  59. main()
  60. {
  61.     std::cout << "/////////////////////////////////////////////////////////\n\n";
  62.     std::cout << "\t\tA comma separated list parser for Spirit...\n\n";
  63.     std::cout << "/////////////////////////////////////////////////////////\n\n";
  64.  
  65.     std::cout << "Give me a comma separated list of numbers.\n";
  66.     std::cout << "Type [q or Q] to quit\n\n";
  67.  
  68.     std::string str;
  69.     while (getline(std::cin, str))
  70.     {
  71.         if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  72.             break;
  73.  
  74.         if (client::parse_numbers(str.begin(), str.end()))
  75.         {
  76.             std::cout << "-------------------------\n";
  77.             std::cout << "Parsing succeeded\n";
  78.             std::cout << str << " Parses OK: " << std::endl;
  79.         }
  80.         else
  81.         {
  82.             std::cout << "-------------------------\n";
  83.             std::cout << "Parsing failed\n";
  84.             std::cout << "-------------------------\n";
  85.         }
  86.     }
  87.  
  88.     std::cout << "Bye... :-) \n\n";
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement