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.
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
:
jay_ast.py
, which contains the classes
representing Jay abstract syntax trees.
You will need to be familiar with these classes.
ast_values.py
, some code used by
the parser to make the abstract syntax trees.
You can ignore this file in this project
(though it will be important in future projects).
__init__.py
, some file that Python needs
for any project.
parser.py
, the Jay parser (we've seen this in class).
prettyPrint.py
, the "main file".
prettyPrinterVisitor.py
the stub class for the
visitor that will implement the pretty printing operation;
this is the one you'll need to finish.
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.
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:
if (a == 1) x = 5; else if (a == 2) x = 6; else x = 7;not
if (a == 1) x = 5; else if (a == 2) x = 6; else x = 7;
BinaryExpr
s.
One hint:
A BinaryExpr
's op
is an object of
class BinOp
(which is defined in ast_values.py
).
A BinOp
has a field named prec
(for
precedence).
Also in ast_values.py
, you can see the map
binOps
which associates strings like
"*", "+", etc with the BinOp
object standing
for the respective operators.
But... your decision on when to parenthesize something depends
on comparing the precedence of a BinaryExpr
's
operator with the precedence of a that expression's subexpression's
operators, assuming it has a BinaryExpr
as a subexpression.
3 * 4 * 5
shouldn't be transformed into
(3 * 4) * 5
.
else
shouldn't be on the same line as the
closing curly brace of a block in a then clause. That is,
don't do this:
if (....) { ... } else {It should be
if (....) { ... } else {
while (x < 3){
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
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
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.