unistd。在vc++中编译bison & flex程序时遇到的困难。

时间:2022-06-07 00:41:40

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

我使用的是bison & flex(通过cygwin下载)和vc++。当我编译程序时,我有一个错误:

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

The corresponding code in the flex-generated file is:

在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

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

如果我在flex文件中定义YY_NO_UNISTD_H (.l),这个错误将会消失,但是我还会得到其他一些错误:

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

所有这些错误都发生在flex生成的扫描器中。

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?

我知道这是因为unistd。h不存在于windows中。我要写我自己的大学吗?如果是这样,如何编写它来消除这些错误呢?

8 个解决方案

#1


13  

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.

lexer使用isatty来确定输入流是否为终端或管道/文件。lexer使用此信息来改变其缓存行为(lexer在非终端时读取大量输入)。如果您知道您的程序将永远不会在交互式的类型中使用,您可以添加%选项,而不是您的lexer。当程序与用户输入一起运行时,使用%option interactive。当两者都使用时,您可以生成一个交互式的lexer,当在批处理模式中使用时,它会造成性能损失,或者提供您自己的isatty函数。

#2


12  

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

在您的.l文件中使用%option istd来消除对unistd.h的依赖。

#3


9  

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

如果有人仍然存在这个问题,Flex会和unistd一起出现。在它的devel文件内。我发现在这里:

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

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

简而言之,只需确保编译器能够到达它。在我的例子中,它只是添加了“C:\GnuWin32\include”到附加的包含目录。

#4


3  

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

使用win_flex。exe选项——wincompat,你不需要破解你的lex文件。

#5


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.

unistd。h是一个UNIX标头,所以它不在vc++中;你最好的方法可能是在Cygwin(或mingw/msys)中使用g++编译它。你也可以看看这个问题。

#6


1  

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

我使用的flex 2.5.4来自GnuWin32项目,它不检查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.

在我的版本中,Flex查找unistd。只有当被编译为c++时,如果你的yylval没有使用任何c++结构,你可以省去所有的麻烦。

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:

我必须在yylval中使用STL(使用一个指针使它成为POD类型),所以为了使flex编译在c++中,我创建了这个简单的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).

这就是它所需要的(实际上,我可以复制unistd)。h文件附带GnuWin32,就像flyontheweb建议的那样。

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.

在Bison中,我将yylval所需的STL头文件放入%code中,并将当前目录添加到makefile中包含的路径中。

#7


0  

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.

我已经太迟了,但无论如何,我将分享我的发现,以拯救仍在寻找答案的人。在我的案例中,有一个空的unistd。h文件在编译器查找标题的位置。

#8


0  

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

这篇文章很老,但我面临着同样的问题,这是应该做的。WinFlexBison

#1


13  

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.

lexer使用isatty来确定输入流是否为终端或管道/文件。lexer使用此信息来改变其缓存行为(lexer在非终端时读取大量输入)。如果您知道您的程序将永远不会在交互式的类型中使用,您可以添加%选项,而不是您的lexer。当程序与用户输入一起运行时,使用%option interactive。当两者都使用时,您可以生成一个交互式的lexer,当在批处理模式中使用时,它会造成性能损失,或者提供您自己的isatty函数。

#2


12  

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

在您的.l文件中使用%option istd来消除对unistd.h的依赖。

#3


9  

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

如果有人仍然存在这个问题,Flex会和unistd一起出现。在它的devel文件内。我发现在这里:

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

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

简而言之,只需确保编译器能够到达它。在我的例子中,它只是添加了“C:\GnuWin32\include”到附加的包含目录。

#4


3  

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

使用win_flex。exe选项——wincompat,你不需要破解你的lex文件。

#5


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.

unistd。h是一个UNIX标头,所以它不在vc++中;你最好的方法可能是在Cygwin(或mingw/msys)中使用g++编译它。你也可以看看这个问题。

#6


1  

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

我使用的flex 2.5.4来自GnuWin32项目,它不检查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.

在我的版本中,Flex查找unistd。只有当被编译为c++时,如果你的yylval没有使用任何c++结构,你可以省去所有的麻烦。

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:

我必须在yylval中使用STL(使用一个指针使它成为POD类型),所以为了使flex编译在c++中,我创建了这个简单的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).

这就是它所需要的(实际上,我可以复制unistd)。h文件附带GnuWin32,就像flyontheweb建议的那样。

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.

在Bison中,我将yylval所需的STL头文件放入%code中,并将当前目录添加到makefile中包含的路径中。

#7


0  

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.

我已经太迟了,但无论如何,我将分享我的发现,以拯救仍在寻找答案的人。在我的案例中,有一个空的unistd。h文件在编译器查找标题的位置。

#8


0  

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

这篇文章很老,但我面临着同样的问题,这是应该做的。WinFlexBison