Flex发生C错误:缺少类型说明符? [英] C error with Flex: type specifier missing?
问题描述
在iTerm2 bash中运行以下代码.代码文件是使用Vim创建的.
Running the code below in iTerm2 bash. Code file was created using Vim.
/* just like Unix wc */
%{
int chars = 0;
int words = 0;
int lines = 0;
%}
%%
[a-zA-Z]+ { words++; chars += strlen(yytext); }
\n { chars++; lines++; }
. { chars++; }
%%
main(int argc, char **argv)
{
yylex();
printf("%8d%8d%8d\n", lines, words, chars);
}
我运行了命令
$flex fb1-1.1
$cc lex.yy.c -lfl
这是它返回的错误
fb1-1.1:17:1: warning: type specifier missing, defaults to 'int'
[-Wimplicit-int]
main(int argc, char **argv)
^
1 warning generated.
ld: library not found for -lfl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
现在可以使用.将main()更改为
Works now. Changed the main() to
int main(int argc, char* argv[])
也将-lfl更改为-ll
Also ran changed -lfl to -ll
$flex fb1-1.1
$cc lex.yy.c -ll
$./a.out
this is a text
^D
1 4 15
推荐答案
由注释组装而成(因为比查找重复对象更容易):
Assembled from comments (because it was easier than finding a dupe):
-
在现代C语言(即本世纪的C语言)中,所有函数都需要返回类型,并且
main
仅有的两个合法原型为:
In modern C (that is, C from this century), all functions need a return type and the only two legal prototypes for
main
are:
int main(void)
int main(int argc, char* argv[])
第一种过时的书写方式是 int main()
.
An obsolescent way to write the first is int main()
.
在Max OS上,flex发行版不包含 libfl.a
.它带有 libl.a
.因此,请使用 -ll
而不是 -lfl
.但是更好的办法是通过在您的序言中放置以下声明来告诉flex不需要 yywrap
来避免该问题:
On Max OS, the flex distro doesn't include libfl.a
. It comes with libl.a
. So use -ll
instead of -lfl
. But much better is to avoid the problem by telling flex not to require yywrap
by putting the following declaration in your prologue:
%option noyywrap
更好的方法是使用以下内容:
Even better is to use the following:
%option noinput nounput noyywrap nodefault
在启用警告的情况下进行编译时,
noinput
和 nounput
将避免未使用的功能"警告(应始终这样做). nodefault
告诉flex不要插入默认操作,并在必要时发出警告.默认操作是在stdout上回显不匹配的字符,这通常是不希望的,而且经常造成混淆.
noinput
and nounput
will avoid "unused function" warnings when you compile with warnings enabled (which you should always do). nodefault
tells flex to not insert a default action, and to produce a warning if one would be necessary. The default action is to echo the unmatched character on stdout, which is usually undesirable and often confusing.
这篇关于Flex发生C错误:缺少类型说明符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!