1

I'm trying to complete an assignment on making a lexical analyser, but I keep getting this error every time I try to make a lex.cc.y file using flex:

"fofo.l", line:13 warning rule cannot be matched.
"fofo.l", line:14 warning rule cannot be matched.

%{
#include <stdio.h>
#define Relop    1
#define Addop    2
#define Mulop    3
#define Assignop 4
#define Not      5
%}
%%
["<"|">"|"<="|">="|"=="|"!="]* { return Relop; }
["+"|"-"|"||"]*                { return Addop; }
["*"|"/"|"%"|"&&"]*            { return Mulop; }
["="]*                         { return Assignop; }
["!"]*                         { return Not; }
.                              { return -1; }
%%
int main () {
    int token;
    while ((token = yylex())) {
        switch (token) {
            case Relop:    printf("Relop: %s\n", yytext);    break;
            case Addop:    printf("Addop: %s\n", yytext);    break;
            case Mulop:    printf("Mulop: %s\n", yytext);    break;
            case Assignop: printf("Assignop: %s\n", yytext); break;
            case Not:      printf("Not: %s\n", yytext);      break;
            default:       printf("Error: %s not recognized\n", yytext);
        }
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ali Zubair
  • 25
  • 3
  • 9
  • You need to reread the manual about the meaning of [] – rici Oct 22 '15 at 19:18
  • well i was able to fix the problem after a while, but for some reason, I get "Realop" as output for both of these "!" and "=" characters, whereas i should be getting "Not" and "Assignop" respectively. I also happen to get the minus sign "-" as unrecognized. – Ali Zubair Oct 22 '15 at 20:49
  • Did you figure out what [] means and why you shouldn't be using them as you do? – rici Oct 22 '15 at 20:55
  • we define a class of characters using the [] operators. it said that there are exceptions such as "\^-" characters where "-" defines a range. so in order to use "-" as a character we need to type it at either in the beginning or the end. however it still hasn't worked for me. – Ali Zubair Oct 22 '15 at 21:06
  • "!=" is four characters, one of which is repeated. I don't think that is what you wanted. – rici Oct 22 '15 at 23:46

1 Answers1

1

As indicated in the comments, the problem is with the improper use of the character set pattern of []. This indicates a set of single characters. The interpretation of the pattern:

["="]*

Would match the characters: ",= or ε. i.e. not just the equals symbol. It would also match """""""""" or ==========

The pattern:

["*"|"/"|"%"|"&&"]* 

would match the characters: ",*,|,%,& or ε and not just the multiply operators. It would also match """", |||||||||| etc. The stick symbol | would not represent or.

The correct pattern would be to remove the bracketing leaving a choice of patterns. The resultant flex program would look like this:

%{
#include <stdio.h>
#define Relop    1
#define Addop    2
#define Mulop    3
#define Assignop 4
#define Not      5
%}
%%
"<"|">"|"<="|">="|"=="|"!=" { return Relop; }
"+"|"-"|"||"                { return Addop; }
"*"|"/"|"%"|"&&"            { return Mulop; }
"="                         { return Assignop; }
"!"                         { return Not; }
.                           { return -1; }
%%
int main () {
    int token;
    while ((token = yylex())) {
        switch (token) {
            case Relop:    printf("Relop: %s\n", yytext);    break;
            case Addop:    printf("Addop: %s\n", yytext);    break;
            case Mulop:    printf("Mulop: %s\n", yytext);    break;
            case Assignop: printf("Assignop: %s\n", yytext); break;
            case Not:      printf("Not: %s\n", yytext);      break;
            default:       printf("Error: %s not recognized\n", yytext);
        }
    }
}

I wrote the full answer to assist others who might have made this common beginners mistake only after it was clear the OP had fixed the error!

Community
  • 1
  • 1
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129