The Java Programming Language

Calculator Language Interpreter Example Program


Click below to go directly to a specific section:
Description | Source Code | Sample Run | Program Notes

Description

This page contains a Java applet that implements a small calculator language. The user can enter an expression to be evaluated in the Input Expression text field below. When the Calculate button is pressed, the expression is fist checked for syntax and then evaluated if correct. If a syntax error is detected, the input string is echoed to the Output text area with the position of the error indicated. If the expression is syntactically correct, the result obtained in the evaluation is displayed. The Clear button simply clears the contents of both the input and output areas.

The syntax is given by the following productions:

        <expression> ::= <value> | <value> <operator> <expression>
        <value>      ::= <number> | <sign> <number>
        <number>     ::= <unsigned> | <unsigned> . <unsigned>
        <unsigned>   ::= <digit> | <digit> <unsigned>
        <digit>      ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
        <sign>       ::= + | -
        <operator>   ::= + | - | * | /

No operator precedence rules are recognized and no spaces are allowed.


Source Code

Click
here to view the source code.

Click here to download a zip file containing the source code.


Sample Run

Sorry, but you need a Java-capable browser to view this applet.

Click here to download a zip file containing the Java executable applet classes calcApplet.class and calcPanel.class.


Program Notes

This program was compiled using the Java Developers Kit, Version 1.0.2, for Win32 on x86 from Sun Microsystems running under Windows 95. It has been tested with Netscape Navigator 3.0 and Microsoft Internet Explorer 3.0.
[Back] [Home]

Last modified: 10:35 PM on 11/24/1996