编译 bison 时与 unistd.h 相关的困难vc下的flex程序++

发布于 2024-09-01 11:44:00 字数 969 浏览 5 评论 0原文

我正在使用野牛 & Flex(通过 cygwin 下载)与 vc++。当我编译程序时,出现错误:

...: 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 文件(.l)中定义 YY_NO_UNISTD_H,此错误将会消失,但我收到其他几个错误:

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

如何修复此问题问题?

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

我知道这是因为 unistd.h 在 Windows 中不存在。我必须编写自己的 unistd.h 吗?如果是的话如何编写才能消除这些错误?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

舂唻埖巳落 2024-09-08 11:44:00

词法分析器使用 istty 来确定输入流是终端还是管道/文件。词法分析器使用此信息来更改其缓存行为(词法分析器在不是终端时读取大量输入)。如果您知道您的程序永远不会以交互方式使用,则可以将 %option never-interactive 添加到您的词法分析器中。当程序通过用户输入运行时,请使用%option Interactive。当需要两种用途时,您可以生成一个交互式词法分析器,这在批处理模式下使用时会带来性能损失,或者提供您自己的 istty 函数。

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.

惜醉颜 2024-09-08 11:44:00

.l 文件中使用 %option nounistd 来删除对 unistd.h 的依赖。

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

场罚期间 2024-09-08 11:44:00

使用带有选项 --wincompat 的 win_flex.exe 并且您不需要破解您的 lex 文件

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

人海汹涌 2024-09-08 11:44:00

以防万一有人仍然遇到这个问题,Flex 在其开发文件中附带了 unistd.h。我在这里找到了这个:

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

简而言之,只需确保您的编译器可以访问它即可。就我而言,它只是将“C:\GnuWin32\include”添加到附加包含目录中

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

一袭水袖舞倾城 2024-09-08 11:44:00

unistd.h 是一个 UNIX 头文件,因此它不存在于 VC++ 中;你最好的选择可能是在 Cygwin (或 mingw/msys)中使用 g++ 来编译它。您还可以查看此问题 寻求其他建议。

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.

耳钉梦 2024-09-08 11:44:00

我使用的是来自 GnuWin32 项目的 flex 2.5.4,它不检查 YY_NO_UNISTD_H

在我的版本中,Flex 仅在编译为 C++ 时才查找 unistd.h,因此如果您的 yylval 不使用任何 C++ 构造,您就可以省去所有这些麻烦。

我必须在 yylval 中使用 STL(使用指针使其成为 POD 类型),因此为了在 C++ 中进行 Flex 编译,我创建了这个简单的 unistd.h

#include <io.h>

这就是所需要的全部(实际上,我可以复制GnuWin32附带的unistd.h文件,例如flyontheweb 建议)。

PS 总结一下:在 Bison 中,我将 yylval 所需的 STL 头文件放入 %code require {} 中,并将当前目录添加到我的 makefile 中的 INCLUDE 路径中。

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.

烟凡古楼 2024-09-08 11:44:00

我已经太晚了,但无论如何,我会分享我的发现,以拯救仍在寻找答案的人。
就我而言,在编译器查找标头的位置有一个空的 unistd.h 文件对我有用。

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.

伪心 2024-09-08 11:44:00

好吧,这篇文章很旧,但我面临着同样的问题,这里有一些应该可行的东西。
WinFlexBison

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

云巢 2024-09-08 11:44:00

我最近在升级到 Angular 14 后遇到了这个问题。

npm install -glatest-version

解决了我的问题。

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

npm install -g latest-version

resolved my issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文