Search code examples
bisonflex-lexeryacclex

flex's unrecognized rule (about an optional phrase)


I wrote a simple flex file:

%{
#include "question.tab.h"
%}

%%

COMMON(/[45]\.0)?   return GOTCHA;
[a-z]+              return ETC;

%%

and tried to compile.(flex -d question.l)
Then the output was as follows.

question.l:7: unrecognized rule
question.l:7: unrecognized rule
question.l:7: unrecognized rule
question.l:7: unrecognized rule
question.l:7: unrecognized rule
question.l:7: unrecognized rule
question.l:7: unrecognized rule
question.l:7: unrecognized rule
question.l:7: unrecognized rule
question.l:7: unrecognized rule

I wanted flex to return GOTCHA when it catches COMMON, COMMON/4.0, COMMON/5.0.

What part of regexp should I fix?

Thank you for advance.


Solution

  • The / sign is a special character in flex. It's used for lookaheads and the way you're using it is illegal (flex only allows lookaheads at the very end of the expression, not inside groups).

    Since you don't actually want a lookahead, but to match a literal slash, you should either put it in quotes or escape it with a backslash.