문제

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.

도움이 되었습니까?

해결책

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top