CSE 413 Spring 2000

Assignment 5 -- Compiler I/O

Due at the beginning of lecture, Wednesday, May 10.

Overview

This is the first of a sequence of assignments that will create a small, but complete compiler.  You are free to work with a partner on this project - in fact, we encourage you to do so.  However, if you do work with a partner, you should plan on working with the same person for the entire compiler project.

Written problems

Here are a few problems involving regular expressions and finite automata.  These are a warmup for the next part of the compiler, the scanner.  Do these problems individually, even if you have a partner for the programming part. 

  1. Describe the languages (sets of strings) generated by the following regular expressions.
    1. 0(0|1)*0
    2. ((\epsilon|0)1*)*  [\epsilon stands for the empty string]
    3. 0*10*10*10*
  2. Give regular expressions that will generate the following sequences.
    1. All strings containing a's, b's, and c's where the first a preceeds the first b.
    2. All strings of a's and b's with an even number of a's.
    3. All strings of lower-case letters that contain the 5 vowels (aeiou) in alphabetical order (with, of course, possibly other letters in between and before and after).
  3. Give a regular expression (or set of expressions) for C-style comments.  That is, a /*, followed by anything except */, followed by */.  You can restrict the alphabet to lower-case letters, spaces, *, and /.
  4. Give the diagram of a finite automata (state diagram) that recognizes C-style comments as defined by the regular expressions in your answer to the previous question.

Programming project - Compiler I/O

For this part of the assignment, implement and test a Java class EchoIO that will handle input and output for the compiler.  Here is a skeleton for this class.  Some details are given below.

public classs EchoIO {

   // constructors:
   
   // open input and output files whose names are given as arguments
   public EchoIO(String inFile, String outFile) { ... }
   
   // open named input file.  open an output file with the same name,
   // but with an extension of ".asm"
   public EchoIO(String inFile) { ... }
   
   // This one is optional:
   // use a FileDialog to select an input file and open it.  open an
   // output file with the same but with an extension of ".asm"
   public EchoIO() { ... }
   
   // methods:
   
   // return the next line from the input file; throw an exception if
   // no more input (end of file).  the input line is automatically
   // printed to the output file if echoing is true
   public String readLine() throws IOException { ... }
   
   // print line to output file
   public void println(String line) { ... }
   
   // properties and methods to set/get their values:
   
   private boolean echoing;	// true when echoing input to output
   public void setEchoing(boolean val) { ...}
   public boolean getEchoing() { ... }
   
   private String echoPrefix;	// string to concatenate to front of
                                // lines automatically echoed to output
   public void setEchoPrefix(String val) { ... }
   public String getEchoPrefix() { ... }
   
   // additional private instance variables and methods as needed
   ...
   
   // test program
   // create an EchoIO object and test it.  main should accept 1 or 2
   // filename arguments, or no arguments if you've implemented the
   // optional 0-argument constructor
   public static void main(string [] args) { ... }
}

 An instance of EchoIO will manage the input and output files for the compiler.  When the compiler starts up, it will create an instance of EchoIO and the EchoIO object's constructor should open the input and output files.  The scanner can then use the readLine() method to read the source program.  Similarly, the code generator will use the println() method to write the x86 assembly code to the output file.  EchoIO's readLine() and println() methods should call the readLine() and println() methods of the underlying file streams to do the actual input and output.

The input is just an ordinary text file.  The output will also be a text file whose principle contents will be lines of generated x86 assembly-language code.  But it is also convenient if lines from the input file (the source code) appear interspersed in the assembly language code as comments.

The echoing and echoPrefix variables control whether the input lines are copied to the output file as they are read.  If echoing is true, then each input line should be written to the output file when it is read, with the echoPrefix string concatenated to the front of it (this will allow us to begin each echoed line in the output file with an assembly-language comment symbol so the assembler won't attempt to process it).  The set and get methods for these properties can be used to turn echoing on and off and change the prefix string.

EchoIO is required to have two constructors, and a third is optional.  The two-argument constructor parameters are the names of the input and output files. The single-argument constructor should treat its argument as the name of the input file.  It should create the output file name by taking the input file name (say test.txt) and replacing the extension with ".asm" (text.asm).  Optionally, you can add a third, zero-argument constructor that allows the user to pick the input file with a dialog box.  If something goes wrong when attempting to create the file(s), the constructor should throw an appropriate exception.  (You may need to add a throws clause to the constructor declarations).

Class EchoIO should include its own test program (method main).  The test program should accept one or two file names as command line arguments and use the appropriate constructors to create an EchoIO object.  If you implemented the optional 0-argument constructor, then your test program should be runnable with no arguments, and should call the 0-argument constructor to open the files.  Once the files are open, the test program should verify that the input and output methods of EchoIO work properly, both with and without automatic echoing of input lines.

Project Turnin

Turn in your program electronically using the links below.

Hand in the online receipt along with your written problems in class on May 10.

Addendum 5/7/00

After this assignment was originally handed out, a different way of handling the constructors for class EchoIO was suggested.  It simplifies things if EchoIO has a constructor that accepts an array of strings and opens the correct input and output files depending on whether there are 1 or 2 (or, optionally, 0) elements in the array.  So, if you wish, you can implement the following constructor instead of the ones described above:

class EchoIO {
   ...
   // Construct an EchoIO object for files specified by args.
   // If args has two elements, open args[1] for input and args[2] for output
   // If args has one element, open args[1] for input and open a file for output
   // with the same filename as args[1] but with an extension of ".asm"
   // (optional part of assignment): If args has no elements, use a FileDialog
   // to select the input file, then open a file for output with the same filename
   // but with an extension of ".asm"

   public EchoIO(String[] args) { ... }
   ...
}

In addition to your turnin receipt and written problems, please hand in some short test output demonstrating that your program works correctly.