Need to parse real numbers in a european locale, with comma separator

40 views
Skip to first unread message

tkau...@gmail.com

unread,
Jan 23, 2015, 2:01:22 PM1/23/15
to jep-...@googlegroups.com
Consider the decimal number, often represented with a  dot  as the decimal point.  (example   2.1 )

Then consider other locales, of which there are many, that use a comma to separate the decimal portion.  (example  2,1  in Germany)

When Jep parses  "2,1"  it regards the comma as a list separator and treats that as two numbers;  a   2  and a   1   .

I would think we would need to supply a different tokenizer so Jep won't treat the  2,1  as a list.  And then take some other action so the evaluator will accept the comma as marking the decimal place.

Is there any guidance on how to do this?

Thank you.

Richard Morris

unread,
Jan 23, 2015, 4:14:17 PM1/23/15
to jep-...@googlegroups.com, tkau...@gmail.com
This should be relatively straight forward using the Configurable Parser


You basically needed to specify the regexp to match your desired form of number. "(\\d+\\,?\\d*)|(\\,\\d+)"
you will need a more complex one for numbers with exponents.
ConfigurableParser cp = new ConfigurableParser();
cp.addHashComments();
cp.addSlashComments();
cp.addDoubleQuoteStrings();
cp.addWhiteSpace();

//cp.addExponentNumbers();
cp.addTokenMatcher( 

new NumberTokenMatcher("(\\d+\\,?\\d*)|(\\,\\d+)") );

cp.addOperatorTokenMatcher(); cp.addSymbols("(",")","[","]",","); cp.setImplicitMultiplicationSymbols("(","["); cp.addIdentifiers(); cp.addSemiColonTerminator(); cp.addWhiteSpaceCommentFilter(); cp.addBracketMatcher("(",")"); cp.addFunctionMatcher("(",")",","); cp.addListMatcher("[","]",","); cp.addArrayAccessMatcher("[","]"); // Construct the Jep instance and set the parser jep = new Jep(); jep.setComponent(cp);

Richard Morris

unread,
Jan 23, 2015, 5:15:36 PM1/23/15
to jep-...@googlegroups.com, tkau...@gmail.com
After accepting numbers in the correct format you would also need to use some other symbol for the other places "," is normally used. Semi-colon is an obvious choice. Its used as a statement terminator, this is an optional feature which you may not need.

ConfigurableParser cp = new ConfigurableParser();
cp.addHashComments();
cp.addSlashComments();
cp.addDoubleQuoteStrings();
cp.addWhiteSpace();

//cp.addExponentNumbers();
cp.addTokenMatcher( new NumberTokenMatcher("(\\d+\\,?\\d*)|(\\,\\d+)") );
cp.addOperatorTokenMatcher();
cp.addSymbols("(",")","[","]",";"); // changed to use semi-colon
cp.setImplicitMultiplicationSymbols("(","["); cp.addIdentifiers(); // cp.addSemiColonTerminator(); // need to comment this out cp.addWhiteSpaceCommentFilter(); cp.addBracketMatcher("(",")"); cp.addFunctionMatcher("(",")",";"); // changed to use semi-colon cp.addListMatcher("[","]",";"); // changed to use semi-colon

cp.addArrayAccessMatcher("[","]"); // Construct the Jep instance and set the parser jep = new Jep(); jep.setComponent(cp);

On Friday, 23 January 2015 19:01:22 UTC, tkau...@gmail.com wrote:
Reply all
Reply to author
Forward
0 new messages