compound attribute rules - pair FAQ

Patrick Welche <[email protected]>
Newsgroups gmane.comp.parsers.spirit.general
Message-ID <20160614111816.GA5710@quark>
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
[email protected]
https://lists.sourceforge.net/lists/listinfo/spirit-general
works.cpp (text/plain, 755 B)
#define BOOST_SPIRIT_X3_DEBUG

#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

namespace x3 = boost::spirit::x3;

struct result
{
	std::string first, second;
};

BOOST_FUSION_ADAPT_STRUCT(result,
	first, second
)

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.first << ' '
		                       << res.second << std::endl;

	return 0;
}
fails.cpp (text/plain, 816 B)
#define BOOST_SPIRIT_X3_DEBUG

#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/std_pair.hpp>

namespace x3 = boost::spirit::x3;

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;
}
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.