我是lex / yacc的新手,正在学习本教程:https://www.youtube.com/watch?v=54bo1qaHAfk这是我的lex文件
%{
#include "main.h"
#include <stdio.h>
%}
%%
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
"&" {return RUN_DAEMON;}
"|" {return SYM_PIPE;}
">" {return RED_STDOUT;}
"<" {return RED_STDIN;}
">>" {return APP_STDOUT;}
[ \t\n]+ {;}
. {printf("unexpected character\n");}
%%
int yywrap(){
return 1;
}
但是在运行lex
命令后,我尝试使用lex.yy.c
编译gcc
,但此错误使我成为垃圾邮件
sbash.l: In function ‘yylex’:
sbash.l:7:5: error: expected expression before ‘[’ token
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
^
sbash.l:7:6: error: ‘a’ undeclared (first use in this function)
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
^
sbash.l:7:6: note: each undeclared identifier is reported only once for each function it appears in
sbash.l:7:14: error: ‘_a’ undeclared (first use in this function)
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
^~
sbash.l:7:17: error: ‘zA’ undeclared (first use in this function)
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
^~
sbash.l:7:20: error: ‘Z0’ undeclared (first use in this function)
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
^~
sbash.l:7:29: error: expected expression before ‘{’ token
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
^
sbash.l:13:7: error: stray ‘\’ in program
[ \t\n]+ {;}
^
sbash.l:13:9: error: stray ‘\’ in program
[ \t\n]+ {;}
[不幸的是,即使用谷歌搜索,我也找不到问题所在(许多示例的表达式与我的代码完全一样)。我的lex版本是2.6.1,在CentOS8上]
如Flex manual chapter on flex input file format中所述,模式规则必须从左边缘开始:
flex输入的rules部分包含一系列格式如下的规则:
pattern action
其中模式必须不缩进,并且操作必须在同一行上开始。 (添加了一些重点)
规则部分的缩进线按原样传递。特别是,将第一个规则之前的缩进线插入到yylex
函数的顶部,这通常很有用。但是flex并没有尝试验证以这种方式包含的代码是否有效;编译生成的扫描仪时,将检测到错误。