精神语法不会编译:函数模板参数错误?

Spirit grammar won't compile: function template argument error?

本文关键字:函数模板 参数 错误 编译 语法      更新时间:2023-10-16

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

我对语法和开始规则进行了多种属性定义,但对错误没有影响。

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

上述精神文件中的第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规则中没有船长。出于内部原因,精神只能在呼叫规则时失去船长。可以通过在使用站点中内置underdotstar来轻松解决该问题。

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

提示:您可以使用raw指令,并在其中使用文字解析器,以使更多的性能和清洁器看起来更加清洁以看起来像解析器。考虑一下: qi::lexeme[qi::raw[(qi::alpha | '_') >> qi::alnum % '.'] >> ".*"]