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 things:
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.
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.
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)
./prettyPrint.py samples/Nesty.jay
(This assumes that prettyPrinter.py
is set
with executable permissions.
If it does, you'll see an x
in an appropriate place
when you do an ls -l
:
tvandrun@milner2:solution$ ls -l prettyPrint.py -rwxr--r-- 1 tvandrun tvandrun 323 Jan 29 21:41 prettyPrint.py ^ |
If it doesn't have executable permissions, you can set it with
chmod u+x prettyPrint.py
.)
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 other 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/ubuntu/cs365/turnin/xxxxxx/proj1
where "xxxxxx" is [elliot|payton|davidemmanuel|nathan|gill|brandon|sean|simon]. I will grade your project by running it against a collection of test files.
DUE: Monday, Feb 10, 5:00 pm.