如何在Windows上编译LEX/YACC文件?
我有 Lex
和 YACC
文件来解析我的文件(.l
文件和 .y
文件)。
如何编译这些文件以及如何在Windows平台上为它们制作等效的.c
文件?
I'm having Lex
and YACC
files to parse my files (.l
file and .y
file).
How to compile those files and how to make equivalent .c
file for them in windows platform?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
至于今天(2011-04-05,更新于 2017-11-29),您将需要最新版本的:
flex-2.5.4a-1.exe
bison-2.4.1-setup。 exe
C:\GnuWin32
。不要不要将其安装在默认 (C:\Program Files (x86)\GnuWin32) 中,因为 bison 在目录名称中存在空格问题,更不用说括号了。另外,考虑在默认目录中安装 Dev-CPP (
C :\Dev-Cpp
)之后,设置 PATH 变量以包含

gcc
的 bin 目录(在C:\Dev-Cpp\bin
)和flex\bison
(在C:\GnuWin32\bin
中)。为此,请复制以下内容:;C:\Dev-Cpp\bin;C:\GnuWin32\bin
并将其附加到PATH
变量的末尾,该变量在下图所示的地方:如果该图的分辨率不好,您可以看到 这里一步一步。
打开提示符,cd 到“.l”和“.y”所在的目录,然后使用以下命令编译它们:
flex hello.l
bison -dy hello.y
gcc lex.yy.c y.tab.c -o hello.exe
您将能够运行该程序。我制作了一个简单测试的源代码(臭名昭著的
Hello World
):Hello.l
Hello.y
编辑:避免“警告:yyerror 和 yylex 的隐式定义”。
免责声明:请记住,这个答案非常旧(自 2011 年以来!),如果您因版本和功能更改而遇到问题,您可能需要更多研究,因为我无法更新此答案以反映新的问题。谢谢,我希望这对您和许多人来说都是一个很好的切入点。
更新:如果需要做一些事情(非常小的更改),请查看 github 上的官方存储库:https:// github.com/drbeco/hellex
快乐黑客。
As for today (2011-04-05, updated 2017-11-29) you will need the lastest versions of:
flex-2.5.4a-1.exe
bison-2.4.1-setup.exe
After that, do a full install in a directory of your preference without spaces in the name. I suggest
C:\GnuWin32
. Do not install it in the default (C:\Program Files (x86)\GnuWin32) because bison has problems with spaces in directory names, not to say parenthesis.Also, consider installing Dev-CPP in the default directory (
C:\Dev-Cpp
)After that, set the PATH variable to include the bin directories of

gcc
(inC:\Dev-Cpp\bin
) andflex\bison
(inC:\GnuWin32\bin
). To do that, copy this:;C:\Dev-Cpp\bin;C:\GnuWin32\bin
and append it to the end of thePATH
variable, defined in the place show by this figure:If the figure is not in good resolution, you can see a step-by-step here.
Open a prompt, cd to the directory where your ".l" and ".y" are, and compile them with:
flex hello.l
bison -dy hello.y
gcc lex.yy.c y.tab.c -o hello.exe
You will be able to run the program. I made the sources for a simple test (the infamous
Hello World
):Hello.l
Hello.y
Edited: avoiding "warning: implicit definition of yyerror and yylex".
Disclaimer: remember, this answer is very old (since 2011!) and if you run into problems due to versions and features changing, you might need more research, because I can't update this answer to reflect new itens. Thanks and I hope this will be a good entry point for you as it was for many.
Updates: if something (really small changes) needs to be done, please check out the official repository at github: https://github.com/drbeco/hellex
Happy hacking.
您(可能想要)的是 Flex 2.5.4(有些人现在正在“维护”它并生成更新的版本,但在我看来,他们把事情搞砸了,而不是解决任何真正的缺点)和byacc 1.9(同样)。 (编辑 2017 年 11 月 17 日:Flex 2.5.4 在 Sourceforge 上不再可用,Flex github 存储库只能回溯到 2.5.5。但显然您仍然可以从位于 < 的 Gnu ftp 服务器获取它a href="ftp://ftp.gnu.org/old-gnu/gnu-0.2/src/flex-2.5.4.tar.gz" rel="nofollow noreferrer">ftp://ftp.gnu.org /old-gnu/gnu-0.2/src/flex-2.5.4.tar.gz.)
由于它不可避免地会被推荐,所以我会警告反对使用野牛。 Bison 最初是由 Robert Corbett 编写的,他后来又写了 Byacc,他公开表示当时他并不真正知道或理解自己在做什么。不幸的是,由于年轻又愚蠢,他在 GPL 下发布了它,现在 GPL 粉丝将其视为生活疾病的答案,尽管它自己的作者基本上说它应该被视为本质上是一个 beta 测试产品 - 但通过复杂的根据 GPL 粉丝的推理,byacc 的许可证没有足够的限制来符合“免费”的资格!
What you (probably want) are Flex 2.5.4 (some people are now "maintaining" it and producing newer versions, but IMO they've done more to screw it up than fix any real shortcomings) and byacc 1.9 (likewise). (Edit 2017-11-17: Flex 2.5.4 is not available on Sourceforge any more, and the Flex github repository only goes back to 2.5.5. But you can apparently still get it from a Gnu ftp server at ftp://ftp.gnu.org/old-gnu/gnu-0.2/src/flex-2.5.4.tar.gz.)
Since it'll inevitably be recommended, I'll warn against using Bison. Bison was originally written by Robert Corbett, the same guy who later wrote Byacc, and he openly states that at the time he didn't really know or understand what he was doing. Unfortunately, being young and foolish, he released it under the GPL and now the GPL fans push it as the answer to life's ills even though its own author basically says it should be thought of as essentially a beta test product -- but by the convoluted reasoning of GPL fans, byacc's license doesn't have enough restrictions to qualify as "free"!
这里有适用于 Windows 的 Flex 和 Bison 端口: http://gnuwin32.sourceforge.net/
flex 是 lex 的免费实现。 bison 是 yacc 的免费实现。
There are ports of flex and bison for windows here: http://gnuwin32.sourceforge.net/
flex is the free implementation of lex. bison is the free implementation of yacc.
您可以找到最新的windows版本的flex &野牛在这里:http://sourceforge.net/projects/winflexbison/
You can find the latest windows version of flex & bison here: http://sourceforge.net/projects/winflexbison/
总有 Cygwin。
There's always Cygwin.
完整安装 Windows 版 Git(带有 Unix 工具) 和
bison< /code> 和
flex
将位于bin
文件夹中。Go for the full installation of Git for windows (with Unix tool), and
bison
andflex
would come with it in thebin
folder.另外值得注意的是,WinFlexBison 已打包用于巧克力包管理器。安装它,然后去:
...在撰写本文时包含 Bison 2.7 和 Bison 2.7。弹性 2.6.3。
还有
winflexbison3
(在撰写本文时)有 Bison 3.0.4 和 Bison 3.0.4。弹性 2.6.3。Also worth noting that WinFlexBison has been packaged for the Chocolatey package manager. Install that and then go:
...which at the time of writing contains Bison 2.7 & Flex 2.6.3.
There is also
winflexbison3
which (at the time of writing) has Bison 3.0.4 & Flex 2.6.3.最简单的方法是下载并安装cygwin,并在安装过程中下载gcc和flex包。
然后运行 lex 文件,例如。 abc.l
我们写
the easiest method is to download and install cygwin and download gcc and flex packages during installation.
Then to run a lex file for eg. abc.l
we write
我遇到了同样的问题,它有一个非常简单的解决方案。
执行 'Lex' 程序的步骤:
示例
I was having the same problem, it has a very simple solution.
Steps for executing the 'Lex' program:
Example