Search code examples
clex

Lex case insensitive word detection


I need to treate a string in C where certain words, if present, have to be converted to uppercase. My first choice was to work it in LEX something like this:

%%
word1    {setToUppercase(yytext);RETURN WORD1;}
word2    {setToUppercase(yytext);RETURN WORD2;}
word3    {setToUppercase(yytext);RETURN WORD3;}
%%

The problem I see is that I don't get to detect if some of the chars are uppercase (f.e. Word1, wOrd1...). This could mean a one by one listing:

%%
word1   |
Word1   |
WOrd1   
 {setToUppercase(yytext);RETURN WORD1;}

%%

Is there a way of defining that this especific tokens are to be compared in a case insensitive mode? I have found that I can compile the lexer to be case insensitive, but this can affect other pars of my program.

If not, any workaround suggestion?


Solution

  • Seems that the way that works is this one:

    (W|w)(O|o)(R|r)(D|d) {setToUppercase(yytext);}