其他分享
首页 > 其他分享> > c – 当某些结构字段被省略或与结构声明中的顺序不一样时,如何实现正确的解析?

c – 当某些结构字段被省略或与结构声明中的顺序不一样时,如何实现正确的解析?

作者:互联网

所以我有一个解析器,它将7.5 * [someAlphanumStr]或7.5 [someAlphanumStr]等字符串解析为此结构:

struct summand {
    float factor;
    std::string name;
    summand(const float & f):factor(f), name(""){}
    summand(const std::string & n):factor(1.0f), name(n){}
    summand(const float & f, const std::string & n):factor(f), name(n){}
    summand():factor(0.0f), name(""){}
};

但另外我需要能够解析像[someAlphanumStr] * 7.4,[someAlphanumStr] 5,7.4和[someAlphanumStr]这样的字符串.在最后两种情况下(7.4和[someAlphanumStr])我想设置被省略为默认值的字段的值,为此我已经为我的struct summand构造函数编写了一个参数.

下面是我生成的代码和结果:

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>

#include <iostream>
#include <string>
#include <vector>

namespace client
{
    namespace spirit = boost::spirit;
    namespace qi     = boost::spirit::qi;
    namespace ascii  = boost::spirit::ascii;

    struct summand {
        float factor;
        std::string name;
        summand(const float & f):factor(f), name(""){}
        summand(const std::string & n):factor(1.0f), name(n){}
        summand(const float & f, const std::string & n):factor(f), name(n){}
        summand():factor(0.0f), name(""){}
    };
}

BOOST_FUSION_ADAPT_STRUCT(client::summand,
                      (float, factor)
                      (std::string, name)
                      )

namespace client {

    template <typename Iterator>
    struct summand_parser : qi::grammar<Iterator, summand(), ascii::space_type>
    {
        summand_parser() : summand_parser::base_type(summand_rule)
        {
            using namespace ascii;

            summand_rule %= (qi::float_ >> -qi::lit('*') >> '[' >> qi::lexeme[alpha >> *alnum] >> ']')|('[' >> qi::lexeme[alpha >> *alnum] >> ']' >> -qi::lit('*') >> qi::float_)|(qi::float_)|('[' >> qi::lexeme[alpha >> *alnum] >> ']');

        }

        qi::rule<Iterator, summand(), ascii::space_type> summand_rule;
    };
}

void parseSummandsInto(std::string const& str, client::summand& summands)
{
    typedef std::string::const_iterator It;
    static const client::summand_parser<It> g;

    It iter = str.begin(),
    end = str.end();

    bool r = phrase_parse(iter, end, g, boost::spirit::ascii::space, summands);

    if (r && iter == end)
        return;
    else
        throw "Parse failed";
}

int main()
{
    std::vector<std::string> inputStrings = {"7.5*[someAlphanumStr]", "7.5[someAlphanumStr]", "[someAlphanumStr]*7.4", "[someAlphanumStr]5", "7.4", "[someAlphanumStr]"};

    std::for_each(inputStrings.begin(), inputStrings.end(), [&inputStrings](std::string & inputStr) {
        client::summand parsed;
        parseSummandsInto(inputStr, parsed);
        std::cout << inputStr << " -> " << boost::fusion::as_vector(parsed) << std::endl;
    });
}

结果(Coliru):

+ clang++ -std=c++11 -O0 -Wall -pedantic main.cpp
+ ./a.out
+ c++filt -t
7.5*[someAlphanumStr] -> (7.5 someAlphanumStr)
7.5[someAlphanumStr] -> (7.5 someAlphanumStr)
[someAlphanumStr]*7.4 -> (115 )
[someAlphanumStr]5 -> (115 )
7.4 -> (7.4 )
[someAlphanumStr] -> (115 omeAlphanumStr)

感谢所有人的明确答案和建议,特别是我很感谢@sehe.

解决方法:

使用Spirit [1]完成任何事情的方法是使用小步骤,沿途严格简化.

不要忍受“残酷”(就像随机重复的子表达式).而且,明确是好的.在这种情况下,我首先提取重复的子表达式并重新格式化以便易读:

    name_rule   = '[' >> qi::lexeme[alpha >> *alnum] >> ']';
    factor_rule = qi::float_;

    summand_rule %= 
          (factor_rule >> -qi::lit('*') >> name_rule)
        | (name_rule   >> -qi::lit('*') >> factor_rule)
        | (factor_rule)
        | (name_rule)
        ;

在那里,已经好多了,我没有改变一件事.可是等等!它不再编译

    qi::rule<Iterator, std::string(), ascii::space_type> name_rule;
    qi::rule<Iterator, float(),       ascii::space_type> factor_rule;

事实证明,语法只是“发生”才能编译,因为Spirit的属性兼容性规则是如此宽松/宽松,以至于匹配名称的字符只被分配给因子部分(115来自哪里:0x73是来自s的ASCII) someAlphanumStr).

OOPS / TL; DW我在这里写了一篇很长的分析,曾经,但是我通过关闭我的浏览器来破坏它,所以只有一个旧的草案缓存服务器端:(我现在将它归结为底线:

Guideline Use either constructor overloads to assign to your exposed attribute type, or use Fusion Sequence adaptation, but don’t mix the two: they will interfere in surprising/annoying ways.

别担心,我当然不会让你空手而归.我只是“手动”指导因素并在各自的“插槽”(成员)中命名组件[2].

继承属性是一种保持这种清晰易用的甜蜜方式:

// assuming the above rules redefined to take ("inherit") a summand& attribute:
qi::rule<Iterator, void(summand&), ascii::space_type> name_rule, factor_rule;

只需在语义操作中添加一个简单的赋值:

name_rule   = as_string [ '[' >> lexeme[alpha >> *alnum] >> ']' ] 
                        [ _name   = _1 ];
factor_rule = double_   [ _factor = _1 ];

现在,“魔法尘埃”当然是如何定义_name和_factor的.我更喜欢使用绑定,而不是phx :: at_c< N>由于维护费用:

static const auto _factor = phx::bind(&summand::factor, qi::_r1);
static const auto _name   = phx::bind(&summand::name,   qi::_r1);

看到?这非常简洁,清楚地显示了正在发生的事情.此外,这里没有实际需要使用Fusion适配进行加法.

现在,最后,我们也可以简化主要规则:

    summand_rule = 
              factor_rule (_val) >> - ( -lit('*') >> name_rule   (_val) )
            | name_rule   (_val) >> - ( -lit('*') >> factor_rule (_val) )
        ;

这样做,只需通过使尾随部分可选,将单分支分支组合成双分支分支.

请注意summand默认构造函数如何处理默认值:

struct summand {
    float factor;
    std::string name;

    summand() : factor(1.f), name("") {}
};

注意这是如何消除了相当复杂的.

查看运行Live on Coliru的完全适应的样本打印:

7.5*[someAlphanumStr] -> (7.5 someAlphanumStr)
7.5[someAlphanumStr] -> (7.5 someAlphanumStr)
[someAlphanumStr]*7.4 -> (7.4 someAlphanumStr)
[someAlphanumStr]5 -> (5 someAlphanumStr)
7.4 -> (7.4 )
[someAlphanumStr] -> (1 someAlphanumStr)

完整的代码清单

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

namespace client {
    namespace qi     = boost::spirit::qi;
    namespace phx    = boost::phoenix;
    namespace ascii  = boost::spirit::ascii;

    struct summand {
        float factor;
        std::string name;

        summand() : factor(1.f), name("") {}
    };
}

namespace client {

    template <typename Iterator>
    struct summand_parser : qi::grammar<Iterator, summand(), ascii::space_type>
    {
        summand_parser() : summand_parser::base_type(summand_rule)
        {
            using namespace ascii;

            static const auto _factor = phx::bind(&summand::factor, qi::_r1);
            static const auto _name   = phx::bind(&summand::name,   qi::_r1);

            name_rule   = qi::as_string [ '[' >> qi::lexeme[alpha >> *alnum] >> ']' ] 
                                          [ _name   = qi::_1 ] ;
            factor_rule = qi::double_     [ _factor = qi::_1 ] ;

            summand_rule = 
                      factor_rule (qi::_val) >> - ( -qi::lit('*') >> name_rule   (qi::_val) )
                    | name_rule   (qi::_val) >> - ( -qi::lit('*') >> factor_rule (qi::_val) )
                ;

            BOOST_SPIRIT_DEBUG_NODES((summand_rule)(name_rule)(factor_rule))
        }

        qi::rule<Iterator, void(summand&), ascii::space_type> name_rule, factor_rule;
        qi::rule<Iterator, summand(),      ascii::space_type> summand_rule;
    };
}

bool parseSummandsInto(std::string const& str, client::summand& summand)
{
    typedef std::string::const_iterator It;
    static const client::summand_parser<It> g;

    It iter(str.begin()), end(str.end());
    bool r = phrase_parse(iter, end, g, boost::spirit::ascii::space, summand);

    return (r && iter == end);
}

int main()
{
    std::vector<std::string> inputStrings = {
        "7.5*[someAlphanumStr]",
        "7.5[someAlphanumStr]",
        "[someAlphanumStr]*7.4",
        "[someAlphanumStr]5",
        "7.4",
        "[someAlphanumStr]",
    };

    std::for_each(inputStrings.begin(), inputStrings.end(), [&inputStrings](std::string const& inputStr) {
        client::summand parsed;
        if (parseSummandsInto(inputStr, parsed))
            std::cout << inputStr << " -> (" << parsed.factor << " " << parsed.name << ")\n";
        else
            std::cout << inputStr << " -> FAILED\n";
    });
}

[1]可以说,技术上还有其他任何东西

[2]您可以保留FUSION_ADAPT_STRUCT但不再需要它,如您所见

标签:boost-spirit-qi,c,struct,parsing,boost-spirit
来源: https://codeday.me/bug/20190831/1775371.html