27

I'm using bison & flex (downloaded via cygwin) with vc++. When I compile the program I got an error:

...: fatal error C1083: Cannot open include file: 'unistd.h': No such file or directory

The corresponding code in the flex-generated file is:

#ifndef YY_NO_UNISTD_H
/* Special case for "unistd.h", since it is non-ANSI. We include it way
 * down here because we want the user's section 1 to have been scanned first.
 * The user has a chance to override it with an option.
 */
/* %if-c-only */
#include <unistd.h>
/* %endif */
/* %if-c++-only */
/* %endif */
#endif

If I define YY_NO_UNISTD_H in the flex file(.l) this error will disappear, but I get several other errors:

...: error C2447: '{' : missing function header (old-style formal list?)
...: warning C4018: '<' : signed/unsigned mismatch
...: error C3861: 'isatty': identifier not found

How can I fix this problem?

All these errors occur in the flex-generated scanner.

I know it's because unistd.h doesn't exist in windows. Do I have to write my own unistd.h? If so how to write it in order to eliminate those errors?

Lesmana
  • 25,663
  • 9
  • 82
  • 87
Haiyang
  • 1,489
  • 4
  • 15
  • 19

9 Answers9

19

isatty is used by the lexer to determine if the input stream is a terminal or a pipe/file. The lexer uses this information to change its caching behavior (the lexer reads large chunks of the input when it is not a terminal). If you know that your program will never be used in an interactive kind, you can add %option never-interactive to you lexer. When the program is run with user input, use %option interactive. When both uses are desired, you can either generate an interactive lexer, which gives a performance loss when used in batch mode, or provide your own isatty function.

Rudi
  • 19,366
  • 3
  • 55
  • 77
  • For me, with flex 2.6.4 and bison 3.7.4, using `%option never-interactive` didn't remove the `#ifndef YY_NO_UNISTD_H #include #endif`. I had to `#define YY_NO_UNISTD_H 1` as well in `lexer.l`. But that only fixes the `lexer.cpp`. The `lexer.hpp` will still contain the `#ifndef YY_NO_UNISTD_H` block without the `#define YY_NO_UNISTD_H`. So I have probably misunderstood this answer, because I don't know how it really helps. – rturrado Jun 13 '23 at 17:29
17

Use %option nounistd in your .l file to remove the dependence on unistd.h.

Dr. Alex RE
  • 1,772
  • 1
  • 15
  • 23
  • 5
    I found it in the docs, but it just will not work for me. `unrecognized %option unistd` (even though I specified nounistd, so it seems to try to resolve it. Any idea? In manual: http://flex.sourceforge.net/manual/Code_002dLevel-And-API-Options.html#index-nounistd-320 – Kissaki Jun 19 '12 at 14:59
  • flex version 2.5.4 seems to ignore this option – David Lopez Jan 17 '22 at 19:33
  • Perhaps add `%{ #define YY_NO_UNISTD_H %}` to the top (use three lines for this)? – Dr. Alex RE Jan 18 '22 at 20:25
  • Or just define a dummy `unistd.h` header file. If linking fails with an error that `isatty()` is not defined, then add `int isatty(int) { return 0; }` to your C code or `extern "C" `int isatty(int) { return 0; }` to your C++ code. – Dr. Alex RE Jan 20 '22 at 16:58
10

just in case somebody's still this problem, Flex comes with unistd.h within its devel files. I found this here:

http://sourceforge.net/tracker/index.php?func=detail&aid=931222&group_id=23617&atid=379173

to put it short, just make sure your compiler can reach it. in my case it's just adding "C:\GnuWin32\include" to the additional inclusion directories

10

use win_flex.exe with option --wincompat and you dont need to hack your lex file

Alexander Egorov
  • 593
  • 10
  • 14
2

unistd.h is a UNIX header, so it's not present in VC++; your best bet is probably to compile it using g++ in Cygwin (or mingw/msys). You could also look at this question for other suggestions.

Community
  • 1
  • 1
tzaman
  • 46,925
  • 11
  • 90
  • 115
1

I am too late but anyway I will share my findings to save someone still looking for answer. In my case having an empty unistd.h file in the location where compiler looks for headers works for me.

1

I'm using flex 2.5.4 that comes from the GnuWin32 project, which doesn't check for YY_NO_UNISTD_H.

In my version, Flex looks for unistd.h only when being compiled as C++, so you can save yourself all this trouble if your yylval doesn't use any C++ constructs.

I had to use the STL in yylval (using a pointer to make it a POD type), so in order to make flex compile in C++ I created this simple unistd.h:

#include <io.h>

That's all it takes (actually, I could copy the unistd.h file that comes with GnuWin32, like flyontheweb suggests).

P.S. To wrap things up: in Bison I put yylval's required STL header files in %code requires {} and added the current directory to the INCLUDE paths in my makefile.

Community
  • 1
  • 1
Yariv
  • 576
  • 5
  • 4
0

Well this post is old but I face the same problem and here is something that should work. WinFlexBison

smit
  • 959
  • 1
  • 9
  • 20
  • and here is an article about how to use this with vs http://www.di-mgt.com.au/flex_and_bison_in_msvc.html – smit Aug 09 '14 at 20:40
0

I ran into this problem recently after upgrading to Angular 14.

npm install -g latest-version

resolved my issue.

Henk Kruger
  • 177
  • 1
  • 4