Last but one line has
> (L + 0,5),
You want
> (L + 0.5),
BTW you may find it easier to break the input into multiple lines
that way its easier to use the error message to identify the bug
String eqn = "A_N_E =\n"+
" if (CT == 1,\n"+
" (L_c / L_u) -1,\n"+
" if (CT == 2,\n"+
" if (TP == 1,\n"+
" 0.46,\n"+
" if (TP == 2,\n"+
" (84/(L * PE(81/(L + 0.5)))) -1,\n"+
" if (TP == 3,\n"+
" (81/(L * PE(78/(L + 0.5)))) -1,\n"+
" if (CT == 3,\n"+
" if (TP == 1 || 2,\n"+
" 0.46,\n"+
" if (TP == 3,\n"+
" 1- L_c/ (PE(L_B/(L + 0,5)) * L ),\n"+
" 0))))))))";
jep.parse(eqn);
Which gives me
com.singularsys.jep.ParseException: Encountered "," at line 15, column 47.
Line 15 column 47 is easier to find than line 1, column 409.
Rich
--
Richard Morris
Web: www.singsurf.org www.pfaf.org
Email: ri...@singsurf.org
Tel: (+44) 01208 872963
Post: 1 Lerryn View, Lerryn, Lostwithiel, Cornwall, PL22 0QJ
You do have the syntax right, I think its a problem of just counting the
number of brackets right.
>>>> A_N_E =
>>>> if (CT == 1,
>>>> (L_c / L_u) -1,
>>>> if (CT == 2,
>>>> if (TP == 1,
>>>> 0.46,
>>>> if (TP == 2,
>>>> (84/(L * PE(81/(L + 0.5)))) -1,
>>>> if (TP == 3,
>>>> (81/(L * PE(78/(L + 0.5)))) -1,
>>>> if (CT == 3,
>>>> if (TP == 1 || 2,
>>>> 0.46,
>>>> if (TP == 3,
>>>> 1- L_c/ (PE(L_B/(L + 0,5)) * L ),
>>>> 0))))))))
Looking that this a switch statement might make things simpler
public class Switch extends PostfixMathCommand {
private static final long serialVersionUID = 330L;
public Switch() {
this.numberOfParameters = -1;
}
public void run(Stack<Object> stack) throws EvaluationException {
Object vals[] = new Object[this.curNumberOfParameters];
for(int i=this.curNumberOfParameters-1;i>=0;++i)
vals[i] = stack.pop();
int index = (Integer) vals[0];
if(index>= this.curNumberOfParameters)
throw new EvaluationException("Switch: condition out of range
"+index+" max "+(this.curNumberOfParameters-1));
stack.push(vals[index]);
}
}
using the above finction which could be added with
jep.addFunction("switch",new Switch());
you could then transform your code to
>>>> A_N_E =
>>>> switch(CT,
>>>> (L_c / L_u) -1,
>>>> switch(TP,
>>>> 0.46,
>>>> (84/(L * PE(81/(L + 0.5)))) -1,
>>>> (81/(L * PE(78/(L + 0.5)))) -1),
>>>> switch(TP,
>>>> 0.46,
>>>> 0.46,
>>>> 1- L_c/ (PE(L_B/(L + 0.5)) * L )))
Rich
--
Richard Morris
Web: www.singsurf.org www.pfaf.org
Email: ri...@singsurf.org
Cann't we have any solution to parse 3,5 as floating point number..without changing COMMA to DOT?
static class CommaNumberTokenMatcher extends NumberTokenMatcher {
private static final long serialVersionUID = 1L;
public CommaNumberTokenMatcher() {
super("(\\d+\\,?\\d*)|(\\,\\d+)");
}
@Override
public Token buildToken(String s) {
String s2=s.replace(',','.'); // convert string changing , to .
return super.buildToken(s2); // create a Token using the string, calls Double.valueof(String);
}
}
@Test
public void testCommaNumber() throws JepException {
ConfigurableParser cp = new ConfigurableParser();
cp.addHashComments();
cp.addSlashComments();
cp.addSingleQuoteStrings();
cp.addDoubleQuoteStrings();
cp.addWhiteSpace();
cp.addTokenMatcher(new CommaNumberTokenMatcher());
cp.addExponentNumbers();
cp.addOperatorTokenMatcher();
cp.addSymbols("(",")","[","]",";"); // changed
cp.setImplicitMultiplicationSymbols("(","[");
cp.addIdentifiers();
cp.addSemiColonTerminator();
cp.addWhiteSpaceCommentFilter();
cp.addBracketMatcher("(",")");
cp.addFunctionMatcher("(",")",";"); // changed
cp.addListMatcher("[","]",";"); // changed
cp.addArrayAccessMatcher("[","]");
Jep jep = new Jep(cp);
jep.parse("24,9");
Object result = jep.evaluate();
assertEquals(24.9,result);
jep.parse("if(2,1 > 3,4 ; 5,6 ; 7,8)");
result = jep.evaluate();
assertEquals(7.8,result);
jep.parse("[2,1;3,4]");
result = jep.evaluate();
assertArrayEquals(new Object[]{2.1,3.4}, ((List<?>) result).toArray());
}
--
You received this message because you are subscribed to the Google Groups "Jep Java Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jep-users+...@googlegroups.com.
To post to this group, send email to jep-...@googlegroups.com.
Visit this group at http://groups.google.com/group/jep-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
<DEFAULT> TOKEN:
{
<INDENTIFIER1: <LETTER1>(<LETTER1>|<DIGIT1>|".")*>
|
< #LETTER1:
[
"\u0024", // $
"\u0041"-"\u005a", // A - Z
"\u005f", // _
"\u0061"-"\u007a", // a - z
...
]
>
|
< #DIGIT1: ["0"-"9"] >
}
<NO_DOT_IN_IDENTIFIERS> TOKEN:
{
<INDENTIFIER2: <LETTER2>(<LETTER2>|<DIGIT2>)*>
|
< #LETTER2:
[
"\u0024", // $
"\u0041"-"\u005a", // A - Z
"\u005f", // _
"\u0061"-"\u007a", // a - z
...
]
>
|
< #DIGIT2: ["0"-"9"] >
}
Latter in the BNF grammar you need to allow for both types of token
String Identifier() :
{
Token t;
}
{
( t = <INDENTIFIER1> | t = <INDENTIFIER2> ) { return t.image; }
}
You can then call
public void setInitialTokenManagerState(int state)
to set the state, using the constants defined in JccParserConstants.
Good luck, I've found working with the JavaCC grammar quite a tricky thing to get right and would not recommend it.
Richard