Boost :: The result of the spirit of the phrase_parse

Greetings to all whom I am new to boost and enhance :: spirit, so I apologize for asking about noob.

When I use the function qi::phrase_parse, the function returns only the bool variable, which indicates whether the parsing was successful or not, but I do not know where I can find the result of the parsing ... some kind of syntax tree, etc.

If I use a macro, the #define BOOST_SPIRIT_DEBUGXML representation of the tree prints to standard output, but these nodes must be saved somewhere. could you help me?

+5
source share
1 answer

. qi::parse, qi::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, utree: http://www.boost.org/doc/libs/1_50_0/libs/spirit/doc/html/spirit/support/utree.html

+8

All Articles