2012-02-15 66 views
3
/*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编译此错误与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 

谁能帮助我呢?

谢谢。

+0

我编辑了问题补充编译代码标签之间的误差。它更容易阅读。 – Birei 2012-02-15 15:27:38

回答

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;} 
%% 
+0

感谢上面的代码的确编译,但是当我尝试编译c文件时,使用Dev-cpp得到以下错误: [链接器错误]未定义的引用'yywrap' [链接器错误]未定义对'yywrap' [链接器错误]对'WinMain @ 16'的未定义引用 ld返回1退出状态 – vs2010noob 2012-02-15 15:41:50

+0

@ vs2010noob:在我的linux中,一切正常:'flex lex.l && cc -o exe lex.yy.c -lfl'。确保链接到flex库。 – Birei 2012-02-15 16:09:10