Project 2: Pretty-printing

In this project you will practice working with syntax and you will write your first visitor for this course. The program you will finish will take a Jay program as input and "pretty print" it, that is print it with conventional spacing and indentation.

1. Setup

In a directory for this class and project, copy and untar the files for this project

cp ~tvandrun/Public/cs365/proj2.tar .
tar xvf proj2.tar

The subdirectory jay contains the subdirectories parser, abssyntree, and astvisitor. The only file you will need to modify is jay/astvisitor/PrettyPrintingVisitor.java, but you will want to be familiar with the classes in jay/abssyntree.

The subdirectory samples contains two sample Jay files. You should also write some of your own for your testing.

2. Your task

Your task is to finish the class PrettyPrintingVisitor.java. It should walk through the Jay abstract syntax tree and print the program to the screen. Some specifications for the pretty printing:

For reference, this is how Nesty.jay should look:

public class Nesty { 
    public static void main(String[] args) {
        int i, j, k;
        int x;
        i = 0;
        if (i < 0) 
            if (i > 5) 
                k = 7;
            else 
                j = 1;
        while (i < 20) {
            j = 0;
            while (j < 20) {
                k = 0;
                while (k < 20) 
                    if (k / 2 < 10) {
                        System.out.println(k * j * i);
                        k = k + 1;
                    }
                    else if (k == 15) {
                        System.out.println(k);
                        k = k + 1;
                    }
                    else 
                        k = k + 1;
                j = j + 1;
            }
            i = i + 1;
        }
    }
}

To run your program, type (for example)

java jay.PrettyPrint Nesty.jay

3. Suggestions

Part of me wants to write "this isn't as hard as it sounds." Another part wants to write "this is trickier than you think." I suppose it depends on how hard you think it is.

Most of pretty printing isn't so bad. There are two sources of difficulty. First is getting used to thinking in terms of visitors. Learn it well now, and it will payoff big later in the semester. Part of the purpose of this assignment is to give you a relatively straightforward problem.

The other source of difficulty is dealing with the details of avoiding cascading ifs.

Since Jay is a strict subset of Java, all Jay programs are runnable Java programs. I have a tool that can be used to run Jay programs as Java programs. You can find it at ~tvandrun/Public/cs365/util/jayasjava.py. To run it, type (for example)

./jayasjava.py Nesty.jay

4. Turn-in

Copy your PrettyPrintingVisitor.java to

/cslab/class/cs365/turnin/proj2/xxxxxx

where "xxxxxx" is [lily|chet|christopher|tim|david|ben|caleb]. I will grade your project by running it against a collection of test files.

DUE: Monday, Feb 1, 5:00 pm.


Thomas VanDrunen
Last modified: Wed Jan 20 13:47:59 CST 2010