Archive for March, 2008

Lex and Yacc(Bison) in Windows

Posted on March 16, 2008. Filed under: Computers | Tags: , , , |

This post is for all those guys and babes out there who could’nt figure out how to compile their lex and yacc programs in windows. Students in my college install a whole Linux operating system to do their compiler lab practices. Why do it the geeky Linux way if it can be done under Windows? Also, why waste your hard disk space and time(for loading the Linux OS) to do such a simple task.

To begin with, you must download the files below first(around 400KB in total). All the files should be in the same directory(flex.exe is standalone but bison.exe is not). (Sorry for the trouble guys!!! The link is now corrected)

Another requirement is that you must have a c/c++ compiler(like Turbo C++ ) installed in your system. I believe everyone who has reached compiler level programming will have a c/c++ compiler installed in their system(duh!).

Now you can start your work.

1. Create a lex program and save it(like ‘x.l’ or ‘x.txt’)

2. Reach your directory through command prompt.

3. Call flex by “flex <filename>” (eg: “flex x.l”)

4. If your lex input file doesnt have any errors, a file name “lex.yy.c” will be generated.

5. I suggest you rename “lex.yy.c” to some other more sensible name like “lex.c”, because double extensions is not a good idea in windows environment. Move the file to the folder of your compiler.

6 . Call the compiler and input lex.c to it (eg: “tcc lex.c”, where tcc is the Turbo C compiler; use any C/C++ compiler you have). If what you have is a more sophisticated environment like visual studio, you have to do the normal procedure to get a program to compile. If you use Turbo C, an object file “lex.obj’ and the executable “lex.exe” will be generated if there are no compilation problems

7. Thats it. Now just run lex.exe. Its your lexical analyser.

It is very similar to the way you do it in linux.

All the seven steps look like a complicated procedure. I created a batch file(.bat files similar to shell scrpits in linux) , to do the whole job from calling flex to get the lex.exe up and running. My batch file(rflex.bat) looks like below.

flex input.txt
ren lex.yy.c lex.c
copy lex.c f:\tc\bin\
cd tc
cd bin
tcc lex.c
cd “Compiler lab”

del lex.yy.c
del lex.c

And i called this batch file from “i:\Compiler lab\” command prompt, the place where my flex .exe is stored. The path of my compiler is “f:\tc\bin\tcc.exe “. The batch file will get much smaller, if your compiler’s path is included in system paths or environmental paths or variables. By the way, your lexical analyser will stop after you press ctrl+z(^z character) and press enter(similar to ctrl+d in linux).

So you will have to modify the above batch file if want to use it in your environment. After successfully making the batch file all you have to do is make your input file and call the batch file from your flex and bison folder.

To do yacc or bison, you have to do a similar procedure. I leave that part to you. If you can’t figure it out, just post a comment requesting how to do it. I have successfully created a batch for that too. For more information, click the link below.

Read Full Post | Make a Comment ( 45 so far )

Liked it here?
Why not try sites on the blogroll...