Getting: warning, rule cannot be matched
Asked Answered
C

2

11

I am working on building a lexical and syntax analyzer. I am getting the following warning when I try to use flex with my .l file.

littleDuck.l:26: warning, rule cannot be matched

Rule 26 is the one that starts with {cteI}, my rules section is the following:

[ \t\n]     ;
{RW}        {return RESERVED;}
{id}        {return ID;}
{ops}       {return OPERATOR;}
{seps}      {return SEPARATOR;}
{cteI}      {yylval.ival = atoi(yytext); return INT;}
{cteF}      {yylval.fval = atof(yytext); return FLOAT;}
{ctestring} {yylval.sval = strdup(yytext); return STRING;}
.       ;

Also, my definitions section is this:

RW      program|var|int|float|print|else|if
id      ([a-z]|[A-Z)([a-z]|[A-Z]|[0-9])*
ops     "="|"<"|">"|"<>"|"+"|"-"|"/"|"*"
seps    ":"|","|";"|"{"|"}"|"("|")"
cteI    [0-9]+
cteF    {cteI}(\.{cteI}((e|E)("+"|"-")?{cteI})?)?
ctestring   (\".*\")

Why is this warning appearing, and how can I modify my file so it will not appear?

Camail answered 24/2, 2013 at 22:25 Comment(0)
R
16

The warning tells you that anything that might be matched by {cteI} will always be matched by some earlier rule. In your case, it indicates that a rule doesn't match what you expect it does, probably due to a typo. In your case, its the {id} rule, which you define as:

([a-z]|[A-Z)([a-z]|[A-Z]|[0-9])*

Notice the nesting of the parentheses and square brackets. Adding spaces for clarity, this is

( [a-z] | [A-Z)([a-z] | [A-Z] | [0-9] )*

This will match any sequence of letters, digits, or the characters ( ) or [. You probably meant:

([a-z]|[A-Z])([a-z]|[A-Z]|[0-9])*

which can be more clearly written as

[a-zA-Z][a-zA-Z0-9]*
Resistant answered 24/2, 2013 at 22:50 Comment(1)
Thank you very much! I am just starting to learn, I did not now I could use | that way. It is definitely clearer. Also, I did not notice that missing square bracket, I changed a lot of things but did not expect that to be the problem.Camail
R
3

Review the order of your rules because it's important for example!

  1 [.] 
  2 "foo"

here the second rule will never be matched.

Rabbi answered 1/1, 2019 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.