【问题标题】:Boost::Spirit result of phrase_parseBoost::Spirit 的phrase_parse 结果
【发布时间】:2012-07-19 07:02:31
【问题描述】:

大家好,我是 boost 和 boost::spirit 的新手,所以对于菜鸟问题​​我很抱歉。

当我使用qi::phrase_parsefunction 时,该函数仅返回 bool 变量,指示解析是否成功,但我不知道在哪里可以找到解析结果...某种语法树等.

如果我使用宏 #define BOOST_SPIRIT_DEBUG 树的 XML 表示会打印在标准输出上,但这些节点必须存储在某个地方。你能帮我吗?

【问题讨论】:

  • 没有代码示例很难回答您的问题
  • 我认为任何精神问题都不是菜鸟问题。

标签: c++ parsing boost-spirit


【解决方案1】:

您可以“绑定”属性引用。 qi::parseqi::phrase_parse(和相关)接受将用于接收公开属性的可变参数。

一个简单的例子是:(EDIT也包括了一个utree例子)

#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_utree.hpp>

namespace qi = boost::spirit::qi;

int main()
{
    using namespace qi;

    std::string input("1 2 3 4 5");
    std::string::const_iterator F(input.begin()), f(F), l(input.end());

    std::vector<int> ints;
    if (qi::phrase_parse(f = F, l, *qi::int_, qi::space, ints))
        std::cout << ints.size() << " ints parsed\n";

    int i;
    std::string s;
    // it is variadic:
    if (qi::parse(f = F, l, "1 2 " >> qi::int_ >> +qi::char_, i, s))
        std::cout << "i: " << i << ", s: " << s << '\n';

    std::pair<int, std::string> data;
    // any compatible sequence can be used:
    if (qi::parse(f = F, l, "1 2 " >> qi::int_ >> +qi::char_, data))
        std::cout << "first: " << data.first << ", second: " << data.second << '\n';

    // using utree:
    boost::spirit::utree tree;
    if (qi::parse(f = F, l, "1 2 " >> qi::int_ >> qi::as_string [ +qi::char_ ], tree))
        std::cout << "tree: " << tree << '\n';

}

输出:

5 ints parsed
i: 3, s:  4 5
first: 3, second:  4 5
tree: ( 3 " 4 5" )

一些具有类似“AST”的数据结构的解析器示例:

如果你想拥有一个非常通用的 AST 结构,请查看utreehttp://www.boost.org/doc/libs/1_50_0/libs/spirit/doc/html/spirit/support/utree.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多