Does Flex support file nesting (C/C++ like include files)?

lerhlerh@yahoo.com
10 Aug 2000 00:04:37 -0400

          From comp.compilers

Related articles
Does Flex support file nesting (C/C++ like include files)? lerhlerh@yahoo.com (2000-08-10)
Re: Does Flex support file nesting (C/C++ like include files)? kth@srv.net (Kevin Handy) (2000-08-13)
Re: Does Flex support file nesting (C/C++ like include files)? rkrayhawk@aol.com (2000-08-21)
| List of all articles for this month |

From: lerhlerh@yahoo.com
Newsgroups: comp.compilers
Date: 10 Aug 2000 00:04:37 -0400
Organization: Deja.com - 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:


statement1
statement2
#include "file1"
statement3
#include "file2"
#include "file4"
statement4
:
:
statementN


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]*\" {
                                        :
:
FileNameStack.push(filename);
                                if( include_stack_ptr >= MAX_INCLUDE_DEPTH ){
// Report error.
--include_stack_ptr;
yyterminate();
} else {
FILE* IncludeFile = fopen(filename, "r");
if( !IncludeFile ) {
// Report error.
} else {
LineNoStack[include_stack_ptr] = LineNo;
LineNo = 1 ;
yyrestart(IncludeFile);
YY_BUFFER_STATE NewState = InputDeck__create_buffer
(IncludeFile, YY_BUF_SIZE);
include_stack[include_stack_ptr++] =
yy_current_buffer;
InputDeck__switch_to_buffer( NewState );
}
}


<<EOF>> {
if( --include_stack_ptr < 0)
yyterminate();
}


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!!




Peter.
[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.
-John]


Post a followup to this message

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