문제

i'm trying to make a lexical analyser(on VS2008 using flex and bison) who will only pick alphanumerics(john4323snow) and number( 1234 ). The problem is that at the end of the alphanumerics and numbers it also puts characters like #$%@. I don't understand why. thank you for your time!

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int k;
%}

%option noyywrap

%%
0|[1-9]* {
        k = atoi(yytext);
        printf("Found the Number %d", k); 
        }
 [a-zA-Z][a-zA-Z0-9]* {printf("Found the Identifier %s", yytext); }
%%
void main(int argc, char *argv[])
{
    char infile[20];
    strcpy(infile, argv[1]);
    yyin = fopen(infile, "r");
    if(yyin == NULL)
    {
        printf("open failed\n");
    }
    else
    {
        yylex();
    }
    system("pause");
}
도움이 되었습니까?

해결책

The strings that do not fall into any pattern, the string is printed to the standard output.

So be changed as follows

%%
0|[1-9]* {
        k = atoi(yytext);
        printf("Found the Number %d", k); 
        }
 [a-zA-Z][a-zA-Z0-9]* {printf("Found the Identifier %s\n", yytext); }
. ;
"\n" ;
%%
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top