如何从循环中构建提升::精神规则

how to build boost::spirit rule from a loop?

本文关键字:规则 构建 循环      更新时间:2023-10-16

我想使用boost spirit定义一个物理单位解析器。解析器必须考虑单位和前缀。为此,我也有将前缀和单位存储在 SI 中的映射,这些前缀和单位在下面伪代码中定义。

std::map<std::string,double> prefixes;
// prefix - milli
prefixes["m"]=1.0e-03;
// prefix - centi
prefixes["c"]=1.0e-02;
...
std::map<std::string,std::vector<int>> units;
// time - seconds
units["s"] = {1,0,0,0,0,0,0};
// length - meters
units["m"] = {0,1,0,0,0,0,0};
...

这些映射不是固定的(尤其是后面的映射(,因为用户可以决定定义新的前缀和单位。

我构建了以下语法来解析字符串(如 kgcm(,并获取一个std::pair<double,std::vector<int>>作为输出,该将包含与成功解析的字符串匹配的前缀和单位。

UnitParser.h

struct UnitParser : qi::grammar<std::string::const_iterator,std::pair<double,std::vector<int>>()>
{
    UnitParser();
    qi::rule<std::string::const_iterator,std::pair<double,std::vector<int>>()> prefixedUnitRule;
    qi::rule<std::string::const_iterator,double()> prefixRule;
    qi::rule<std::string::const_iterator,std::vector<int>()> unitRule;
};

及其CPP对应方UnitParser.cpp:

UnitParser::UnitParser() : UnitParser::base_type(prefixedUnit)
{
    using namespace qi;
    using namespace phx;
    prefixedUnitRule = prefixRule >> unitRule;
    for (const auto& p : prefixes)
        prefixRule = string(p.first)[_val=p.second] | prefixRule.copy()[_val=p.second];
    for (const auto& p : units)
        unitRule = string(p.first)[_val=p.second] | unitRule.copy()[_val=p.second];
}

此实现可编译但产生错误的结果。

我的问题/问题是,如何使用前缀和单位映射上的循环构建prefixRuleunitRule规则?

您似乎正在寻找qi::symbols<>

template <typename It>
struct parser : qi::grammar<It, parsed_unit()> {
    parser() : parser::base_type(start) {
        _prefix.add
            ("m",  1.0 * std::milli::num / std::milli::den)
            ("c",  1.0 * std::centi::num / std::centi::den)
            ("d",  1.0 * std::deci::num / std::deci::den)
            ("da", 1.0 * std::deca::num / std::deca::den)
            ("h",  1.0 * std::hecto::num / std::hecto::den)
            ("k",  1.0 * std::kilo::num / std::kilo::den);
        _unit.add
            ("s", units::s)
            ("m", units::m);
        start = _prefix       >> _unit >> qi::eoi
              | qi::attr(1.0) >> _unit >> qi::eoi;
    }
  private:
    qi::symbols<char, std::reference_wrapper<units::unit const> > _unit;
    qi::symbols<char, double> _prefix;
    qi::rule<It, parsed_unit()> start;
};

一个完整的演示,使用维度和一些特征的std::array,以便我们可以有效地从对维度的引用转换为解析的单元:

演示

住在科里鲁

#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/spirit/include/qi.hpp>
#include <ratio>
namespace qi = boost::spirit::qi;
namespace units {
    using unit = std::array<int, 6>;
    static const unit s = {{ 1, 0, 0, 0, 0, 0} };
    static const unit m = {{ 0, 1, 0, 0, 0, 0} };
}
namespace boost { namespace spirit { namespace traits {
    template <> struct is_container<units::unit, void> : mpl::false_ { };
    template <typename T>
        struct assign_to_attribute_from_value<typename std::reference_wrapper<T const>, T, void> :
            assign_to_attribute_from_value<T, T, void> { };
} } }
using parsed_unit = std::pair<double/* factor*/, units::unit/* dimension*/>;
template <typename It>
struct parser : qi::grammar<It, parsed_unit()> {
    parser() : parser::base_type(start) {
        _prefix.add
            ("m",  1.0 * std::milli::num / std::milli::den)
            ("c",  1.0 * std::centi::num / std::centi::den)
            ("d",  1.0 * std::deci::num / std::deci::den)
            ("da", 1.0 * std::deca::num / std::deca::den)
            ("h",  1.0 * std::hecto::num / std::hecto::den)
            ("k",  1.0 * std::kilo::num / std::kilo::den);
        _unit.add
            ("s", units::s)
            ("m", units::m);
        start = _prefix       >> _unit >> qi::eoi
              | qi::attr(1.0) >> _unit >> qi::eoi;
    }
  private:
    qi::symbols<char, std::reference_wrapper<units::unit const> > _unit;
    qi::symbols<char, double> _prefix;
    qi::rule<It, parsed_unit()> start;
};
int main() {
    using It = std::string::const_iterator;
    parser<It> p;
    for (std::string const input : { 
            "mm", "cm", "dm", "m", "dam", "hm", "km",
            "ms", "cs", "ds", "s", "das", "hs", "ks",
        })
    {
        std::cout << "--- Test: '" << input << "'n";
        auto f = input.begin(), l = input.end();
        parsed_unit u;
        bool ok = qi::phrase_parse(f, l, p, qi::space, u);
        if (ok) {
            std::cout << "Parsed: {";
            std::copy(u.second.begin(), u.second.end(), std::ostream_iterator<int>(std::cout));
            std::cout << "}, scale factor: e" << std::log10(u.first) << " (~" << u.first << ")n";
        }
        else
            std::cout << "Parse failedn";
        if (f != l)
            std::cout << "Remaining input: '" << std::string(f,l) << "'n";
    }
}

指纹

--- Test: 'mm'
Parsed: {010000}, scale factor: e-3 (~0.001)
--- Test: 'cm'
Parsed: {010000}, scale factor: e-2 (~0.01)
--- Test: 'dm'
Parsed: {010000}, scale factor: e-1 (~0.1)
--- Test: 'm'
Parsed: {010000}, scale factor: e0 (~1)
--- Test: 'dam'
Parsed: {010000}, scale factor: e1 (~10)
--- Test: 'hm'
Parsed: {010000}, scale factor: e2 (~100)
--- Test: 'km'
Parsed: {010000}, scale factor: e3 (~1000)
--- Test: 'ms'
Parsed: {100000}, scale factor: e-3 (~0.001)
--- Test: 'cs'
Parsed: {100000}, scale factor: e-2 (~0.01)
--- Test: 'ds'
Parsed: {100000}, scale factor: e-1 (~0.1)
--- Test: 's'
Parsed: {100000}, scale factor: e0 (~1)
--- Test: 'das'
Parsed: {100000}, scale factor: e1 (~10)
--- Test: 'hs'
Parsed: {100000}, scale factor: e2 (~100)
--- Test: 'ks'
Parsed: {100000}, scale factor: e3 (~1000)