为什么qi::skip处理来自词法分析器的令牌时会失败?

Why does qi::skip fail with tokens from the lexer?

本文关键字:令牌 失败 skip qi 处理 为什么 词法分析器      更新时间:2023-10-16

我使用boost::spirit lex和qi来解析一些源代码。

我已经使用词法分析器跳过了输入字符串中的空白。我想做的是根据解析器中的上下文切换跳过注释。

这是一个基本的演示。我的问题见Grammar::Grammar()中的注释:

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/lex_lexertl.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <iostream>
namespace lex = boost::spirit::lex;
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
typedef lex::lexertl::token<char const*, boost::mpl::vector<std::string>, boost::mpl::false_ > token_type;
typedef lex::lexertl::actor_lexer<token_type> lexer_type;
struct TokenId
{
   enum type
   {
      INVALID_TOKEN_ID = lex::min_token_id,
      COMMENT
   };
};
struct Lexer : lex::lexer<lexer_type>
{
public:
   lex::token_def<std::string> comment;
   lex::token_def<std::string> identifier;
   lex::token_def<std::string> lineFeed;
   lex::token_def<std::string> space;
   Lexer()
   {
      comment = "\/\*.*?\*\/|\/\/[^\r\n]*";
      identifier = "[A-Za-z_][A-Za-z0-9_]*";
      space = "[\x20\t\f\v]+";
      lineFeed = "(\r\n)|\r|\n";
      this->self = space[lex::_pass = lex::pass_flags::pass_ignore];
      this->self += lineFeed[lex::_pass = lex::pass_flags::pass_ignore];
      this->self.add
         (comment, TokenId::COMMENT)
         (identifier)
         (';')
         ;
   }
};
typedef Lexer::iterator_type Iterator;
void traceComment(const std::string& content)
{
   std::cout << "  comment: " << content << std::endl;
}
class Grammar : public qi::grammar<Iterator>
{
   typedef token_type skipped_t;
   qi::rule<Iterator, qi::unused_type, qi::unused_type> m_start;
   qi::rule<Iterator, qi::unused_type, qi::unused_type, skipped_t> m_variable;
   qi::rule<Iterator, std::string(), qi::unused_type> m_comment;
public:
   Lexer lx;
public:
   Grammar() :
      Grammar::base_type(m_start)
   {
// This does not work (comments are not skipped in m_variable)
      m_start = *(
            m_comment[phx::bind(&traceComment, qi::_1)]
         |  qi::skip(qi::token(TokenId::COMMENT))[m_variable]
         );
      m_variable = lx.identifier >> lx.identifier >> ';';
      m_comment = qi::token(TokenId::COMMENT);
/** But this works:
      m_start = *(
         m_comment[phx::bind(&traceComment, qi::_1)]
         | m_variable
         );
      m_variable = qi::skip(qi::token(TokenId::COMMENT))[lx.identifier >> lx.identifier >> ';'];
      m_comment = qi::token(TokenId::COMMENT);
*/
   }
};
void test(const char* code)
{
   std::cout << code << std::endl;
   Grammar parser;
   const char* begin = code;
   const char* end = code + strlen(code);
   tokenize_and_parse(begin, end, parser.lx, parser);
   if (begin == end)
      std::cout << "-- OK --" << std::endl;
   else
      std::cout << "-- FAILED --" << std::endl;
   std::cout << std::endl;
}
int main(int argc, char* argv[])
{
   test("/* kept */ int foo;");
   test("int /* ignored */ foo;");
   test("int foo /* ignored */;");
   test("int foo; // kept");
}

输出为:

/* kept */ int foo;
  comment: /* kept */
-- OK --
int /* ignored */ foo;
-- FAILED --
int foo /* ignored */;
-- FAILED --
int foo; // kept
  comment: // kept
-- OK --

skipped_t有什么问题吗?

根据我的经验,你所描述的行为是我所期望的。

my_rule = qi::skip(ws) [ foo >> lit(',') >> bar >> lit('=') >> baz ];

这和写

是一样的
my_rule = *ws >> foo >> *ws >> lit(',') >> *ws >> bar >> *ws >> lit('=') >> *ws >> baz;

(假设ws是没有属性的规则)。如果它在语法中有一个属性,那么该属性将被忽略,就像使用qi::omit一样。)

值得注意的是,船长不会在foo规则内繁殖。因此,foo, barbaz在上面仍然可以是空白敏感的。skip指令所做的是导致语法不关心该规则中的前导空格,或者该规则中',''='周围的空白。

更多信息在这里:http://boost-spirit.com/home/2010/02/24/parsing-skippers-and-skipping-parsers/


编辑:

另外,我不认为skipped_t在做你认为它在那里的事情。

当您使用自定义跳过器时,最直接的方法是指定解析器的实际实例作为该规则的跳过解析器。当你使用类型而不是对象时,例如qi::skip(qi::blank_type),这是一种速记,其中标记类型qi::blank_type已经通过先前的模板声明链接到类型qi::blank,并且qi知道当它在某些地方看到qi::blank_type时,它应该实例化qi::blank解析器对象。

我没有看到任何证据表明你已经设置了那个机器,你只是将skipped_t类型定义为token_type。你应该做什么,如果你想这样工作(如果它甚至是可能的,我不知道)是阅读关于qi定制点,而不是声明qi::skipped_t作为一个空结构体,它通过一些模板锅炉板链接到规则m_comment,这大概是你实际上想要跳过。(如果你跳过所有类型的所有标记,那么你不可能匹配任何东西,这样就没有意义了,所以我不确定你的意图是让token_type成为船长。)

我的猜测是,当qi在参数列表中看到typedef token_type时,它要么忽略它,要么将其解释为规则返回值的一部分或类似的东西,不确定它到底会做什么。