error : unrecognized rule in flex tool
Asked Answered
B

1

7

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?

Borough answered 2/1, 2014 at 9:14 Comment(1)
I don't think you meant to tag this w/ Adobe/Apache Flex--a UI Framework. I retagged it to flex-lexer; but I'm not sure if that is correct either.Bluejacket
C
8

\" 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

Chiton answered 2/1, 2014 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.