在函数“yylex”中:“变量”未声明
In function ‘yylex’: 'Variable’ undeclared
我正在使用词法分析。为此,我正在使用 Flex
并获取以下问题。
work.l
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
。
Rezwans-iMac:laqb-2 rezwan$ flex work.l
然后,我使用以下命令。
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
。
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
,它就可以正常工作。
现在我有两个问题:
int cnt = 0,num_lines=0,num_chars=0;
这一行前必须用1 tab
吗?为什么?逻辑解释。
是否有解决此错误的另一种方法?
我不太确定制表符问题,但一个解释是,如果您不放置制表符并在第一部分写下类似内容:
int cnt = 0;
然后请注意,在第一部分中,您还可以这样写 "shortcuts":
Digit [0-9]
这是一个定义数字是什么的正则表达式,而不是一直写[0-9]
来表示数字。
所以在第一列写 int cnt = 0;
而不使用制表符就像定义关键字 int 一样(这可能就是错误告诉你 did you mean int’?
的原因)。所以tab是区分以上两种情况的一种方式。
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 代码
我正在使用词法分析。为此,我正在使用 Flex
并获取以下问题。
work.l
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
。
Rezwans-iMac:laqb-2 rezwan$ flex work.l
然后,我使用以下命令。
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
。
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
,它就可以正常工作。
现在我有两个问题:
int cnt = 0,num_lines=0,num_chars=0;
这一行前必须用1 tab
吗?为什么?逻辑解释。是否有解决此错误的另一种方法?
我不太确定制表符问题,但一个解释是,如果您不放置制表符并在第一部分写下类似内容:
int cnt = 0;
然后请注意,在第一部分中,您还可以这样写 "shortcuts":
Digit [0-9]
这是一个定义数字是什么的正则表达式,而不是一直写[0-9]
来表示数字。
所以在第一列写 int cnt = 0;
而不使用制表符就像定义关键字 int 一样(这可能就是错误告诉你 did you mean int’?
的原因)。所以tab是区分以上两种情况的一种方式。
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; %}
所以我认为最好的方法是在 %{ %}
.