Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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.
- --- input file ---
- This is a test file
- 1 2 3
- */
- namespace myParser
- {
- using namespace std;
- namespace x3 = boost::spirit::x3;
- namespace ascii = boost::spirit::x3::ascii;
- using ascii::blank;
- using x3::_attr;
- using x3::char_;
- using x3::double_;
- using x3::eol;
- using x3::int_;
- using x3::parse;
- using x3::skip;
- auto description = *(char_ - eol) >> eol; // 1st line, just comments
- auto numbers = int_ >> int_ >> int_; // 2nd line, 3 numbers
- auto point = double_ >> double_ >> double_; // lines 3 - several thousand, 3 doubles
- auto element = int_ >> *(char_) >> int_ >> int_ >> int_; // after points, many of these also
- auto points = skip(blank) [
- *(point >> eol)
- ];
- auto elements = skip(blank) [
- *(element >> eol)
- ];
- auto pr = [&] (auto& ctx) {
- string desc;
- desc = _attr(ctx);
- cout << "desc = " << desc << endl;
- };
- auto input = *description[pr]
- >> numbers;
- // >> points
- // >> elements;
- }
- int main()
- {
- namespace x3 = boost::spirit::x3;
- string bigfile("/tmp/large_file");
- mapped_file_source map(bigfile);
- istringstream ss(map.data());
- ss.close();
- boost::spirit::istream_iterator sof(ss), eof;
- x3::parse(sof, eof, myParser::input);
- if (sof != eof)
- cout << "Error: could not parse whole file." << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment