如何在Windows上编译LEX/YACC文件?

发布于 2024-10-27 08:57:37 字数 155 浏览 11 评论 0原文

我有 LexYACC 文件来解析我的文件(.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 技术交流群。

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

发布评论

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

评论(9

故笙诉离歌 2024-11-03 08:57:37

至于今天(2011-04-05,更新于 2017-11-29),您将需要最新版本的:

  1. flex-2.5.4a-1.exe

  2. bison-2.4.1-setup。 exe

  3. < p>之后,在您偏好的目录中进行完整安装名称中不含空格。我建议C:\GnuWin32。不要不要将其安装在默认 (C:\Program Files (x86)\GnuWin32) 中,因为 bison 在目录名称中存在空格问题,更不用说括号了。
  4. 另外,考虑在默认目录中安装 Dev-CPP (C :\Dev-Cpp)

  5. 之后,设置 PATH 变量以包含 gcc 的 bin 目录(在 C:\Dev-Cpp\bin)和 flex\bison(在 C:\GnuWin32\bin 中)。为此,请复制以下内容: ;C:\Dev-Cpp\bin;C:\GnuWin32\bin 并将其附加到 PATH 变量的末尾,该变量在下图所示的地方:
    Win-7 下逐步设置 PATH 变量。
    如果该图的分辨率不好,您可以看到 这里一步一步

  6. 打开提示符,cd 到“.l”和“.y”所在的目录,然后使用以下命令编译它们:

    1. flex hello.l
    2. bison -dy hello.y
    3. gcc lex.yy.c y.tab.c -o hello.exe

命令创建词法分析器、解析器和可执行文件。

您将能够运行该程序。我制作了一个简单测试的源代码(臭名昭著的 Hello World):

Hello.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); }
         ;

编辑:避免“警告: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:

  1. flex-2.5.4a-1.exe

  2. bison-2.4.1-setup.exe

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

  4. Also, consider installing Dev-CPP in the default directory (C:\Dev-Cpp)

  5. After that, set the PATH variable to include the bin directories of gcc (in C:\Dev-Cpp\bin) and flex\bison (in C:\GnuWin32\bin). To do that, copy this: ;C:\Dev-Cpp\bin;C:\GnuWin32\bin and append it to the end of the PATH variable, defined in the place show by this figure:
    step-by-step to set PATH variable under Win-7.
    If the figure is not in good resolution, you can see a step-by-step here.

  6. Open a prompt, cd to the directory where your ".l" and ".y" are, and compile them with:

    1. flex hello.l
    2. bison -dy hello.y
    3. gcc lex.yy.c y.tab.c -o hello.exe

Commands to create lexical analyzer, parser and executable.

You will be able to run the program. I made the sources for a simple test (the infamous Hello World):

Hello.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".

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.

冷清清 2024-11-03 08:57:37

您(可能想要)的是 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"!

空心空情空意 2024-11-03 08:57:37

这里有适用于 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.

笑脸一如从前 2024-11-03 08:57:37

您可以找到最新的windows版本的flex &野牛在这里:http://sourceforge.net/projects/winflexbison/

You can find the latest windows version of flex & bison here: http://sourceforge.net/projects/winflexbison/

ぺ禁宫浮华殁 2024-11-03 08:57:37

总有 Cygwin

There's always Cygwin.

清君侧 2024-11-03 08:57:37

完整安装 Windows 版 Git(带有 Unix 工具)bison< /code> 和 flex 将位于 bin 文件夹中。

Go for the full installation of Git for windows (with Unix tool), and bison and flex would come with it in the bin folder.

や莫失莫忘 2024-11-03 08:57:37

另外值得注意的是,WinFlexBison 已打包用于巧克力包管理器。安装它,然后去:

choco install 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:

choco install winflexbison

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

童话里做英雄 2024-11-03 08:57:37

最简单的方法是下载并安装cygwin,并在安装过程中下载gcc和flex包。
然后运行 ​​lex 文件,例如。 abc.l

我们写

flex abc.l
gcc lex.yy.c -o abc.exe
./abc.exe

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

flex abc.l
gcc lex.yy.c -o abc.exe
./abc.exe
酒几许 2024-11-03 08:57:37

我遇到了同样的问题,它有一个非常简单的解决方案。
执行 'Lex' 程序的步骤:

  1. 工具 -> 'Lex File Compiler'
  2. 工具 -> 'Lex Build'
  3. 工具 -> 'Open CMD'
  4. 然后在命令提示符下输入 'name_of_file.exe' example->'1 .exe'
  5. 然后输入整个输入按 Ctrl + Z 并按 Enter。

示例

I was having the same problem, it has a very simple solution.
Steps for executing the 'Lex' program:

  1. Tools->'Lex File Compiler'
  2. Tools->'Lex Build'
  3. Tools->'Open CMD'
  4. Then in command prompt type 'name_of_file.exe' example->'1.exe'
  5. Then entering the whole input press Ctrl + Z and press Enter.

Example

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