Life Of Navin

Random Musings, Random Bullshit.


LEX and YACC on Windows

We've just started compiler design as part of the Language Translators subject at college and a practical aspect of it involves using LEX and YACC for Grammar Lexical Analysis and Parser Generation. Since LEX and YACC are both originally developed for the *nix OSes (read: Linux), there was a sudden rush of classmates downloading linux distros (Fedora being the default choice as it's used on lab systems). In many cases, people actually downloaded entire distro DVDs just for LEX/YACC!

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
Link2) 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

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

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

[0-9]*[02468]"\n" { return EVEN; }
[0-9]*[13579]"\n" { return ODD; }
int main(void)
return 0;

int yywrap(void)
return 0;

int yyerror(void)

Similarly create a YACC file as well, say "exp.y" and edit it as follows

%token EVEN ODD


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

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 -o nameofexecutable.exe" )


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.


M Satish said...

sir I am studying compiler design and i am be very much thanking for your help with lex and yacc

Abhinav Raj said...

Nice article bro, I really like your technical articles, but why not just use Cygwin and then build the sources?

Anonymous said...

you can use windows flex and bison with minimal dependencies from

bogdan347 said...

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.

Anonymous said...

Excellent tutorial...

Anonymous said...

Great work.It is very useful for beginners of the compiler design.Thanx

Ramya Dhanaraj said...

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.

pradeep said...

How can you i use lex and yacc in visual c++ 2010?

Anonymous said...

when i try to compile using the command "gcc lex.yy.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

Anonymous said...

I love you man! thanks a lot for keeping it simple!

Anonymous said...

After writing the command
gcc lex.yy.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?

Anonymous said...

i am getting this error too!
error: #include expects "FILENAME" or

Arun M said...

'gcc' is not recognized as an internal or external command,
operable program or batch file.

Anonymous said...

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.

Anonymous said...

bt what was the final command...i cant compile and run flex files.and rest of part i didn't understand plz clarify it

Shashank said...

Thanks man this article saved me from a lot of trouble.

dabal mahara said...

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'
/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

Iqra Sam said...

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..........

Jayram Gaonkar said...

gcc comand is not working

silvestein said...

A very well written article. Keep up the good work,Sir!

SARITA said...




Finally after all these years, here's to the beginning of what was there, what is there and hopefully what will remain!! So here are my thoughts & words -Online!!