Flex工具的错误

时间:2012-02-15 14:41:44

标签: lex lexical-analysis

/*Regular Definitions*/
delim[\t\n]
WS{delim}+
letter[A-Za-z]
digit[0-9]
id{letter}({letter|digit})*
number{digit}+(\.{digit}+?(E[+-]?{digit}+)?

%%
{WS}{/*do nothing*/}
if{printf("\nIF found");return 0;}
then{printf("\nTHEN found");return 0;}
else{printf("\nELSE found");return 0;}
{id}{printf("\nID found");return 0;}
{number}{printf("\nNUMBER found");return 0;}
"<"{printf("\nLess than symbol found.");return 0;}
"<="{printf("\nLess than or Equals to symbol found.");return 0;}
"="{printf("\nEquals to symbol found.");return 0;}
"<>"{printf("\nNot equals to symbol found.");return 0;}
">"{printf("\nGreater than symbol found.");return 0;}
">="{printf("\nGreater than or equal to symbol found.");return 0;}
%%

使用flex编译本

G:\>flex Lex.l

我收到以下错误:

"Lex.l", line 14: unrecognized rule

"Lex.l", line 14: unrecognized rule

"Lex.l", line 14: unrecognized rule

"Lex.l", line 14: unrecognized rule

"Lex.l", line 14: unrecognized rule

"Lex.l", line 27: EOF encountered inside an action

任何人都可以帮我吗?

感谢。

1 个答案:

答案 0 :(得分:3)

1.-用括号括起每个表示的交替。

{letter}|{digit}

2.-在number定义中遗漏了右括号。我在第一次?之前添加了它,但不确定。

number{digit}+(\.{digit}+)?(E[+-]?{digit}+)?

3.-在规则部分中,使用空格分隔模式和C代码。

{WS}        {/*do nothing*/}

这应该有效,或者至少可以编译:

/*Regular Definitions*/
delim[\t\n]
WS{delim}+
letter[A-Za-z]
digit[0-9]
id{letter}({letter}|{digit})*
number{digit}+(\.{digit}+?(E[+-]?{digit}+)?)

%%
{WS}        {/*do nothing*/}
if          {printf("\nIF found");return 0;} 
then        {printf("\nTHEN found");return 0;} 
else        {printf("\nELSE found");return 0;} 
{id}        {printf("\nID found");return 0;} 
{number}    {printf("\nNUMBER found");return 0;} 
"<"         {printf("\nLess than symbol found.");return 0;} 
"<="        {printf("\nLess than or Equals to symbol found.");return 0;} 
"="         {printf("\nEquals to symbol found.");return 0;} 
"<>"        {printf("\nNot equals to symbol found.");return 0;} 
">"         {printf("\nGreater than symbol found.");return 0;} 
">="        {printf("\nGreater than or equal to symbol found.");return 0;} 
%%