As for today (2011-04-05, updated 2017-11-29) you will need the lastest versions of:
flex-2.5.4a-1.exe
bison-2.4.1-setup.exe
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.
Also, consider installing Dev-CPP in the default directory (C:Dev-Cpp
)
After that, set the PATH variable to include the bin directories of gcc
(in C:Dev-Cppin
) and flexison
(in C:GnuWin32in
). To do that, copy this: ;C:Dev-Cppin;C:GnuWin32in
and append it to the end of the PATH
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
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")"
" { return HI; }
("tchau"|"bye")"
" { return BYE; }
. { yyerror("Unknown char"); }
%%
int main(void)
{
yyparse();
return 0;
}
int yywrap(void)
{
return 0;
}
int yyerror(char *errormsg)
{
fprintf(stderr, "%s
", 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
"); }
;
bye:
BYE { printf("Bye World
"); 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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…