質問

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