Does Flex support file nesting (C/C++ like include files)?
10 Aug 2000 00:04:37 -0400

          From comp.compilers

Related articles
Does Flex support file nesting (C/C++ like include files)? (2000-08-10)
Re: Does Flex support file nesting (C/C++ like include files)? (Kevin Handy) (2000-08-13)
Re: Does Flex support file nesting (C/C++ like include files)? (2000-08-21)
| List of all articles for this month |

Newsgroups: comp.compilers
Date: 10 Aug 2000 00:04:37 -0400
Organization: - Before you buy.
Keywords: lex, question, comment

I am trying to do a parsar, using bison and flex, to read a file of the
following format:

#include "file1"
#include "file2"
#include "file4"

The problems I am having are:

1. Flex doesn't detect EOF of the first include file. Hence yywrap is
not called. Flex simply returns another token not in the input, causing
paring error.

2. Flex doesnt seem to clear the internal state after switching to
another buffer. I've tried to reset the state by doing yyrestart(yyin)
(and even yy_flush_buffer()) as follows:

INCLUDE[ \t]+\"[^\"\n]*\" {
                                if( include_stack_ptr >= MAX_INCLUDE_DEPTH ){
// Report error.
} else {
FILE* IncludeFile = fopen(filename, "r");
if( !IncludeFile ) {
// Report error.
} else {
LineNoStack[include_stack_ptr] = LineNo;
LineNo = 1 ;
YY_BUFFER_STATE NewState = InputDeck__create_buffer
(IncludeFile, YY_BUF_SIZE);
include_stack[include_stack_ptr++] =
InputDeck__switch_to_buffer( NewState );

<<EOF>> {
if( --include_stack_ptr < 0)

If I move all the include statements to the end of the input file, Flex
does work perfectly. So File chaining works as expected.

What have I done wrong?

Could anyone confirm Flex does support file nesting?

Your help is much appreciated! Many thanks!!

[Yes, flex does support file nesting. If you're not getting the EOF
correctly, you're probably smashing the buffer or pointers to it somehow.

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.