c++ - Flex 语法产生错误 : scanner push-back overflow

标签 c++ c lex flex-lexer

我正在使用一种语法,它使用越边解析(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/

相关文章:

c++ - getline() 返回错误

c - gcc 不会矢量化简单循环

C:为什么这段代码会打印这个?

c++ - 在 lex 文件中声明 hash_map 时出错

Python PLY Lex 歧义

c++ - C++/CUDA 项目的 Cmake 文件

c++ - 如何在 C++ 中创建带有引用成员的数组?

c++ - 使用模板进行隐式类型转换

C 字符串静态缓冲区字节大小

ubuntu - 在 lex 上定义表达式不适用于某些字母