Project 1: 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. This project also will ease you into Python programming.

1. Setup

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

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

In this you'll find the following things:

2. Your task

Your task is to finish the class PrettyPrinterVisitor. 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)

./prettyPrint.py samples/Nesty.jay

(This assumes that prettyPrinter.py is set with executable permissions. If it does, you'll see an x in an appropriate place when you do an ls -l:

tvandrun@milner2:solution$ ls -l prettyPrint.py 
-rwxr--r-- 1 tvandrun tvandrun 323 Jan 29 21:41 prettyPrint.py
   ^
   |

If it doesn't have executable permissions, you can set it with chmod u+x prettyPrint.py.)

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 four sources of difficulty. First is getting used to Python in general; second is getting used to the AST classes; third is thinking in terms of visitors. Learn these 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 to try Python, ASTs, and visitors out.

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

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.all/ubuntu/cs365/turnin/xxxxxx/proj1

where "xxxxxx" is [elliot|payton|davidemmanuel|nathan|gill|brandon|sean|simon]. I will grade your project by running it against a collection of test files.

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


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