简体   繁体   English

yy_scan_string()完成后,词法分析停止

[英]lexical analysis stops after yy_scan_string() is finished

I use flex to make a lexical analyzer. 我使用flex制作词法分析器。 I want to analyse some define compiler statements which are in the form: #define identifier identifier_string. 我想分析一些定义为以下形式的编译器语句:#define标识符identifier_string。 I keep a list of (identifier identifier_string) pair. 我保留(identifier identifier_string)对的列表。 So when I reach in the file a identifier that is #define list I need to switch the lexical analysis from main file to analyse the corresponding identifier_string. 因此,当我到达文件中的标识符是#define列表时,我需要将词法分析从主文件切换到分析相应的identifier_string。 (I don't put the complete flex code because is too big) here's the part: (我没有输入完整的flex代码,因为太大了),这是一部分:

{IDENTIFIER}                    {   // search if the identifier is in list
                                    if( p = get_identifier_string(yytext) )
                                    {
                                        puts("DEFINE MATCHED");
                                        yypush_buffer_state(yy_scan_string(p));
                                    }
                                    else//if not in list just print the identifier
                                    {
                                        printf("IDENTIFIER %s\n",yytext);
                                    }
                                }
<<EOF>>                         {
                                    puts("EOF reached");
                                    yypop_buffer_state();
                                    if ( !YY_CURRENT_BUFFER )
                                    {
                                        yyterminate();
                                    }
                                    loop_detection = 0;
                                }

The analysis of the identifier_string executes just fine. 对identifier_string的分析执行得很好。 Now when the EOF is reached I want to switch back at the initial buffer and resume the analysis. 现在,当到达EOF时,我想切换回初始缓冲区并继续分析。 But it finishes just printing EOF reached. 但是仅完成达到EOF的打印即可。

Although that approach seems logical, it won't work because yy_scan_string replaces the current buffer, and that happens before the call to yypush_buffer_state . 尽管这种方法似乎合乎逻辑,但由于yy_scan_string 替换了当前缓冲区,并且在调用yypush_buffer_state之前发生,因此它yypush_buffer_state Consequently, the original buffer is lost, and when yypop_buffer_state is called, the restored buffer state is the (now terminated) string buffer. 因此,原始缓冲区会丢失,并且当yypop_buffer_state时,恢复的缓冲区状态是(现在已终止)字符串缓冲区。

So you need a little hack: first, duplicate the current buffer state onto the stack, and then switch to the new string buffer: 因此,您需要一点技巧:首先,将当前缓冲区状态复制到堆栈上,然后切换到新的字符串缓冲区:

/* Was: yypush_buffer_state(yy_scan_string(p)); */
yypush_buffer_state(YY_CURRENT_BUFFER);
yy_scan_string(p);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM