why i got this warning "rule cannot be matched" in my Lexer Program

57 views Asked by At

I have this code in flex program ".l"

"if" {return IF;}
"else" {return ELSE;}
"For" {return FOR;}
. {printf("SYNTAX ERROR .. EXIT ..");exit(0);}

and this code in bison program ".y"

%token IF ELSE FOR

but i got that error of

warning, rule cannot be matched
warning, rule cannot be matched
warning, rule cannot be matched
2

There are 2 answers

1
Hako_Sama On BEST ANSWER

i found the problem

i needed to write the code on a single line like this

"if" { return IF; }

and not like this

"if" {
    return IF;
}
1
Akshitha Rao On

I feel it depends on the version of lex you are using.

In the version that I am using right now (flex 2.6.4), all three syntaxes work perfectly fine, even with a newline.

But an older version threw an error when writing it on a single line "if" {return IF;}. It was solved after padding return IF; with white spaces as "if" { return IF; }.