bison – How to compile LEX/YACC files on Windows?

bison – How to compile LEX/YACC files on Windows?

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-Cppbin) and flexbison (in C:GnuWin32bin). To do that, copy this: ;C:Dev-Cppbin;C:GnuWin32bin and append it to the end of the PATH variable, defined in the place show by this figure:
    step-by-step
    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

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, %sn, 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 Worldn);   }
        ;
bye:    
        BYE    { printf(Bye Worldn); 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 cant 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.

What you (probably want) are Flex 2.5.4 (some people are now maintaining it and producing newer versions, but IMO theyve 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 itll inevitably be recommended, Ill 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 didnt 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 lifes 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, byaccs license doesnt have enough restrictions to qualify as free!

bison – How to compile LEX/YACC files on Windows?

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.

Leave a Reply

Your email address will not be published.