Search code examples
bisonflex-lexerlex

Flex/Lex unrecognized rule for paranthesis'


%{

%}

%%
'*'     {return 0;}
'('     {return 1;}
')'     {return 2;}
%%

int yywrap(){}

An example code like above. It gives the error message as:

zort.l:7: unrecognized rule
zort.l:8: unrecognized rule
zort.l:8: unrecognized rule
zort.l:8: unrecognized rule

It only gives the error for paranthesis characters. Doesn't give any error for other characters. What is the reason? Are paranthesis' exceptions? How to solve it?


Solution

  • For flex patterns, single and double quotes are not the same. Currently, your first rule says that you want to match a single quote ('), zero or more times (*), followed by another single quote '. The other rules don't have a meaning.

    Assuming you want to instead match literal asterisks and parentheses, try double quotes instead:

    %%
    "*"     {return 0;}
    "("     {return 1;}
    ")"     {return 2;}
    %%
    

    The manual has more information about what can be specified as a pattern (it's an extended form of regular expressions).