我正在使用一种语法,它使用越边解析(iow:制表符作为 block 定界符)。该语法使用缩进堆栈来跟踪嵌套 block ,并在遇到 EOF 时尝试使用适当的结束标记包装 block 。
std::stack<int> indent_stack;
int indent_size;
%x indent
%s normal
%s wrap
%%
<wrap>[ ] {
if(indent_stack.top() > 0)
{
indent_stack.pop();
if(indent_stack.top() > 0) unput(' ');
return DEDENT;
}
else
yyterminate();
}
<<EOF>> {
if(indent_stack.top() > 0)
{
BEGIN(wrap);
unput(' ');
}
else
yyterminate();
}
<indent>[\t] {indent_size++;}
<indent>[\n] {indent_size = 0;}
<indent>. {
unput(*yytext);
if(indent_size > indent_stack.top())
{
indent_stack.push(indent_size);
yytext[0] = '\0';
return INDENT;
}
else if(indent_size < indent_stack.top())
{
indent_stack.pop();
yytext[0] = '\0';
return DEDENT;
}
else
{
BEGIN(normal);
}
}
/* And so begin <normal> rules. */
乍一看,这个语法似乎在对输入文件进行词法分析时起作用:yyin = fopen(...)
。
然而,当我尝试对输入字符串进行词法分析时:state = yy_scan_string(...)
,对 yylex
的第一次调用因错误 flex 而崩溃扫描仪推回溢出
。
最佳答案
没有代码很难说,但我的直觉指向
<<EOF>>
规则:
什么时候
(indent_stack.top() > 0)
你是unput
- 无限循环:EOF
保持始终为真,并且 BEGIN(wrap)(wrap 是一个包含开始条件,没有 <<EOF>>
)似乎在该上下文中什么都不做。
在<<EOF>>
中很容易有无限循环当我们有规则
没有 yyterminate、yyaccept、return 或类似条款的分支。
关于c++ - Flex 语法产生错误 : scanner push-back overflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16700462/