Re: How to use spirit's qi attributes properly
Dan Bloomquist <[email protected]> Tue, 29 May 2018 16:19:07 -0700
| Newsgroups | gmane.comp.parsers.spirit.general |
|---|---|
| Message-ID | <[email protected]> |
Roeber, Steffen wrote:
>
> Hi,
>
> I want to parse something like "a=1;b=2;{c=3;d=4;}e=5;f=6;". My
> problem is how to handle the block within the '{}' chars. The values
> within the block shall be parsed (but not propagated to attribute of
> r1) if ignoreBlock is true.
>
> usingnamespaceboost::spirit;
> usingnamespacestd;
> boolignoreBlock =true;
> qi::rule<std::string::iterator,vector<pair<string,int>>()>r1;
> qi::rule<std::string::iterator,pair<string,int>()>r2;
> qi::rule<std::string::iterator,vector<pair<string,int>>()>r3;
> r1 =*(r2 |(qi::eps(phoenix::ref(ignoreBlock)==true)>>qi::omit[r3])|r3);
> r2 =qi::as<string>()[+qi::alnum]>>'='>>qi::int_ >>';';
> r3 ='{'>>*r2 >>'}';
> strings ={"a=1;b=2;{c=3;d=4;}e=5;f=6;"};
> vector<pair<string,int>>v;
> phrase_parse(s.begin(),s.end(),r1, boost::spirit::ascii::space,v);
Hi Steffen, All,
So I thought, why not just use x3 and wrote:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <vector>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/adapted/std_pair.hpp>
int main()
{
using namespace boost::spirit::x3;
std::vector<std::pair<std::string, int>> v;
auto const item_parser = *alnum >> '=' >> int_ >> ';'; //r2
auto const set_parser = *(item_parser | omit['{' >> *item_parser >>
'}']);
std::string s( "a=1;b=2;{c=3;d=4;}e=5;f=6;" );
auto b = s.begin();
phrase_parse(b, s.end(), set_parser, space, v);
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Steffen, please note the inclusion of 'std_pair.hpp'. x3 needs it and I
think qi does too from your error. I didn't try to qi build, so I don't
know for sure.
But my solution does not work! tracing I see 'omit' causes an unused
attribute but an initialized pair makes its way back to the container
move. My logic is most likely flawed or you just can't 'omit' as an
alternative inside the kleene operator. But I don't know. My output was:
a 1
b 2
0 << from "",0 initialized pair
e 5
f 6
Best, Dan.
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot