Yulin Z.
Yulin Z.

Reputation: 3

Rule cannot be matched when running flex to generate a lexical analyzer

I am trying to write a specification for a flex lexical analyzer. One of the rules is that the identifier (e.g., "foo", "Foo_Bar", "year_2020", "Jan30") cannot end with "_", such as "foo_". So far I have the following to catch that error:

letter [A-Za-z]
digit [0-9]
identifier {letter}({letter}|{digit}|\_)*({letter}|{digit})*
number {digit}+
character [0-9a-zA-Z_]

...

    /*Ends with underscore*/
{letter}({character}*({letter}|{number})+)?\_
{
    printf("Error at line %d, column %d: identifier \"%s\" cannot end with an underscore.\n", num_lines, num_cols, yytext);
    exit(1);
}

However I am getting an error "mini_l.lex:89: warning, rule cannot be matched", line 89 is {letter}({character}*({letter}|{number})+)?\_.

Could someone please help me out?

Upvotes: 0

Views: 111

Answers (1)

rici
rici

Reputation: 241791

That error message means that the preceding rules in your flex definition already cover every case which might be matched with this rule. Since you don't provide any information about the previous rules, it's not possible to suggest which rule or rules might be making this rule redundant, but that's what you should be looking for.

Also, please remember that in a flex definition, an action must start on the same line as the pattern. It is not possible to put the action on the next line, as you have done; that will cause the action to be misinterpreted as a pattern. So it's possible that the problem is actually the pattern {, since flex occasionally reports errors with a slightly-incorrect line number.

Upvotes: 1

Related Questions