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 python files in the package jay:

Additionally, you'll find

  • samples, a folder containing a couple of sample Jay files. You will also need to write your own Jay files for testing.

    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)

    pyton jay/prettyPrint.py samples/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 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 fourth 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/linux/class/cs365/(your id)/proj1
    

    I will grade your project by running it against a collection of test files.

    DUE: Friday, Feb 12, 5:00 pm.


    Thomas VanDrunen
    Last modified: Thu Jan 7 13:28:55 CST 2016