import java.awt.TextField;
import java.awt.Label;
import java.awt.Event;
import java.awt.Frame;
import java.awt.Graphics;
import java.applet.Applet;
/**
* This class implements an applet that allows someone to type in
* a simple arithmetic expression and see the corresponding parse tree.
*
* NOTE: Due to the current implementation of MyStringTokenizer,
* all input symbols must be surrounded by blanks.
*
* NOTE: The current implementation makes no attempt to detect
* errors in the string typed to this program.
*
* NOTE: This applet can be invoked by using appletviewer on
* the file ExpressionsApplet.html
*
* source
*
* @version 1.0 18 January 1998
* @author Robert E. Webber
* @see ParseTree
* @see MyStringTokenizer
* @see Expressions#DisplayValueOfParseTree
*/
public class Expressions extends Applet {
Label ThePrompt;
TextField TheInputField;
Label ValuePrompt;
/**
* TheDisplayRegion points to that portion of the screen that can
* be used for displaying the parse tree. It is initialized in
* init(). The constants TheHorizontalCoordinateForDisplay,
* The VerticalCoordinateForDisplay, TheHorizontalWidthForDisplay,
* and TheVerticalHeightForDisplay are used to declare this portion
* of the screen.
* @see Expressions#init
*/
Graphics TheDisplayRegion;
static final int TheHorizontalCoordinateForDisplay = 5;
static final int TheVerticalCoordinateForDisplay = 90;
static final int TheHorizontalWidthForDisplay = 490;
static final int TheVerticalHeightForDisplay = 300;
/**
* DefaultScreenWidth and DefaultScreenHeight are used by init
* to set the initial screen size.
* @see Expressions#init
*/
static final int TheDefaultScreenWidth = 500;
static final int TheDefaultScreenHeight = 400;
/**
* TheDefaultPrompt is used by init to set the message that
* accompanies the input text field.
* @see Expressions#init
*/
static final String TheDefaultPrompt = "Enter an expression into text field.";
/**
* init is invoked by the Applet class to initialize the variables
* of this class. Specifically,
*
* - a screen is set up of the default size
*
- a default prompt is allocated space on the screen
*
- an area for expressions to be typed is allocated space on the screen
*
- a display region for the parse tree is set up on the screen
*
*/
public void init() {
resize(TheDefaultScreenWidth, TheDefaultScreenHeight);
ThePrompt = new Label(TheDefaultPrompt);
add(ThePrompt);
TheInputField = new TextField(40);
add(TheInputField);
ValuePrompt = new Label("Value: ");
add(ValuePrompt);
TheDisplayRegion = this.getGraphics()
.create(
TheHorizontalCoordinateForDisplay,
TheVerticalCoordinateForDisplay,
TheHorizontalWidthForDisplay,
TheVerticalHeightForDisplay);
}
/**
* handleEvent is invoked by the Applet class whenever there is an
* event that needs to be processed. This handler has been set up
* to respond to someone typing carriage return in the text field.
* It then passes the String residing in the text field to the method
* BuildAdditiveSubtree in ParseTree, which then constructs the appropriate
* parse tree for this input. A display region is then cleared and
* used to display the parse tree for the input. Finally, the method
* DisplayValueOfParseTree is invoked (to show the value that would
* result from computing the expression where the constants are of type
* double.
* @param TheEvent the information describing the event being passed to this handler.
* @see ParseTree
* @see ParseTree#BuildAdditiveSubtree
* @see ParseTree#Display
* @see Expressions#DisplayValueOfParseTree
*/
public boolean handleEvent(Event TheEvent) {
ParseTree TheParseTree;
if ((TheEvent.target instanceof TextField) &&
(TheEvent.id == Event.ACTION_EVENT)) {
TheParseTree = ParseTree.BuildAdditiveSubtree(TheInputField.getText());
TheDisplayRegion.clearRect(0, 0,
TheHorizontalWidthForDisplay,
TheVerticalHeightForDisplay);
TheParseTree.Display(TheDisplayRegion);
DisplayValueOfParseTree(TheParseTree);
return true;
}
return false;
}
/**
* The following method is a hook for your extension to this
* class to display the value that corresponds to the arithmetic
* expression represented by a parse tree. This routine is invoked
* by handleEvent.
* @param TheParseTree the parse tree that corresponds to the expression typed to this program.
* @see Expressions#handleEvent
*/
public void DisplayValueOfParseTree(ParseTree TheParseTree) {
ValuePrompt.setText("Value: " + ComputeValueOfParseTree(TheParseTree));
}
/* This method calculates the value of the expression contained in
* the parse tree.
*/
public double ComputeValueOfParseTree(ParseTree TheParseTree) {
Double tempdouble;
if (TheParseTree != null) {
// System.out.println( TheParseTree.GetToken() + " <" + TheParseTree.GetNodeType() +"> ");
if (TheParseTree.GetNodeType().equals("AdditiveExpression")) {
if (TheParseTree.GetToken().equals("+"))
return (ComputeValueOfParseTree(TheParseTree.GetLeftSubtree())
+ ComputeValueOfParseTree(TheParseTree.GetRightSubtree()));
else if (TheParseTree.GetToken().equals("-"))
return (ComputeValueOfParseTree(TheParseTree.GetLeftSubtree())
- ComputeValueOfParseTree(TheParseTree.GetRightSubtree()));
else
return ComputeValueOfParseTree(TheParseTree.GetLeftSubtree());
}
else if (TheParseTree.GetNodeType().equals("MultiplicativeExpression")) {
if (TheParseTree.GetToken().equals("*"))
return (ComputeValueOfParseTree(TheParseTree.GetLeftSubtree())
* ComputeValueOfParseTree(TheParseTree.GetRightSubtree()));
else if (TheParseTree.GetToken().equals("/"))
return (ComputeValueOfParseTree(TheParseTree.GetLeftSubtree())
/ ComputeValueOfParseTree(TheParseTree.GetRightSubtree()));
else
return ComputeValueOfParseTree(TheParseTree.GetLeftSubtree());
}
else if (TheParseTree.GetNodeType().equals("PrimaryExpression")) {
if (TheParseTree.GetToken().equals("("))
return ComputeValueOfParseTree(TheParseTree.GetLeftSubtree());
else {
tempdouble = Double.valueOf(TheParseTree.GetToken());
return tempdouble.doubleValue();
}
}
}
return 0.0;
}
}