I succeeded to parse strings like "A, (B, C), (D, E, (F, G)), H"
.
But, I failed to save the matched results to a C++ struct.
I couldn't figure out the rule's right attribute types.
Following is a minimum test case for the parsing.
TEST_CASE("recursive match", "[qi]")
{
namespace qi = boost::spirit::qi;
using qi::char_;
struct recursive_match
: qi::grammar<std::string::iterator, qi::ascii::space_type>
{
recursive_match() : recursive_match::base_type(div_)
{
subdiv_ = '(' >> div_ >> ')';
div_ = (char_("A-Z") | subdiv_) % ',';
}
qi::rule<std::string::iterator, qi::ascii::space_type> subdiv_;
qi::rule<std::string::iterator, qi::ascii::space_type> div_;
};
std::string s = "A, (B, C), (D, E, (F, G)), H";
auto begin = s.begin();
auto end = s.end();
recursive_match rule_;
bool r = qi::phrase_parse(begin, end, rule_, qi::ascii::space);
REQUIRE(r);
REQUIRE(begin == end);
}
Anything will be helpful for me. Thanks.
I would suggest a recursive variant:
Now, you'd declare the rules slightly more simply:
Where the definition is something like
Full Demo
Adding some code for debug output:
Live On Coliru
Prints: