This project gives you experience building an assembler and interpreter for a simple stack machine. Your goal is to get the three sample programs to execute properly through your interpeter.
high level code
would be translated as follows:
output
assembly code
output
As you can see, the Interp class main program invokes the assembler first to break the assembly code down into bytecodes and then launches the interpreter. I how provided the shell or the complete class definition for everything you need.
Assembler
Here is the outline of the grammar:
/** A simple-extendable assembler. Allows integer data declarations
* code labels and label operands (for which it computes an address).
* Allows forward-references of code labels, but data declarations must
* all appear before the code. You pass in a set of instruction names
* via the construction, which defines the assembly code. Subclass
* this parser and override the functionality methods such as gen()
* to make this parser actually do something.
*/
grammar Assembler;
@members {
// Define the functionality required by the parser for code generation
protected void gen(Token instrToken) {;}
protected void gen(Token instrToken, int operand) {;}
protected void checkForUnresolvedReferences() {;}
protected void declareData(Token idToken) {;}
protected int getOperandAddress(Token idToken) { return 0;}
protected void defineLabel(Token idToken) {;}
}
program : ... ;
...
ID : ('.')? ('a'..'z'|'A'..'Z')+ ; // ID and ".decl"
INT : ('-')? ('0'..'9')+ ;
WS : (' '|'\t')+ {skip();}
;
NEWLINE
: {getCharPositionInLine()>=1}?=> '\r'? '\n'
;
NEWLINE_BY_ITSELF
: {getCharPositionInLine()==0}?=> '\r'? '\n' {skip();}
;
COMMENT
: ';' .* '\n' {skip();}
;
You need to fill in the rules and then add some simple actions that make calls to those methods described above as shown in the following rule:
data: '.decl' ID NEWLINE {declareData($ID);}
;
You will also need a very simple symbol table (compared to your last project). Here are the various classes you need:
The Assembler subclass of the parser, fills in the abstract methods defined in a grammar:
Interpreter
Submission
You will create a jar file called asm.jar containing grammars, source, and *.class files and place in your lib directory:
https://www/svn/userid/cs652/proj4/trunk/lib
You can use the svn account for development of the software to if you would like, but I will only be looking at your asm.jar file in the lib directory.
Grading
Make sure your jar has the Interp and other classes in the default package with a main method so that I can test your code. Your program must be able to run the examples given.
You will be graded on the actions within the grammar and Assembler.java subclass as well as the instruction implementations in the interpreter. I will test your program on valid and invalid input.