How to restart FLEX scanners?

jenglish@flightlab.com (Joe English)
3 Feb 1999 23:55:26 -0500

          From comp.compilers

Related articles
How to restart FLEX scanners? jenglish@flightlab.com (1999-02-03)
Re: How to restart FLEX scanners? gneuner@dyn.com (1999-02-12)
Re: How to restart FLEX scanners? rkrayhawk@aol.com (1999-02-16)
Re: How to restart FLEX scanners? jenglish@flightlab.com (1999-02-18)
| List of all articles for this month |

From: jenglish@flightlab.com (Joe English)
Newsgroups: comp.compilers
Date: 3 Feb 1999 23:55:26 -0500
Organization: Advanced Rotorcraft Technology, Inc.
Keywords: lex, comment

I have an interactive parser built with lex/yacc that I'm trying to
port to flex. It's giving me considerable problems.


The language has a "load file" command, which leads to the usual
re-entrancy problems. I'm solving this in the usual way: the grammar
is written to return one statement at a time, and the read-eval-print
loop looks something like this:




extern PARSE_TREE parse_tree; /* set by yyparse() %start rule */
int parse_file(FILE *fp)
{
YY_BUFFER_STATE old = YY_CURRENT_BUFFER;
YY_BUFFER_STATE new = yy_create_buffer(fp, ...);
yy_switch_to_buffer(new);
while (yyparse() != EOF_SIGNAL)
evaluate(parse_tree);
yy_delete_buffer(new);
if (old != NULL)
yy_switch_to_buffer(old);
}


yywrap() always returns 1, which causes yylex() to return EOF, which
in turn causes yyparse() to return EOF_SIGNAL, and finally
parse_file() restores the previous input buffer and returns.


Everything seems to work fine so far: if main() calls
parse_file(stdin) (which can indirectly call parse_file(), recursively
to any depth and breadth) there are no problems. But if main() calls
parse_file() a second time, the program invariably gets a segmentation
fault somewhere in the bowels of malloc() or free().


This indicates that somebody is scribbling over memory that they
shouldn't be. I suspect that yylex() is the culprit, since this code
has been in production for several years (using stock lex on several
different Unices) with no problems.


My question is: what am I doing wrong? Is there any way to get a FLEX
scanner to start over again with a fresh input buffer after the
initial one is exhausted?


Thanks in advance for any advice,


--Joe English


    jenglish@flightlab.com
[Don't do that. Neither yylex() nor yyparse() can be called recursively.
-John]


Post a followup to this message

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