Spring Semester 2002


Lab Notes Twelve: A Simple Interpreter (Evaluator, Really).
Here's some code that might help with assignment five:
import java.applet.*;
import java.awt.*; 
import java.awt.event.*; 

public class One extends Applet implements MouseListener, 
					   MouseMotionListener  {
  String where = "out"; 
  public void init() { 
    addMouseListener(this); addMouseMotionListener(this); 
  } 
  public void paint(Graphics g) { 
    if (where.equals("in")) { System.out.println("No smile."); }
    else { System.out.println("Broad smile."); }
  }

  public void mouseMoved(MouseEvent e) { System.out.println("A-ha!"); }
  public void mouseDragged(MouseEvent e) { }

  public void mouseClicked(MouseEvent e) { }
  public void mousePressed(MouseEvent e) { }
  public void mouseReleased(MouseEvent e) { }
  public void mouseExited(MouseEvent e) { 
    where = "out"; 
    repaint(); 
  }
  public void mouseEntered(MouseEvent e) { 
    where = "in";    
    repaint(); 
  }

}

The lab assignment you start working on today should help you with Homework Six.

A201/A597 LAB ASSIGNMENT TWELVE

For this assignment you are to write an arithmetic expression evaluator that is able to perform additions and multiplications, store results in named locations (variables) and allow them in expressions as well, as illustrated in the sample session below:

frilled.cs.indiana.edu%javac Eval.java
frilled.cs.indiana.edu%java Eval
Arithmetic Evaluator 1.0 Copyright A201 Systems Ltd.
Eval> 1
1
Eval> 1 + 2
3
Eval> 1 + 2 * 3
7
Eval> 1 * 2 + 3
5
Eval> a = 2
2
Eval> a
2
Eval> a = a + 3
5
Eval> a
5
Eval> b = 1
1
Eval> c = a + b
6
Eval> a
5
Eval> b
1
Eval> c
6
Eval> c = c * a + b
31
Eval> c
31
Eval> exit
frilled.cs.indiana.edu%
You may safely assume that your only operands are integers and that the names of variables are comprised of only alphabetical characters. You do not need to do any exquisite error-checking (it is OK for your program to exit with an error message if the user makes a mistake as long as all correctly written expressions are evaluated correctly). Help is available, but please refer to it only after you have exhausted your very own resources. Here's the help I can offer, to help you get
started.


Last updated: Apr 11, 2002 by Adrian German for A201