Spirit: parsing tab separated values and no_skip
Hi Harmut,
Did you uncomment the line:
//#include
compilation error. (Hartmut Kaiser) ----------------------------------------------------------------------
Message: 1 Date: Thu, 13 May 2010 18:28:19 -0500 From: "Hartmut Kaiser"
To: Subject: Re: [Boost-users] Spirit: parsing tab separated values and no_skip compilation error. Message-ID: <0d5301caf2f3$fa1c64e0$ee552ea0$@gmail.com> Content-Type: text/plain; charset="iso-8859-1" I am a beginner with Spirit. I just read the tutorial and I am playing around with some examples. Environment: boost 1.43, Windows Vista, Code::Blocks, mingw, gcc 4.5.
My goal is to build a tab separated values parser. If you have an example on how to parse something like: string/t/tdouble/tdouble it would be great. Contiguous tabs should be considered as one tab, not an empty field.
To start I just tried to parse a tab separated list of strings but I am having some difficulties.
Is there another skip parser to be used instead of space (for full code see the end of the message below) ? I have changed the commas to tabs in the code below but it failed. I guess it was because it was skipping them (space skipper).
??????? bool r = phrase_parse(first, last,
??????????? //? Begin grammar ??????????? ( ??????????????? +(char_ - ',') % ',' ??????????? ) ??????????? , ??????????? //? End grammar
??????????? space, v);
This code works with commas but it fails with '\t'.
I tried to use no_skip[] but it failed to compile the header #include
: C:\boost_1_43_0\boost\spirit\home\qi\directive\no_skip.hpp|30|error: 'no_skip' is not a member of 'boost::spirit::tag'|
For full error message see the end of the message below.
Any suggestions ?
Thank you for your attention, Mau.
#include
#include #include #include #include //#include #include <iostream> #include <string> #include <vector>
#define VECTOR_TYPE std::string
namespace client { ??? namespace qi = boost::spirit::qi; ??? namespace ascii = boost::spirit::ascii;
??? ////////////////////////////////////////////////////////////////////// ///// ??? //? Our number list compiler ??? ////////////////////////////////////////////////////////////////////// ///// ??? //[tutorial_numlist4 ??? template <typename Iterator> ??? bool parse_numbers(Iterator first, Iterator last, std::vector
& v) ??? { ??????? using qi::double_; ??????? using qi::phrase_parse; ??????? using qi::_1; ??????? using ascii::space; ??????? using qi::char_; ??????? bool r = phrase_parse(first, last,
??????????? //? Begin grammar ??????????? ( ??????????????? +(char_ - ',') % ',' ??????????? ) ??????????? , ??????????? //? End grammar
??????????? space, v);
??????? if (first != last) // fail if we did not get a full match ??????????? return false; ??????? return r; ??? } ??? //] }
//////////////////////////////////////////////////////////////////////////
// //? Main program
//////////////////////////////////////////////////////////////////////////
// int main() { ??? std::cout << "/////////////////////////////////////////////////////////\n\n"; ??? std::cout << "\t\tA comma separated list parser for Spirit...\n\n"; ??? std::cout << "/////////////////////////////////////////////////////////\n\n";
??? std::cout << "Give me a comma separated list of numbers.\n"; ??? std::cout << "The numbers will be inserted in a vector of
numbers\n";
??? std::cout << "Type [q or Q] to quit\n\n";
??? std::string str; ??? while (getline(std::cin, str)) ??? { ??????? if (str.empty() || str[0] == 'q' || str[0] == 'Q') ??????????? break;
??????? std::vector
v; ??????? if (client::parse_numbers(str.begin(), str.end(), v)) ??????? { ??????????? std::cout << "-------------------------\n"; ??????????? std::cout << "Parsing succeeded\n"; ??????????? std::cout << str << " Parses OK: " << std::endl; ??????????? for (std::vector
::size_type i = 0; i < v.size(); ++i) ??????????????? std::cout << i << ": " << v[i] << std::endl; ??????????? std::cout << "\n-------------------------\n"; ??????? } ??????? else ??????? { ??????????? std::cout << "-------------------------\n"; ??????????? std::cout << "Parsing failed\n"; ??????????? std::cout << "-------------------------\n"; ??????? } ??? }
??? std::cout << "Bye... :-) \n\n"; ??? return 0; }
This example compiles fine for me (VC10, g++ 4.5). Is it supposed to fail?
Regards Hartmut
Mauricio,
Did you uncomment the line: //#include
? It is commented out in the code below. If I include this line, compilation fails.
Compiles fine for me (gcc 4.5, msvc 10).
You also have to check optimizations because the code below is not using no_skip[] in the grammar so there is a chance that a smart compiler that is not in debug mode might ignore the non-used code. In my case I am in debug mode, no optmizations and including the qi_no_skip.hpp header suffices to get the error.
No optimization here. Regards Hartmut --------------- Meet me at BoostCon www.boostcon.com
1. Re: Spirit: parsing tab separated values and no_skip
compilation error. (Hartmut Kaiser) ----------------------------------------------------------------------
Message: 1 Date: Thu, 13 May 2010 18:28:19 -0500 From: "Hartmut Kaiser"
To: Subject: Re: [Boost-users] Spirit: parsing tab separated values and no_skip compilation error. Message-ID: <0d5301caf2f3$fa1c64e0$ee552ea0$@gmail.com> Content-Type: text/plain; charset="iso-8859-1" I am a beginner with Spirit. I just read the tutorial and I am playing around with some examples. Environment: boost 1.43, Windows Vista, Code::Blocks, mingw, gcc 4.5.
My goal is to build a tab separated values parser. If you have an example on how to parse something like: string/t/tdouble/tdouble it would be great. Contiguous tabs should be considered as one tab, not an empty field.
To start I just tried to parse a tab separated list of strings but I am having some difficulties.
Is there another skip parser to be used instead of space (for full code see the end of the message below) ? I have changed the commas to tabs in the code below but it failed. I guess it was because it was skipping them (space skipper).
??????? bool r = phrase_parse(first, last,
??????????? //? Begin grammar ??????????? ( ??????????????? +(char_ - ',') % ',' ??????????? ) ??????????? , ??????????? //? End grammar
??????????? space, v);
This code works with commas but it fails with '\t'.
I tried to use no_skip[] but it failed to compile the header #include
: C:\boost_1_43_0\boost\spirit\home\qi\directive\no_skip.hpp|30|error: 'no_skip' is not a member of 'boost::spirit::tag'|
For full error message see the end of the message below.
Any suggestions ?
Thank you for your attention, Mau.
#include
#include #include #include #include //#include #include <iostream> #include <string> #include <vector>
#define VECTOR_TYPE std::string
namespace client { ??? namespace qi = boost::spirit::qi; ??? namespace ascii = boost::spirit::ascii;
??? ////////////////////////////////////////////////////////////////////// ///// ??? //? Our number list compiler ??? ////////////////////////////////////////////////////////////////////// ///// ??? //[tutorial_numlist4 ??? template <typename Iterator> ??? bool parse_numbers(Iterator first, Iterator last, std::vector
& v) ??? { ??????? using qi::double_; ??????? using qi::phrase_parse; ??????? using qi::_1; ??????? using ascii::space; ??????? using qi::char_; ??????? bool r = phrase_parse(first, last,
??????????? //? Begin grammar ??????????? ( ??????????????? +(char_ - ',') % ',' ??????????? ) ??????????? , ??????????? //? End grammar
??????????? space, v);
??????? if (first != last) // fail if we did not get a full match ??????????? return false; ??????? return r; ??? } ??? //] }
////////////////////////////////////////////////////////////////////// ////
// //? Main program
////////////////////////////////////////////////////////////////////// ////
// int main() { ??? std::cout << "/////////////////////////////////////////////////////////\n\n"; ??? std::cout << "\t\tA comma separated list parser for Spirit...\n\n"; ??? std::cout << "/////////////////////////////////////////////////////////\n\n";
??? std::cout << "Give me a comma separated list of numbers.\n"; ??? std::cout << "The numbers will be inserted in a vector of numbers\n"; ??? std::cout << "Type [q or Q] to quit\n\n";
??? std::string str; ??? while (getline(std::cin, str)) ??? { ??????? if (str.empty() || str[0] == 'q' || str[0] == 'Q') ??????????? break;
??????? std::vector
v; ??????? if (client::parse_numbers(str.begin(), str.end(), v)) ??????? { ??????????? std::cout << "-------------------------\n"; ??????????? std::cout << "Parsing succeeded\n"; ??????????? std::cout << str << " Parses OK: " << std::endl; ??????????? for (std::vector
::size_type i = 0; i < v.size(); ++i) ??????????????? std::cout << i << ": " << v[i] << std::endl; ??????????? std::cout << "\n-------------------------\n"; ??????? } ??????? else ??????? { ??????????? std::cout << "-------------------------\n"; ??????????? std::cout << "Parsing failed\n"; ??????????? std::cout << "-------------------------\n"; ??????? } ??? }
??? std::cout << "Bye... :-) \n\n"; ??? return 0; }
This example compiles fine for me (VC10, g++ 4.5). Is it supposed to fail?
Regards Hartmut
participants (2)
-
Hartmut Kaiser
-
Mauricio Gomes