Você está na página 1de 6

COS30023 Semester 2, 2019 Dr.

Markus Lumpe

Swinburne University of Technology

Faculty of Science, Engineering and Technology

ASSIGNMENT COVER SHEET

Subject Code: COS30023


Subject Title: Languages in Software Development
Assignment number and title: 5, RPN & Stack Machine
Due date: October 17, 2019, 08:30
Lecturer: Dr. Markus Lumpe

Your name: Your student id:

Fri 08:30 Fri 12:30 Fri 14:30


Check Tutorial

Marker's comments:

Problem Marks Obtained


PCodeMachine 96
Total 96

Extension certification:

This assignment has been given an extension and is now due on

Signature of Convener:

1
COS30023 Semester 2, 2019 Dr. Markus Lumpe

Problem Set 6: RPN & Stack Machine

Start with the solution of Tutorial – Abstract Syntax – RPN – Stack Machine.
The goal of this assignment is to implement a virtual RPN stack machine for the RPN language.
In particular, we want to use the Visitor pattern and implement the RPN stack machine as a
visitor for the abstract syntax tree nodes.
The visitor methods have to implement the semantics of the RPN instructions. There are two
sets of visitor methods: one for the instructions and one for the arguments:
public interface PCodeVisitor
{
public void visit( Add aInstruction );
public void visit( Sub aInstruction );
public void visit( Mul aInstruction );
public void visit( Div aInstruction );
public void visit( Dup aInstruction );
public void visit( Print aInstruction );
public void visit( Load aInstruction );
public void visit( Store aInstruction );

public Double visit( PCodeVariable aArgument );


public Double visit( PCodeNumber aArgument );
}

The instruction methods do not return a result, whereas the argument methods return a
Double value. Remember, a visitor has to traverse the object structure.
The visitor (i.e., the virtual RPN stack machine) should be implemented in the package
machine to separate it from the rest of the front-end. The RPN stack machine requires two
instance variables:
• fStack of type Stack< Double > to provide the value stack for the RPN machine,
and
• fMemory of type Hashtable< String, Double > to emulate the RPN machine’s
memory.
The utility classes are defined in java.util.*.
In addition, the RPN machine should also implement two auxiliary methods:
• void printStackTrace() to print the content of the value stack to the system
console, and
• void printMemoryTrace() to print the content of the RPN machine’s memory to
the system console.
Naturally, the RPN machine needs a constructor to properly initialize the instance variables.

You will need to update and extend the classes developed in the tutorial.

2
COS30023 Semester 2, 2019 Dr. Markus Lumpe

Instruction semantics:

• print string
Sends value to output by prepending string.
Stack transition: …, value à …
• add
Performs floating point operand1 + operand2 and pushes result.
Stack transition: …, operand1, operand2 à …, result
• sub
Performs floating point operand1 - operand2 and pushes result.
Stack transition: …, operand1, operand2 à …, result
• mul
Performs floating point operand1 * operand2 and pushes result.
Stack transition: …, operand1, operand2 à …, result
• div
Performs floating point operand1 / operand2 and pushes result.
Stack transition: …, operand1, operand2 à …, result
• dup
Duplicates the value on top of the stack.
Stack transition: …, value à …, value, value
• load argument
Loads argument onto the stack. Valid arguments are IEEE floating point literals or
variables. A variable denotes a memory location in the stack machine. More precisely, the
stack machine maintains a hash table that maps variables to floating point values.
Stack transition: … à …, argument
• store variable
Stores the top of the stack (i.e., value) into the location denoted by variable. If the
memory location does not exist in the stack machine, then a new one is created.
Otherwise, the old value is just overridden.
Stack transition: …, value à …

3
COS30023 Semester 2, 2019 Dr. Markus Lumpe

Revised abstract syntax:

• class PCode:
package ast;

import parser.Token;

public abstract class PCode extends Position


{
public PCode( Token aInstruction )
{
super( aInstruction );
}

public PCode( Token aInstruction, Position aEnd )


{
super( aInstruction, aEnd );
}

public PCode( Token aStart, Token aEnd )


{
super( aStart, aEnd );
}

public abstract String toString();

public abstract void accept( PCodeVisitor aVisitor );


}

• class PCodeArgument:
package ast;

import parser.Token;

public abstract class PCodeArgument extends Position


{
public PCodeArgument( Token aToken )
{
super( aToken );
}

public PCodeArgument( Token aStart, Token aEnd )


{
super( aStart, aEnd );
}

public abstract String toString();

public abstract Double accept( PCodeVisitor aVisitor );


}

4
COS30023 Semester 2, 2019 Dr. Markus Lumpe

Revised PCodeParser skeleton:

PARSER_BEGIN(PCodeParser)

package parser;

import java.io.*;
import java.util.*;
import ast.*;
import machine.*;

public class PCodeParser


{
public static void main( String[] args )
{
try
{
PCodeParser lParser = new PCodeParser( new FileInputStream( args[0] ) );

ArrayList< PCode > lInstructions = lParser.Program();

System.out.println( "PCode accepted:" );


for ( PCode pc : lInstructions )
{
System.out.println( pc );
}

PCodeMachine lMachine = new PCodeMachine();


System.out.println( "Running program: " );

for ( PCode pc : lInstructions )


{
pc.accept( lMachine );
}

lMachine.printStackTrace();
lMachine.printMemoryTrace();
}
catch (ParseException e)
{
System.out.println( "Syntax error: " + e.getMessage() );
}
catch( java.io.FileNotFoundException e )
{
System.err.println( "Program error: " + e.getMessage() );
}
}
}

PARSER_END(PCodeParser)

5
COS30023 Semester 2, 2019 Dr. Markus Lumpe

Sample program and output:

load 2E+1
dup
load 4E0
mul
dup
store $a
load 2E-1
mul
dup
print "20 * 4 * 0.2 = "
load 1
add
dup
print "20 * 4 * 0.2 + 1 = "
store $x

produces

PCode accepted:
load 20.0
dup
load 4.0
mul
dup
store $a
load 0.2
mul
dup
print "20 * 4 * 0.2 = "
load 1.0
add
dup
print "20 * 4 * 0.2 + 1 = "
store $x
Running program:
20 * 4 * 0.2 = 16.0000
20 * 4 * 0.2 + 1 = 17.0000
Stack:
1: 20.0000
Memory:
$x: 17.0000
$a: 80.0000

Submission deadline: Thursday, October 17, 2019, 08:30.


Submission procedure: on paper (class PCodeMachine).

Você também pode gostar