SRD

parser2

SRD
Jun 18th, 2017
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. /*
  2. Trying to get a basic example using my files going. I have a very large file which has many different lines. The following always bails on "could not parse whole file." I've cut the input file down to below.
  3.  
  4. --- input file ---
  5. This is a test file
  6. 1 2 3
  7. */
  8.  
  9. namespace myParser
  10. {
  11.     using namespace std;
  12.  
  13.     namespace x3 = boost::spirit::x3;
  14.     namespace ascii = boost::spirit::x3::ascii;
  15.  
  16.     using ascii::blank;
  17.     using x3::_attr;
  18.     using x3::char_;
  19.     using x3::double_;
  20.     using x3::eol;
  21.     using x3::int_;
  22.     using x3::parse;
  23.     using x3::skip;
  24.  
  25.     auto description = *(char_ - eol) >> eol; // 1st line, just comments
  26.     auto numbers = int_ >> int_ >> int_;      // 2nd line, 3 numbers
  27.     auto point = double_ >> double_ >> double_;  // lines 3 - several thousand, 3 doubles
  28.     auto element = int_ >> *(char_) >> int_ >> int_ >> int_; // after points, many of these also
  29.  
  30.     auto points = skip(blank) [
  31.         *(point >> eol)
  32.     ];
  33.    
  34.     auto elements = skip(blank) [
  35.         *(element >> eol)
  36.     ];
  37.    
  38.     auto pr = [&] (auto& ctx) {
  39.         string desc;
  40.         desc = _attr(ctx);
  41.         cout << "desc = " << desc << endl;
  42.     };            
  43.    
  44.     auto input = *description[pr]
  45.         >> numbers;
  46.  
  47. //        >> points  
  48. //        >> elements;
  49. }
  50.  
  51.  
  52. int main()
  53. {
  54.     namespace x3 = boost::spirit::x3;
  55.  
  56.     string bigfile("/tmp/large_file");
  57.  
  58.     mapped_file_source map(bigfile);
  59.     istringstream ss(map.data());
  60.     ss.close();
  61.  
  62.     boost::spirit::istream_iterator sof(ss), eof;
  63.  
  64.     x3::parse(sof, eof,  myParser::input);
  65.  
  66.     if (sof != eof)
  67.         cout << "Error: could not parse whole file." << endl;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment