Problem Solving Lab #2

Problem Solving Lab #2
Using flex and bison

This lab is being done in order to help you learn various topics discussed in lecture. For attendance, you will all be asked to submit to try. You may work in pairs. Since this lab is for your learning and different people work at different paces, it's quite probable that you will not finish all activities in the two hours alotted for labs. You are not required to do anything you don't finish, although you may want to try some of the exercises to increase your understanding of the material.


Flex and Bison Sites and Information

It's always helpful to know where help is. The Unix man pages for flex and bison are fairly complete. With that said, you can also find information at:

dinosaur.compilertools.net
The on-line Bison manual


Part I - Flex

Here is the flex file for a very simple calculator. You can compile the file with the following commands:

% flex calc.lex; gcc -o calc lex.yy.c

You can then run the calculator with the 'calc' command and can quit out of it using ctrl-d.

Do the following:

  1. Figure out what the calculator does with things not described by the regular expressions.
  2. Add division and subtraction to the calculator.
  3. Keep track of the number of lines that have been input. Make sure that you set the lineNumber variable in yylval.ti for each token and print out the line count when you print out variable information.
  4. Extend the variables to handle longer variable names. The variables must start with an alphabetic character or '_' character and may be followed by 0 or more alphabetic/numerical/'_'/'-' characters.
  5. Add floating point numbers to the input. For this exercise, a floating point number is a sequence of 0 or more digits followed by a '.' character and 0 or more digits. One side of the number must have digits. The print statement for real numbers should be: printf("FLOAT: %0.0f\n", yylval.ti.value);
  6. (Extra) Add sqrt
  7. (Extra) Allow floating point numbers to be of the form 1.e-3


Part II - Bison


Submission

Each of you should submit (from your own account) to try an empty file called: here.txt. In order to create the file, type:

% touch here.txt

You will be told the command to use for submitting the file in your lab.

Each of you should submit (from your own account) a file called reflections.txt after the lab is over. The goal of the file is for you to reflect on what you have learned in the lab, how far you got in the lab, what you had problems with, what I could do better next time, what you would have liked to do, and to give me feedback about the lab.

In addition, you are required to fix the problem with the grammar in the bison file without using the %left or %right bison directives.

In order to submit these files, type:

% try jdb-grd project22-1 reflections.txt calc.y

These files will be due this Thur. at midnight with the late deadline on Friday at midnight.