【发布时间】:2016-06-19 21:20:12
【问题描述】:
我在使用 boost::spirit 解析时尝试使用语义规则创建 AST。 AST 必须只为输入的一部分构建,输入的另一部分应该在没有 sintax 树的情况下解析。
例如,对于这样的输入字符串:“self.usedFoo(Bar).filter(self.baz > baz)”或“self.Foo.filter(true >)" AST 应该只用于粗体部分。
还有一个问题:解析器运行多次解析语法并调用语义操作(instatntiating AST 节点)也多次,所以我遇到了可怕的内存泄漏。
简单的源代码:
语法:
line = stmt | stmt >> "filter.(" >> filter >> ')';
filter %= (filterterm)
filterterm %= (filterfactor)
filterfactor = value [phoenix::bind(&ValueFilterSemanticNode::Instantiate, qi::_val, qi::_1)];
实例化节点:
static void ValueFilterSemanticNode::Instantiate(QVariant &res, QVariant &value)
{
qDebug() << " Creating new Value Node...";
ValueFilterSemanticNode *n = new ValueFilterSemanticNode();
qDebug() << " " << n;
n->value = QVariant(value.toInt());
res = QVariant::fromValue(n);
}
输入:
self.filter(1)
调试:
Creating new Value Node...
0x22fdfd0
Creating new Value Node...
0x22fe030
Creating new Value Node...
0x22fde50
[...many many lines...]
Creating new Value Node...
0x22fe238
Creating new Value Node...
0x22fe218
Running Filter test
Value node running... 0x22fe218
Check result = QVariant(int, 1)
因此,如您所见,节点实例化次数过多会导致内存泄漏。
【问题讨论】:
标签: c++ boost boost-spirit boost-spirit-qi