|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.sc3d.apt.sss.v3.Parser org.sc3d.apt.sss.v3.Calculator
public class Calculator
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:
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 |
---|
public static final Grammar SUM
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 |
---|
public Calculator()
Method Detail |
---|
public java.math.BigInteger evaluate(Sentence expression)
public Calculator.Expression postProcessExpression(Tree.Production raw)
public java.lang.Object postProcess(Tree raw)
postProcess
in class Parser
raw
- the non-null parse-tree.
public static void main(java.lang.String[] args) throws java.io.IOException
java.io.IOException
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |