Escolar Documentos
Profissional Documentos
Cultura Documentos
Markus Lumpe
Marker's comments:
Extension certification:
Signature of Convener:
1
COS30023 Semester 2, 2019 Dr. Markus Lumpe
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 );
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
• class PCode:
package ast;
import parser.Token;
• class PCodeArgument:
package ast;
import parser.Token;
4
COS30023 Semester 2, 2019 Dr. Markus Lumpe
PARSER_BEGIN(PCodeParser)
package parser;
import java.io.*;
import java.util.*;
import ast.*;
import machine.*;
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
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