在函数"yylex"中:未声明“变量"
我正在使用词法分析.为此,我正在使用Flex
并获取以下问题.
I am working with Lexical Analysis. For this I am using Flex
and I fetch following Problems.
int cnt = 0,num_lines=0,num_chars=0; // Problem here.
%%
[" "]+[a-zA-Z0-9]+ {++cnt;}
\n {++num_lines; ++num_chars;}
. {++num_chars;}
%%
int yywrap()
{
return 1;
}
int main()
{ yyin = freopen("in.txt", "r", stdin);
yylex();
printf("%d %d %d\n", cnt, num_lines,num_chars);
return 0;
}
然后,我使用以下命令,它可以正常工作并创建lex.yy.c
.
then, I use following command and it work properly and create lex.yy.c
.
Rezwans-iMac:laqb-2 rezwan $ flex work.l
Rezwans-iMac:laqb-2 rezwan$ flex work.l
然后,我使用以下命令.
then, I use following command.
Rezwans-iMac:laqb-2 rezwan $ gcc lex.yy.c -o b
Rezwans-iMac:laqb-2 rezwan$ gcc lex.yy.c -o b
并关注error
:
work.l: In function ‘yylex’:
work.l:3:4: error: ‘cnt’ undeclared (first use in this function); did you mean int’?
[" "]+[a-zA-Z0-9]+ {++cnt;}
^~~
int
work.l:3:4: note: each undeclared identifier is reported only once for each function it appears in
work.l:4:4: error: ‘num_lines’ undeclared (first use in this function)
\n {++num_lines; ++num_chars;}
^~~~~~~~~
work.l:4:17: error: ‘num_chars’ undeclared (first use in this function); did you mean ‘num_lines’?
\n {++num_lines; ++num_chars;}
^~~~~~~~~
num_lines
work.l: In function ‘main’:
work.l:15:23: error: ‘cnt’ undeclared (first use in this function); did you mean ‘int’?
return 0;
^
int
work.l:15:28: error: ‘num_lines’ undeclared (first use in this function)
return 0;
^
work.l:15:38: error: ‘num_chars’ undeclared (first use in this function); did you mean ‘num_lines’?
return 0;
^
num_lines
如果我这样修改work.l
文件,我就不会超越error
.
I am not getting above error
, if I modify work.l
file like this .
int cnt = 0,num_lines=0,num_chars=0; // then work properly above command.
%%
[" "]+[a-zA-Z0-9]+ {++cnt;}
\n {++num_lines; ++num_chars;}
. {++num_chars;}
%%
int yywrap()
{
return 1;
}
int main()
{ yyin = freopen("in.txt", "r", stdin);
yylex();
printf("%d %d %d\n", cnt, num_lines,num_chars);
return 0;
}
也就是说,如果我在int cnt = 0,num_lines=0,num_chars=0;
行之前使用1 tab
,它将正常工作.
That is to say, If I use 1 tab
before this line int cnt = 0,num_lines=0,num_chars=0;
, it works properly.
现在我有两个问题:
-
是否必须在该行
int cnt = 0,num_lines=0,num_chars=0;
之前使用1 tab
?为什么?进行逻辑解释.
Is necessary use
1 tab
before this lineint cnt = 0,num_lines=0,num_chars=0;
? why? explain logically.
是解决此错误的另一种方法?
Is another solution to solve this error ?
我对制表符问题不太确定,但一个解释是,如果您不放置制表符并在第一部分中写类似以下内容的话:>
I'm not very sure about the tab problem but one explanation is that if you don't place tab and write in the first section something like:
int cnt = 0;
然后请注意,在第一部分中,您还可以编写快捷方式",例如:
Then note that in the first section you may also write "shortcuts" like:
Digit [0-9]
这是一个正则表达式,用于定义数字是什么,而不是一直写[0-9]
来表示数字.
which is a regular expression that defines what digit is instead of writing all the time [0-9]
to denote digit.
因此,在不使用制表符的情况下在第一列中编写int cnt = 0;
时,就像定义关键字int一样(这可能就是错误告诉您did you mean int’?
的原因).因此,tab是一种区分上述两种情况的方法.
So when writing int cnt = 0;
in the first column without using tab is like defining the keyword int (that's probably why the error tells you did you mean int’?
). So tab is a way to distinguish the two above cases.
根据flex ,要编写c/c ++代码,它必须位于内部:
%{ ... c/c++ code... %}
,因此对于您的示例:
According to flex in order to write c/c++ code it needs to be inside:
%{ ... c/c++ code... %}
so for your example:
%{
int cnt = 0,num_lines=0,num_chars=0;
%}
所以我认为最好的方法是在%{ %}
中编写您的c代码.
So I thing the best way is to write your c-code inside %{ %}
.