如何将迭代器传递给精神qi中的函数

时间:2012-07-24 19:20:54

标签: c++ parsing boost-spirit boost-spirit-qi

template <typename Iterator>
struct parse_grammar
: qi::grammar<Iterator, std::string()>
{
    parse_grammar()
        : parse_grammar::base_type(start_p, "start_p"){
            a_p = ',' > qi::double_;
            b_p = *a_p;
            start_p = qi::double_ > b_p >> qi::eoi;
        }

    qi::rule<Iterator, std::string()> a_p;
    qi::rule<Iterator, std::string()> b_p;
    qi::rule<Iterator, std::string()> start_p;
};


// implementation

std::vector<double> parse(std::istream& input, const std::string& filename)

{

// iterate over stream input

    typedef std::istreambuf_iterator<char> base_iterator_type;
    base_iterator_type in_begin(input);

    // convert input iterator to forward iterator, usable by spirit parser
    typedef boost::spirit::multi_pass<base_iterator_type> forward_iterator_type;
    forward_iterator_type fwd_begin = boost::spirit::make_default_multi_pass(in_begin);
    forward_iterator_type fwd_end;

    // prepare output
    std::vector<double> output;
    // wrap forward iterator with position iterator, to record the position
    typedef classic::position_iterator2<forward_iterator_type> pos_iterator_type;
    pos_iterator_type position_begin(fwd_begin, fwd_end, filename);
    pos_iterator_type position_end;

    parse_grammar<pos_iterator_type> gram;

    // parse
    try
    {
        qi::phrase_parse(
                position_begin, position_end,                     // iterators over input
                gram,                                         // recognize list of doubles
                ascii::space);                                         // comment skipper
    }
    catch(const qi::expectation_failure<pos_iterator_type>& e)
    {
        const classic::file_position_base<std::string>& pos = e.first.get_position();
        std::stringstream msg;
        msg <<
            "parse error at file " << pos.file <<
            " line " << pos.line << " column " << pos.column << std::endl <<
            "'" << e.first.get_currentline() << "'" << std::endl <<
            " " << "^- here";
        throw std::runtime_error(msg.str());
    }

    // return result
    return output;
}

我有以上示例代码(此处使用来自boost-spirit网站的代码)。

在规则a_p的语法中,我想使用语义动作并调用方法并将迭代器传递给它,如下所示:

a_p = ',' > qi::double_[boost::bind(&parse_grammar::doStuff(), this, 
    boost::ref(position_begin), boost::ref(position_end)];

如果方法doStuff的签名是这样的:

void doStuff(pos_iterator_type const& first, pos_iterator_type const& last);

任何想法如何做到这一点? 我不介意任何方式(如果我可以使用boost :: phoenix或者不确定的方法),只要迭代器以其当前状态传递的方法。

1 个答案:

答案 0 :(得分:3)

我不完全确定为什么你认为你'需要'你所描述的。我担心你的实际任务的解决方案可能很简单:

start_p = qi::double_ % ',' > qi::eoi;

然而,由于实际问题非常有趣,并且将位置交互器与istream_buf结合使用(而不仅仅是通常的(较慢的)boost::spirit::istream_iterator)具有它的优点,我将展示你如何用语义行为来做到这一点。

对于

的简单(但相当完整)测试主体
int main()
{
    std::istringstream iss(
            "1, -3.4 ,3.1415926\n"
            ",+inF,-NaN  ,\n"
            "2,-.4,4.14e7\n");

    data_t parsed = parse(iss, "<inline-test>");

    std::cout << "Done, parsed " << parsed.size() << " values ("
        << "min: " << *std::min_element(parsed.begin(), parsed.end()) << ", "
        << "max: " << *std::max_element(parsed.begin(), parsed.end()) << ")\n";
}

带有语义操作的输出现在变为:

debug ('start_p') at <inline-test>:1:[1..2] '1'  = 1
debug ('start_p') at <inline-test>:1:[4..8] '-3.4'   = -3.4
debug ('start_p') at <inline-test>:1:[10..19]   '3.1415926'  = 3.14159
debug ('start_p') at <inline-test>:2:[2..6] '+inF'   = inf
debug ('start_p') at <inline-test>:2:[7..11]    '-NaN'   = -nan
debug ('start_p') at <inline-test>:3:[1..2] '2'  = 2
debug ('start_p') at <inline-test>:3:[3..6] '-.4'    = -0.4
debug ('start_p') at <inline-test>:3:[7..13]    '4.14e7'     = 4.14e+07
Done, parsed 8 values (min: -3.4, max: inf)

http://liveworkspace.org/code/8a874ef3...

上查看 直播

注意它是怎样的

  • 演示了对实际解析器实例名称('start_p')
  • 的访问
  • 演示了完整的源迭代器范围
  • 显示了如何在语义操作中进行专门处理
  • 我仍然建议使用qi::double_来解析原始输入,因为这是我所知道的唯一可以轻松处理所有情况的事情(请参阅测试数据和其他问题:{{3 }})
  • 演示通过显示已解析值的统计信息,将实际数据有效地解析到向量

完整代码

以下是供将来参考的完整代码:

#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/support_multi_pass.hpp>
#include <boost/spirit/include/classic_position_iterator.hpp>
#include <boost/phoenix/function/adapt_function.hpp>

namespace qi      = boost::spirit::qi;
namespace phx     = boost::phoenix;
namespace classic = boost::spirit::classic;
namespace ascii   = boost::spirit::ascii;

typedef std::vector<double> data_t;

///////// USING A FREE FUNCTION
//
template <typename Grammar, typename Range>
    double doStuff_(Grammar &grammar, Range pos_range)
{
    // for efficiency, cache adhoc grammar:
    static const qi::rule   <typename Range::iterator, double()> r_double = qi::double_;
    static const qi::grammar<typename Range::iterator, double()> g_double(r_double); // caching just the rule may be enough, actually

    double value = 0;
    qi::parse(pos_range.begin(), pos_range.end(), g_double, value);

    std::cout << "debug ('" << grammar.name() << "') at "
       << pos_range.begin().get_position().file   << ":"
       << pos_range.begin().get_position().line   << ":["
       << pos_range.begin().get_position().column << ".." 
       << pos_range.end  ().get_position().column << "]\t" 
       << "'" << std::string(pos_range.begin(),pos_range.end()) << "'\t = "
       << value
       << '\n';

    return value;
}

BOOST_PHOENIX_ADAPT_FUNCTION(double, doStuff, doStuff_, 2)

template <typename Iterator, typename Skipper>
struct parse_grammar : qi::grammar<Iterator, data_t(), Skipper>
{
    parse_grammar()
        : parse_grammar::base_type(start_p, "start_p")
    {
        using qi::raw;
        using qi::double_;
        using qi::_1;
        using qi::_val;
        using qi::eoi;
        using phx::push_back;

        value_p = raw [ double_ ] [ _val = doStuff(phx::ref(*this), _1) ];
        start_p = value_p % ',' > eoi;

        // // To use without the semantic action (more efficient):
        // start_p = double_ % ',' >> eoi;
    }

    qi::rule<Iterator, data_t::value_type(), Skipper> value_p;
    qi::rule<Iterator, data_t(), Skipper> start_p;
};

// implementation
data_t parse(std::istream& input, const std::string& filename)
{
    // iterate over stream input
    typedef std::istreambuf_iterator<char> base_iterator_type;
    base_iterator_type in_begin(input);

    // convert input iterator to forward iterator, usable by spirit parser
    typedef boost::spirit::multi_pass<base_iterator_type> forward_iterator_type;
    forward_iterator_type fwd_begin = boost::spirit::make_default_multi_pass(in_begin);
    forward_iterator_type fwd_end;

    // wrap forward iterator with position iterator, to record the position
    typedef classic::position_iterator2<forward_iterator_type> pos_iterator_type;
    pos_iterator_type position_begin(fwd_begin, fwd_end, filename);
    pos_iterator_type position_end;

    parse_grammar<pos_iterator_type, ascii::space_type> gram;

    data_t output;
    // parse
    try
    {
        if (!qi::phrase_parse(
                position_begin, position_end,  // iterators over input
                gram,                          // recognize list of doubles
                ascii::space,                  // comment skipper
                output)                        // <-- attribute reference
           )
        {
            std::cerr << "Parse failed at " 
               << position_begin.get_position().file   << ":"
               << position_begin.get_position().line   << ":"
               << position_begin.get_position().column << "\n";
        }
    }
    catch(const qi::expectation_failure<pos_iterator_type>& e)
    {
        const classic::file_position_base<std::string>& pos = e.first.get_position();
        std::stringstream msg;
        msg << "parse error at file " << pos.file
            << " line "               << pos.line
            << " column "             << pos.column
            << "\n\t'"                << e.first.get_currentline()
            << "'\n\t "               << std::string(pos.column, ' ') << "^-- here";

        throw std::runtime_error(msg.str());
    }

    return output;
}

int main()
{
    std::istringstream iss(
            "1, -3.4 ,3.1415926\n"
            ",+inF,-NaN  ,\n"
            "2,-.4,4.14e7\n");

    data_t parsed = parse(iss, "<inline-test>");

    std::cout << "Done, parsed " << parsed.size() << " values ("
        << "min: " << *std::min_element(parsed.begin(), parsed.end()) << ", "
        << "max: " << *std::max_element(parsed.begin(), parsed.end()) << ")\n";
}