Well, I guess this is my first Blog entry. I’m at the tail end of the four weeks I’ve had between the end of summer school and the beginning of the Fall semester. I’m working on a CS Masters degree at Washington University in St. Louis.
The goal of this is technical articles, so for the first entry I’m going to throw something out here that I created a couple of years back when I needed to have flex and bison working with Visual C++ v7.0.
On 8 Nov 2003 I wrote the following as a readme file for porting a Cygwin flex/bison example to Visual C++.
This explains how to use flex and bison with Visual C++ v7.0.
I’ve only used this with C. With a little extra work you can probably get it to work with C++ as well. The rest of your source code can be C++, but the flex .yy.c and bison .tab.c files will need to be compiled as C code. That means do not change the file extensions.
You’ll need to have cygwin installed. Go to www.cygwin.org and grab the setup program. I’ve found Cygwin (Unix tools for Windows) to be very helpful.
I’m assuming you have some idea of what flex and bison do and how they work. If you don’t, there are numerous examples on the web.
The developer needs to provide Lex file, file.lex, and bison file, file.y.
The flex tool takes file.lex as input and produces a file called file.lex.yy.c. The bison tool takes file.y as input and produces two files: file.tab.c & file.tab.h.
The file.lex.yy.c and file.tab.c files need to be compiled into object files: file.lex.yy.o and file.tab.o.
Then, a linker can be used to assemble these object files and the rest of your compiled code into an executable.
Yuck. This is a lot of moving parts to keep track of. What is even worse, is neither of these tools were meant to work with one another.
But, that is ok. Because, with a few minutes of work they can.
First, create a blank win32 solution space with win32 support and all other Microsoft supported crap disabled.
Next, find libfl.a static library that is included with the Cygwin installation. This will be kept in /usr/lib or c:\cygwin\usr\lib, depending upon which side of the fence you happen to be standing on. Rename this library to fl.lib and put it in your project directory.
Add this library to the additional dependencies line of the linker input section of the project properties panel.
Add the file.y and file.lex files to your Project. In the properties section add the following information:
command line: c:\cygwin\bin\bison -d $(InputPath)
command line: c:\cygwin\bin\flex -t $(InputPath) > $(InputPath).yy.c
Compile both of these. This will produce the arithmetic.tab.h, arithmetic.tab.c,arithmetic.lex.yy.c files.
Now, add all three of these files to your project.
At the time of writing this, I wasn’t sure how to add an explicit dependency to a c/c++ file in vc70. So, you can make sure that things are built in the proper order by putting file.y and file.lex below the other three files in the Solution Explorer.
I put both of this files in a seperate “lex” folder.
Create one other c++ file called main.cpp. Add a main() function here that looks like the following:
extern “C” int mainline();
int main(int argc, char **argv)
where mainline(); is a function defined in file.y and now is located in file.tab.c.
I have no doubt there are numerous other ways to do this. There may be an easier way of getting the flex library than installing all of Cygwin, but Cygwin is a nice thing to have lying around.
You can find a Microsoft Visual C++ Project here .
As always, you should scan anything you download for viruses.