使用局部变量提升精神

时间:2014-05-20 14:54:27

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

我想基于先前解析的值定义规则,i。即输入字符串具有以下结构:D <double number>I <integer number>。我保留一个本地布尔变量,无论第一个读取字符是D还是I。完整的代码是:

#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <string>

namespace qi = boost::spirit::qi;
namespace spirit = boost::spirit;
namespace ascii = boost::spirit::ascii;
using boost::phoenix::ref;

template <typename Iterator>
struct x_grammar : public qi::grammar<Iterator, std::string(), ascii::space_type>
{
public:
    x_grammar() : x_grammar::base_type(start_rule, "x_grammar")
    {
        using namespace qi;
        bool is_int = false;
        start_rule = lit("I")[ref(is_int) = true] | lit("D")[ref(is_int) = false] > digit_rule;
        if(ref(is_int)()) {
            digit_rule = int_[std::cout << "int " << _1 << ".\n"];
        } else {
            digit_rule = double_[std::cout << "double " << _1 << ".\n"];
        }
    }
private:
    qi::rule<Iterator, std::string(), ascii::space_type> start_rule;
    qi::rule<Iterator, std::string(), ascii::space_type> digit_rule;
};

int main()
{
    typedef std::string::const_iterator iter;
    std::string storage("I 5");
    iter it_begin(storage.begin());
    iter it_end(storage.end());
    std::string read_data;
    using boost::spirit::ascii::space;
    x_grammar<iter> g;
    try {
        bool r = qi::phrase_parse(it_begin, it_end, g, space, read_data);
        if(r) {
            std::cout << "Pass!\n";
        } else {
            std::cout << "Fail!\n";
        }
    } catch (const qi::expectation_failure<iter>& x) {
        std::cout << "Fail!\n";
    }
    return 0;
}                                                                                                                                                    

输出为:double Pass! !!它既不识别if语句,也不打印已解析的数字!

注意:我知道还有其他直接解析上述示例的方法。我必须解析的实际字符串看起来很复杂,这个例子只是说明了我想要实现的内容。总体目标是使用局部变量并根据这些变量定义其他规则。

我使用过4.6.1和Boost 1.55版本。

1 个答案:

答案 0 :(得分:2)

    if(ref(is_int)()) {

在这里你评估施工期间的状况。这不是它的工作原理。该规则将始终采用相同的分支。

相反,看看Nabialek技巧:http://boost-spirit.com/home/articles/qi-example/nabialek-trick/

以下是适用于您的样本的完整Nabialek技巧 Live On Coliru

  1. 您需要制作std::cout << "int" lazy 演员(通过至少包裹phx::ref(std::cout)phx::val("int")作为凤凰演员)

    < / LI>
  2. 您仍然没有使用属性传播(std::string()),因为它在存在语义操作时被禁用(请参阅上一个答案)。但可以传播来自Nabialek子规则的值:

  3. #define BOOST_SPIRIT_USE_PHOENIX_V3
    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/phoenix.hpp>
    #include <string>
    
    namespace qi = boost::spirit::qi;
    namespace spirit = boost::spirit;
    namespace ascii = boost::spirit::ascii;
    namespace phx = boost::phoenix;
    using boost::phoenix::ref;
    
    
    template <typename Iterator>
    struct x_grammar : public qi::grammar<Iterator, ascii::space_type, qi::locals<qi::rule<Iterator, ascii::space_type>*> >
    {
    public:
        x_grammar() : x_grammar::base_type(start_rule, "x_grammar")
        {
            using namespace qi;
    
            int_rule = int_   [std::cout << phx::val("int ") << _1 << ".\n"];
            dbl_rule = double_[std::cout << phx::val("double ") << _1 << ".\n"];
            subrules.add
                ("I", &int_rule)
                ("D", &dbl_rule);
    
            start_rule = subrules[_a = _1] >> lazy(*_a);
        }
    private:
        typedef qi::rule<Iterator, ascii::space_type> subrule;
    
        qi::symbols<char, subrule*> subrules;
        qi::rule<Iterator, ascii::space_type, qi::locals<subrule*> > start_rule;
        qi::rule<Iterator, ascii::space_type> int_rule, dbl_rule;
    };
    
    int main()
    {
        typedef std::string::const_iterator iter;
        std::string storage("I 5");
        iter it_begin(storage.begin());
        iter it_end(storage.end());
        using boost::spirit::ascii::space;
        x_grammar<iter> g;
        try {
            bool r = qi::phrase_parse(it_begin, it_end, g, space);
            if (r) {
                std::cout << "Pass!\n";
            }
            else {
                std::cout << "Fail!\n";
            }
        }
        catch (const qi::expectation_failure<iter>&) {
            std::cout << "Fail!\n";
        }
        return 0;
    }