I would like to parse dot language (http://www.graphviz.org/content/dot-language). It\'s a graph definition language that defines nodes and connections between them. A typic
Making the list operator accept a minimum number of elements would require creating a brand new parser introducing that behaviour because, unlike repeat
, it is not configured to do so. I hope the following example can help you understand how you can use a >> +(omit[b] >> a)
to achieve what you want.
Running on WandBox
#include
#include
#include
#include
namespace qi= boost::spirit::qi;
void print(const std::vector& data)
{
std::cout << "{ ";
for(const auto& elem : data) {
std::cout << elem << " ";
}
std::cout << "} ";
}
void print(const std::pair& data)
{
std::cout << "[ " << data.first << ", " << data.second << " ]";
}
template
void parse(const std::string& str, const Parser& parser, Attrs&... attrs)
{
std::string::const_iterator iter=std::begin(str), end=std::end(str);
bool result = qi::phrase_parse(iter,end,parser,qi::space,attrs...);
if(result && iter==end) {
std::cout << "Success.";
int ignore[] = {(print(attrs),0)...};
std::cout << "\n";
} else {
std::cout << "Something failed. Unparsed: \"" << std::string(iter,end) << "\"\n";
}
}
template
void parse_with_nodes(const std::string& str, const Parser& parser)
{
std::vector nodes;
parse(str,parser,nodes);
}
template
void parse_with_nodes_and_attr(const std::string& str, const Parser& parser)
{
std::vector nodes;
std::pair attr_pair;
parse(str,parser,nodes,attr_pair);
}
int main()
{
qi::rule node=+qi::alnum;
qi::rule(),qi::space_type> attr = +qi::alpha >> '=' >> qi::double_;
parse_with_nodes("node1->node2", node % "->");
parse_with_nodes_and_attr("node1->node2 arrowsize=1.0", node % "->" >> attr);
parse_with_nodes("node1->node2", node >> +("->" >> node));
//parse_with_nodes_and_attr("node1->node2 arrowsize=1.0", node >> +("->" >> node) >> attr);
qi::rule(),qi::space_type> at_least_two_nodes = node >> +("->" >> node);
parse_with_nodes_and_attr("node1->node2 arrowsize=1.0", at_least_two_nodes >> attr);
}