How to compile LEX/YACC files on Windows?
Asked Answered
D

9

66

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?

Drawtube answered 28/3, 2011 at 7:47 Comment(0)
Y
120

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.

Younglove answered 5/4, 2011 at 0:41 Comment(10)
bison -dy hello.y does not produce any file. Its just showing some codes in the prompt. How do i create the y.tab.c ?Drawtube
@Pramodh That is strange... What codes it is showing? Error codes? Or some source code?Younglove
I did this procedure in 2 different machines now, and both work. Are you sure you are following all of it step-by-step? In the command prompt screenshot above, do you got the same screen for flex (no errors)? When you press <enter> key after typing the bison -dy hello.y command, what exactly you got? If you give exact details maybe we can figure out the problem. If you think that is too much for a comment, maybe you could ask a question with the error.Younglove
Please help me for this too https://mcmap.net/q/297374/-how-to-run-lex-fileDrawtube
Thank you EJP for your input and downvote. Good critics is what make this site great. Although in this case I may disagree by the simple fact that the picture referred makes the answer as a whole much more beautiful to the eye and easy to understand. My best.Younglove
This is an excellent reply, thank you. I am having an issue though when I compile the lex.yy.c file. I am getting errors they can be seen here: pastebin.com/fV3sgSX6 I have checked everything twice....Russophobe
It is probably some version wrong. Compilers evolved, and you are probably using something more advanced and generating wrong C code for the example. There is a change I can make in the answer to help you, but I need to check in other environments. What is your OS? Check back the edition in the answer.Younglove
I was getting the warning hello.l: In function 'main': hello.l:19:4: warning: implicit declaration of function 'yyparse'; did you mean 'yymore'? [-Wimplicit-function-declaration] yyparse(); ^~~~~~~ yymore so I added the declaration int yyparse(void); in the head of hello.l but this seems to be just a dirty hack, how should it be corrected?Echt
Just as a heads up, this source works on Linux too, and it was a very short and helpful sample program.Nice
Thanks jrh. You discovered my secret! :) It was first originally written for linux, then tested and debugged on windows and linux.Younglove
R
13

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

Rattlebrained answered 28/3, 2011 at 8:22 Comment(2)
Hey Jerry, do you have a link to Corbett stating that at the time he didn't really know or understand what he was doing? Thanks.Aesir
@RayHulha: sorry, but I don't have a link handy.Rattlebrained
D
10

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.

Dildo answered 28/3, 2011 at 7:53 Comment(0)
F
8

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

Flareup answered 27/10, 2011 at 20:28 Comment(0)
R
7

There's always Cygwin.

Retene answered 28/3, 2011 at 7:53 Comment(0)
M
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.

Maintop answered 5/2, 2015 at 10:23 Comment(0)
C
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.

Crosspatch answered 31/10, 2017 at 11:25 Comment(0)
E
2

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
Esbjerg answered 14/4, 2016 at 10:21 Comment(2)
Easiest if you like hundreds of megabytes of disk space to be consumed. Not otherwise.Gormandize
this is actually the way I was advised to do it in my PhD classButterflies
S
0

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

Subordinate answered 7/3, 2019 at 17:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.