c++ - 精神文法无法编译 : function template argument error?

标签 c++ parsing compiler-errors boost-spirit

编译这个简单的 Spirit 语法会导致似乎是一个错误(尽管错误消息很大),可能与我的 skipper 或我错误使用的其他模板参数有关。

我尝试了语法和开始规则的各种属性定义,但对错误没有影响。

#include <string>
#include "/usr/local/boost/include/boost/spirit/include/qi.hpp"
#include "/usr/local/boost/include/boost/spirit/include/qi_symbols.hpp"

using std::string;

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

boost::spirit::qi::rule<string::const_iterator>
    skipper_rule = qi::space | '#' >> *(qi::char_ - qi::eol) >> qi::eol;

typedef BOOST_TYPEOF(skipper_rule) skipper_T;

template <typename Iterator, typename Skipper>
    class Grammar1 : boost::spirit::qi::grammar<Iterator, Skipper>
{
    public:

    typedef boost::spirit::qi::rule<Iterator, Skipper>  rule_nil_T;
    typedef boost::spirit::qi::rule<Iterator, string()> rule_str_T;

    rule_nil_T under =  qi::char_('_');
    rule_nil_T dot   =  qi::char_('.');
    rule_nil_T star  =  qi::char_('*');

    rule_str_T file_name_star = qi::lexeme [ (qi::alpha | under) >> *(qi::alnum | under) >> dot >> star ];

    rule_nil_T start = +file_name_star;

    Grammar1(void) : Grammar1::base_type(start)  { };
    ~Grammar1(void) { };

    void parseInputFile(Iterator itr, Iterator itr_end)
    {
        bool b;
        bool r = phrase_parse(itr, itr_end, start, skipper_rule);
        if (r && itr == itr_end)
        {
            std::cout << "Parsing succeeded\n";
        } else
        {
            string rest(itr, itr_end);
            std::cout << "stopped at: \": " << rest << "\"\n";
       }
    }
};


int main(int argc, char **argv)
{
    Grammar1<string::const_iterator, skipper_T> g;
    string input("input1.* input2.*");

    g.parseInputFile(input.cbegin(), input.cend());

 }

错误消息的重要部分似乎是:

/usr/local/boost/include/boost/spirit/home/qi/nonterminal/rule.hpp:304:17:
error: no match for call to ‘(const function_type *REMOVED*)’
                 if (f(first, last, context, skipper))
                 ^

上述 Spirit 文件第 304 行的注释指出我 可能使用了不兼容的船长,但我尝试了更简单的 ascii::space_type 船长但无济于事。

错误信息的其余部分:

usr/local/boost/include/boost/function/function_template.hpp:1048:7: note: 
candidate is:
 class function<BOOST_FUNCTION_PARTIAL_SPEC>
       ^
/usr/local/boost/include/boost/function/function_template.hpp:758:17: note:
 boost::function4<R, T1, T2, T3, T4>:: *REMOVED*
     result_type operator()(BOOST_FUNCTION_PARMS) const
                 ^
/usr/local/boost/include/boost/function/function_template.hpp:758:17: note:
   no known conversion for argument 4 from ‘const 
boost::spirit::unused_type’ to ‘const boost::spirit::qi::reference<const 
boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*,
 std::basic_string<char> > > >&’

知道我做错了什么吗?我的编译器是 gcc 4.8.5。

最佳答案

您正在尝试使用 rule_nil_T 规则,该规则在 rule_str_T 规则中有船长,而实际上没有。由于内部原因,Spirit 只能在调用规则时失去船长。在使用处内联underdotstar即可轻松解决问题。

注意:Qi 中的规则调用有开销。为这样一个简单的解析器创建规则,然后在重复解析器中使用它并不是一个好主意。

提示:您可以利用 raw 指令并在其中使用文字解析器以获得更高的性能和更清晰的解析器外观。考虑一下:qi::lexeme[qi::raw[(qi::alpha | '_') >> qi::alnum % '.'] >> ".*"].

关于c++ - 精神文法无法编译 : function template argument error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55308433/

相关文章:

c++ - 使用 Boost::Spirit 解析具有未知键的 'key = value' 列表

python - 错误发生时如何自动启动交互式 session ?

c - 我的 gcc 编译器给了我一个错误,显示为(函数)的类型冲突

c++ - OpenCV 中的重复率

c++ - 调用遗留 C API 时,如何在现代 C++ 中正确地将指向结构的指针转换为指向其他结构的指针?

c++ - 为什么 Microsoft 有 IHTMLDocument、IHTMLDocument2、...、IHTMLDocument8?

java - Android - JSON 是在不变的 SQLite 数据库中存储大小变化的数据的好选择吗?

c++ - Raspberry Pi 2 某些 GPIO 引脚不工作

javascript - 如何获取 JavaScript *代码文件*(输入中的字符串形式的代码)的 JSON(或等效)表示形式?

visual-c++ - C2491 : 'std::numpunct<_Elem>::id' : definition of dllimport static data member not allowed