Next: Accessor Methods, Previous: Global Replacement, Up: Reentrant Detail
yylex_init
and yylex_destroy
must be called before and
after yylex
, respectively.
int yylex_init ( yyscan_t * ptr_yy_globals ) ; int yylex ( yyscan_t yyscanner ) ; int yylex_destroy ( yyscan_t yyscanner ) ;
The function yylex_init
must be called before calling any other
function. The argument to yylex_init
is the address of an
uninitialized pointer to be filled in by flex
. The contents of
ptr_yy_globals
need not be initialized, since flex
will
overwrite it anyway. The value stored in ptr_yy_globals
should
thereafter be passed to yylex()
and yylex_destroy(). Flex
does not save the argument passed to yylex_init
, so it is safe to
pass the address of a local pointer to yylex_init
. The function
yylex
should be familiar to you by now. The reentrant version
takes one argument, which is the value returned (via an argument) by
yylex_init
. Otherwise, it behaves the same as the non-reentrant
version of yylex
.
yylex_init
returns 0 (zero) on success, or non-zero on failure,
in which case, errno is set to one of the following values:
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
.
Below is an example of a program that creates a scanner, uses it, then destroys it when done:
int main () { yyscan_t scanner; int tok; yylex_init(&scanner); while ((tok=yylex()) > 0) printf("tok=%d yytext=%s\n", tok, yyget_text(scanner)); yylex_destroy(scanner); return 0; }