如何在 Windows 上编译 LEX/YACC 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5456011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compile LEX/YACC files on Windows?
提问by Thorin Oakenshield
I'm having Lex
and YACC
files to parse my files (.l
file and .y
file).
我有Lex
与YACC
文件解析我的文件(.l
文件和.y
文件)。
How to compile those files and how to make equivalent .c
file for them in windows platform?
如何编译这些文件以及如何.c
在 Windows 平台上为它们制作等效文件?
回答by Dr Beco
As for today (2011-04-05, updated 2017-11-29) you will need the lastest versions of:
至于今天(2011-04-05,2017-11-29 更新),您将需要以下最新版本:
After that, do a full install in a directory of your preference without spaces in the name. I suggest
C:\GnuWin32
. Do notinstall 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-CPPin 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
之后,在您喜欢的目录中进行完整安装,名称中没有空格。我建议
C:\GnuWin32
。难道不是在它安装默认(C:\ Program Files文件(x86)的\的GnuWin32),因为野牛与目录名中的空间问题,而不是说括号。另外,请考虑在默认目录 ( ) 中安装Dev-CPP
C:\Dev-Cpp
之后,设置 PATH 变量以包含
gcc
(inC:\Dev-Cpp\bin
) 和flex\bison
(inC:\GnuWin32\bin
)的 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
You will be able to run the program. I made the sources for a simple test (the infamous Hello World
):
您将能够运行该程序。我制作了一个简单测试的来源(臭名昭著的Hello World
):
Hello.l
你好.l
%{
#include "y.tab.h"
int yyerror(char *errormsg);
%}
%%
("hi"|"oi")"\n" { return HI; }
("tchau"|"bye")"\n" { return BYE; }
. { yyerror("Unknown char"); }
%%
int main(void)
{
yyparse();
return 0;
}
int yywrap(void)
{
return 0;
}
int yyerror(char *errormsg)
{
fprintf(stderr, "%s\n", errormsg);
exit(1);
}
Hello.y
你好
%{
#include <stdio.h>
#include <stdlib.h>
int yylex(void);
int yyerror(const char *s);
%}
%token HI BYE
%%
program:
hi bye
;
hi:
HI { printf("Hello World\n"); }
;
bye:
BYE { printf("Bye World\n"); exit(0); }
;
Edited: avoiding "warning: implicit definition of yyerror and yylex".
编辑:避免“警告:yyerror 和 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.
免责声明:请记住,这个答案很旧(自 2011 年以来!),如果您因版本和功能更改而遇到问题,您可能需要更多研究,因为我无法更新此答案以反映新项目。谢谢,我希望这对你来说是一个很好的切入点,就像对许多人一样。
Updates: if something (really small changes) needs to be done, please check out the official repository at github: https://github.com/drbeco/hellex
更新:如果需要做一些事情(非常小的改动),请查看 github 上的官方存储库:https: //github.com/drbeco/hellex
Happy hacking.
快乐的黑客。
回答by Jerry Coffin
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.)
您(可能想要)是 Flex 2.5.4(有些人现在正在“维护”它并生产更新版本,但 IMO 他们做了更多的事情来搞砸而不是修复任何真正的缺点)和byacc 1.9(同样)。(编辑 2017-11-17:Flex 2.5.4 在 Sourceforge 上不再可用,并且 Flex github 存储库只能回到 2.5.5。但您显然仍然可以从ftp://的 Gnu 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 againstusing 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"!
由于不可避免地会被推荐,我会警告不要使用 Bison。Bison 最初是由 Robert Corbett 编写的,他也是后来编写 Byacc 的那个人,他公开表示当时他并不真正知道或理解他在做什么。不幸的是,年轻而愚蠢的他在 GPL 下发布了它,现在 GPL 粉丝将其作为解决生活弊病的方法,尽管它的作者基本上说它应该被视为本质上是一个 beta 测试产品——但令人费解的是GPL 粉丝的推理,byacc 的许可证没有足够的限制来获得“免费”的资格!
回答by lesmana
There are ports of flex and bison for windows here: http://gnuwin32.sourceforge.net/
这里有适用于 Windows 的 flex 和 bison 端口:http: //gnuwin32.sourceforge.net/
flexis the free implementation of lex. bisonis the free implementation of yacc.
回答by Lexxmark
You can find the latest windows version of flex & bison here: http://sourceforge.net/projects/winflexbison/
您可以在此处找到 flex 和 bison 的最新 Windows 版本:http: //sourceforge.net/projects/winflexbison/
回答by Alastair Maw
Also worth noting that WinFlexBison has been packagedfor the Chocolatey package manager. Install that and then go:
还值得注意的是,WinFlexBison 已被打包用于Chocolatey 包管理器。安装它然后去:
choco install winflexbison
...which at the time of writing contains Bison 2.7 & Flex 2.6.3.
...在撰写本文时包含 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.
还有winflexbison3
哪个(在撰写本文时)有 Bison 3.0.4 和 Flex 2.6.3。
回答by u2041954
Go for the full installation of Git for windows (with Unix tool), and bison
and flex
would come with it in the bin
folder.
去完全安装的用于(使用Unix工具)窗口的Git和bison
和flex
会来与它的bin
文件夹中。
回答by aayush shah
the easiest method is to download and install cygwinand download gcc and flex packages during installation. Then to run a lex file for eg. abc.l
最简单的方法是下载安装cygwin,安装时下载gcc和flex包。然后运行一个 lex 文件,例如。abc.l
we write
我们写
flex abc.l
gcc lex.yy.c -o abc.exe
./abc.exe
回答by Amit
I was having the same problem, it has a very simple solution.
Steps for executing the 'Lex' program:
我遇到了同样的问题,它有一个非常简单的解决方案。
执行“Lex”程序的步骤:
- Tools->'Lex File Compiler'
- Tools->'Lex Build'
- Tools->'Open CMD'
- Then in command prompt type 'name_of_file.exe' example->'1.exe'
- Then entering the whole input press Ctrl + Z and press Enter.
- 工具->'Lex 文件编译器'
- 工具->'Lex Build'
- 工具->'打开CMD'
- 然后在命令提示符中输入'name_of_file.exe'example->'1.exe'
- 然后输入整个输入按 Ctrl + Z 并按 Enter。