manpagez: man pages & more
info flex
Home | html | info | man

File: flex.info,  Node: Init and Destroy Functions,  Next: Accessor Methods,  Prev: Global Replacement,  Up: Reentrant Detail

19.4.4 Init and Destroy Functions
---------------------------------

In the default C/C++ back end 'yylex_init' and 'yylex_destroy' must be
called before and after 'yylex', respectively.  This may not be true in
other target languages, especially those with automatic memory
management.

         int yylex_init ( yyscan_t * ptr_yy_globals ) ;
         int yylex_init_extra ( YY_EXTRA_TYPE user_defined, yyscan_t * ptr_yy_globals ) ;
         int yylex ( yyscan_t yyscanner ) ;
         int yylex_destroy ( yyscan_t yyscanner ) ;

   In these declarations, 'YY_EXTRA_TYPE' is a placeholder for the type
specifier in the scanner's 'extra-type' option.

   (The scanner type and type declaration syntax will be different in
target languages other than C/C++.)

   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 'yylex_init', overwriting any
previous contents.  The function 'yylex_init_extra' may be used instead,
taking as its first argument a variable of type 'YY_EXTRA_TYPE'.  See
the section on yyextra, below, for more details.

   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' so long as it remains in scope for the duration of all
calls to the scanner, up to and including the call to 'yylex_destroy'.

   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'.

   With the C/C++ back end, both 'yylex_init' and 'yylex_init_extra'
return 0 (zero) on success or non-zero on failure.  'errno' is also set
to one of the following values on failure:

   * ENOMEM Memory allocation error.  *Note memory-management::.
   * EINVAL Invalid argument.

   Other target languages may use different means of passing back an
error indication.

   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(scanner)) > 0)
                 printf("tok=%d  yytext=%s\n", tok, yyget_text(scanner));
     
             yylex_destroy(scanner);
             return 0;
         }

© manpagez.com 2000-2026
Individual documents may contain additional copyright information.