Search code examples
lexlexical-analysis

What if there exists no matched rule in a Lex program because of REJECT?


I'm currently reading the documentation on Lex written by Lesk and Schmidt, and get confused by the REJECT action.

Consider the two rules

a[bc]+   { ... ; REJECT;}
a[cd]+   { ... ; REJECT;}

Input:

ab

Only the first matches, and see what we get from the material.

The action REJECT means ``go do the next alternative.'' It causes whatever rule was second choice after the current rule to be executed.

However, there is no second choice, will there comes a error?


Solution

  • There are really very few use cases for REJECT; I don't think I've ever seen an instance of it in use other than in examples.

    Anyway, unless you specify %option nodefault (or the -s command-line flag), flex will add a default fallback action to your ruleset, equivalent to

    .|\n   ECHO;
    

    In your case, that pattern will match after the REJECT.

    However, it is possible to override the default action; for example, you could add the rule:

    .|\n   REJECT;
    

    In that case, flex really will not have an alternative after the two REJECTs, and it will print an error message on stderr ("flex scanner jammed") and then call exit.