i use flex make lexical analyzer. want analyse define compiler statements in form: #define identifier identifier_string. keep list of (identifier identifier_string) pair. when reach in file identifier #define list need switch lexical analysis main file analyse corresponding identifier_string. (i don't put complete flex code because big) here's part:
{identifier} { // search if identifier in list if( p = get_identifier_string(yytext) ) { puts("define matched"); yypush_buffer_state(yy_scan_string(p)); } else//if not in list print identifier { printf("identifier %s\n",yytext); } } <<eof>> { puts("eof reached"); yypop_buffer_state(); if ( !yy_current_buffer ) { yyterminate(); } loop_detection = 0; }
the analysis of identifier_string executes fine. when eof reached want switch @ initial buffer , resume analysis. finishes printing eof reached.
although approach seems logical, won't work because yy_scan_string
replaces current buffer, , happens before call yypush_buffer_state
. consequently, original buffer lost, , when yypop_buffer_state
called, restored buffer state (now terminated) string buffer.
so need little hack: first, duplicate current buffer state onto stack, , switch new string buffer:
/* was: yypush_buffer_state(yy_scan_string(p)); */ yypush_buffer_state(yy_current_buffer); yy_scan_string(p);
Comments
Post a Comment