gpt4 book ai didi

c - 弹性 "Unrecognized Error"

转载 作者:行者123 更新时间:2023-11-30 15:49:45 27 4
gpt4 key购买 nike

在用 Flex 编写 token 生成器时,我遇到了这个恼人的错误:“无法识别的规则”

我的代码是:

/* Keywords */

TYPE int|double|bool|char
LOGICAL if|else|for|foreach|do|while|switch|return
MACROWORD import|define|ifndef|endif|elseif|udef
MACRO "#"{MACROWORD}

KEYWORD {TYPE}|{LOGICAL}|{MACRO}

/* Literals */

DIGIT [0-9]
DIGITS {DIGIT}+
OPT_FRAC ("."{DIGITS})?
OPT_EXP (E(+|-){DIGITS})?

NUMBER {DIGITS}{OPT_FRAC}{OPT_EXP}

LETTER [a-zA-Z]

/* Identifier */

ID {LETTER}({LETTER}|{DIGIT})*

/* Operators */

OPERATOR "+"|"-"|"*"|"/"|"^"|"=""="|"<""="|">""="|">"|"<"|"!""=""

%%

{KEYWORD} printf("(Keyword, %s)\n", yytext);

{NUMBER} printf("(Numeric Literal, %s)", yytext);

{ID} printf("(Identifier, %s)", yytext);

{OPERATOR} printf("(Operator, %s", yytext);

[ \n\t] /* Ignore Whitespace */

"{" printf("(L Bracket, %s)", yytext);

"}" printf("(R Bracket, %s)", yytext);

"(" printf("(L Parens, %s)", yytext);

")" printf("(R Parens, %s", yytext);

";" printf("(Semicolon, %s", yytext);

%%

main()
{
yylex();
}

遗憾的是,这只会导致:

“Scanner.lex:39:无法识别的规则”重复19次,后跟一个实例:

“Scanner.lex:43:无法识别的规则”

第 39 行是:

{NUMBER}    printf("(Numeric Literal, %s)", yytext);

第 43 行是:

{OPERATOR}  printf("(Operator, %s", yytext);

我在互联网上搜索并发现this answer also on stackoverflow其中建议在定义行的开头添加“^”,因此我将第 39 行引用(编号)更改为:

NUMBER    ^{DIGITS}{OPT_FRAC}{OPT_EXP}

什么也没做。有什么建议吗?

最佳答案

这太可怕了,因为 OPT_EXP 中的 + 没有任何 + 内容。 (这是一个 RE +,而不是文字加号。)

您的 OPERATOR 定义有太多双引号。

关于c - 弹性 "Unrecognized Error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16129107/

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