yy_scan_string()完成后,词法分析停止 [英] lexical analysis stops after yy_scan_string() is finished

查看:230
本文介绍了yy_scan_string()完成后,词法分析停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用flex制作词法分析器.我想分析一些定义为以下形式的编译器语句:#define标识符identifier_string.我保留(identifier identifier_string)对的列表.因此,当我到达文件中的标识符是#define列表时,我需要将词法分析从主文件切换到分析相应的identifier_string. (因为太大,我没有放入完整的flex代码) 这是一部分:

I use flex to make a lexical analyzer. I want to analyse some define compiler statements which are in the form: #define identifier identifier_string. I keep a list of (identifier identifier_string) pair. 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. (I don't put the complete flex code because is too big) here's the part:

{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;
                                }

对identifier_string的分析执行得很好.现在,当到达EOF时,我想切换回初始缓冲区并继续分析.但这仅完成了达到EOF的打印.

The analysis of the identifier_string executes just fine. Now when the EOF is reached I want to switch back at the initial buffer and resume the analysis. But it finishes just printing EOF reached.

推荐答案

尽管该方法似乎合乎逻辑,但由于yy_scan_string 替换了当前缓冲区,并且在调用之前发生,因此该方法不起作用到yypush_buffer_state.因此,原始缓冲区会丢失,并且在调用yypop_buffer_state时,恢复的缓冲区状态是(现在已终止)字符串缓冲区.

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. Consequently, the original buffer is lost, and when yypop_buffer_state is called, the restored buffer state is the (now terminated) string buffer.

因此,您需要一点技巧:首先,将当前缓冲区状态复制到堆栈上,然后切换到新的字符串缓冲区:

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);

这篇关于yy_scan_string()完成后,词法分析停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆