Pergunta

Here's my lex code:

%{
%}

%%
[ \t]*    {printf("tab");}
.+ {
printf("%s", yytext);}
.|\n {
ECHO;
}
%%
int main() {
        yylex();
        return 0;
}


Here's my input data:

#include<stdio.h>
int main(){
for(int i = 0 ; i < 10 ; i ++){
for(int j = 0 ; j < i ; j ++){
    printf("*");
}
printf("\n");
}
}

In my opinion, the .+ rule will stop once it meet '\n'.
But I can't trigger the [ \t] rule at the input line 5.

Foi útil?

Solução

The generated lexical analyzer will always choose the longest token. For your

    printf("*");

rule .+ will give a longer token than [ \t]*, so the former will be chosen.

By the way, you could try this example:

%{
%}

%%
[ \t]*   {printf("| |");}
\n       {printf("newline\n");}
[^ \n]+  {printf("%s", yytext);}

%%
int main() {
        yylex();
        return 0;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top