fork(1) download
  1. #include <boost/config/warning_disable.hpp>
  2. #include <boost/spirit/include/qi.hpp>
  3. #include <boost/spirit/include/phoenix_core.hpp>
  4. #include <boost/spirit/include/phoenix_operator.hpp>
  5. #include <boost/spirit/include/phoenix_stl.hpp>
  6.  
  7. #include <iostream>
  8. #include <string>
  9. #include <vector>
  10.  
  11. namespace client
  12. {
  13. namespace qi = boost::spirit::qi;
  14. namespace ascii = boost::spirit::ascii;
  15. namespace phoenix = boost::phoenix;
  16.  
  17. //ADDED
  18. template <typename Type>
  19. struct fortran_policy : qi::real_policies<Type>
  20. {
  21. template <typename Iterator>
  22. static bool parse_exp(Iterator& first, const Iterator& last)
  23. {
  24. if (first == last || (*first != 'e' && *first != 'E' && *first != 'd' && *first != 'D'))
  25. return false;
  26. ++first;
  27. return true;
  28. }
  29. };
  30. ///////////////////////////////////////////////////////////////////////////////
  31. // Our number list compiler
  32. ///////////////////////////////////////////////////////////////////////////////
  33. //[tutorial_numlist3
  34. template <typename Iterator>
  35. bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v)
  36. {
  37. qi::real_parser<double,fortran_policy<double>> double_; //CHANGED
  38. using qi::phrase_parse;
  39. using qi::_1;
  40. using ascii::space;
  41. using phoenix::push_back;
  42.  
  43. bool r = phrase_parse(first, last,
  44.  
  45. // Begin grammar
  46. (
  47. double_[push_back(phoenix::ref(v), _1)] % ','
  48. )
  49. ,
  50. // End grammar
  51.  
  52. space);
  53.  
  54. if (first != last) // fail if we did not get a full match
  55. return false;
  56. return r;
  57. }
  58. //]
  59. }
  60.  
  61. ////////////////////////////////////////////////////////////////////////////
  62. // Main program
  63. ////////////////////////////////////////////////////////////////////////////
  64. int
  65. main()
  66. {
  67. std::cout << "/////////////////////////////////////////////////////////\n\n";
  68. std::cout << "\t\tA comma separated list parser for Spirit...\n\n";
  69. std::cout << "/////////////////////////////////////////////////////////\n\n";
  70.  
  71. std::cout << "Give me a comma separated list of numbers.\n";
  72. std::cout << "The numbers will be inserted in a vector of numbers\n";
  73. std::cout << "Type [q or Q] to quit\n\n";
  74.  
  75. std::string str;
  76. while (getline(std::cin, str))
  77. {
  78. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  79. break;
  80.  
  81. std::vector<double> v;
  82. if (client::parse_numbers(str.begin(), str.end(), v))
  83. {
  84. std::cout << "-------------------------\n";
  85. std::cout << "Parsing succeeded\n";
  86. std::cout << str << " Parses OK: " << std::endl;
  87.  
  88. for (std::vector<double>::size_type i = 0; i < v.size(); ++i)
  89. std::cout << i << ": " << v[i] << std::endl;
  90.  
  91. std::cout << "\n-------------------------\n";
  92. }
  93. else
  94. {
  95. std::cout << "-------------------------\n";
  96. std::cout << "Parsing failed\n";
  97. std::cout << "-------------------------\n";
  98. }
  99. }
  100.  
  101. std::cout << "Bye... :-) \n\n";
  102. return 0;
  103. }
  104.  
Success #stdin #stdout 0s 3472KB
stdin
1.2,0.2
.2,5.
1.e+23,.23E4
0e+10
1.3D+3
stdout
/////////////////////////////////////////////////////////

		A comma separated list parser for Spirit...

/////////////////////////////////////////////////////////

Give me a comma separated list of numbers.
The numbers will be inserted in a vector of numbers
Type [q or Q] to quit

-------------------------
Parsing succeeded
1.2,0.2 Parses OK: 
0: 1.2
1: 0.2

-------------------------
-------------------------
Parsing succeeded
.2,5. Parses OK: 
0: 0.2
1: 5

-------------------------
-------------------------
Parsing succeeded
1.e+23,.23E4 Parses OK: 
0: 1e+23
1: 2300

-------------------------
-------------------------
Parsing succeeded
0e+10 Parses OK: 
0: 0

-------------------------
-------------------------
Parsing succeeded
1.3D+3 Parses OK: 
0: 1300

-------------------------
Bye... :-)