问题现象:...: fatal error C1083: Cannot open include file: 'unistd.h': No such file or directory
原因,flex生成的代码如下:
#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
如果flex命令行调用加上 --nounistd 或者在词法描述文件中加上 %option nounistd,则可能产生如下错误:
...: error C2447: '{' : missing function header (old-style formal list?) ...: warning C4018: '<' : signed/unsigned mismatch ...: error C3861: 'isatty': identifier not found
解决方法:
1. 包含 <io.h>里面声明了windows版本的isatty函数。
2. 在词法描述文件中加上 %option never-interactive,这个选项向flex表明输入时非交互式的,此时flex不会调用isatty来查询输入设备类型。
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.