Project 2: Pretty-printing

In this project, you will write a operation on parse trees. Given a Jay program represented as a parse tree, your program shall print it to the screen back as a Jay program, but "pretty-printed," that is, with conventional spacing and indentation.

1. Set up

The parser, syntax tree classes, and generic visitors have already been generated for you from the Jay grammar, and they are archived in a tar file. Copy the file from the class directory and untar it.

cp /cslab/class/cs365/proj2.tar .
tar xvf jay.tar

A driver program is also provided in Main.java. You will need to uncomment two lines of this file before you test your code, but you will not be making any other changes.

2. Syntax-printing

To help you understand JTB's syntax trees and the visitor pattern a little better, I've provided a feature called "syntax-printing." This prints out the parse tree, showing what each node (object) in the tree is, and uses indentation to show the structure. For example, syntax-printing the following Jay program

void main() { int x; x = 2 + 3; System.out.println(x); }

produces

Program
   NodeToken: void
   NodeToken: main
   NodeToken: (
   NodeToken: )
   NodeToken: {
   NodeList
      NodeListOptional
         Declaration
            Type
               NodeToken: int
            Identifiers
               NodeToken: x
               NodeListOptional
            NodeToken: ;
   Statements
      NodeListOptional
         Statement
            Assignment
               NodeToken: x
               NodeToken: =
               Expression
                  Conjunction
                     Relation
                        Addition
                           Term
                              Negation
                                 NodeOptional
                                 Factor
                                    NodeToken: 2
                              NodeListOptional
                           NodeListOptional
                              NodeSequence
                                 AdditionOperator
                                    NodeToken: +
                                 Term
                                    Negation
                                       NodeOptional
                                       Factor
                                          NodeToken: 3
                                    NodeListOptional
                        NodeOptional
                     NodeListOptional
                  NodeListOptional
               NodeToken: ;
         Statement
            PrintStatement
               NodeToken: System.out.println
               NodeToken: (
               Expression
                  Conjunction
                     Relation
                        Addition
                           Term
                              Negation
                                 NodeOptional
                                 Factor
                                    NodeToken: x
                              NodeListOptional
                           NodeListOptional
                        NodeOptional
                     NodeListOptional
                  NodeListOptional
               NodeToken: )
               NodeToken: ;
   NodeToken: }
   NodeToken: 

This is to help you in two ways:

3. Pretty-Printing

Pretty-printing refers to formating a file so that it is easily readable for humans. Write a class PrettyPrintingVisitor to implement Visitor or to extend DepthFirstVisitor which will traverse a parse tree and print to the screen equivalent Jay source code with readable spacing. For example, the program above should appear as

void main() {
    int x;
    x = 2 + 3;
    System.out.println(x);
}

I will be using diff to grade your projects (I will diff the output of your code with what I think the output should be), so we'll need to set a standard guideline for what the format should be. Follow the style I have indicated above (for example, opening curly brace on the same line as whatever introduces the block, spaces between operands and operators...). Use the same indentation that the default version of Xemacs in the lab uses. Ask if you have any questions on formatting and style.

You can find two Jay examples in the directory given (add.jay and euclid.java). You are also encouraged to write your own examples and to share them with your classmates.

4. Testing

As mentioned above, I have included a driver program. Uncomment the lines indicated to test your pretty-printer. The command

java Main add.jay --s

will "syntax-print" the file add.jay, and you can pretty-print it by

java Main add.jay --p

5. Turn in

To turn in this project, please copy your PrettyPrintingVisitor.java to the turn-in directory I will prepare for you:

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

where "xxxxxx" is [andy|david|tyler|yelemis]. I will grade your project by running it against a collection of test files.

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


Thomas VanDrunen
Last modified: Tue Jan 29 15:10:20 CST 2008