CS 453 Programming Assignment #3 — MiniJava Expression Evaluator

Due Tuesday March 3th (by 11:59pm)

Introduction

This assignment is to be done in groups of two or three. In this assignment you will be writing a program that parses a constant integer expression from a file and evaluates that expression in three possible ways:
  1. during the parse of the expression,
  2. as a visitor over the abstract syntax tree for the expression, and
  3. by generating a MIPS program capable of evaluating the expression.
The goals of this assignment are for you to learn how to

The Assignment

You should create a jar file, ExprEval,jar, that can be executed as follows:
   java -jar ExprEval.jar --one-pass-eval input_file
   
   java -jar ExprEval.jar --two-pass-eval input_file
   
   java -jar ExprEval.jar --two-pass-mips input_file
   

The input_file will contain a single expression followed by a newline. The grammar for the input is as follows:

    Expression ::=
      
        Expression ("+" | "-" | "*" ) Expression

    |   < INTEGER_LITERAL >   
    
    | "(" Expression ")"
For this assignment, no error handling is necessary. In other words, you can assume the input is correct.

For the option --one-pass-eval, the value of the expression should be printed to standard out.

For the option --two-pass-eval, you should generate an AST that is printed to an input_file.ast.dot file and the value of the expression should be computed by visiting the AST. As with the one pass evaluation, the value of the expression should be printed to standard out. We provide you with AST node implementations, an extensible visitor class that traverses all the nodes in the AST in depth-first order, and an example visitor that generates a dot specification of the AST.

When called with the --two-pass-mips option, the expression evaluator should generate an AST that is printed to a input_file.ast.dot file and it should generate a input_file.s file with a MIPS program that evaluates the expression. The MIPS program must be capable of being interpreted by the MARS interpreter.

Getting Started

Download the MJExprStart.tar file and untar it. You should have the following files:
    MJExprStart/
        Makefile
        
        java-cup-11a.jar
        java-cup-11a-runtime.jar
        
        TestCases/
            expr1.txt		
            expr1.txt.ast.dot	
            expr1.txt.s
            
        java_cup/
            ...
            
        src/
            ExprEvalDriver.java
            ExprEvalMainClass.txt	
            ast_visitors/
                DotVisitor.java
            ast/
                analysis/
                    AnalysisAdapter.java		
                    IAnalysis.java
                    DepthFirstAdapter.java		
                    ReversedDepthFirstAdapter.java

                node/
                    IExp.java		
                    IntegerExp.java		
                    Node.java
                    ISwitch.java		
                    MinusExp.java		
                    PlusExp.java
                    ISwitchable.java	
                    MulExp.java		
                    Token.java

            mjparser/
                TokenValue.java

The Makefile works for the reference compiler. You can see what .cup and .lex files you will need to create by looking at how the Makefile calls JLex and JavaCUP to generate the lexer and parsers. Remember to start from example .lex and .cup files that already work.

To pass line and position information from the lexer to the parser. You will need the following information in your .lex file:

package mjparser;
import java_cup.runtime.Symbol;

%%
%cup
%line
%char
%public
...
{number} {return new Symbol(sym.NUMBER, new TokenValue(yytext(), yyline, yychar));}
...
The above only shows how one token is specified. Use the example we did in recitation on February 13th to figure out the rest.

In the .cup file for the parser that generates an AST, you will need to generate AST nodes in the actions. Here is an example:

    expr      ::= NUMBER:n
                  {: RESULT = new IntegerExp(new Token(n.text,n.line,n.pos)); :}

The ast.analysis and ast.node packages will be covered in recitation on Friday. You can use them or organize your program differently. The ast_visitors.DotVisitor class is an example of using the ast.analysis package and can be used as follows:

	        java.io.PrintStream astout =
	          new java.io.PrintStream(
	            new java.io.FileOutputStream(filename + ".ast.dot"));
	            
	        ast_root.apply(new DotVisitor(new PrintWriter(astout)));
	        
	        System.out.println("Printing dot file to " + filename + ".ast.dot");

See example output in the TestCases/ subdirectory of MJExprStart.

To get your code to compile with Eclipse, you will need to put the java-cup-11a-runtime.jar in your build path.

        Project --> Properties --> Java Build Path --> Libraries --> Add Library

Submitting the Assignment

Late Policy

Late assignments will be accepted up to 24 hours past the due date and time for a deduction of 20% and will not accepted past this period. Late means anything after 11:59pm on the day the assignment is due, including 12 midnight.


mstrout@cs.colostate.edu .... March 2, 2010