Resources:
will create the files dumscript.tab.c and dumscript.tab.h.bison -d dumscript.y
Yacc is very often used together with a scanner generator called Lex, and Bison is very often used together with a scanner generator called Flex. Flex is very similar to Lex.
On Windows machines, you have to download and install them separately. If you are managing your own Windows machine, you can download and then install a current version of Bison for Windows. At the same time, you probably want to download and install Flex for Windows.
Here are some local copies from 2009-08-19:
In (some of) the computer rooms at the university, Bison and Flex have been installed and can be run directly, using the commands bison and flex in a command window. It is a rather old version of Bison, but it seems to be working.
Tillägg 2 oktober 2012:
Bison och Glex finns installerade i T117, som används för de bokade labbtiderna i år, men de har glömt att lägga in sökvägen till dem i PATH-variabeln. Vi måste göra det själva. Klicka därför Start -> Control Panel -> Performance and Maintenance -> System -> Advanced -> Environment Variables. Skapa en omgivningsvariabel PATH och ge den värdet C:\Progra~1\GnuWin32\bin Starta sedan ett nytt kommandofönster, så den nya sökvägen fungerar. |
To build a project that contains Bison or Flex specifications, you can run the commands by hand, and then compile and link the C files, but it is strongly advised that you automate this process as much as you can. A simple script ("batch file") may be enough, but more advanced methods are available.
On Unix-like systems, you would probably use make to automate the build process. You can do similar things in Visual Studio. Here is an introduction on how to use Flex and Bison together with Visual Studio 2005, but it works with 2008 too: Custom building and code generators in Visual Studio 2005 (here is a local copy from 2008-08-26). Unfortunately, this does not work with Visual Studio 2010. There are ways to automate the process there too, but it's much more complicated. But you can use a simple script instead.
The instructions say:
Create a new file folder under the project called "Generated Files". Add the existing file example.parser.c to this folder.Right-click on the project name in Solution Explorer, and chose Add and then New Filter. A "filter" is a folder, apparently. |
In my Windows XP, I had to add the directory C:\Program Files\GnuWin32\bin to the environment variable Path for this to work (click Start -> Control Panel -> Performance and Maintenance -> System -> Advanced -> Environment Variables). If you are already able to run bison from a command prompt (and it works), then you don't need to perform this step. |
With Bison for Windows 2.4.1 it didn't work to add C:\Program Files\GnuWin32\bin to the path, since the space in "Program Files" caused a problem. Instead I had to write C:\Progra~1\GnuWin32\bin. |
You may also have to set the environment variable TEMP to a directory where you have write permissions, such as C:/Temp/ (note the trailing slash). |
Unpack, study the files, make a project, compile, and run it. (You can either look in the file Makefile for commands to give, ur you can set up rules in Visual Studio.)
With a hand-coded parser, it was difficult to handle both assignments and expressions. Let your Yacc grammar handle both, and see how easy it is!
Some more things to do:
In C and in C++, the ?: operator only evaluates one of the expressions expr2 and expr3. If you want, you can instead let your operator evaluate both expr2 and expr3.
Even if you don't send a report by e-mail, we advise that you write down your answers, to facilitate communication and for your own later use.