Beruflich Dokumente
Kultur Dokumente
Markus Lumpe
Figure 1: CDC 6400, the first PASCAL host computer at ETH Zürich.1
1
http://www.moorecad.com/standardpascal/cdc6400.html
1
COS30023 Semester 2, 2019 Dr. Markus Lumpe
Reverse Polish notation (RPN) is a mathematical notation in which expressions are expressed
in postfix notation, that is, every operator follows all of its operands. For example, using RPN
we write
34+
to denote the sum of 3 and 4.
Expressions in RPN are evaluated left-to-right. The infix expression 5 + ((1 + 2) * 4) – 3 yields
the following RPN expression
512+4*+3-
To obtain an RPN expression from a given infix expression, we perform three steps:
• add parentheses to all sub terms
• move operator within parenthesized sub term to left
• remove all parentheses for the expression.
For example:
5 + ((1 + 2) * 4) - 3
è (5 + ((1 + 2) * 4)) - 3
è ((5 + ((1 + 2) * 4)) - 3)
è ((5 ((1 2 +) 4 *) +) 3 -)
è 512+4*+3-
An implementation for RPN is a stack machine that uses a pushdown stack rather than
individual machine registers to evaluate sub-expressions in a program. In particular, a stack
machine is programmed with a reverse Polish notation instruction set (sometimes called P-
code in reference to Pascal’s RPN implementation). The semantics of an instruction is given
by its stack transitions. For example, the following describes a binary operation that removes
its operands from the top of the stack and pushes the result back onto the stack:
…, operand1, operand2 à …, result
A given operation may have additional side effects. Storing and loading data from memory or
sending data to the output is considered a side effect for the stack machine.
The purpose of this self-study project is to develop a front-end for a stack machine, set up
the abstract syntax for the reverse Polish notation instruction set, and define an unparse
facility (using Java’s toString() method) to test whether the front-end has successfully
processed its input.
2
COS30023 Semester 2, 2019 Dr. Markus Lumpe
RPN grammar:
Lexical formation:
3
COS30023 Semester 2, 2019 Dr. Markus Lumpe
PCodeParser skeleton:
options
{
STATIC = false;
OUTPUT_DIRECTORY = "parser";
}
PARSER_BEGIN(PCodeParser)
package parser;
import java.io.*;
import java.util.*;
import ast.*;
PARSER_END(PCodeParser)
4
COS30023 Semester 2, 2019 Dr. Markus Lumpe
Abstract syntax:
The abstract syntax for the PCode instructions has to be derived from:
• class Position:
package ast;
import parser.Token;
• class PCode:
package ast;
import parser.Token;
5
COS30023 Semester 2, 2019 Dr. Markus Lumpe
• class PCodeArgument:
package ast;
import parser.Token;
Test output:
PCode accepted:
load 4.0
load 2.0
mul
dup
print "4 * 2 = "
load 1.0
add
dup
print "4 * 2 + 1 = "
store $x