org.sc3d.apt.sss.v3
Class Calculator

java.lang.Object
  extended by org.sc3d.apt.sss.v3.Parser
      extended by org.sc3d.apt.sss.v3.Calculator

public class Calculator
extends Parser

This class is a worked example of how to use SSS in general, and the tools provided in this Java package in particular. It is not itself a particularly useful tool. To get the most out of this example, read its source code.

This class uses an SSS syntax to implement an interactive arbitrary precision integer calculator, using the java.math.BigInteger class as the back-end. If you really wanted to write a calculator, there are easier ways of doing it than using SSS. Having said that:

If you wanted to do something slightly more complicated, the balance tips firmly in favour of using something like SSS, and you might well want to imitate this code.


Nested Class Summary
static class Calculator.Expression
          The data structure that represents an arithmetic expression.
static class Calculator.Negation
          An Expression whose outermost operator is a negation.
static class Calculator.Number
          An Expression that consists only of a number.
static class Calculator.Operation
          An Expression whose outermost operator is an addition, subtraction, multiplication of division.
 
Field Summary
static Grammar SUM
          The grammar of arithmetic expressions that this calculator accepts.
 
Fields inherited from class org.sc3d.apt.sss.v3.Parser
ndfa
 
Constructor Summary
Calculator()
          Constructs a Calculator.
 
Method Summary
 java.math.BigInteger evaluate(Sentence expression)
          Evaluates an arithmetic expression.
static void main(java.lang.String[] args)
          Repeatedly accepts an arithmetic expression from the console and evaluates it.
 java.lang.Object postProcess(Tree raw)
          Returns an Expression, given a Tree representing a parse-tree obeying 'Grammar.SUM'.
 Calculator.Expression postProcessExpression(Tree.Production raw)
          Converts a Tree representing the parse-tree of an 'atom', 'product' or 'sum' into an Expression.
 
Methods inherited from class org.sc3d.apt.sss.v3.Parser
interactiveTest, parse, parseTokens
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SUM

public static final Grammar SUM
The grammar of arithmetic expressions that this calculator accepts. Here is an SSS grammar specification for this Grammar:
 sign ::= {Minus {"-"}}
 atom ::= {Number {sign* NUMBER} Bracket {sign* Round(sum)}}
 multiplicand ::= {Multiply {"*" atom} Divide {"/" atom}}
 product ::= {Product {atom multiplicand*}}
 summand ::= {Add {"+" product} Subtract {"-" product}}
 sum ::= {Sum {product summand*}}
 ROOT sum
 

Constructor Detail

Calculator

public Calculator()
Constructs a Calculator.

Method Detail

evaluate

public java.math.BigInteger evaluate(Sentence expression)
Evaluates an arithmetic expression. If the expression is not syntactically correct, this method attaches appropriate error messages to 'expression' and returns 'null'.


postProcessExpression

public Calculator.Expression postProcessExpression(Tree.Production raw)
Converts a Tree representing the parse-tree of an 'atom', 'product' or 'sum' into an Expression. If the Tree contains errors, this method annotates the Tokens it contains with error messages and returns 'null'.


postProcess

public java.lang.Object postProcess(Tree raw)
Returns an Expression, given a Tree representing a parse-tree obeying 'Grammar.SUM'. If 'raw' contains errors, this method annotates the Tokens it contains with error messages and returns 'null'.

Overrides:
postProcess in class Parser
Parameters:
raw - the non-null parse-tree.
Returns:
a grammar-specific data structure, or 'null' if there were errors.

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
Repeatedly accepts an arithmetic expression from the console and evaluates it.

Throws:
java.io.IOException