Pergunta

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?

Foi útil?

Solução 2

Seems that the way that works is this one:

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

Outras dicas

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)

Its very simple give your patterns and actions as it is,don't worry. While compiling give it as, lex -i filename.l This is on LINUX systems.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top