문제

Need to match integer type but it must be only separated integer. Example:

INTEGER (-?[0-9]+)
NOTENDLINE  [^$]
%%
{INTEGER}/{NOTENDLINE}     {}
%%

If I enter string like "23test", it must be wrong and no integer matched. But my solution don't working as needed. I don't know, what I need in NOTENDLINE.

도움이 되었습니까?

해결책 2

If you want to match an integer but only if it is followed by whitespace, do so directly:

-?[[:digit:]]+/[[:space:]]

That will fail if the integer is at the very end of the file without a newline, but text files are not supposed to end with anything other than a newline character. You can, however, do the following:

-?[[:digit:]]+/[[:space:]]    { /* Handle an integer */ }
-?[[:digit:]]+/.              { /* Handle the error */ }
-?[[:digit:]]+                { /* Handle an integer; this one must be at EOF */ }

다른 팁

Would this work for you? It relies on the fact that the lexer will find the longest matched rule, but if two are equal, the first rule will be used.

%option noyywrap
DIGIT   [0-9]
OTHER   [a-z0-9]*
%%
{DIGIT}+        printf( "Integer: %s (%d)\n", yytext, atoi( yytext ) );
{OTHER}         printf( "Other: %s\n", yytext );
[ \t\n]+        /* eat up whitespace */
%%
int main( int argc, char **argv )
{
        ++argv, --argc;  /* skip over program name */
        if ( argc > 0 )
                yyin = fopen( argv[0], "r" );
        else
                yyin = stdin;
        yylex();
}

Sample Input (file):

test
test123
123
123test

Sample Ouput:

Other: test
Other: test123
Integer: 123 (123)
Other: 123test
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top