我尝试一个小的测试用例,它接受一个范围,例如[5-3],然后将3、4、5推入向量。我只能考虑使用语义动作方法。但是,我使用phoenix :: push_back进行编码的方式似乎不起作用。如果我只是按一个数字(如测试3中的“ 3”)或一个占位符(测试2)。精神将奏效。但是如果我使用for循环来推送。然后大小将为0。基本上没有任何东西。
#include <fstream>
#include <iostream>
#include <vector>
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
namespace px = boost::phoenix;
struct varVec
{
std::vector<unsigned int> uintVec;
};
BOOST_FUSION_ADAPT_STRUCT(varVec,
(std::vector<unsigned int>, uintVec))
template<typename Iterator, typename Skipper>
struct listParser : public qi::grammar<Iterator,
varVec(),
Skipper>
{
listParser() : listParser::base_type(varVecParse)
{
using namespace qi;
varPair =
uint_ [_a = _1]
> '-'
> uint_
[
// test 1
px::bind([](uint lb, uint ub) {
if (ub < lb) {
uint temp = ub; ub = lb; lb = temp; }
for (unsigned int i = lb; i <= ub; i++)
{
px::push_back(qi::_val, i);
std::cout << "i = " << i << std::endl;
}
},
// parameters
qi::_a, qi::_1)
// test 2
// px::push_back(_val, _1)
// test 3
// px::push_back(_val, 3)
]
;
varVecParse = '['
>> varPair
>> ']'
;
}
qi::rule<Iterator, std::vector<unsigned int>(), qi::locals<unsigned int>,
Skipper> varPair;
qi::rule<Iterator, varVec(), Skipper> varVecParse;
};
int main()
{
std::string input ("[ 6- 4]\n");
std::string::const_iterator begin = input.begin();
std::string::const_iterator end = input.end();
listParser<std::string::const_iterator, qi::space_type> parser;
varVec result;
bool success = qi::phrase_parse(begin, end, parser, qi::space, result);
unsigned int size = result.uintVec.size();
std::cout << "size = " << size << std::endl;
if (size > 0)
std::cout << "val = " << result.uintVec[0] << std::endl;
return 0;
}
所以功能是使范围升序并将其推入unsigned int向量。我猜想语义动作中的函数有问题,但不确定是什么问题。
答案 0 :(得分:1)
您需要记住,即使Boost.Phoenix表达式看起来像是“正常”表达式,它们的行为实际上也像lambda,但是它们需要使用它们需要执行的任何参数来调用。视为简化的example:
std::vector<int> val{};
using px::placeholders::arg1;
px::push_back(px::ref(val),1);
std::cout << val.size() << "\n"; //0
px::push_back(px::ref(val),1)();
std::cout << val.size() << "\n"; //1
px::push_back(arg1,1);
std::cout << val.size() << "\n"; //1
px::push_back(arg1,1)(val);
std::cout << val.size() << "\n"; //2
您的情况类似于arg1
示例(但qi::_val
稍微复杂一点)。当在解析器语义动作中使用Phoenix表达式时,Spirit会使用它们所需的参数对其进行“自动”调用,在您的示例中,您没有使用它们,并且从未将uint
插入向量中。
您不应该将Phoenix代码与“普通”代码混合使用。因此,您需要摆脱px::push_back
(请参见Wandbox):
#include <fstream>
#include <iostream>
#include <vector>
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
namespace px = boost::phoenix;
struct varVec
{
std::vector<unsigned int> uintVec;
};
BOOST_FUSION_ADAPT_STRUCT(varVec,
(std::vector<unsigned int>, uintVec))
template<typename Iterator, typename Skipper>
struct listParser : public qi::grammar<Iterator,
varVec(),
Skipper>
{
listParser() : listParser::base_type(varVecParse)
{
using namespace qi;
varPair =
uint_ [_a = _1]
> '-'
> uint_
[ // ADDED
// test 1 vvvvvvvvvvvvvvvvvvvvvv
px::bind([](std::vector<uint>& val, uint lb, uint ub) {
if (ub < lb) {
uint temp = ub; ub = lb; lb = temp; }
for (unsigned int i = lb; i <= ub; i++)
{
val.push_back(i); //<---CHANGED
std::cout << "i = " << i << std::endl;
}
},
// parameters
qi::_val, qi::_a, qi::_1)
//^^^^^^^^^
// ADDED
]
;
varVecParse = '['
>> varPair
>> ']'
;
}
qi::rule<Iterator, std::vector<unsigned int>(), qi::locals<unsigned int>,
Skipper> varPair;
qi::rule<Iterator, varVec(), Skipper> varVecParse;
};
int main()
{
std::string input ("[ 6- 4]\n");
std::string::const_iterator begin = input.begin();
std::string::const_iterator end = input.end();
listParser<std::string::const_iterator, qi::space_type> parser;
varVec result;
bool success = qi::phrase_parse(begin, end, parser, qi::space, result);
unsigned int size = result.uintVec.size();
std::cout << "size = " << size << std::endl;
if (size > 0)
std::cout << "val = " << result.uintVec[0] << std::endl;
return 0;
}
另一种可能性(不推荐)是一路使用Boost.Phoenix(请参见Wandbox):
...
qi::_1_type const upper;
qi::_a_type const lower;
px::local_names::_i_type const cont;
varPair =
uint_ [lower = _1]
> '-'
> uint_
[
px::if_(upper<lower)[px::swap(lower,upper)],
px::let(cont=lower)
[
px::for_(px::nothing,cont<=upper,++cont)
[
px::push_back(qi::_val,cont),
px::ref(std::cout) << "i = " << cont << std::endl
]
]
]
;
...
PS:另一种可能性可能是在解析后的一个步骤中处理向量的生成。这样,在解析过程中,您只需存储范围的边界,然后轻松生成实际需要的向量即可。