通过boost::spirit::qi::symbols documentation的开头段落,我认为从语义动作向qi ::符号添加符号并不太难。不幸的是,它似乎并不像我想象的那么简单。
以下测试代码表明了问题:
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <string>
namespace qi = boost::spirit::qi;
typedef qi::symbols<char, unsigned int> constants_dictionary;
template <typename Iter> struct parser : public qi::grammar<Iter, qi::space_type> {
parser(constants_dictionary &dict) : parser::base_type(start) {
start = qi::lit("@") >> ((+qi::char_) >> qi::uint_)[dict.add(qi::_1, qi::_2)];
}
qi::rule<Iter> start;
};
int main() {
constants_dictionary dict;
parser<std::string::const_iterator> prsr(dict);
std::string test = "@foo 3";
parse(test.begin(), test.end(), prsr, qi::space);
}
从VS2010中提供与qi :: _ 2相关的类型错误:
C:\Users\k\Coding\dashCompiler\spirit_test.cpp(12) : error C2664: 'const boost::
spirit::qi::symbols<Char,T>::adder &boost::spirit::qi::symbols<Char,T>::adder::o
perator ()<boost::spirit::_1_type>(const Str &,const T &) const' : cannot conver
t parameter 2 from 'const boost::spirit::_2_type' to 'const unsigned int &'
with
[
Char=char,
T=unsigned int,
Str=boost::spirit::_1_type
]
Reason: cannot convert from 'const boost::spirit::_2_type' to 'const uns
igned int'
No user-defined-conversion operator available that can perform this conv
ersion, or the operator cannot be called
C:\Users\k\Coding\dashCompiler\spirit_test.cpp(10) : while compiling cla
ss template member function 'parser<Iter>::parser(constants_dictionary &)'
with
[
Iter=std::_String_const_iterator<char,std::char_traits<char>,std::al
locator<char>>
]
C:\Users\k\Coding\dashCompiler\spirit_test.cpp(21) : see reference to cl
ass template instantiation 'parser<Iter>' being compiled
with
[
Iter=std::_String_const_iterator<char,std::char_traits<char>,std::al
locator<char>>
]
(令人讨厌的VS2010错误风格道歉)
我应该使用什么语法来添加(以及稍后删除)此表中的符号?
答案 0 :(得分:6)
这个问题has been answered before。但是,您发布的代码存在相当多的问题,因此我将逐一修复它们,以免您不必盯着错误消息页面。
工作代码(加上输出验证)为 here on liveworkspace.org 。
注意:
语义动作必须是凤凰演员,即你需要
boost::bind
,phoenix::bind
,std::bind
phoenix::lambda<>
或phoenix::function<>
函数指针或多态可调用对象(根据文档)
我建议phoenix::bind
(在这种特殊情况下),我在下面显示
qi::char_
吃所有字符。结合船长,结果
在解析失败时,因为(显然)值中的数字也是
由+qi::char_
吃掉。我基于qi::lexeme[+qi::graph]
qi::lexeme
来“绕过”船长(即阻止+ qi :: graph切割)
跨越空白,因为船长,好吧,跳过它)qi::parse
不带船长;使用qi::phrase_parse
(
出现工作的原因是任何尾随的“可变参数”参数都是
绑定到解析器的公开属性,在本例中是
未指定,因此qi::unused_type
)。test.begin()
和test.end()
传递给
qi::phrase_parse
,你需要明确你想要const迭代器。该
更典型的解决方案是引入明确类型的变量
(first
和last
,例如。)
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <string>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
typedef qi::symbols<char, unsigned int> constants_dictionary;
template <typename Iter> struct parser : qi::grammar<Iter, qi::space_type>
{
parser(constants_dictionary &dict) : parser::base_type(start)
{
start = qi::lit("@") >> (qi::lexeme [+qi::graph] >> qi::uint_)
[ phx::bind(dict.add, qi::_1, qi::_2) ]
;
}
qi::rule<Iter, qi::space_type> start;
};
int main() {
constants_dictionary dict;
parser<std::string::const_iterator> prsr(dict);
const std::string test = "@foo 3";
if (qi::phrase_parse(test.begin(), test.end(), prsr, qi::space))
{
std::cout << "check: " << dict.at("foo") << "\n";
}
}