如何从循环中构建提升::精神规则
how to build boost::spirit rule from a loop?
我想使用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};
...
这些映射不是固定的(尤其是后面的映射(,因为用户可以决定定义新的前缀和单位。
我构建了以下语法来解析字符串(如 kg
或 cm
(,并获取一个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];
}
此实现可编译但产生错误的结果。
我的问题/问题是,如何使用前缀和单位映射上的循环构建prefixRule
和unitRule
规则?
您似乎正在寻找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)
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- 无法在 CLion 中构建 C++ 项目
- 函数向量_指针有不同的原型,我可以构建一个吗
- 如何使用ndk-build.cmd构建Android.so文件
- 此代码是否违反一个定义规则
- libssh 的函数在构建 libssh 时无法在 Qt 和 cmake 错误中找到
- 生成文件不对文件使用隐式规则
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 构建可组合有向图(扫描仪生成器的汤普森构造算法)
- 变量可能尚未初始化[MIRA 2012规则9.1,强制性]
- 如何在bazel中使用make规则链接库构建
- 使用 bazel 从源代码构建张量流服务遇到错误:C++规则'@org_tensorflow//…'编译失败(出口 4)
- 在makefile中更改构建规则以构建多种文件类型
- 重复使用相同的规则来构建不同的目标
- C++ 构建警告:取消引用类型双关指针将违反严格锯齿规则
- 没有规则来使目标"类.cpp,"构建/..."需要x86/class.o' 停止。乌班图中的错误
- 隐式构建规则:GNU Make Multiple Makefiles Multiple Directories
- 如何从循环中构建提升::精神规则
- 为什么Xcode 4.2为.cpp文件应用C文件构建规则脚本?