4

我试图在 Boost.Spirit (2.3) 中创建一个自定义 Parser 类,但没有成功。代码是:

template <class Iter>
class crule : public boost::spirit::qi::parser<crule<Iter> >
{
  rule<Iter> r_;
public:
  crule(const rule<Iter>& r) : r_(r) {}
  template <class T>
  crule(const T& t) : r_(t) {}
  template<class Ctx, class Skip>
  bool parse(Iter& f, const Iter& l, Ctx& context, Skip& skip, typename rule<Iter>::template attribute<Ctx, Iter>::type& attr) const {
    return r_.parse(f, l, context, skip, attr);
  }
  template <class Ctx>
  boost::spirit::info what(Ctx& context) const {
    return r_.what(context);
  }
  template <class Context, class It>
  struct attribute {
    typedef typename rule<Iter>::template attribute<Context, It>::type type;
  };
};

虽然我已经(至少我认为我已经)满足了所有要求,但是当我尝试在解析表达式中使用这个类时会出错:

shell_grammar.h:134: error: no match for 'operator!' in '!shell_grammar<Iter>::token(boost::spirit::qi::rule<Iter, boost::fusion::unused_type, boost::fusion::unused_type, boost::fusion::unused_type>) [with Iter = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >](boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::fusion::unused_type, boost::fusion::unused_type, boost::fusion::unused_type>(((const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::fusion::unused_type, boost::fusion::unused_type, boost::fusion::unused_type>&)((const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::fusion::unused_type, boost::fusion::unused_type, boost::fusion::unused_type>*)(&((shell_grammar<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >*)this)->shell_grammar<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::reserved_words)))))'

shell_grammar.h:134: note: candidates are: operator!(bool) <built-in>

我试图查看其他解析集的实现(例如。not_predicate),但无法弄清楚使它起作用的区别是什么。

动机

我这样做的原因与这个问题有关。我想解析具有特殊词汇规则的 POSIX shell 语言。特别是,即使在词位中也必须应用“skipper parser”,但它必须不同于“phrase level”skipper parser。这是lexeme指令不能做的,并且skip不会预先跳过(AFAIK),这也是我需要的。所以我想创建一个函数

something token(std::string);

这将返回与令牌匹配的规则。一种方法是创建我自己的rule包装器作为终端(因为rule单独不能用于其引用语义),另一种方法是创建一个新的解析器(这将是一个非终端 in proto),并在其中实现 shell 的令牌解析。

4

3 回答 3

3

这是很有可能的,但我发现它比手工编写我自己的词法分析器和递归下降解析器需要更多的工作(并且更难调试)。即使是相当小的 Spirit 语法也会让我花费数周时间与编译器搏斗。

您收到的此错误消息显示了您遇到的问题类型。任何时候你得到一个错误,它都是来自精神深处的一些模板实例化的错误,并添加了许多进一步的模板实例化层来混淆问题。为了有希望破译错误消息,您几乎必须了解整个设施的代码。

我讨厌批评,因为精神是值得努力的。我的硕士论文是关于实现面向对象的编译器生成器,所以我是这个概念的粉丝。我真的很想喜欢它,但是 Spirit 对任何人来说都太难了,除了严肃的 C++ 专家。

为了与可以做的事情进行比较,请查看 Ada OpenToken项目。Spirit 可能更灵活,但在 OpenToken 中编译错误更加敏感,浏览该页面上的版本历史可以看出,他们的很大一部分工作都用于帮助用户调试错误。

于 2010-08-17T13:10:37.507 回答
3

您提供的代码看起来不错(至少就实际解析器的接口而言)。但是为了将自定义解析器与 Spirit 集成,您需要做更多的工作。Spirit 的网站有一个自定义解析器组件的示例,在这里解释了所有必需的步骤。

在我看来,您似乎是在不必要地尝试以艰难的方式做事。但我不完全理解你想要达到的目标,所以我可能错了。如果您解释了您的用例,我相信我们可以提出一个更简单的解决方案。

于 2010-08-18T19:11:30.370 回答
1

BTW this is what I came up to:

You need to register the class as a literal in boost::proto like this:

template <class T>
struct crulexx : public boost::proto::literal<rule<T> >
{
  template <class U>
  crulexx(const U& u) : boost::proto::literal<rule<T> >(rule<T>(u)) {}
};

It works for me in this test. However, I got segfaults in other piece of code using it, which I will have to debug.

于 2010-08-20T12:32:18.553 回答