Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error : unrecognized rule in flex tool

Tags:

flex-lexer

this error : line 32: unrecognized rule is appearing in my code in this line :

{OCTAL}                     printf("Kind= OCTAL, Word= %s, LineNumber= %d\n",yytext,LineNum);

and i think the problem is here :

OCTAL              "\"(0|[1-3][0-7][0-7]|[1-7][0-7]|[1-7])

this is my code :

%{
int LineNum=1;
int count =0;
%}


%x OneLineComment  MultipleLinesComment 

DIGIT              [0-9]

INT                [+|-]?(0|[1-9][0-9]*)

OCTAL              "\"(0|[1-3][0-7][0-7]|[1-7][0-7]|[1-7])  // i think the error is         here !!

DOUBLE             {INT}"."{DIGIT}*((E|e)?[+|-]?[1-9][0-9]*)?

KEYWORD            int|double|while|if|else

IDENTIFIER         [_A-Za-z]+[_A-Za-z0-9]*

Space               " "

Tab                 "\t"

NewLine             "\n"

%%


{INT}                       printf("Kind= INTEGER, Word= %s,            LineNumber= %d\n",yytext,LineNum);

{OCTAL}                     printf("Kind= OCTAL, Word= %s, LineNumber= %d\n",yytext,LineNum);  // the error line

{DOUBLE}                    printf("Kind= DOUBLE, Word= %s, LineNumber= %d\n",yytext,LineNum);

{KEYWORD}                   printf("Kind= KEWORD, Word= %s, LineNumber= %d\n",yytext,LineNum);

{IDENTIFIER}                printf("Kind= IDENTIFIER, Word= %s, LineNumber= %d\n",yytext,LineNum);


"//"                        BEGIN(OneLineComment);
<OneLineComment>.
<OneLineComment>"\n"        {
                            printf("Kind=     OneLineComment, LineNumber= %d\n",LineNum);
                            printf("Kind= WhiteSpace, Word= NewLine, LineNumber= %d\n",LineNum);
                            BEGIN(INITIAL);
                            LineNum++;
                        }

"/*"                        {   count=LineNum;
                               BEGIN(MultipleLinesComment);
                        }
<MultipleLinesComment>.
<MultipleLinesComment>"\n"   LineNum++;
<MultipleLinesComment>"*/"  {
                            printf("Kind=   MultipleLinesComment, LineNumber= %d,ToLineNumber= %d\n",count,LineNum);
                            count=0;
                            BEGIN(INITIAL);
                        }

{Space}                     printf("Kind= WhiteSpace, Word=   Space, LineNumber= %d\n",LineNum);

{Tab}                       printf("Kind= WhiteSpace, Word= Tab, LineNumber= %d\n",LineNum);

{NewLine}                   {
                            printf("Kind= WhiteSpace,   Word= NewLine, LineNumber= %d\n",LineNum);
                            LineNum++;
                        }

.                           printf("Kind= wrongToken, Word= %s, LineNumber= %d\n",yytext,LineNum);

%%

int yywrap()
{
return 1;
}

int main()
{
yylex();
return 0;
}

and when I write it in this way the error goes away :

OCTAL              "\\"(0|[1-3][0-7][0-7]|[1-7][0-7]|[1-7])

What am i doing wrong?

like image 444
Dana Avatar asked Jan 02 '14 09:01

Dana


1 Answers

\" tells flex to match a "

\\ tells flex to match a \

In your original code OCTAL is an illegal identifier because it has an unterminated string. You are "escaping" the second quotation mark by only having a single slash.

http://en.wikipedia.org/wiki/Escape_sequences_in_C

like image 76
Josh Avatar answered Nov 09 '22 16:11

Josh