用flex与bison配合实现类似c编译器中的include功能遇到难题?解决方法

用flex与bison配合实现类似c编译器中的include功能遇到难题?
如题。
大虾们,我在用flex与bison配合实现一个小型编译器的时候,需要类似于c中的#include的功能,我是应该用多编扫描呢?还是可以用一遍扫描就能完成呢?
如果用一编扫描那我需要注意哪些问题呢?


------解决方案--------------------
递归include
------解决方案--------------------
flex的例子里有 ...
------解决方案--------------------
根据flex中的例子
在yywrap函数的开头:

if(include_stack_ptr> 0)
return 1;
------解决方案--------------------
给你一个我写的例子吧,
include我在lex中处理的, 基本上就不用在bision中操作了, 出了一个文件结尾的时候include_stack_ptr> 0情况下不能return 0 外


#INCLUDE BEGIN(incl);
<incl> [ \t]* {/* eat the whitespace*/ }
<incl> [^ \t\n]+ { // got the include file name
if (TOK_PRINT) printf( "[SYSTEM]: > > > Went into %s\n ",yytext);
if (TOK_PRINT) printf( "[SYSTEM]: > > > Include Stack level:%d\n ",include_stack_ptr);

if ( include_stack_ptr > = MAX_INCLUDE_DEPTH )
{
fprintf(stderr, "### Fatal Error! Includes nested too deeply!\n Maybe you got the file include itself and loop forever!\n> > Exit...\n ");
exit(-1);
}
else
{
include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;

yyin = fopen( yytext, "r " );

if ( ! yyin )
{
fprintf(stderr, "### Fatal error! ### \nCan not open included file \ "%s\ "! \n> > Exit...\n ", yytext);
exit(-1);
}
else
{
if (TOK_PRINT) printf( " > > > open file %s!\n ", yytext);

}

yy_switch_to_buffer(yy_create_buffer( yyin, YY_BUF_SIZE ) );

BEGIN(INITIAL);
}
}
< <EOF> > {// end of file
if (include_stack_ptr )
{
srcLineNum--;
if(pre_process) popFileName();// pop filename, for tracking src file line stuff

if (TOK_PRINT) printf( "[SYSTEM]: < < < EOF of Include file, Current Level:%d\n " ,include_stack_ptr);
if ( --include_stack_ptr < 0 )
{
yyterminate();
}

else
{
yy_delete_buffer( YY_CURRENT_BUFFER );
yy_switch_to_buffer(include_stack[include_stack_ptr] );
if (TOK_PRINT) printf( "[SYSTEM]: < < < Out Include file\n ");
}
}
else
yyterminate();

}

. { yylval.val = yytext[0]; return T_BADTOKEN; }