Search code examples
flex-lexer

How to explictly write the default rule of gnu flex?


flex manual say the following.

By default, any text not matched by a flex scanner is copied to the output

I want to understand how to write it explictly. Is it something like this?

%%
. ECHO;

Also, how to disable the default rule?


Solution

  • The default rule is:

    .|\n    ECHO;
    

    (In every start condition)

    Remember that . in (f)lex does not match a newline.

    To disable the default rule, use the declaration

    %option nodefault
    

    Once you do that, you will get a warning if your rules do not cover every eventuality. If you ignore the warning and use the generated scanner, it will stop with a fatal error if the input does not match any pattern.

    Since you hardly ever want the default rule, I recommend always using the above %option.

    If you have some default rule of your own in mind, you can place it at as the last rule in your file:

    <*>.|\n   /* default action here */