为什么qi::skip处理来自词法分析器的令牌时会失败?
Why does qi::skip fail with tokens from the lexer?
我使用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
, bar
和baz
在上面仍然可以是空白敏感的。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
时,它要么忽略它,要么将其解释为规则返回值的一部分或类似的东西,不确定它到底会做什么。
- 如何在C++中使用X509证书模在令牌中查找私钥
- holeMenuProgram.cpp:38:1 错误:'}'令牌之前的预期主表达式
- C++:"("令牌"之前有预期的非限定 id 指向类中成员函数的指针
- Arduino 用于语句错误。令牌之前的预期')' ';'。如何解决这个问题?
- 错误:令牌 { '{' 之前应存在非限定 ID
- 允许哪些令牌作为 #include 的参数?
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- 它在 { 令牌之前给了我预期的标识符
- (错误:令牌之前'<<'预期的主表达式)
- 为什么 ## aka 令牌粘贴运算符不适用于 C 和 C++ 中的注释?
- 错误:"->"令牌之前的预期初始值设定项
- LINUX 操作系统上的错误:令牌之前预期的构造函数、析构函数或类型转换'('?
- 丢失读入的每一行输入中的最后一个令牌
- 如何使用 libCurl 将访问令牌发送到服务器 API
- C++令牌定义成员
- 宏定义中的预处理器令牌两边有两个双引号
- 错误:C++中']'令牌之前的预期主表达式
- 调试断言失败文件、令牌扫描程序和文本文件
- 为什么qi::skip处理来自词法分析器的令牌时会失败?
- 行中的令牌不起作用失败 C++