大家好,我是新手boost和boost::spirit,所以我很抱歉这个新手的问题。
当我使用qi::phrase_parse函数时,该函数只返回指示解析是否成功的布尔变量,但我不知道在哪里可以找到解析语法树等...some排序的结果。
如果我使用宏XML,树的#define BOOST_SPIRIT_DEBUG表示将打印在标准输出上,但这些节点必须存储在某个地方。你能帮帮我吗?
发布于 2012-07-19 19:44:18
你可以“绑定”属性引用。qi::parse、qi::phrase_parse (和相关)接受各种参数,这些参数将用于接收公开的属性。
一个简单的例子是:(编辑也包含了一个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结构,请查看utree:http://www.boost.org/doc/libs/1_50_0/libs/spirit/doc/html/spirit/support/utree.html
https://stackoverflow.com/questions/11555518
复制相似问题