In order to use the MathParser class in your own application, you will need
to be able to use it to correctly parse a numerical computation and an expression
that defines a function,and you will need to be able to evaluate the function
at any numerical value. We begin with an application that does only this so
that we can make clear how the parser works. The application is embedded below
so you can see how it functions. Note that not only can you enter an expression
with variable "*x*" for the definition of *f(x)*, but
you can also use syntax such as sqrt(2) or e^2 to enter numerical values for *x*. In this application, the user should hit the keyboard "Enter"
key to update the output value.

Also note that in order to evaluate expression, the MathParser class uses the native ActionScript math functions and constants. As in Java, JavaScript, C++, and many other programming languages, the ActionScript math functions and constants maintain double precision accuracy. While this is sufficient for the answer checking and graphical examples we will show on pages 2 and 3, it can produce strange results. For example, if one evaluates tan(pi/2) in the applet below, the constant pi/2 clearly cannot be represented by a numerical type exactly, so the function returns the correct double precision value of tangent at x very very close to pi/2, and this is huge.

The moral here is that to build a robust scientific calculator, one must do a lot more work beyond the simple evaluation of the native functions and constants. See the help document linked in the notes section below for more information on ActionScript Math objects.

To create the application above, you first must place a few elements on the
stage in addtion to any labels and instructions you wish to have. Specifically
there must be two input textboxes named **inVal** and **inExp **and a dynamic textbox named **txtResult**. The rest of
the work is done with scripts entered into the Actions panel. The first line
of ActionScript in our code will be the line that imports the custom MathParser
class:

import bkde.as3.parsers.*;

We create two expression parsers, one for expressions involving the single variable x and the other for expressions involving no variables. Note that the syntax requires an array of variable names, and that a variable name can be any string. Also, for a parser of an expression with no variables (as in the case of mpVal), we pass the empty array [ ] to the constructor. The class MathParser is included in bkde.as3.parsers package imported in the first line.

var mpExp:MathParser = new MathParser(["x"]);

var mpVal:MathParser = new MathParser([ ]);

MathParsers have two methods: one for compiling and one for evaluating. These actions are separate because in many applications we need to compile once and evaluate repeatedly. To accomplish the compiling step, we declare two CompiledObject variables. The class Compiled Object is included in bkde.as3.parsers package imported in the first line.

var compobjExp:CompiledObject = new CompiledObject();

var compobjVal:CompiledObject = new CompiledObject();

We make the computation triggered by a press of the ENTER key. The code for the function registered to this event follows below with comments:

stage.addEventListener(KeyboardEvent.KEY_DOWN, compute);

function compute(kevt:KeyboardEvent):void {

// We return if any key other than ENTER is pressed

if (kevt.keyCode != Keyboard.ENTER) {

return;

}

// String variables to hold the contents of the input boxes:

var stVal:String = inVal.text;

var stExpression:String = inExp.text;

// Compile the expressions given by the strings.

compobjVal = mpVal.doCompile(stVal);

compobjExp = mpExp.doCompile(stExpression);

/*

If either box has a syntax error, then return the parser's error message

and put the focus on the offending box.

*/

if (compobjVal.errorStatus == 1) {

txtResult.text = compobjVal.errorMes;

stage.focus = inVal;

return;

}

if (compobjExp.errorStatus == 1) {

txtResult.text = compobjExp.errorMes;

stage.focus = inExp;

return;

}

/*

Use the doEval method of each parser to compute first the number value and then value of putting

this number into the expression. Note that the first argument of doEval is the PolishArray property

of the compiled object, and the second argument is an array of numbers to be passed to the

expression. Since the mpVal parser has no variables, we pass an empty array.

*/

var xVal:Number = mpVal.doEval(compobjVal.PolishArray, []);

var resVal:Number = mpExp.doEval(compobjExp.PolishArray, [xVal]);

// We round the answer off to ten decimal places.

txtResult.text = String(Math.round(resVal*10000000000)/10000000000);

}

#### Download

For the fla file to compile, you must put the unzipped folder of classes "bkde" in the same directory as the fla file.

- Flash CS3 file Computation.fla and custom class files in bkde.zip

#### Notes

- To learn more about ActionScript Math objects, see http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Math.html .
- We do not do detailed error checking in this example. For example, if the
user types the empty string or just "spaces", no syntax error is
thrown but the evaluation returns a result of
**NaN**, meaning "Not a number."