gpt4 book ai didi

flex-lexer - 错误 : unrecognized rule in flex tool

转载 作者:行者123 更新时间:2023-12-04 01:41:10 28 4
gpt4 key购买 nike

此错误:第 32 行:无法识别的规则出现在我的代码中的这一行:

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

我认为问题出在这里:
OCTAL              "\"(0|[1-3][0-7][0-7]|[1-7][0-7]|[1-7])

这是我的代码:
%{
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;
}

当我以这种方式编写它时,错误消失了:
OCTAL              "\\"(0|[1-3][0-7][0-7]|[1-7][0-7]|[1-7])

我究竟做错了什么?

最佳答案

\"告诉 flex 匹配 "\\告诉 flex 匹配 \
在您的原始代码中 OCTAL 是一个非法标识符,因为它有一个未终止的字符串。您仅通过一个斜杠来“转义”第二个引号。

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

关于flex-lexer - 错误 : unrecognized rule in flex tool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20879643/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com