PostScript interpreter

Skip to end of metadata
Go to start of metadata

This lab is related to the PostScript case study. You will build an interpreter for a simplified version of PostScript, but one that can actually draw pictures and so on. Your goal is to get the interpreter functional enough to display the following:

An interpreter for a proper subset of PostScript

I have largely provided the interpreter for you, but you must complete the ANTLR grammar that reads in postscript files and makes calls to the interpreter. Here are the files: zip of PostScript interpreter resources

The overall structure

A main program, Main.java, creates a visible Frame (a window) that your interpreter will pretend is the printed page. When you resize the window the image inside will resize as well. The main program creates Interp object attached to an input file passed as an argument on the command line.

The interpreter itself, Interp.java, has the system elements you would expect given our discussion above about stacks and dictionaries.

Interface to the parser

The operators will manipulate these entities. Your parser will only need to invoke one of two methods to actually make this whole thing work:

Control-flow

Notice that the interpreter is a kind of Canvas so that Interp has a paint() method that is called whenever the surrounding window (the one created in Main ) needs to redraw. The part of paint() you care about is:

In other words, a request to paint the image window asks your parser to start up and process the input. Your parser, in turn, will communicate with the interpreter to push operands and execute instructions.

What it can do

The cool looking nozzle (which I took from the Java 2D examples owned by Sun Microsystems) requires remarkably few instructions to render. They are:

It starts out like this

The key thing to notice is that

defines an operator m to be an alias for moveto . /moveto load asks the current directory to push the definition for moveto which is the procedure to actually do the move. Then the surrounding def instruction associates m with that move procedure.

Our interpreter ignores the dup and scale as it doesn't know those predefined operators.

Operators

The operators are all implemented in package ps.operators and are subclasses of ps.operators.Operator . The subclasses all implement

That method can push/pop a value off the operand stack with Interp.push() / Interp.pop() .

Operands

The data objects are subclasses of ps.PSObject such as ps.PSInt and ps.PSIdentifier . Here is the hierarchy:

and a description of what they are for:

class purpose
PSString a string
PSInt holds an int
PSReal holds a float
PSNumber Just a super class of the numbers so I ask for the float value conveniently.
PSIdentifier holds a literal name reference like /average . Corresponds to token ID in the grammar.
PSArray an array of operands (can have nested arrays); not needed for this lab.
PSProcedure an executable array of operands and operator names in
curly braces {...}
; not needed for this lab

You will create these objects in your parser and ask the interpreter to do something with them.

Goal: The parser

The ANTLR grammar file has a complete lexer and all infrastructure statements needed to make it fit into the existing interpreter. All you have to do is figure out what the grammar for postscript should look like using epsfile as a start rule.

You will then need to add actions to# create and push operands# execute operators

Here is a sample of object creation:

You have access to a variable in the parser called interpreter that you use to access the push() and execute() methods.

You must implement the dowhile operator and PSBoolean plus le operator type to make it work. So 9 10 le pushes a true PSBoolean onto the operand stack. Add print too so we can see values w/o worrying about the drawn page.

dowhile takes 2 procedures as operands.

Test files

I would examine the following test files and use them as a guide for understanding and then abstracting the grammar:

File test.ps should draw a line from 0,0 to 400 400:

File def.ps should draw a line from 0,100 to 300,300:

Then try out the nozzle.ps file of course.

More tests:

Using ANTLR

Ignore that warning...it does the right thing.

Submission

You will create a jar file called ps.jar containing source, grammar, and *.class files and place in your build directory:

https://www/svn/userid/cs652/ps/build/ps.jar

I will run your code by executing the following:

$java -cp "ps.jar:antlr-3.3.jar:$CLASSPATH" Main test.ps

NOTE THAT IT READS FROM THAT FILE NOT FROM STDIN

You can use the svn account for development of the software too if you would like, but I will only be looking at your jar file in the build directory.

For more information, see svn in CS601. Naturally you will have to substitute cs652 for cs601.

Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.