C++ 如何使用强制最小元素量的boost spirit列表运算符?
我想解析dot语言()。它是一种图形定义语言,用于定义节点及其之间的连接。一个典型的语句看起来像C++ 如何使用强制最小元素量的boost spirit列表运算符?,c++,boost,boost-spirit,boost-spirit-qi,C++,Boost,Boost Spirit,Boost Spirit Qi,我想解析dot语言()。它是一种图形定义语言,用于定义节点及其之间的连接。一个典型的语句看起来像node1->node2->node3。最好使用boost::spirit列表操作符%来创建节点列表。天真的做法是: edge_stmt %= ( node_or_subgraph(_r1) % (qi::eps(_r1) >> tok.diredgeop | tok.undiredgeop) ) >> -attr_list; \u r1表示这
node1->node2->node3代码>。最好使用boost::spirit列表操作符%
来创建节点列表。天真的做法是:
edge_stmt %=
(
node_or_subgraph(_r1) % (qi::eps(_r1) >> tok.diredgeop | tok.undiredgeop)
) >> -attr_list;
\u r1
表示这是有向图还是无向图,diredgeop
是->
的标记,undredgeop
分别是-
的标记
问题是上面的代码只对node1成功代码>,这是不正确的。为了得到一个正确的解析器,我必须声明由%
构建的列表中必须至少有两个元素。怎么做
文档中说a%b
等同于a>*(省略[b]>>a)
,这是不正确的。你可能想试试这个:
edge_stmt %=
(
node_or_subgraph(_r1) >>
+(
qi::omit
[
qi::eps(_r1) >> tok.diredgeop | tok.undiredgeop
] >>
node_or_subgraph(_r1)
)
) >> -attr_list;
但这段代码不生成向量,它的合成属性是元组
当然,我可以尝试语义操作,但是有没有一种没有语义操作的优雅替代方法?让列表操作符接受最少数量的元素需要创建一个全新的解析器来引入该行为,因为与repeat
不同,它没有配置为这样做。我希望下面的示例可以帮助您理解如何使用a>+(省略[b]>>a)
来实现您想要的
#包括
#包括
#包括
#包括
名称空间qi=boost::spirit::qi;
无效打印(常量标准::矢量和数据)
{
std::cout>+(“->”>>节点));
//使用节点和属性解析节点(“节点1->节点2箭头大小=1.0”,节点>>+(“->”节点>>属性);
qi::规则至少两个节点=节点>>+(“->”>>节点);
使用节点和属性解析节点(“节点1->节点2箭头大小=1.0”,至少两个节点>>属性);
}
问题是上面的代码只对node1成功代码>,这是不正确的
你逆流而上。只需node1代码>在圆点上很好。因此,安排语法来反映它可能更容易
考虑
Graphviz的语法有相当多的特性,使得很难直接将语法树转换为有用的图形表示
我认为这反映了这样一个事实,即它们自己的解析函数动态地构建一个图,而不是试图表示源语法树
这是显而易见的,因为语义是有状态的,在全局状态、词法范围和子图名称空间之间有着微妙的混合。图形中出现的顺序很重要。节点总是共享一个“全局”名称空间,并且可以隐式声明,这一事实并不能真正简化事情
如何解决
虽然我通常不喜欢语义动作,但这里似乎应该使用语义动作。您可以模仿Graphviz解析器的行为,通过使用一个“事件”响应每个解析的规则,该事件可以由一个有状态的“构建器”处理,从而对域模型进行适当的更改
但是,我尝试过这样做,结果变得非常复杂,主要是因为规则的合成类型不便于构建
分离关注点是消除此类瓶颈的关键
如果您首先解析为纯AST,并从中构建模型,那么解析器和语义逻辑都会大大简化
模型
我发明了以下模型
表示,我认为它很好地捕捉了GraphViz域模型的语义:
TODO(但请参见注释中的更新)
AST
让我们创建一组单独的类来表示源文档。注:
- 这一点紧随其后
- 这是有意的1:1与我们的精神规则稍后
- 它与模型(Id、NodeRef、CompassPoint)共享基本类型,因为它们在输入中按字面表示
名称空间模型{
使用Id=std::string;
使用Attributes=std::map;
枚举类GraphKind{有向,无向};
枚举类罗盘点{n,ne,e,se,s,sw,w,nw,c,};
结构节点{
身份证;
Id端口;
CompassPoint compass\u pt=CompassPoint::;
};
}
名称空间Ast{
使用模型::CompassPoint;
使用Model::Id;
使用Model::NodeRef;
使用Model::GraphKind;
使用optionId=boost::optional;
使用AList=Model::Attributes;
使用AttrList=std::vector
考虑到以下示例输入,尽可能多地使用我当时能想到的边缘情况:
有向图G{
图[rankdir=LR];
节点[形状=记录];
Bar[label=“{\“Bar\”{pin 1 | 2 | 3 | 4 | 5}}];
Foo[label=“{data0 | data1 | data2 | data3 | data4}”Foo\“{out0 | out1 | out2 | gnd | ex0 | hi | lo}”;
Bew[label=“{clk | syn | mux0 | mux1 | signal}}”Bew\“{out0 | out1 | out2}}”;
条形图:p1->Foo:data0;
条:p2->Foo:data1;
条形图:p3->Foo:data2;
条形图:p4->Foo:data3;
条:p5->Foo:data4;
劫持;
Foo:out0->Bew:mux0;
Foo:out1->Bew:mux1;
Bew:clk->Foo:ex0;
Gate[label=“{{”)
解析成功
(0)有向(G{(图{[“rankdir”=“LR”;];
});
(节点{[“形状”=“记录”];
});
((棒{[“标签”=“{\“棒”{pin 1 | 2 | 3 | 4 | 5}}”);
});
((Foo){[“label”=“{data0 | data1 | data2 | data3 | data4}}”Foo{out0 | out1 | out2 | gnd | ex0 | hi | lo}”];
});
((Bew){[“label”=“{clk | syn | mux0 | mux1 | signal}}{Bew\{out0 | out1 | out2}”];
});
({(第1栏));
(Foo data0);;
} {});
({(p2条));
(Foo data1);;
} {});
({(p3条));
(福达
#include <iostream>
#include <vector>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/std_pair.hpp>
namespace qi= boost::spirit::qi;
void print(const std::vector<std::string>& data)
{
std::cout << "{ ";
for(const auto& elem : data) {
std::cout << elem << " ";
}
std::cout << "} ";
}
void print(const std::pair<std::string,double>& data)
{
std::cout << "[ " << data.first << ", " << data.second << " ]";
}
template <typename Parser,typename... Attrs>
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 <typename Parser>
void parse_with_nodes(const std::string& str, const Parser& parser)
{
std::vector<std::string> nodes;
parse(str,parser,nodes);
}
template <typename Parser>
void parse_with_nodes_and_attr(const std::string& str, const Parser& parser)
{
std::vector<std::string> nodes;
std::pair<std::string,double> attr_pair;
parse(str,parser,nodes,attr_pair);
}
int main()
{
qi::rule<std::string::const_iterator,std::string()> node=+qi::alnum;
qi::rule<std::string::const_iterator,std::pair<std::string,double>(),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<std::string::const_iterator,std::vector<std::string>(),qi::space_type> at_least_two_nodes = node >> +("->" >> node);
parse_with_nodes_and_attr("node1->node2 arrowsize=1.0", at_least_two_nodes >> attr);
}
namespace Model {
using Id = std::string;
using Attributes = std::map<Id, std::string>;
enum class GraphKind { directed, undirected };
enum class CompassPoint { n, ne, e, se, s, sw, w, nw, c, _ };
struct NodeRef {
Id id;
Id port;
CompassPoint compass_pt = CompassPoint::_;
};
}
namespace Ast {
using Model::CompassPoint;
using Model::Id;
using Model::NodeRef;
using Model::GraphKind;
using OptionalId = boost::optional<Id>;
using AList = Model::Attributes;
using AttrList = std::vector<AList>;
struct AttrStmt {
enum Group { graph, node, edge } group;
AttrList attributes;
};
struct Attr {
Id key, value;
operator std::pair<Id, Id>() const { return {key, value}; }
};
struct NodeStmt {
NodeRef node_id;
AttrList attributes;
};
struct EdgeStmt;
using Stmt = boost::variant<
AttrStmt,
Attr,
NodeStmt,
boost::recursive_wrapper<EdgeStmt> // includes sub graphs
>;
using StmtList = std::vector<Stmt>;
struct Graph {
OptionalId id;
StmtList stmt_list;
};
struct EdgeStmt {
std::vector<boost::variant<NodeRef, Graph> > hops;
AttrList attributes;
};
struct GraphViz {
bool strict;
GraphKind kind;
Graph graph;
};
}
namespace Parser {
namespace qi = boost::spirit::qi;
namespace px = boost::phoenix;
template <typename It>
struct GraphViz : qi::grammar<It, Ast::GraphViz()> {
GraphViz() : GraphViz::base_type(start) {
using namespace qi;
using boost::spirit::repository::qi::distinct;
auto kw = distinct(char_("a-zA-Z0-9_"));
start = skip(space) [matches[kw["strict"]] >> kind_ >> graph_];
kind_ %= kw["digraph"] >> attr(GraphKind::directed) [ set_arrow_(px::val("->")) ]
| kw["graph"] >> attr(GraphKind::undirected) [ set_arrow_(px::val("--")) ]
;
graph_ = -ID_ >> stmt_list;
subgraph_ = -(kw["subgraph"] >> -ID_) >> stmt_list;
string_ = '"' >> *('\\' >> char_ | ~char_('"')) >> '"';
ID_ = string_ | +char_("a-zA-Z0-9_");
stmt_list = '{' >> *(stmt >> -lit(';')) >> '}';
stmt = attr_stmt
| attribute
| node_stmt
| edge_stmt
;
attr_stmt = kw[attr_group] >> attr_list;
attribute = ID_ >> '=' >> ID_;
node_stmt = node_id >> -attr_list >> !arrow_;
edge_stmt
= (node_id | subgraph_) % arrow_ >> -attr_list
;
a_list = '[' >> *(attribute >> -omit[char_(",;")]) >> ']';
attr_list = +a_list;
node_id
= ID_ >> (
(attr(Ast::Id{})) >> (':' >> kw[compass_pt]) >> !lit(':')
| (':' >> ID_ | attr(Ast::Id{})) >> (':' >> kw[compass_pt] | attr(Ast::CompassPoint::_))
)
;
BOOST_SPIRIT_DEBUG_NODES(
(graph_) (subgraph_)
(a_list) (attr_list)
(stmt) (attr_stmt) (attribute) (node_stmt) (edge_stmt) (stmt_list)
(node_id)
(start)(kind_)(ID_)(string_)
)
}
private:
////////////////////////
using Skipper = qi::space_type;
//////////////////////////////
// Arrows depend on GraphKind
qi::symbols<const char> arrow_;
struct set_arrow_t { // allow dynamic setting
qi::symbols<const char>& _ref;
void operator()(const char* op) const { _ref.clear(); _ref.add(op); }
};
px::function<set_arrow_t> set_arrow_ { {arrow_} };
////////////////////////
// enums using symbols<>
struct AttrGroup : qi::symbols<const char, Ast::AttrStmt::Group> {
AttrGroup() { add
("graph", Ast::AttrStmt::Group::graph)
("node", Ast::AttrStmt::Group::node)
("edge", Ast::AttrStmt::Group::edge);
}
} attr_group;
struct CompassPoint : qi::symbols<const char, Ast::CompassPoint> {
CompassPoint() { add
("n", Ast::CompassPoint::n)
("ne", Ast::CompassPoint::ne)
("e", Ast::CompassPoint::e)
("se", Ast::CompassPoint::se)
("s", Ast::CompassPoint::s)
("sw", Ast::CompassPoint::sw)
("w", Ast::CompassPoint::w)
("nw", Ast::CompassPoint::nw)
("c", Ast::CompassPoint::c)
("_", Ast::CompassPoint::_);
}
} compass_pt;
////////////////////////
// productions
qi::rule<It, Ast::Graph(), Skipper> graph_, subgraph_;
qi::rule<It, Ast::AList(), Skipper> a_list;
qi::rule<It, Ast::AttrList(), Skipper> attr_list;
qi::rule<It, Ast::NodeRef(), Skipper> node_id; // misnomer
qi::rule<It, Ast::Stmt(), Skipper> stmt;
qi::rule<It, Ast::AttrStmt(), Skipper> attr_stmt;
qi::rule<It, Ast::Attr(), Skipper> attribute;
qi::rule<It, Ast::NodeStmt(), Skipper> node_stmt;
qi::rule<It, Ast::EdgeStmt(), Skipper> edge_stmt;
qi::rule<It, Ast::StmtList(), Skipper> stmt_list;
// implicit lexemes
using GraphKind = Ast::GraphKind;
qi::rule<It, Ast::GraphViz()> start;
qi::rule<It, GraphKind()> kind_;
qi::rule<It, Ast::Id()> ID_;
qi::rule<It, std::string()> string_;
};
}
digraph G {
graph [rankdir = LR];
node[shape=record];
Bar[label="{ \"Bar\"|{<p1>pin 1|<p2> 2|<p3> 3|<p4> 4|<p5> 5} }"];
Foo[label="{ {<data0>data0|<data1>data1|<data2>data2|<data3>data3|<data4>data4}|\"Foo\" |{<out0>out0|<out1>out1|<out2>out2|<GND>gnd|<ex0>ex0|<hi>hi|<lo>lo} }"];
Bew[label="{ {<clk>clk|<syn>syn|<mux0>mux0|<mux1>mux1|<signal>signal}|\"Bew\" |{<out0>out0|<out1>out1|<out2>out2} }"];
Bar:p1 -> Foo:data0;
Bar:p2 -> Foo:data1;
Bar:p3 -> Foo:data2;
Bar:p4 -> Foo:data3;
Bar:p5 -> Foo:data4;
hijacked;
Foo:out0 -> Bew:mux0;
Foo:out1 -> Bew:mux1;
Bew:clk -> Foo:ex0;
Gate[label="{ {<a>a|<b>b}|OR|{<ab>a\|b} }"];
Foo:hi -> Gate:a;
Foo:lo -> Gate:b;
Gate:ab -> Bew:signal;
subgraph cluster1 {
graph [
label=G1];
2;
3;
2 -> 4;
3 -> 9;
3 -> 12;
9 -> 11;
9 -> 10;
10 -> 3;
}
subgraph cluster2 {
graph [label=G2];
10 -> 3;
more;
subgraph clusterNested {
graph [label=nested];
innermost;
hijacked[shape=diamond];
}
}
subgraph cluster1 {
graph [label=G1_override];
11 -> 4;
last;
hijacked;
subgraph clusterNested {
graph [label="can override nested?"];
{
unnested;
first_override;
} [color=red]
};
}
10[shape=circle][color=red];
10[shape=circle color=red];
10[shape=circle; color=red,];
subgraph clusterNested {
graph [label="can't override nested"];
unnested;
second_override;
}
more -> last;
}
Parse success
(0 directed ( G {(graph {["rankdir"="LR"; ];
});
(node {["shape"="record"; ];
});
((Bar _) {["label"="{ \"Bar\"|{<p1>pin 1|<p2> 2|<p3> 3|<p4> 4|<p5> 5} }"; ];
});
((Foo _) {["label"="{ {<data0>data0|<data1>data1|<data2>data2|<data3>data3|<data4>data4}|\"Foo\" |{<out0>out0|<out1>out1|<out2>out2|<GND>gnd|<ex0>ex0|<hi>hi|<lo>lo} }"; ];
});
((Bew _) {["label"="{ {<clk>clk|<syn>syn|<mux0>mux0|<mux1>mux1|<signal>signal}|\"Bew\" |{<out0>out0|<out1>out1|<out2>out2} }"; ];
});
({(Bar p1 _);
(Foo data0 _);
} {});
({(Bar p2 _);
(Foo data1 _);
} {});
({(Bar p3 _);
(Foo data2 _);
} {});
({(Bar p4 _);
(Foo data3 _);
} {});
({(Bar p5 _);
(Foo data4 _);
} {});
((hijacked _) {});
({(Foo out0 _);
(Bew mux0 _);
} {});
({(Foo out1 _);
(Bew mux1 _);
} {});
({(Bew clk _);
(Foo ex0 _);
} {});
((Gate _) {["label"="{ {<a>a|<b>b}|OR|{<ab>a|b} }"; ];
});
({(Foo hi _);
(Gate a _);
} {});
({(Foo lo _);
(Gate b _);
} {});
({(Gate ab _);
(Bew signal _);
} {});
((subgraph _) {});
((cluster1 _) {});
({(-- {(graph {["label"="G1"; ];
});
((2 _) {});
((3 _) {});
({(2 _);
(4 _);
} {});
({(3 _);
(9 _);
} {});
({(3 _);
(12 _);
} {});
({(9 _);
(11 _);
} {});
({(9 _);
(10 _);
} {});
({(10 _);
(3 _);
} {});
});
} {});
((subgraph _) {});
((cluster2 _) {});
({(-- {(graph {["label"="G2"; ];
});
({(10 _);
(3 _);
} {});
((more _) {});
((subgraph _) {});
((clusterNested _) {});
({(-- {(graph {["label"="nested"; ];
});
((innermost _) {});
((hijacked _) {["shape"="diamond"; ];
});
});
} {});
});
} {});
((subgraph _) {});
((cluster1 _) {});
({(-- {(graph {["label"="G1_override"; ];
});
({(11 _);
(4 _);
} {});
((last _) {});
((hijacked _) {});
((subgraph _) {});
((clusterNested _) {});
({(-- {(graph {["label"="can override nested?"; ];
});
({(-- {((unnested _) {});
((first_override _) {});
});
} {["color"="red"; ];
});
});
} {});
});
} {});
((10 _) {["shape"="circle"; ];
["color"="red"; ];
});
((10 _) {["color"="red"; "shape"="circle"; ];
});
((10 _) {["color"="red"; "shape"="circle"; ];
});
((subgraph _) {});
((clusterNested _) {});
({(-- {(graph {["label"="can't override nested"; ];
});
((unnested _) {});
((second_override _) {});
});
} {});
({(more _);
(last _);
} {});
}))
Remaining unparsed input: '
'