Take the 2-minute tour ×

How do I convert the user input to lowercase in Flex? Looked on line but didn't find anything helpful.

When I try to do this:

yylval.string = strdup(tolower( yytext) ); 

I get error stating: passing arg 1 of 'tolower' makes integer from pointer without a cast.

Thanks, Sarah

share|improve this question

2 Answers 2

#include <stdio.h>
#include <ctype.h>
[A-Z]    putchar(tolower(*yytext));

If you want to convert an entire string to lower case, you need to call tolower in a loop, as tolower only converts a single character:

for (i = 0; yytext[i]; i++)
    yytext[i] = tolower(yytext[i]);
yylval.string = strdup(yytext);
share|improve this answer
Thanks. I'm using union. how would I change the case for this: yylval.str = strdup( yytext ); –  sap Apr 26 '12 at 17:15
Your solution is good but this is called after the token is matched. But I want the user to enter a word in any cased and it should be matched with a rule. This page esiee.fr/~najmanl/compil/Flex/flex_2.html is talking about a macro, but not sure how to use it. –  sap Apr 27 '12 at 17:21
Thanks ... but no upvote or accept! –  Kaz Apr 28 '12 at 18:58
well this is not the solution I'm looking for. –  sap Apr 30 '12 at 13:50
@sap: If you have a specific solution in mind, why ask the question? This answers the question that you asked. –  Chris Dodd Jul 22 '12 at 18:45

you could redefine the YY_INPUT macro in the top 'C' section:

#define YY_INPUT(buf,result,max_size) \
        { \
        int c = '*', n; \
        for ( n = 0; n < max_size && \
                 (c = getc( yyin )) != EOF && c != '\n'; ++n ) \
            buf[n] = (char) tolower(c); \
        if ( c == '\n' ) \
            buf[n++] = (char)  tolower(c); \
        if ( c == EOF && ferror( yyin ) ) \
            YY_FATAL_ERROR( "input in flex scanner failed" ); \
        result = n; \
share|improve this answer
Not a good idea, the details of said macro could very well be different in the next version of flex (or the prehistoric one you're forced to use when installing at Big Client's site). –  vonbrand Feb 2 '13 at 17:17

Your Answer


