'How to add local variables to yylex function in flex lexer?

I was writing a lexer file that matches simple custom delimited strings of the form xyz$this is stringxyz. This is nearly how I did it:

%{
char delim[16];
uint8_t dlen;
%}

%%

.*$ {
    dlen = yyleng-1;
    strncpy(delim, yytext, dlen);
    BEGIN(STRING);
}

<STRING>. {
    if(yyleng >= dlen) {
        if(strncmp(delim, yytext[yyleng-dlen], dlen) == 0) {
            BEGIN(INITIAL);
            return STR;
        }
    }
    yymore();
}

%%

Now I wanted to convert this to reentrant lexer. But I don't know how to make delim and dlen as local variables inside yylex apart from modifying generated lexer. Someone please help me how should I do this.

I don't recommend to store these in yyextra because, these variables need not persist across multiple calls to yylex. Hence I would prefer an answer that guides me towards declaring these as local variables.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source