Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lex case insensitive word detection

Tags:

c

lex

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?

like image 465
jordi Avatar asked Mar 27 '14 11:03

jordi


1 Answers

You could set case-insensitivity in the .l file:

%option caseless

You could call flex -i.

Or you could state individual rules to be case-insensitive:

(?i:word)
like image 81
DevSolar Avatar answered Sep 23 '22 16:09

DevSolar