Project 3: Translating from JayJay to Jay

In class we've seen the language JayJay (Jay extended for extra control structures); we would like to have an interpreter for it, as we had for Jay. But why waste all the work we've done already in writing a Jay interpreter? Since JayJay doesn't actually add any new functionality, we could write a JayJay interpreter merely by writing a translator from JayJay to Jay and slap it on top of the existing Jay interpreter. That's what you'll do in this project.

1. Setup

Copy and untar the framework for this project.

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

2. The main idea

The directories for this project contain

The main program for JayJay (jayjay/jayjay.py) reads in a JayJay file, produces an abstract syntax representation for it, and then applies a TranslatorVisitor on the tree, to produce a corresponding Jay program. It then uses the Jay interpreter from the jay package to execute that program. jayjay/jayjay.py can give you the big picture.

Your task, then, is to write TranslatorVisitor.py.

3. Options

There are two ways to go about this translation task. One option is to have your visitor produce the text of a Jay program (so, its result would be a string). The Jay parser would then be used to parse that text and produce the abstract syntax trees for the Jay program. (One of the given files contains a JayJay uglyPrinterVisitor, which you could use as a model (or more than a model---a source for a big chunk of the code) for producing Jay text.)

The other option would be to skip the text and the Jay parser and for your visitor directly to modify the JayJay syntax tree until it is a Jay syntax tree. This option would require you to produce new AST nodes and replace others. Note that Python duck typing means that JayJay AST classes and Jay AST classes can work together in the same tree; this would not be the case if we were writing in Java.

This project is set up so you may choose either option. If you choose the text-result option, then your resulting Jay program must be stored in an attribute (ie, instance variable) in the visitor called resultText. If this holds a non-None value, then the jayjay program will parse it through the Jay parser to make a Jay AST; otherwise, the original JayJay AST will assumed now to be a Jay AST. Here's the code that does that (precondition: the JayJay AST is stored in ast:

    transVis = TranslatorVisitor()
    transVis.visit(ast)
    if transVis.resultText :
        if prnt :
            print transVis.resultText
        try :
            jAst = jay.parser.parser.parse(transVist.resultText)
        except SyntaxError :
            print "Jay syntax error"
            raise
    else :
        jAst = ast

Postcondition: the Jay AST is stored in ast.

I don't think either option is inherently any easier or harder; they each have their advantages and liabilites. (It was a starker comparison last time I taught this class when this project was in Java.)

4. Tools

The project comes with several tools to help in your testing etc.

Moreover, the program jayjay has some options that may be useful to you as you test the program. The --print flag will print out the Jay program before it runs. The --no-ex will suppress execution of the Jay program.

5. Turn in

Copy your TranslatorVisitor.py 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 19, 5:00 pm.


Thomas VanDrunen
Last modified: Thu Jan 7 13:35:17 CST 2016