每次检测到匹配时,我都试图将数据结构插入到向量中,但即使在编译时也是如此。代码是下一个:
#include <string>
#include <boost/xpressive/xpressive.hpp>
#include <boost/xpressive/regex_actions.hpp>
using namespace boost::xpressive;
struct Data
{
int integer;
double real;
std::string str;
Data(const int _integer, const double _real, const std::string& _str) : integer(_integer), real(_real), str(_str) { }
};
int main()
{
std::vector<Data> container;
std::string input = "Int: 0 - Real: 18.8 - Str: ABC-1005\nInt: 0 - Real: 21.3 - Str: BCD-1006\n";
sregex parser = ("Int: " >> (s1 = _d) >> " - Real: " >> (s2 = (repeat<1,2>(_d) >> '.' >> _d)) >> " - Str: " >> (s3 = +set[alnum | '-']) >> _n)
[::ref(container)->*push_back(Data(as<int>(s1), as<double>(s2), s3))];
sregex_iterator cur(input.begin(), input.end(), parser);
sregex_iterator end;
for(; cur != end; ++cur)
smatch const &what = *cur;
return 0;
}
编译失败&#34; push_back&#34;由于我在内部使用Data对象而导致语义动作,并且它无法懒惰地使用它(我猜,我不太确定)。
拜托,有人可以帮我吗?
注意 - 我不熟悉MS VS 2010(不完全符合c ++ 11标准),所以请不要使用可变参数模板和emplace_back解决方案。谢谢。
答案 0 :(得分:3)
你应该让这个动作成为一个懒惰的演员。您的Data
构造函数调用不是。
<强> Live On Coliru 强>
#include <string>
#include <boost/xpressive/xpressive.hpp>
#include <boost/xpressive/regex_actions.hpp>
namespace bex = boost::xpressive;
struct Data {
int integer;
double real;
std::string str;
Data(int integer, double real, std::string str) : integer(integer), real(real), str(str) { }
};
#include <iostream>
int main() {
std::vector<Data> container;
std::string const& input = "Int: 0 - Real: 18.8 - Str: ABC-1005\nInt: 0 - Real: 21.3 - Str: BCD-1006\n";
using namespace bex;
bex::sregex const parser = ("Int: " >> (s1 = _d) >> " - Real: " >> (s2 = (repeat<1,2>(_d) >> '.' >> _d)) >> " - Str: " >> (s3 = +set[alnum | '-']) >> _n)
[bex::ref(container)->*bex::push_back(bex::construct<Data>(as<int>(s1), as<double>(s2), s3))];
bex::sregex_iterator cur(input.begin(), input.end(), parser), end;
for (auto const& what : boost::make_iterator_range(cur, end)) {
std::cout << what.str() << "\n";
}
for(auto& r : container) {
std::cout << "[ " << r.integer << "; " << r.real << "; " << r.str << " ]\n";
}
}
打印
Int: 0 - Real: 18.8 - Str: ABC-1005
Int: 0 - Real: 21.3 - Str: BCD-1006
[ 0; 18.8; ABC-1005 ]
[ 0; 21.3; BCD-1006 ]
我会用精神来做这件事。 Spirit具有直接解析为基础数据类型的原语,这样不易出错且效率更高。
使用Phoenix,非常相似: Live On Coliru
使用Fusion适应,它变得更有趣,更简单:
<强> Live On Coliru 强>
现在想象一下:
你会如何在Xpressive中做到这一点?以下是你如何使用Spirit做到的。请注意,附加约束如何不会改变语法。与基于正则表达式的解析器形成对比。
<强> Live On Coliru 强>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted/struct.hpp>
namespace qi = boost::spirit::qi;
struct Data {
int integer;
double real;
std::string str;
};
BOOST_FUSION_ADAPT_STRUCT(Data, integer, real, str);
#include <iostream>
int main() {
std::vector<Data> container;
using It = std::string::const_iterator;
std::string const& input = "iNT: 0 - Real: 18.8 - Str: ABC-1005\n\nInt: 1-Real:21.3 -sTR:BCD-1006\n\n";
qi::rule<It, Data(), qi::blank_type> parser = qi::no_case[
qi::lit("int") >> ':' >> qi::auto_ >> '-'
>> "real" >> ':' >> qi::auto_ >> '-'
>> "str" >> ':' >> +(qi::alnum|qi::char_('-')) >> +qi::eol
];
It f = input.begin(), l = input.end();
if (parse(f, l, qi::skip(qi::blank)[*parser], container)) {
std::cout << "Parsed:\n";
for(auto& r : container) {
std::cout << "[ " << r.integer << "; " << r.real << "; " << r.str << " ]\n";
}
} else {
std::cout << "Parse failed\n";
}
if (f != l) {
std::cout << "Remaining input: '" << std::string(f,l) << "'\n";
}
}
仍打印
Parsed:
[ 0; 18.8; ABC-1005 ]
[ 1; 21.3; BCD-1006 ]
进一步的想法:你将如何
如果你可以使用c ++ 14,Spirit X3可以更高效,并且比Spirit Qi或Xpressive方法编译更快:
<强> Live On Coliru 强>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/adapted/struct.hpp>
struct Data {
int integer;
double real;
std::string str;
};
BOOST_FUSION_ADAPT_STRUCT(Data, integer, real, str);
namespace Parsers {
using namespace boost::spirit::x3;
static auto const data
= rule<struct Data_, ::Data> {}
= no_case[
lit("int") >> ':' >> int_ >> '-'
>> "real" >> ':' >> double_ >> '-'
>> "str" >> ':' >> +(alnum|char_('-')) >> +eol
];
static auto const datas = skip(blank)[*data];
}
#include <iostream>
int main() {
std::vector<Data> container;
std::string const& input = "iNT: 0 - Real: 18.8 - Str: ABC-1005\n\nInt: 1-Real:21.3 -sTR:BCD-1006\n\n";
auto f = input.begin(), l = input.end();
if (parse(f, l, Parsers::datas, container)) {
std::cout << "Parsed:\n";
for(auto& r : container) {
std::cout << "[ " << r.integer << "; " << r.real << "; " << r.str << " ]\n";
}
} else {
std::cout << "Parse failed\n";
}
if (f != l) {
std::cout << "Remaining input: '" << std::string(f,l) << "'\n";
}
}
打印(它变得无聊):
Parsed:
[ 0; 18.8; ABC-1005 ]
[ 1; 21.3; BCD-1006 ]