Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

C error with Flex: type specifier missing?

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); }

         { chars++; lines++; }
.          { chars++; }

%%

 main(int argc, char **argv)
{
  yylex();
  printf("%8d%8d%8d
", lines, words, chars);
}

I ran commands

$flex fb1-1.1
$cc lex.yy.c -lfl

This is the error that it returns

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)

EDIT: Works now. Changed the main() to

int main(int argc, char* argv[])

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 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assembled from comments (because it was easier than finding a dupe):

  1. 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[])
    

    An obsolescent way to write the first is int main().

  2. 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 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...