/* Generated By:JavaCC: Do not edit this line. CalcEngine.java */
/*
This file is part of the software framework JStatCom,
a library for building applications for data based analysis.
Copyright (C) 2005 Markus Kraetzig
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
package com.jstatcom.parser;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import com.jstatcom.model.JSCConstants;
@SuppressWarnings("unchecked")
public class CalcEngine implements CalcEngineConstants {
/**
* Subclassing HashMap to enforce case insensitivity of variable names.
*/
private final Map variableMap = new HashMap() {
public Object put(Object keyName, Object varArray) {
if (keyName != null)
keyName = keyName.toString().toLowerCase();
return super.put(keyName, varArray);
}
public Object get(Object keyName) {
if (keyName != null)
keyName = keyName.toString().toLowerCase();
return super.get(keyName);
}
public boolean containsKey(Object keyName) {
if (keyName != null)
keyName = keyName.toString().toLowerCase();
return super.containsKey(keyName);
}
};
private final Stack argStack = new Stack();
private final StringBuffer outputBuffer = new StringBuffer();
public static void main(String args[]) {
CalcEngine parser = new CalcEngine(System.in);
/* Defines the variable space. */
parser.putVariable("a", new double[2]);
parser.putVariable("b", new double[4]);
}
/**
* Default constructor.
*/
public CalcEngine() {
this(new StringReader(""));
}
/**
* Deletes all variables from the variableMap.
*/
public void clearVariableMap() {
variableMap.clear();
}
/**
* Define the variables that can be used during the parsing. The dataArray
* is copied before it is put into the variableMap.
*
* @param keyName
* name of the variable
* @param dataArray
* is a (T x 1) array with the data
* @throws IllegalArgumentException
* if (JSCConstants.isValidName(keyName) != null || dataArray ==
* null)
*/
public void putVariable(String keyName, double[] dataArray) {
String error = JSCConstants.isValidName(keyName);
if (error != null)
throw new IllegalArgumentException("Bad key: " + error);
if (dataArray == null)
throw new IllegalArgumentException("Data array was null.");
double[] varArray = new double[dataArray.length];
for (int i = 0; i < dataArray.length; i++)
varArray[i] = dataArray[i];
variableMap.put(keyName, varArray);
}
/**
* Retrieves all keys that represent the variable names.
*
* @return String[] containing the keys
*/
public String[] getVariableNames() {
Set keys = variableMap.keySet();
String[] varNames = new String[keys.size()];
int i = 0;
for (Iterator iter = keys.iterator(); iter.hasNext();)
varNames[i++] = iter.next().toString();
return varNames;
}
/**
* Retrieves the underlying data for a variableName.
*
* @param variableName
* the name of the variable
* @return double[] the data
*/
public double[] getVariable(String variableName) {
return (double[]) variableMap.get(variableName);
}
/**
* Retrieves the contents of the output buffer of the last parser run.
*
* @return String the contents of the output buffer
*/
public String getOutput() {
String output = "";
if (outputBuffer.length() > 0)
output = outputBuffer.toString();
outputBuffer.delete(0, outputBuffer.length());
return output;
}
/**
* Reinitializes engine and parses an input String.
*
* @param inputString
* the String to parse
* @throws ParseException
* if parsing failed, contains error description
* @throws TokenMgrError
* if tokenizing failed, contains error description
*/
public void parseString(String inputString) throws ParseException,
TokenMgrError {
if (!inputString.endsWith(";"))
inputString = inputString + ";";
ReInit(new StringReader(inputString));
outputBuffer.delete(0, outputBuffer.length());
toeval();
}
/**
* The functions that are defined for CalcEngine.
*
* @throws ParseException
*/
private void callFunctionForName(String functName, int argNum)
throws ParseException {
try {
CalcFunctions.callFunctionForName(functName, argNum, argStack);
} catch (IllegalArgumentException ex) {
throw new ParseException(ex.getMessage());
}
}
final public int toeval() throws ParseException {
label_1: while (true) {
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
case EOFEXP:
;
break;
default:
jj_la1[0] = jj_gen;
break label_1;
}
jj_consume_token(EOFEXP);
}
label_2: while (true) {
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
case MINUS:
case CONSTANT:
case VAR:
case FUNCNAME:
case LHS:
case 20:
;
break;
default:
jj_la1[1] = jj_gen;
break label_2;
}
equation();
label_3: while (true) {
jj_consume_token(EOFEXP);
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
case EOFEXP:
;
break;
default:
jj_la1[2] = jj_gen;
break label_3;
}
}
}
jj_consume_token(0);
{
if (true)
return 1;
}
throw new Error("Missing return statement in function");
}
/**
* To be uncommented for commandLineVersion.
*/
// int toeval1() :
// {}
// {
// ()* (equation() ()+)+
// { return 1; }
// }
/**
* Expression of the form ()? sum()
*/
final public void equation() throws ParseException {
String lhs;
String currentLHS = null;
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
case LHS:
jj_consume_token(LHS);
/*
* Set the name of the current variable that the result is assigned
* to
*/
lhs = token.image;
currentLHS = lhs.substring(0, lhs.lastIndexOf('=')).trim();
break;
default:
jj_la1[3] = jj_gen;
;
}
sum();
/*
* Assign result to the LHS of the equation and put it to the Map of
* variables
*/
double[] result = (double[]) argStack.peek();
if (currentLHS != null) {
double[] assignedArray = new double[result.length];
/* Copy the result. */
for (int i = 0; i < result.length; i++)
assignedArray[i] = result[i];
variableMap.put(currentLHS, assignedArray);
currentLHS = null;
} else {
// Print out the result in case it is not assigned.
double[] x = (double[]) argStack.pop();
for (int i = 0; i < x.length; i++)
outputBuffer.append(x[i] + "\n");
outputBuffer.append("\n");
}
}
final public void sum() throws ParseException {
Token x;
term();
label_4: while (true) {
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
case PLUS:
case MINUS:
;
break;
default:
jj_la1[4] = jj_gen;
break label_4;
}
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
case PLUS:
x = jj_consume_token(PLUS);
break;
case MINUS:
x = jj_consume_token(MINUS);
break;
default:
jj_la1[5] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
term();
/* Assemble the result as a double[] */
double[] bb = (double[]) argStack.pop();
double[] aa = (double[]) argStack.pop();
int length = 1;
try {
length = CalcFunctions.getLength(aa, bb);
} catch (IllegalArgumentException ex) {
{
if (true)
throw new ParseException(ex.getMessage());
}
}
double[] result = new double[length];
if (x.kind == PLUS)
for (int i = 0; i < result.length; i++)
result[i] = ((aa.length == 1) ? aa[0] : aa[i])
+ ((bb.length == 1) ? bb[0] : bb[i]);
else
for (int i = 0; i < result.length; i++)
result[i] = ((aa.length == 1) ? aa[0] : aa[i])
- ((bb.length == 1) ? bb[0] : bb[i]);
argStack.push(result);
}
}
final public void term() throws ParseException {
Token x;
exp();
label_5: while (true) {
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
case MULTIPLY:
case DIVIDE:
case ROWAPPEND:
;
break;
default:
jj_la1[6] = jj_gen;
break label_5;
}
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
case MULTIPLY:
x = jj_consume_token(MULTIPLY);
break;
case DIVIDE:
x = jj_consume_token(DIVIDE);
break;
case ROWAPPEND:
x = jj_consume_token(ROWAPPEND);
break;
default:
jj_la1[7] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
exp();
/* Assemble the result as a double[] */
double[] bb = (double[]) argStack.pop();
double[] aa = (double[]) argStack.pop();
int length = 1;
double[] result = null;
if (x.kind == MULTIPLY || x.kind == DIVIDE) {
try {
length = CalcFunctions.getLength(aa, bb);
} catch (IllegalArgumentException ex) {
{
if (true)
throw new ParseException(ex.getMessage());
}
}
result = new double[length];
} else
result = new double[aa.length + bb.length];
if (x.kind == MULTIPLY) {
for (int i = 0; i < result.length; i++)
result[i] = ((aa.length == 1) ? aa[0] : aa[i])
* ((bb.length == 1) ? bb[0] : bb[i]);
} else {
if (x.kind == DIVIDE) {
for (int i = 0; i < result.length; i++)
result[i] = ((aa.length == 1) ? aa[0] : aa[i])
/ ((bb.length == 1) ? bb[0] : bb[i]);
} else {
int index = 0;
for (int i = 0; i < aa.length; i++, index++)
result[index] = aa[i];
for (int i = 0; i < bb.length; i++, index++)
result[index] = bb[i];
}
}
argStack.push(result);
}
}
final public void exp() throws ParseException {
unary();
label_6: while (true) {
if (jj_2_1(2147483647)) {
;
} else {
break label_6;
}
jj_consume_token(EXP);
exp();
/* Assemble the result as a double[] */
double[] bb = (double[]) argStack.pop();
double[] aa = (double[]) argStack.pop();
int length = 1;
try {
length = CalcFunctions.getLength(aa, bb);
} catch (IllegalArgumentException ex) {
{
if (true)
throw new ParseException(ex.getMessage());
}
}
double[] result = new double[length];
for (int i = 0; i < result.length; i++)
result[i] = Math.pow(aa[i], ((bb.length == 1) ? bb[0] : bb[i]));
argStack.push(result);
}
}
final public void unary() throws ParseException {
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
case MINUS:
jj_consume_token(MINUS);
element();
/* Assemble the result as a double[] */
double[] aa = (double[]) argStack.pop();
double[] result = new double[aa.length];
for (int i = 0; i < result.length; i++)
result[i] = -aa[i];
argStack.push(result);
break;
case CONSTANT:
case VAR:
case FUNCNAME:
case 20:
element();
break;
default:
jj_la1[8] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
}
final public void element() throws ParseException {
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
case CONSTANT:
jj_consume_token(CONSTANT);
double[] result = new double[1];
try {
result[0] = Double.valueOf(token.image).doubleValue();
} catch (NumberFormatException ee) {
result[0] = Double.NaN;
}
argStack.push(result);
break;
case FUNCNAME:
function();
break;
case 20:
jj_consume_token(20);
sum();
jj_consume_token(21);
break;
case VAR:
variable();
break;
default:
jj_la1[9] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
}
final public void variable() throws ParseException {
String var;
jj_consume_token(VAR);
/**
* Here we need to retrieve the double[] belonging to the variable name.
* Use the Map with "variable names" "double[]" entries. Push the value
* of the variable on to the stack.
*/
var = token.image;
String varName = var;
double[] result = null;
if (variableMap.containsKey(varName)) {
double[] values = (double[]) variableMap.get(varName);
result = new double[values.length];
for (int i = 0; i < values.length; i++)
result[i] = values[i];
} else
/* The referenced variable is not known. */
{
if (true)
throw new ParseException("Variable \"" + varName
+ "\" is not available.");
}
argStack.push(result);
}
final public void function() throws ParseException {
String funcName;
int args = 0;
jj_consume_token(FUNCNAME);
funcName = token.image;
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
case MINUS:
case CONSTANT:
case VAR:
case FUNCNAME:
case 20:
sum();
args++;
label_7: while (true) {
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
case 22:
;
break;
default:
jj_la1[10] = jj_gen;
break label_7;
}
jj_consume_token(22);
sum();
args++;
}
break;
default:
jj_la1[11] = jj_gen;
;
}
jj_consume_token(21);
funcName = funcName.substring(0, funcName.indexOf("(")).trim();
callFunctionForName(funcName, args);
}
final private boolean jj_2_1(int xla) {
jj_la = xla;
jj_lastpos = jj_scanpos = token;
try {
return !jj_3_1();
} catch (LookaheadSuccess ls) {
return true;
} finally {
jj_save(0, xla);
}
}
final private boolean jj_3_1() {
if (jj_scan_token(EXP))
return true;
return false;
}
public CalcEngineTokenManager token_source;
SimpleCharStream jj_input_stream;
public Token token, jj_nt;
private int jj_ntk;
private Token jj_scanpos, jj_lastpos;
private int jj_la;
public boolean lookingAhead = false;
private int jj_gen;
final private int[] jj_la1 = new int[12];
static private int[] jj_la1_0;
static {
jj_la1_0();
}
private static void jj_la1_0() {
jj_la1_0 = new int[] { 0x20, 0x1d1080, 0x20, 0x80000, 0xc0, 0xc0,
0xb00, 0xb00, 0x151080, 0x151000, 0x400000, 0x151080, };
}
final private JJCalls[] jj_2_rtns = new JJCalls[1];
private boolean jj_rescan = false;
private int jj_gc = 0;
public CalcEngine(java.io.InputStream stream) {
this(stream, null);
}
public CalcEngine(java.io.InputStream stream, String encoding) {
try {
jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1);
} catch (java.io.UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
token_source = new CalcEngineTokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 12; i++)
jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++)
jj_2_rtns[i] = new JJCalls();
}
public void ReInit(java.io.InputStream stream) {
ReInit(stream);
}
public void ReInit(java.io.InputStream stream, String encoding) {
try {
jj_input_stream.ReInit(stream, encoding, 1, 1);
} catch (java.io.UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
token_source.ReInit(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 12; i++)
jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++)
jj_2_rtns[i] = new JJCalls();
}
public CalcEngine(java.io.Reader stream) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
token_source = new CalcEngineTokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 12; i++)
jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++)
jj_2_rtns[i] = new JJCalls();
}
public void ReInit(java.io.Reader stream) {
jj_input_stream.ReInit(stream, 1, 1);
token_source.ReInit(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 12; i++)
jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++)
jj_2_rtns[i] = new JJCalls();
}
public CalcEngine(CalcEngineTokenManager tm) {
token_source = tm;
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 12; i++)
jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++)
jj_2_rtns[i] = new JJCalls();
}
public void ReInit(CalcEngineTokenManager tm) {
token_source = tm;
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 12; i++)
jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++)
jj_2_rtns[i] = new JJCalls();
}
final private Token jj_consume_token(int kind) throws ParseException {
Token oldToken;
if ((oldToken = token).next != null)
token = token.next;
else
token = token.next = token_source.getNextToken();
jj_ntk = -1;
if (token.kind == kind) {
jj_gen++;
if (++jj_gc > 100) {
jj_gc = 0;
for (int i = 0; i < jj_2_rtns.length; i++) {
JJCalls c = jj_2_rtns[i];
while (c != null) {
if (c.gen < jj_gen)
c.first = null;
c = c.next;
}
}
}
return token;
}
token = oldToken;
jj_kind = kind;
throw generateParseException();
}
static private final class LookaheadSuccess extends java.lang.Error {
}
final private LookaheadSuccess jj_ls = new LookaheadSuccess();
final private boolean jj_scan_token(int kind) {
if (jj_scanpos == jj_lastpos) {
jj_la--;
if (jj_scanpos.next == null) {
jj_lastpos = jj_scanpos = jj_scanpos.next = token_source
.getNextToken();
} else {
jj_lastpos = jj_scanpos = jj_scanpos.next;
}
} else {
jj_scanpos = jj_scanpos.next;
}
if (jj_rescan) {
int i = 0;
Token tok = token;
while (tok != null && tok != jj_scanpos) {
i++;
tok = tok.next;
}
if (tok != null)
jj_add_error_token(kind, i);
}
if (jj_scanpos.kind != kind)
return true;
if (jj_la == 0 && jj_scanpos == jj_lastpos)
throw jj_ls;
return false;
}
final public Token getNextToken() {
if (token.next != null)
token = token.next;
else
token = token.next = token_source.getNextToken();
jj_ntk = -1;
jj_gen++;
return token;
}
final public Token getToken(int index) {
Token t = lookingAhead ? jj_scanpos : token;
for (int i = 0; i < index; i++) {
if (t.next != null)
t = t.next;
else
t = t.next = token_source.getNextToken();
}
return t;
}
final private int jj_ntk() {
if ((jj_nt = token.next) == null)
return (jj_ntk = (token.next = token_source.getNextToken()).kind);
return (jj_ntk = jj_nt.kind);
}
private java.util.Vector jj_expentries = new java.util.Vector();
private int[] jj_expentry;
private int jj_kind = -1;
private int[] jj_lasttokens = new int[100];
private int jj_endpos;
private void jj_add_error_token(int kind, int pos) {
if (pos >= 100)
return;
if (pos == jj_endpos + 1) {
jj_lasttokens[jj_endpos++] = kind;
} else if (jj_endpos != 0) {
jj_expentry = new int[jj_endpos];
for (int i = 0; i < jj_endpos; i++) {
jj_expentry[i] = jj_lasttokens[i];
}
boolean exists = false;
for (java.util.Enumeration e = jj_expentries.elements(); e
.hasMoreElements();) {
int[] oldentry = (int[]) (e.nextElement());
if (oldentry.length == jj_expentry.length) {
exists = true;
for (int i = 0; i < jj_expentry.length; i++) {
if (oldentry[i] != jj_expentry[i]) {
exists = false;
break;
}
}
if (exists)
break;
}
}
if (!exists)
jj_expentries.addElement(jj_expentry);
if (pos != 0)
jj_lasttokens[(jj_endpos = pos) - 1] = kind;
}
}
public ParseException generateParseException() {
jj_expentries.removeAllElements();
boolean[] la1tokens = new boolean[23];
for (int i = 0; i < 23; i++) {
la1tokens[i] = false;
}
if (jj_kind >= 0) {
la1tokens[jj_kind] = true;
jj_kind = -1;
}
for (int i = 0; i < 12; i++) {
if (jj_la1[i] == jj_gen) {
for (int j = 0; j < 32; j++) {
if ((jj_la1_0[i] & (1 << j)) != 0) {
la1tokens[j] = true;
}
}
}
}
for (int i = 0; i < 23; i++) {
if (la1tokens[i]) {
jj_expentry = new int[1];
jj_expentry[0] = i;
jj_expentries.addElement(jj_expentry);
}
}
jj_endpos = 0;
jj_rescan_token();
jj_add_error_token(0, 0);
int[][] exptokseq = new int[jj_expentries.size()][];
for (int i = 0; i < jj_expentries.size(); i++) {
exptokseq[i] = (int[]) jj_expentries.elementAt(i);
}
return new ParseException(token, exptokseq, tokenImage);
}
final public void enable_tracing() {
}
final public void disable_tracing() {
}
final private void jj_rescan_token() {
jj_rescan = true;
for (int i = 0; i < 1; i++) {
try {
JJCalls p = jj_2_rtns[i];
do {
if (p.gen > jj_gen) {
jj_la = p.arg;
jj_lastpos = jj_scanpos = p.first;
switch (i) {
case 0:
jj_3_1();
break;
}
}
p = p.next;
} while (p != null);
} catch (LookaheadSuccess ls) {
}
}
jj_rescan = false;
}
final private void jj_save(int index, int xla) {
JJCalls p = jj_2_rtns[index];
while (p.gen > jj_gen) {
if (p.next == null) {
p = p.next = new JJCalls();
break;
}
p = p.next;
}
p.gen = jj_gen + xla - jj_la;
p.first = token;
p.arg = xla;
}
static final class JJCalls {
int gen;
Token first;
int arg;
JJCalls next;
}
}
Ads help maintain this website.