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.
Copy and untar the framework for this project.
cp ~tvandrun/Public/cs365/proj4.tar tar xvf proj4.tar
The directories for this project contain
jay
package, containing most of the work we have
done on Jay
jayjay
package, containing the parser, abstract syntax,
and visitors for JayJay, including a skeleton for JayJay2JayVisitor
,
which you will write.
Count.jayjay
, a sample JayJay program.
The main program for JayJay (jayjay.JayJay
)
reads in a JayJay file, produces an abstract syntax representation for it,
and then applies a JayJay2JayVisitor
on the tree,
to produce a corresponding Jay
program.
It then uses the Jay interpreter from the jay
package
to execute that program.
Read jayjay/Jay.java
and make sure you understand the big picture.
Your task, then, is to write the JayJay2JayVisitor.java
.
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.
The other option would be to skip the text and the Jay parser
and for your visitor directly to produce Jay abstract syntax trees---that is,
it would instantiate the classes in jay.abssyntree
,
and its result would be a jay.abssyntree.Program
.
This project is set up so you may choose either option.
The jayjay
package has an interface
TranslationResult
with classes
TextResult
and AbsSynTreeResult
.
Your JayJay2JayVisitor
needs to
return a TranslationResult
, so
you can return either of the classes, depending on which option you
choose.
The jayjay.JayJay
main method is
coded to handle either case.
I don't think either option is inherently any easier or harder; they each have their advantages and liabilites. Here's a quick comparision:
Choosing the text option will mean that your visitor
will look somewhat like your pretty-printer from project 2.
In either case, you are going to output Jay code.
The difference is that this time it doesn't have to be pretty
(just correct).
Moreover, you can use jayjay.astvisitor.UglyPrinter
(see below) as a model.
The text option also makes it easier to visualize your output, for
debugging purposes.
The downside of the text option is that when you make a mistake
in what you output, you won't know about it until the Jay parser
rejects it.
This will make error tracing a little difficult.
The main advantage of the abstract syntax tree option is
that in order to put Jay constructs together, you need to use
the jay.abssyntree
classes correctly,
and your program won't compile if you dont.
(So, if your visitor is producing incorrect Jay, with the
text option you won't know until the Jay parser throws an exception;
in the abstract syntax tree option, you'll know immediately
when Eclipse draws a red squiggle under part of your code.)
One disadvantage of the abstract syntax tree option is
that your JayJay2JayVisitor
will
need to work with classes from both the jayjay.abssyntree
package and the jay.abssyntree
package,
which have a lot classes with the same name.
For example, the same file will have references to
jay.abssyntree.Program
and
jayjay.abssyntree.Program
The project comes with several tools to help in your testing etc.
java jay.Jay SomeJayProgram.jay
and java jay.TypeCheck SomeJayProgram.jay
jayjay.astvisitor.UglyPrinter
.
It is merely a way to print out a piece of JayJay for debugging purposes.
For example, if can't figure out what one of your visit methods
is getting as a parameter, instantiate UglyPrinter
to print it to the screen:
public void visit(Conditional n) { (new UglyPrinter()).visit(n); ... }
java jayjay.TypeCheck SomeProgram.jayjay
jayasjava.py
and jayjayasjava.py
which execute Jay and JayJay programs as if they were Java programs.
These are not in the tar file; you can find them in
~tvandrun/Public/cs365/util
Moreover, the program jayjay.JayJay
has some options that
may be useful to you as you test the program:
Usage: java jayjay.JayJay [options] filename Options and filename may appear in any order. In case of conflict, later options override earlier. Only one file will be processed, so the last thing on the line not recognized as an option will be treated as the filename. Options are: --pjj (ugly-)print JayJay code --pj print (ugly) Jay output of JayJay-to-Jay translation --ppj pretty-print resulting Jay code --no-tc supress type-checking of resulting Jay code --no-x supress execution of resulting Jay code --h or -h display this help message Notes on options: Printing raw Jay output is possible only if the JayJay-to-Jay translator returns a TextResult. If a TextResult is returned, then pretty-printing Jay is possible only if translation result parses correctly. Type-checking and execution are on by default, must be supressed if unwanted. Jay code must by type-checked before execution; supressing type-checking also surpresses execution.
Copy your JayJay2JayVisitor.java
to
/cslab/class/cs365/turnin/proj4/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: Wednesday, Feb 17, 5:00 pm.