views:

158

answers:

2

I get the warning

warning, -s option given but default rule can be matched

if you google

option given but default rule can be matched

You'll get results of flex homepage and an entry in a man page.

warning, -s option given but default rule can be matched' means that it is possible (perhaps only in a particular start condition) that the default rule (match any single character) is the only one that will match a particular input. Since -s' was given, presumably this is not intended.

My build file doesnt have a -s option. I wrote

bison -y -d calc1.y
flex calc1.l
gcc code...

How do i fix this warning?

Here is a tiny version of my lex file. This file also triggers the warning

%{
#include "y.tab.h"
%}
%option noyywrap nodefault yylineno

%%

[0-9]+      {
                return INTEGER;
            }

[-+()=/*{},;\n] { return *yytext; }

[ \t]    /* skip whitespace */

[a-zA-Z0-9_]* { printf("lex Unknown character = '%s'", yytext); yyerror("lex Unknown character"); }

%%
A: 

The "flex" command on your system could have been aliased to be the flex command with an -s option. That could occur in any of several startup scripts.

The warning is telling you that there are some inputs that aren't matched by any of the rules in your input file. The warning is there to get you to make sure you have considered all cases in your regular expressions.

If you truly want the default rule to be used, then add it to the end of the rules in your input file. Try adding the rules

. ECHO;
\n ECHO;

Update: The input file you provide includes the %option nodefault, which is the same as specifying "-s" on the command line, so that mystery is solved. Also, the file does not have a rule for ".". There are some characters not covered, '@' for example.

I'm also suspicious of the "-" symbol in the character class. It is supposed to be a range operator, but it may work as a character in the first position as you have it. I don't know. It might be a good idea to use "\-" instead.

You could change the last rule to

. { printf("lex Unknown character = '%s'", yytext); yyerror("lex Unknown character"); }

to get rid of the warning (and to prevent the resulting scanner from crashing).

Also, could you change the title of the question to be about flex instead of bison?

UncleO
I have `.` and `[-+()=/*{},;\n]` as a rule. Shouldnt that cover all inputs? (does . cover \r, \v, \b? \a? etc)
acidzombie24
. is supposed to cover every character but newline. I tried flex -s with just the above two rules, and the warning didn't appear. When I removed the second rule, the warning showed up.
UncleO
+2  A: 

%option nodefault is equivalent to -s

mkl

related questions