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

时间:2022-11-13 09:37:32

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?

如何编译这些文件,以及如何在windows平台中为它们生成等价的.c文件?

8 个解决方案

#1


87  

As for today (2011-04-05, updated 2017-11-29) you will need the lastest versions of:

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

  1. flex-2.5.4a-1.exe

    flex - 2.5.4a - 1. - exe

  2. bison-2.4.1-setup.exe

    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.

    在此之后,在您的首选项目录中进行完整的安装,而不使用名称空间。我建议C:\ GnuWin32。不要在默认情况下安装它(C:\程序文件(x86)\GnuWin32),因为bison在目录名中存在空格问题,而不是插入括号。

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

    另外,考虑在缺省目录中安装devc - cpp (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:
    如何在Windows上编译LEX/YACC文件?
    If the figure is not in good resolution, you can see a step-by-step here.

    在此之后,设置PATH变量以包含gcc的bin目录(在C:\Dev-Cpp\bin)和flex\bison(在C:\GnuWin32\bin)中。要做到这一点,请复制这个:;C:\Dev-Cpp\bin;C:\GnuWin32\bin,并将其附加到PATH变量的末尾,由这个图来定义:如果这个数字没有很好的分辨率,您可以在这里一步一步地看到。

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

    打开一个提示,cd到你的目录。l“和”。y"是,然后用:

    1. flex hello.l
    2. flex hello.l
    3. bison -dy hello.y
    4. 野牛dy hello.y
    5. gcc lex.yy.c y.tab.c -o hello.exe
    6. gcc lex.yy。c y.tab。c - o hello . exe

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

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

你将能够运行这个程序。我做了一个简单测试的来源(臭名昭著的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".

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

快乐的黑客。

#2


11  

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://ftp.gnu.org/old-gnu/ gnu.2 /src/flex-2.5.4.tar.gz的Gnu ftp服务器上得到它。

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"!

由于不可避免地会被推荐,我警告不要使用Bison。Bison最初是由Robert Corbett写的,他后来写了Byacc,他公开表示当时他还不知道或者不知道他在做什么。不幸的是,被年幼无知,他现在在GPL下发布GPL的粉丝们把它作为生活的答案的问题,即使自己的作者基本上是说它应该被认为是一个测试产品的复杂的推理,但GPL球迷,byacc执照时没有足够的限制成为“免费”!

#3


9  

There are ports of flex and bison for windows here: http://gnuwin32.sourceforge.net/

这里有flex和bison的端口:http://gnuwin32 sourceforge.net/。

flex is the free implementation of lex. bison is the free implementation of yacc.

flex是lex的免费实现。bison是yacc的免费实现。

#4


7  

There's always Cygwin.

总是有Cygwin。

#5


7  

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

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

#6


3  

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

为windows(使用Unix工具)进行Git的完整安装,bison和flex将会出现在bin文件夹中。

#7


1  

Also worth noting that WinFlexBison has been packaged for the Chocolatey package manager. Install that and then go:

同样值得注意的是,WinFlexBison已经被包装为巧克力包装经理。安装后就可以了:

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。

#8


-1  

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

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

we write

我们写

flex abc.l

flex abc.l

gcc lex.yy.c -o abc.exe

gcc lex.yy。c - o abc.exe

./abc.exe

。/ abc.exe

#1


87  

As for today (2011-04-05, updated 2017-11-29) you will need the lastest versions of:

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

  1. flex-2.5.4a-1.exe

    flex - 2.5.4a - 1. - exe

  2. bison-2.4.1-setup.exe

    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.

    在此之后,在您的首选项目录中进行完整的安装,而不使用名称空间。我建议C:\ GnuWin32。不要在默认情况下安装它(C:\程序文件(x86)\GnuWin32),因为bison在目录名中存在空格问题,而不是插入括号。

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

    另外,考虑在缺省目录中安装devc - cpp (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:
    如何在Windows上编译LEX/YACC文件?
    If the figure is not in good resolution, you can see a step-by-step here.

    在此之后,设置PATH变量以包含gcc的bin目录(在C:\Dev-Cpp\bin)和flex\bison(在C:\GnuWin32\bin)中。要做到这一点,请复制这个:;C:\Dev-Cpp\bin;C:\GnuWin32\bin,并将其附加到PATH变量的末尾,由这个图来定义:如果这个数字没有很好的分辨率,您可以在这里一步一步地看到。

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

    打开一个提示,cd到你的目录。l“和”。y"是,然后用:

    1. flex hello.l
    2. flex hello.l
    3. bison -dy hello.y
    4. 野牛dy hello.y
    5. gcc lex.yy.c y.tab.c -o hello.exe
    6. gcc lex.yy。c y.tab。c - o hello . exe

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

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

你将能够运行这个程序。我做了一个简单测试的来源(臭名昭著的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".

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

快乐的黑客。

#2


11  

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://ftp.gnu.org/old-gnu/ gnu.2 /src/flex-2.5.4.tar.gz的Gnu ftp服务器上得到它。

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"!

由于不可避免地会被推荐,我警告不要使用Bison。Bison最初是由Robert Corbett写的,他后来写了Byacc,他公开表示当时他还不知道或者不知道他在做什么。不幸的是,被年幼无知,他现在在GPL下发布GPL的粉丝们把它作为生活的答案的问题,即使自己的作者基本上是说它应该被认为是一个测试产品的复杂的推理,但GPL球迷,byacc执照时没有足够的限制成为“免费”!

#3


9  

There are ports of flex and bison for windows here: http://gnuwin32.sourceforge.net/

这里有flex和bison的端口:http://gnuwin32 sourceforge.net/。

flex is the free implementation of lex. bison is the free implementation of yacc.

flex是lex的免费实现。bison是yacc的免费实现。

#4


7  

There's always Cygwin.

总是有Cygwin。

#5


7  

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

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

#6


3  

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

为windows(使用Unix工具)进行Git的完整安装,bison和flex将会出现在bin文件夹中。

#7


1  

Also worth noting that WinFlexBison has been packaged for the Chocolatey package manager. Install that and then go:

同样值得注意的是,WinFlexBison已经被包装为巧克力包装经理。安装后就可以了:

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。

#8


-1  

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

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

we write

我们写

flex abc.l

flex abc.l

gcc lex.yy.c -o abc.exe

gcc lex.yy。c - o abc.exe

./abc.exe

。/ abc.exe