Omar Khaled
Omar Khaled

Reputation: 13

Lex - Warning, rule cannot be matched

I'm trying to make .l file but warning occur in certain lines that rule can't be matched

"true"                      return TRUE;
"false"                     return FALSE;
"int"                       return INT;
"char"                      return CHAR;
"float"                     return FLOAT;
"bool"                      return BOOLEAN;

"if"                    return IF;
"else"                  return ELSE;
"for"                   return FOR;
"while"                 return WHILE;
"do"                    return DO;
"switch"                return SWITCH;
"case"                  return CASE;

"break"                 return BREAK;
"default"               return DEFAULT;

Upvotes: 0

Views: 5980

Answers (1)

rici
rici

Reputation: 241741

If the lines you pasted into your question are the lines which triggered the warning, then it is probably because you have a catch-all rule for identifiers before the keyword rules. A minimal example would look like this:

[[:space:]]               /* ignore whitespace */
[[:alpha:]][[:alnum:]]*   return ID;  /* Wrong!! */
keyword                   return KEYWORD;
.                         return *yytext;

That will produce the warning on the third line.

Order matters in (f)lex definitions, because if two rules both match the same token and no rule matches a longer token, then the first rule wins.

This is explained in the flex manual

Upvotes: 3

Related Questions