Lex Debugging

Lex has facilities that enable debugging. This feature may vary with different versions of lex so you should consult documentation for details. The code generated by lex in file lex.yy.c includes debugging statements that are enabled by specifying command-line option "-d". Debug output in flex (a GNU version of lex) may be toggled on and off by setting yy_flex_debug. Output includes the rule applied and corresponding matched text. If you’re running lex and yacc together then specify the following in your yacc input file:

extern int yy_flex_debug;
int main(void) {
    yy_flex_debug = 1;

Alternatively, you may write your own debug code by defining functions that display information for the token value and each variant of the yylval union. This is illustrated in the following example. When DEBUG is defined the debug functions take effect and a trace of tokens and associated values is displayed.

%union {
    int ivalue;

#ifdef DEBUG
    int dbgToken(int tok, char *s) {
        printf("token %s\n", s);
        return tok;
    int dbgTokenIvalue(int tok, char *s) {
        printf("token %s (%d)\n", s, yylval.ivalue);
        return tok;
    #define RETURN(x) return dbgToken(x, #x)
    #define RETURN_ivalue(x) return dbgTokenIvalue(x, #x)
    #define RETURN(x) return(x)
    #define RETURN_ivalue(x) return(x)


[0-9]+      {
                yylval.ivalue = atoi(yytext);

"if"        RETURN(IF);
"else"      RETURN(ELSE);