Next: , Previous: , Up: Reentrant Detail   [Contents][Index]

19.4.5 Accessing Variables with Reentrant Scanners

Accessor methods (get/set functions) provide access to common flex variables.

Many scanners that you build will be part of a larger project. Portions of your project will need access to flex values, such as yytext. In a non-reentrant scanner, these values are global, so there is no problem accessing them. However, in a reentrant scanner, there are no global flex values. You can not access them directly. Instead, you must access flex values using accessor methods (get/set functions). Each accessor method is named yyget_NAME or yyset_NAME, where NAME is the name of the flex variable you want. For example:

    /* Set the last character of yytext to NULL. */
    void chop ( yyscan_t scanner )
        int len = yyget_leng( scanner );
        yyget_text( scanner )[len - 1] = '\0';

The above code may be called from within an action like this:

    .+\n    { chop( yyscanner );}

You may find that %option header-file is particularly useful for generating prototypes of all the accessor functions. See option-header.