Re: Does Flex support file nesting (C/C++ like include files)? (RKRayhawk)
21 Aug 2000 00:05:11 -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 |

From: (RKRayhawk)
Newsgroups: comp.compilers
Date: 21 Aug 2000 00:05:11 -0400
Organization: AOL
References: 00-08-048
Keywords: lex

You may be dealing with a C coding error, not a flex issue.

You have a line ...

FILE* IncludeFile = fopen(filename, "r");

that is tricking you. (The previous post is quoted below in toto for those
joining late).

The subtlety here is that you are actually declaring the file pointer
inside of the curly brackets of an else clause. When the else clause
goes out of scope, your file pointer goes out of scope.

Flex is not recursive; it is actually simply linear, you need to do a
slightly better job of pushing and poping your control fields, focus
especially on the original file pointer and its related control
fields. Your symptoms indicate that you are poping only enough to be
able to process more INCLUDE statements, and nothing more.

When you succeed by puting all of your includes at the end, you have
disproven your assertion that flex can not see the EOF of the first

It does, but you do not have enough pop logic in the <<EOF>> action.

You are also doing a similar kind of thing with

YY_BUFFER_STATE NewState = InputDeck__create_buffer
(IncludeFile, YY_BUF_SIZE);

These things are working at all only because you do not have enough
other code to trounce the transient stack area that the else clause is
using. But you are, in effect relying on data items that really
nolonger exist as you loop.

You may wish to get the declares out of those curly brackets and up to
the top of the basic function (main() or whatever your looping
within). This is a case where C is too flexible for your purposes. Get
basic, layout your data above all these nefarious if/else
valleys. Study it there and you will see you need to do some more
savings and unsavings.

There is a stack feature to the language that is partly clear to you
and partly tricky, just get the declares out of the subjunctive
clauses and it will look more obvious to you.

It is interesting that some Cs will not allow those kind of
declares. Some Cs force all of your declares up at the top of a
function. In those compilers any declare after your first executable
is an error.

But your situation is just a step even further. As I understand C,
considering a curly bracket anywhere inside the curly brackets of your
function, the declares within the inner curly brackets go out of scope
on the close curly brackets.

This is true even of subjunctive clauses (if/else) and case statements

Consider this

int your_func()
int lasts_longer;
    { int you;
    } // you is dead
// anything here can really trounce you,
// even if you copied pointers to it
// lasts_longer is still in scope

} // all fall down

Hope that Helps

Bob Rayhawk

Original post:
Date: 10 Aug 2000 00:04:37 -0400

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.

Robert Rayhawk

Post a followup to this message

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