-2

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

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 
Brian
  • 3,850
  • 3
  • 21
  • 37

1 Answers1

1

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.

rici
  • 234,347
  • 28
  • 237
  • 341
  • @antti: In a definition, `()` and `(void)` mean exactly the same thing (sect. 6.7.6.3p14). And `main` cannot be declared without a definition. So I've become convinced that `int main() {…}` is just fine :) Two of three examples in the standard use `int main()`. – rici Nov 26 '17 at 18:00
  • thanks. how does MacOS interpret './a.out' ? my commands aren't giving me errors anymore so I run '.a.out' after them but nothing is printing. Im expecting an output but my bash sits there until I hit ^C – user1224312424 Nov 26 '17 at 18:00
  • @rici yes, but `int main()` does not *declare* a prototype. This means that the compiler is not required to do constraint checks for recursively calling `main` ;) – Antti Haapala -- Слава Україні Nov 26 '17 at 18:03
  • @user: it's waiting for you to type something, followed by an end-of-input (control-z/control-d deoending on OS). – rici Nov 26 '17 at 18:07
  • @antti: i don't believe that is correct. A definition is also a declaration. " An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters." – rici Nov 26 '17 at 18:08
  • @rici definition is a declaration, but `int main() { ... }` is not a declaration *with a prototype*. It is just a definition of a `main` function that has no parameters. I've written about that [here](https://stackoverflow.com/a/41805712/918959). – Antti Haapala -- Слава Україні Nov 26 '17 at 18:11
  • @antti: Well, clang and gcc seem to agree with you about that. – rici Nov 26 '17 at 18:47