Re: compound attribute rules - pair FAQ

Lee Clagett <[email protected]>
Newsgroups gmane.comp.parsers.spirit.general
Message-ID <20160617131834.24f8a42a@laptop-m1330>
On Tue, 14 Jun 2016 12:18:16 +0100
Patrick Welche <[email protected]> wrote:

> After the ease of writing a parser with spirit, I'm getting stuck on
> working out a compatible type for the generated AST. (The idea is to
> avoid semantic actions, right?)
> 
> To figure out the basic building blocks, I just tried
> 
>     +alnum >> ' ' >> +alnum
> 
> which I think gives
> 
>     vector<char>     Unused    vector<char>
> 
> aka
>     string Unused string
> 
> But then, can one carry on and remove Unused
>     a: A, b: Unused --> (a >> b): A
> 
>     string string
>     tuple <string, string>
>     pair <string, string>
> ?
> 
> The attached simple experiment suggests not, as "fails.cpp" doesn't
> compile, works.cpp does.
> 
> What am I missing? Some other error?
> 
> 

Adding the relevant code that was attached:

> struct result
> {
> 	std::pair<std::string, std::string> both;
> };
>
> BOOST_FUSION_ADAPT_STRUCT(result,
> 	both
> )
>
> int main()
> {
> 	std::string buf("one two");
> 	std::string::const_iterator iter = buf.begin();
> 	std::string::const_iterator end = buf.end();
> 	result res;
> 	bool ok = x3::parse(iter, end, +x3::alnum >> ' ' >>
> +x3::alnum, res); if (!ok)
>		std::cout << "Parsing failed\n";
>	else if (iter != end)
>		std::cout << "Bit left over at the end: "
>		          << std::string(iter, end) << std::endl;
>	else
>		std::cout << "found: " << res.both.first << ' '
>		                       << res.both.second << std::endl;
>
>	return 0;
> }

The problem is the "double" wrapping. An adapted struct _is_ a
sequence, and so is an adapted `std::pair`. So the attribute provided
to the parse function is `sequence<sequence<std::string, std::string>>`
which is incorrect. Use the pair from the other code snippet or put
both `std::string`s in the struct directly:

    struct result
    {
        std::string one;
        std::string two;
    };

    BOOST_FUSION_ADAPT_STRUCT(result, one, two);

Lee

------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are 
consuming the most bandwidth. Provides multi-vendor support for NetFlow, 
J-Flow, sFlow and other flows. Make informed decisions using capacity planning
reports. http://sdm.link/zohomanageengine
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.