Skip to content

TinyJ Assignment 2

Due Date: Thursday, December 11, 2025

Try to finish by: Tuesday, December 9 (to have more time for Assignment 3 and exam preparation)

Prerequisite: Complete TinyJ Assignment 1 first

Note: If mars fails to operate normally or becomes inaccessible at any time after 6 p.m. on the due date, the deadline will not be extended. Try to submit no later than noon on that day, and on an earlier day if possible.

This assignment counts 1.5% towards your grade if computed using rule A.

Your assignment is to complete a compiler that does the following whenever its input is a syntactically valid TinyJ source file:

  1. Check that declarations and uses of identifiers are consistent with Java’s scope rules
  2. Translate the source file into a sequence of instructions for a stack-based virtual machine
  3. Write an “enhanced parse tree” showing static addresses, stackframe offsets, method code start addresses, and instruction generation times
  4. Write a list of the instructions generated to the output file

The 35 virtual machine instructions that may be generated by the compiler are shown below. A specification of their effects is given in the PDF document Memory-allocation-VM-instruction-set-and-hints-for-asn-2 (posted to Brightspace):

OperationOperand 1Operand 2
STOP
PUSHNUM<integer>
PUSHSTATADDR<address of static variable>
PUSHLOCADDR<local offset or parameter's stackframe variable>
LOADFROMADDR
SAVETOADDR
WRITELNOP
WRITEINT
WRITESTRING<address char first of><address char last of>
READINT
CHANGESIGN
NOT
ADD
SUB
MUL
DIV
MOD
AND
OR
EQ
LT
GT
NE
GE
LE
JUMP<address of target instruction>
JUMPONFALSE<address of target instruction>
CALLSTATMETHOD<address of first instruction of method's body>
INITSTKFRM<no. of local vars declared in method>
RETURN<no. of parameters the method has>
HEAPALLOC
ADDTOPTR
PASSPARAM
DISCARDVALUE
NOP

After completing TinyJ Assignment 1, do the following:

  1. Login to your xxxxx_yyyy316 mars account and enter /home/faculty/ykong/TJ2setup at the xxxxx_yyyy316@mars:~$ prompt
  2. Wait for TJ2setup done to appear on the screen (no error messages should appear)

On your PC or Mac (if doing the assignment locally)

Section titled “On your PC or Mac (if doing the assignment locally)”

If you are doing or have already done TinyJ Assignment 1 on your PC/Mac and plan to do this assignment on the same machine:

  1. Open a PowerShell (PC) or Terminal (Mac) window
  2. Change to your working directory:
    Terminal window
    cd ~/316java
  3. Download the file TJasn.jar from your mars home directory to ~/316java on your PC/Mac using scp or sftp
  4. Extract and compile the files:
    Terminal window
    jar xvf TJasn.jar
    javac -cp . TJasn/TJ.java
    javac -cp . TJasn/virtualMachine/*.java

The only file you need to change is TJasn/ParserAndTranslator.java.

In this file, each /* ???????? */ comment must be replaced with appropriate code. For most of these comments (specifically, lines 511-723), a good way to start is:

  1. Copy code you wrote for Parser.java in TinyJ Assignment 1
  2. Make changes so appropriate TinyJ virtual machine instructions will be generated

Important: For the comment on line 511, only copy 3 statements. Don’t copy the lines from Parser.java corresponding to:

  • Lines 482-3, 500, 502-4, 507, and 513
  • Lines 515, 519, and 537-8

To have a good chance of finishing before the deadline:

  • Immediately after completing Assignment 1: Copy code from corresponding parts of Parser.java into the /* ???????? */ comments on lines 511-723
  • No later than Friday, December 5: Fill in the /* ???????? */ gaps on lines 638-682
  • Before the deadline: Fill in remaining gaps on lines 492, 495, 511, 549, 593, 610, 627, and 723

Some helpful hints are given in the PDF document Memory-allocation-VM-instruction-set-and-hints-for-asn-2 (posted to Brightspace).

After editing TJasn/ParserAndTranslator.java, recompile it:

Terminal window
javac -cp . TJasn/ParserAndTranslator.java

On mars, this assumes your working directory is your home directory. On your PC/Mac, this assumes your working directory is ~/316java.

First, recompile your program:

Terminal window
javac -cp . TJasn/ParserAndTranslator.java

Then run your program on a TinyJ source file:

Terminal window
java -cp . TJasn.TJ TinyJ-source-file-name output-file-name

Example:

Terminal window
java -cp . TJasn.TJ CS316ex12.java 12.out

Working directory assumptions:

  • mars: your home directory
  • PC/Mac: ~/316java

When you are asked these questions, respond as follows:

Want debugging stop or post-execution dump? (y/n)
→ enter: y
Enter MINIMUM no. of instructions to execute before debugging stop.
(Enter -1 to get a post-execution dump but no debugging stop.):
→ enter: 0
Stop after executing what instruction? (e.g., PUSHNUM)
(Enter * to stop after executing just 0 instructions.):
→ enter: *

You can run the instructor’s solution to Assignment 2 similarly:

On mars or Mac:

Terminal window
java -cp TJsolclasses:. TJasn.TJ TinyJ-source-file-name output-file-name
java -cp TJsolclasses:. TJasn.TJ CS316ex12.java 12.sol

On PC (PowerShell):

Terminal window
java -cp "TJsolclasses;." TJasn.TJ TinyJ-source-file-name output-file-name
java -cp "TJsolclasses;." TJasn.TJ CS316ex12.java 12.sol

For correct TinyJ programs:

  • The output files from your solution and the instructor’s solution must contain the same “Instructions Generated:” lists (found near the end, just above the “Data memory dump”)
  • The files need not be identical otherwise

For each test file CS316ex{k}.java ({k} = 0 to 15):

  1. Run your program: java -cp . TJasn.TJ CS316ex{k}.java {k}.out
  2. Run the instructor’s solution: java -cp TJsolclasses:. TJasn.TJ CS316ex{k}.java {k}.sol
  3. Compare the output files using:
    • mars/Mac: diff -c {k}.out {k}.sol
    • PC: fc.exe /n {k}.out {k}.sol

Your solution is correct even if files differ, provided the “Instructions Generated:” lists match.

This assignment is to be submitted no later than Thursday, December 11, 2025. If mars becomes inaccessible after 6 p.m. on the due date, the deadline will not be extended. Try to submit no later than noon on that day, and on an earlier day if possible.

  1. Add a comment at the beginning of your completed ParserAndTranslator.java that gives your name and the names of any students you worked with (you may work with up to two other students)

  2. Leave your completed ParserAndTranslator.java in the TJasn directory of your xxxxx_yyyy316 mars account

  3. Verify your submission by entering at the xxxxx_yyyy316@mars:~$ prompt:

    Terminal window
    less TJasn/ParserAndTranslator.java

    Check that it displays the beginning of your file (including the comment you added)

  4. Enter the submission command at the xxxxx_yyyy316@mars:~$ prompt:

    Terminal window
    submit_TJ_asn2

Important: If your submitted version cannot be compiled without error on mars, you will receive no credit.

If you did this assignment on your PC/Mac, copy TJasn/ParserAndTranslator.java to mars using these steps:

  1. Open a PowerShell/Terminal window on your PC/Mac
  2. Change to your working directory:
    Terminal window
    cd ~/316java
  3. Copy the file to mars (requires connection to qwifi-secured network or QC VPN):
    Terminal window
    scp TJasn/ParserAndTranslator.java xxxxx_yyyy316@mars.cs.qc.cuny.edu:TJasn
    Where xxxxx_yyyy316 is your mars account username

You will be prompted to enter your mars account password.

IMPORTANT: After copying, you must still do steps 3 and 4 of the submission steps above. The assignment won’t be submitted until you do step 4!

See the 1st-day-announcements document on Brightspace for information on how to verify that your assignment has been submitted successfully.