- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想解析点语言 ( http://www.graphviz.org/content/dot-language )。它是一种图形定义语言,用于定义节点和它们之间的连接。典型的语句类似于 node1->node2->node3;
。最好使用 boost::spirit 列表运算符 %
来制作节点列表。一个天真的方法是:
edge_stmt %=
(
node_or_subgraph(_r1) % (qi::eps(_r1) >> tok.diredgeop | tok.undiredgeop)
) >> -attr_list;
_r1
表示这是有向图还是无向图,diredgeop
是->
的token,undiredgeop
分别是--
的标记。
问题是上面的代码只会对 node1;
成功,这是不正确的。为了获得正确的解析器,我必须以某种方式声明 %
构建的列表中必须至少有两个元素。怎么办?
文档说 a % b
等同于 a >> *(omit[b] >> a)
,这是不正确的。有人可能想试试这个:
edge_stmt %=
(
node_or_subgraph(_r1) >>
+(
qi::omit
[
qi::eps(_r1) >> tok.diredgeop | tok.undiredgeop
] >>
node_or_subgraph(_r1)
)
) >> -attr_list;
但是这段代码并没有产生 vector ,它的合成属性是一个元组。
我当然可以尝试语义操作,但是有没有没有语义操作的优雅替代方案?
最佳答案
The problem is the above code will succeed for just
node1;
, which is incorrect.
你逆流而上。只需 node1;
在 DOT 中就可以了。因此,安排语法来反射(reflect)它可能更容易。
Graphviz 的语法有很多特质,很难将语法树直接转换为有用的图形表示。
我认为这反射(reflect)了这样一个事实,即他们自己的解析函数动态地构建了一个图,根本没有试图表示源语法树。
这是显而易见的,因为语义是有状态的,在全局状态、词法范围和子图命名空间之间有微妙的混合。图中出现的顺序很重要。节点总是共享一个“全局”命名空间并且可以隐式声明这一事实是一个并不能真正简化事情的因素。
虽然我通常不是语义 Action 的粉丝¹,但语义 Action 似乎是这里要使用的东西。您可以通过使用“事件”响应每个解析的规则来模仿 Graphviz 的解析器的行为,该“事件”可以由有状态的“构建器”处理,从而导致对域模型进行适当的更改。
但是,我尝试这样做,它变得非常复杂,主要是因为规则的合成类型不便于构建。
关注点分离是消除此类瓶颈²的关键。
如果您首先解析为纯 AST,然后从中构建您的模型,那么解析器和语义逻辑都会得到极大的简化。
我发明了以下 Model
表示,我认为它很好地捕捉了 GraphViz 领域模型的语义:
TODO(但请参阅评论中的更新)
让我们创建一组单独的 类来表示源文档。注意:
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;
};
}
语法简单地紧跟规范,并将 1:1 映射到 Ast,因此我们不必再做任何魔术(¹ 再次)。
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_;
};
}
DEMO TIME
In fact, this part already parses GraphViz documents. No online compiler was willing to accept this (exceeding the resource limits). Here's the fully sample from this stage: https://wandbox.org/permlink/AYmxpD6lzOdhOeiS
Given the following sample input, exercising as many of the edge cases I could think of at the time:
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;
}The output, from my machine (with full debug info in pastebin)
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: '
'
¹ Boost Spirit: "Semantic actions are evil"?
² 我将这类复杂性归类为“阻抗失配”,这是我最初从对象关系映射框架中学到的一个术语
关于c++ - 如何使用具有强制性最小元素数量的boost spirit list运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46334463/
我有几个带有视频和图像的 Bootstrap slider 。在 slider 之外,我想要一个可以转到包含视频的幻灯片的按钮。包含视频的幻灯片的数量因 slider 而异。我想做的是获取幻灯片的数量
我在编写一个查询时遇到了一些问题。 我有一个由文件及其大小(以字节为单位)组成的表。它看起来像这样: FileUrl | FileSize ------------------ xyz.docx |
我有一个带 iframe 的网站和一个带另一个 iframe 的网站,所以它是一个 iframe 内嵌另一个 iframe(都在不同的域上)。那么有没有办法从父div或父主div的url(parent
以下表达式在 JavaScript 中给出了特殊的结果。 typeof (5 + "7") // Gives string typeof (5 - "7") // Gives number 如
我有一个名为“交易”的表,每当有人在我的网站上进行购买时,我都会在其中输入用户 ID、购买类型和金额。 我想向每个用户显示过去 7 天的这些统计信息。 目前,我有这个: $data = array()
我一整天都在努力寻找解决这一挑战的办法。 我有一张 table : id | amount | type | date |
我正在尝试在 10 个数据节点的集群中测试 Map reduce 程序的性能。在此过程中,我使用了 5 个 Reducers,然后是 10 个等等。 我在想增加 reducer 的数量也会使工作完成得
我正在使用 html5 输入 type="number"。我想监视此输入的变化,但是: 因为在支持它的浏览器中 它有旋转控件 我不能只监视 .keyup, 因为我不想等待它失去焦点,所以我不能只监视
我的购物车表格有问题。我创建了一个如下所示的表格: SQL Fiddle 我的问题是我希望能够选择产品 ID,并计算该产品 ID 在表格中重复的次数,以便我可以显示用户在购物车中拥有的商品数量。 寻找
我使用许多包含来显示我网站的一小部分。使用许多 include 是否合适,或者我应该减少它们(尽可能多)。包含函数要多花多少时间? 我的主页加载速度很慢。有什么方法可以让它加载更快。 (我的主页每天在
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: length of array in function argument 我的数组大小是5。例如: arrC
是否有标准的 Python 方法来处理 Python 中的物理单位/数量?我看到了来自不同领域(如物理学或神经科学)的不同模块特定解决方案。但我更愿意使用标准方法而不是“孤岛”解决方案,因为其他人应该
基本上就像标题所说的那样,有没有办法从 JavaScript 程序中查看事件循环中当前存在多少个 promise ?最好在 Deno 上。 最佳答案 Deno v1.26 添加了一个内部 API,可用
我只是想知道大型项目-比如说航空公司的预订系统,它可能有多少类/对象。 对象:客户,飞机,机场,路线,机票,订单。这就是我能想到的。该项目可能是成千上万的代码行,那么是否可能会有更多的类(执行与对象无
如果有办法限制Scala中未处理的 future 数量,我将无法提供资金。 例如下面的代码: import ExecutionContext.Implicits.global for (i
从昨天开始,我一直在努力做到这一点,尽管还没有运气。我找到了解决方案,在我想要完成的事情上总是有细微的差别。 我试图获得所有可能的组合,稍微像这样:combination_k ,但我也希望相同的项目与
我正在尝试更新 1500 个 QuickBooks Online 库存项目的现有数量。我可以从商店中提取 1500 种产品。 这个更新可以做吗?我看到手头没有数量的物品: https://develo
我想与工作人员一起扩展应用程序。 可能有 1 名 worker 或 100 名 worker ,我想无缝扩展它们。 这个想法是使用副本集。然而,由于特定领域的原因,扩展它们的适当方法是让每个工作人员知
Android Studio 有没有办法显示 XML 布局中存在的 View 数量?众所周知,布局应该包含 <=80 个 View ,因此超过此值就会出现此警告,因此告知数量会非常有帮助。 Layou
虽然编码时总是出现有关 IBOutlet 保留计数的相同问题:从 NIB 取消归档对象后保留计数?何时对 IBOutlet 使用 @property?设置时保留还是分配? Mac 和 iPhone 之
我是一名优秀的程序员,十分优秀!