在语义动作中添加到气符表中

Add to a spirit qi symbol table in a semantic action

本文关键字:添加 语义      更新时间:2023-10-16

根据boost::spirit::qi::symbols文档的开头一段,我认为从语义操作向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:UserskCodingdashCompilerspirit_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:UserskCodingdashCompilerspirit_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:UserskCodingdashCompilerspirit_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错误风格道歉)

我应该使用什么语法来从此表中添加(以及稍后删除)符号?

这个问题以前已经回答过了。然而,你发布的代码有很多问题,所以我会逐一修复,以免你不必要地盯着错误消息页面看。

工作代码(加上输出验证)位于liveworkspace.org上。

注:

  1. 语义操作必须是Phoenix参与者,即您需要

    • boost::bindphoenix::bindstd::bind
    • phoenix::lambda<>phoenix::function<>
    • 函数指针或多态可调用对象(根据文档)

      我推荐phoenix::bind(在这种特殊情况下),它显示在下面

  2. 解析程序的队长和启动规则不匹配
  3. qi::char_吃掉所有字符。再加上船长在解析失败时,因为(显然)值中的数字也是被CCD_ 8吃掉。我向您展示了基于qi::lexeme[+qi::graph]的众多解决方案之一
  4. 使用qi::lexeme"绕过"队长(即防止+qi::graph进行切割跨越空白,因为队长跳过了它)
  5. qi::parse不需要船长;使用qi::phrase_parse出现工作的原因是任何尾随的"variadic"参数绑定到解析器的公开属性,在本例中为因此为qi::unused_type
  6. 如果要将test.begin()test.end()直接传递到qi::phrase_parse,您需要明确您想要const迭代器。这个更典型的解决方案是引入显式类型的变量(例如firstlast


#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";
    }
}