[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: a question about yyrestart & reusing a scanner
From: |
Aaron Jackson |
Subject: |
Re: a question about yyrestart & reusing a scanner |
Date: |
Thu, 21 Apr 2005 16:49:04 -0400 |
On Apr 21, 2005, at 8:29 AM, Cheng Po-wen wrote:
Hi,
I have a a question about how to reuse a scanner,
The document (which is flex.pdf -- Flex, version 2.5.31) says
there is no need to destroy a scanner if you plan to reuse it. A flex
scanner (both reentrant
and non-reentrant) may be restarted by calling yyrestart
my question is :
1. Is it safe, for a long lived applicaion to reuse a scanner all the
time by calling
yyrestart(..) ?
will there be any dangling resources, which are not released or freed
by
doing so???
Looking at the man page:
Essentially there is no difference
between just assigning yyin to a new input file or using
yyrestart() to
do so; the latter is available for compatibility with previous
versions
of flex, and because it can be used to switch input files in the
middle
of scanning. It can also be used to throw away the
current input
buffer, by calling it with an argument of yyin; but better is
to use
YY_FLUSH_BUFFER (see above). Note that yyrestart() does not
reset the
start condition to INITIAL (see Start Conditions, below).
I read that to say yyrestart just points yyin to point to the next
input file, and flushes the current input buffer. Other than the
current input buffer, I guess it doesn't free any resources... I also
read that to say yyrestart is not the preferred way to do this.
2. Does yy_scan_buffer (or yy_scan_string) internally do the "yyrestart
things" ?
what I mean is my scanner scans "strings", instead of "stdin".
so, if i want to reuse a scanner,
do I have to call yyrestart(NULL , my_scanner), and then call
yy_scan_buffer(.....) ???
or I can just call yy_scan_buffer(.....), and it internally does the
"yyrestart things" ?
I have had success doing the following in a loop (while also using
yacc/bison):
struct yy_buffer_state* bufferState;
s="some string";
...
bufferState = yy_scan_string(*s);
yyparse();
yy_delete_buffer(bufferState);
...
This seems to work as I expect it to. I am just learning flex myself,
so everything above may be totally wrong...
Aaron
THANX
Sting
The function yylex_destroy should be called to free resources used by
the scanner.
After yylex_destroy is called, the contents of yyscanner should not be
used. Of course,
there is no need to destroy a scanner if you plan to reuse it. A flex
scanner (both reentrant
and non-reentrant) may be restarted by calling yyrestart
--
Sting, Cheng Po-wen(鄭博文)
Phone : 886-3-5914545 Fax : 886-3-5820085
E-Mail : address@hidden
Internet Software Technology Division(W100)
Computer & Communication Research Lab. / ITRI
_______________________________________________
Help-flex mailing list
address@hidden
http://lists.gnu.org/mailman/listinfo/help-flex