gpt4 book ai didi

lex - lex 中无法识别的规则

转载 作者:行者123 更新时间:2023-12-04 23:32:47 31 4
gpt4 key购买 nike

我正在用 lex 编写一个程序,它给了我以下错误:

scan.l:49:无法识别的规则

第 49 行是:{number} {return(NUM);}
编辑:
但是,错误似乎与之前的行直接相关,{id} {return(ID);} .
它会直接在该规则之后列出该行作为错误来源,即使它是空白的。

这是我的代码:

#include <stdio.h>

%token BOOL, ELSE, IF, TRUE, WHILE, DO, FALSE, INT, VOID
%token LPAREN, RPAREN, LBRACK, RBRACK, LBRACE, RBRACE, SEMI, COMMA, PLUS, MINUS, TIMES
%token DIV, MOD, AND, OR, NOT, IS, ADDR, EQ, NE, LT, GT, LE, GE
%token NUM, ID, PUNCT, OP

int line = 1, numAttr;
char *strAttr;
%}

/* regular definitions */

delim [ \t]
ws {delim}+
letter [A-Za-z]
digit [0-9]
id ({letter} | _)({letter} | {digit} | _)*
number {digit}+

%%

{ws} {/* no action and no return */}
[\n] {line++;}
bool {return(BOOL);}
else {return(ELSE);}
if {return(IF);}
true {return(TRUE);}
while {return(WHILE);}
do {return(DO);}
false {return(FALSE);}
int {return(INT);}
void {return(VOID);}

{id} {return(ID);} // error is on these two lines
{number} {return(NUM);} //

"(" {yylval = LPAREN; return(PUNCT);}
")" {yylval = RPAREN; return(PUNCT);}
"[" {yylval = LBRACK; return(PUNCT);}
"]" {yylval = RBRACK; return(PUNCT);}
"{" {yylval = LBRACE; return(PUNCT);}
"}" {yylval = RBRACE; return(PUNCT);}
";" {yylval = SEMI; return(PUNCT);}
"," {yylval = COMMA; return(PUNCT);}

"+" {yylval = PLUS; return(OP);}
"-" {yylval = MINUS; return(OP);}
"*" {yylval = TIMES; return(OP);}
"/" {yylval = DIV; return(OP);}
"%" {yylval = MOD; return(OP);}
"&" {yylval = ADDR; return(OP);}
"&&" {yylval = AND; return(OP);}
"||" {yylval = OR; return(OP);}
"!" {yylval = NOT; return(OP);}
"!=" {yylval = NE; return(OP);}
"=" {yylval = IS; return(OP);}
"==" {yylval = EQ; return(OP);}
"<" {yylval = LT; return(OP);}
"<=" {yylval = LE; return(OP);}
">" {yylval = GT; return(OP);}
">=" {yylval = GE; return(OP);}

%%

那条规则有什么问题?谢谢。

最佳答案

上一行导致了问题。如果在 {id} 之间添加一个空格规则和{number}规则,您会注意到错误的行号没有改变。

模式中不允许使用空格。定义 {id} 如下:

id      ({letter}|_)({letter}|{digit}|_)*

关于lex - lex 中无法识别的规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2706839/

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