CSCI 400

Bison simpleCalc Intro Exercise


  1. Download simpleCalc.y and simpleCalc.l
  2. Create calculator program:
  3. As a convenience, you can use the batch file mbison.bat instead of typing all the above:
  4. Andy Niccolai provided this makefile for *nix.
  5. Aaron LeBahn provided this mbison script for *nix.
  6. Test with valid sentences (e.g., 3+6-4) and invalid sentences.
  7. Read and try to understand simpleCalc.l and simpleCalc.y.  If you have questions, see the slides in the PowerPoint.  More questions  - Ask!
  8. Do the exercises described below.  You can put all the exercises in one file.


Downloads

If you want to use Flex and Bison on your own computer you can find free downloads for Windows at:
    Flex: http://gnuwin32.sourceforge.net/packages/flex.htm
    Bison: http://gnuwin32.sourceforge.net/packages/bison.htm
   gcc: http://sourceforge.net/projects/gcw/

Or do a search to find versions for other OSs.  For Windows environments you will probably need to add flex and bison to your PATH.  This varies based on version of Windows, but for many the Path is available via the Control Panel.  I am not sure the version of gcc above is the same one I used or that is installed in CTLM.  Another option is to download the Dev C++ IDE, which should also install a gcc compiler on your system.  That's what I did originally.


Exercises

Exercise #1:

Once you have gotten the simpleCalc program to run, change it to handle + and * with correct precedence using the grammar with terms and factors presented in chapter 4 of text:
Expr -> Expr + Term
        | Term
Term -> Term * Factor
        | Factor
Factor -> (Expr)
        | NUMBER

Exercise #2:

Now change simpleCalc.l to accept floating point values OR integers.
Details to change simpleCalc.y to accept floating point values (char* will be needed later):
The union belongs in the declaration section of the .y file, above/before the token and type declarations.

Exercise #3:

Update simpleCalc to accept statements like @myVar = 3.4*4
Output will be: myVar = 13.6
Exercise #4:

Update simpleCalc to accept a "program" of expressions.  Example program:

{
@myvar=1+2;
@anotherVar=2*3.5
}

Exercise #5:

Update the program to determine the total value of all expressions in your program.  NOTE: This does not represent an actual calculator or program, but is used to illustrate concepts that will be useful for the homework.

Specific Requirements

This assignment is worth 10 points.


Submission:

Zip your .y and .l files and submit on Blackboard. If you worked with a partner, be sure to include both names in the comments.