我是lex的新手,我在文件ilk.l中编写了非常简单的程序
%option main
%%
float val;
[+-]?[0-9]*(.)?[0-9]+ {sscanf(yytext, "%f", &val);
printf(">%f<", val); }
保存该文件后,运行以下命令:
-bash-4.2$ lex ilk.l
-bash-4.2$ gcc lex.yy.c
ilk.l: In function ‘yylex’:
ilk.l:3:1: error: ‘val’ undeclared (first use in this function)
float val;
^
ilk.l:3:1: note: each undeclared identifier is reported only once for each function it appears in
-bash-4.2$
我对错误的原因感到困惑。你能解释一下我做错了什么,我该如何改正吗?
变量应该在声明部分如下声明:
%{
//declaration section
float val;
%}
%option main
%%
[+-]?[0-9]*(.)?[0-9]+ {sscanf(yytext, "%f", &val);printf(">%f<", val);}
%%