compound attribute rules - pair FAQ

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

compound attribute rules - pair FAQ

Patrick Welche
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?


Cheers,

Patrick

------------------------------------------------------------------------------
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. https://ad.doubleclick.net/ddm/clk/305295220;132659582;e
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general

works.cpp (789 bytes) Download Attachment
fails.cpp (851 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: compound attribute rules - pair FAQ

Lee Clagett-2
On Tue, 14 Jun 2016 12:18:16 +0100
Patrick Welche <[hidden email]> 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
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general