NOTE: When people say they use LEX and YACC, almost always, what they mean is FLEX (Which is an alternative to Lex) and BISON (which is part of the GNU project) /BYACC respectively.
Sticking with the "When in doubt, try another hole" philosophy, I just googled around a little and came up with a much easier solution.
Here's how to set up FLEX and BISON on Windows systems
1) Download the Windows version of FLEX
2) Download the Windows version of BISON
3) Download a C/C++ Compiler (If you say TurboC, I'll reach out through the monitor and thump your head)... I recommend DevCPP or Code::Blocks
4) Install all 3. It's recommended to install in folders WITHOUT spaces in their names. I use 'C:\GNUWin32' for FLEX and BISON and 'C:\CodeBlocks' for Code::Blocks
5) Now add the BIN folders of both folders into your PATH variable. Incase you don't know how to go about this, see how to do it on Windows XP ,Windows Vista and Windows 7. You will have to add ';C:\GNUWin32\bin;C:\CodeBlocks\bin' to the end of your PATH
6) Open up a CMD prompt and type in the following
C:\flex --version
flex version 2.5.4
C:\>bison --version
bison (GNU Bison) 2.4.1
Written by Robert Corbett and Richard Stallman.
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
C:\>gcc --version
gcc (GCC) 3.4.2 (mingw-special)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
If you do not get the output as above, restart your system and try again.
That's it! You now have a Bison and Flex set up! Now for some coding! :)
Create a Lex file, let's say "exp.l" and open it up in your favorite text editor (read: Notepad++)
%{
#include
#include "y.tab.h"
%}
%%
[0-9]*[02468]"\n" { return EVEN; }
[0-9]*[13579]"\n" { return ODD; }
%%
int main(void)
{
yyparse();
return 0;
}
int yywrap(void)
{
return 0;
}
int yyerror(void)
{
printf("Error\n");
exit(1);
}
Similarly create a YACC file as well, say "exp.y" and edit it as follows
%token EVEN ODD
%%
input:
EVEN { printf("EVEN NUMBER\n"); return 0; }
|ODD { printf("ODD NUMBER\n"); return 0; }
;
Now in the CMD prompt, navigate to the folder where the exp.l and exp.y files are and run the following commands:
C:\LT\example>flex exp.l
C:\LT\example>bison -dy exp.y
C:\LT\example>gcc lex.yy.c y.tab.c
At each step, assuming all is well, you won't get any output. Note that the final step compiles the C files generated by FLEX and BISON. And generates a executable named a.exe (To use a different name, change last command to "gcc lex.yy.c y.tab.c -o nameofexecutable.exe" )
C:\LT\example>a.exe
666
EVEN NUMBER
C:\LT\example>a.exe
667
ODD NUMBER
Now just run the program (through the command prompt itself). For and even number input, it prints out "EVEN NUMBER" and exits and "ODD NUMBER" is printed for odd numbers. A simple example, but I guess it gets the point through!
Enjoy! :)
PS The GnuWin project is a superawesome project which "provides ports of tools with a GNU or similar open source license, to Windows". The number of packages that they've ported to Windows is amazing. Do check it out.
PPS Yes, I'm a FOSS/Linux enthusiast, but that doesn't mean I have to praise Linux all day long! Projects like GNUWin, which take code to the masses, are nothing short of brilliant! Linux distributions have so many advantages over Windows for any real developer, but I honestly believe every developer has the right to "choose" his/her development OS and environment, be it PC, Linux or Mac. Do try out Linux distros as well (infact, I'd highly recommend you try a few before deciding which suits you best! Most distros are free!), but don't get dragged down by an environment! An OS is simply a tool to enable you, the developer, to work your magic! :)
Update: Here's a Zip file of FLEX, BISON and the Source Code used in this post.
21 comments :
sir I am studying compiler design and i am be very much thanking for your help with lex and yacc
Nice article bro, I really like your technical articles, but why not just use Cygwin and then build the sources?
you can use windows flex and bison with minimal dependencies from http://sourceforge.net/projects/winflexbison/
Very nice piece of technical article. I like it very much mainly because:
1. it's short, simple and addresses the issue stated in its title (thumbs up for that)
2. it's exactly what I needed :D
Anyway, I take the liberty to add the following: for those of you who are using GCC "4.5.0 20100311 (experimental)" (like me) you may want to add
%{
#include <stdio.h>
#include <stdlib.h>
%}
to exp.y, right before %token EVEN ODD in order to have an error free compilation.
Excellent tutorial...
Great work.It is very useful for beginners of the compiler design.Thanx
i have installed the flex,bison,dev-cpp in windows xp. icopied the progrm from the example given and saved as lex.l and kkk.y. but while running in command prompt it shows the error of flex:can't open lex.l and also same for bison.plz clarify me.
How can you i use lex and yacc in visual c++ 2010?
when i try to compile using the command "gcc lex.yy.c y.tab.c" it shows the error" gcc is not recognized as an internal and external command "what can i do in order to inorder to compile
I love you man! thanks a lot for keeping it simple!
After writing the command
gcc lex.yy.c y.tab.c -o exp.exe
I am getting the following error.
exp.1:2:10: #include expects "FILENAME" or
exp.1:25:3: warning: no newine at end of file
Can anyone help me with this?
i am getting this error too!
error: #include expects "FILENAME" or
'gcc' is not recognized as an internal or external command,
operable program or batch file.
For those of you saying "'gcc' is not recognized as an internal or external command,
operable program or batch file."
Please READ instructions! You need to download a compiler in Step 3, install it in Step 4, and add bin folders to your system path in Step 5...
If you need help with installing a compiler, try MinGW out. Or maybe try TDM-GCC instead.
bt what was the final command...i cant compile and run flex files.and rest of part i didn't understand plz clarify it
Thanks man this article saved me from a lot of trouble.
when i want to compile lex.yy.c following error is being generated. I have dev-cpp 5.11 TDM-GCC 4.9.2 with path variables set in windows 8. plz help how to fix this error
C:\prolex>gcc lex.yy.c
C:\Users\dsm\AppData\Local\Temp\ccyIiVSf.o:lex.yy.c:(.text+0x3c5): undefined reference to `yywrap'
C:\Users\dsm\AppData\Local\Temp\ccyIiVSf.o:lex.yy.c:(.text+0xc8b): undefined reference to `yywrap'
C:/Dev-Cpp/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/lib/../lib
/libmingw32.a(lib64_libmingw32_a-crt0_c.o): In function `main':
C:/crossdev/src/mingw-w64-v3-git/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
when i m writing in cmd to Compile the Flex program,... flex exp.l the message appears "flex can't open the 'exp.l'"
same message for Bison, What to do???
PLZ Help..........
gcc comand is not working
A very well written article. Keep up the good work,Sir!
GREAT WORK .IT WAS REALLY VERY HELPFUL TO LEARN LEX AND YACC PROGRAM THROUGH THIS BLOG.THANKS.
Post a Comment