How to replace "==" by a single "="

25 views
Skip to first unread message

Marcus Gattinger

unread,
Jan 3, 2012, 5:41:32 PM1/3/12
to Jep Java Users
Hi,

suppose I do not want to support variable assignment in an expression
and therefore replace the assignment operator by the equality
operator.

An example would be more clear on that. Given the two expressions
if(x='Test',1,0)
and
if(x=='Test',1,0)

how can I achieve that the parser treats both expression the same (i.
e. always perform a test for equality)?

Regards,
Marcus

Richard

unread,
Jan 4, 2012, 5:48:40 AM1/4/12
to Jep Java Users
Marcus,

Changing the symbol for an operator is quite straightforward using the
Configurable parser
Jep jep = new Jep(new StandardConfigurableParser());

// Get the existing operator
Operator eq1=jep.getOperatorTable().getEQ();
// Create a new equality operator with the symbol =, the same
function and the same set of flags
Operator eq2 = new Operator("=",eq1.getPFMC(),eq1.getFlags());
// add it with the same precedence level
((OperatorTable2)jep.getOperatorTable()).addOperator(new
OperatorKey(){},eq2,eq1);
// remove the assignment operator

((OperatorTable2)jep.getOperatorTable()).removeOperator(OperatorTable2.SpecialOperators.ASSIGN);
jep.reinitializeComponents();

so
if(x='Test',1,0)
and
if(x=='Test',1,0)

will both parse. If you just want one symbol its easier to just to do
jep.getOperatorTable().getAssign().setSymbol(":=");

Richard

On Jan 3, 10:41 pm, Marcu s Gattinger <gattinger1...@googlemail.com>
wrote:

Marcus Gattinger

unread,
Jan 4, 2012, 3:12:42 PM1/4/12
to Jep Java Users
Thank you, Richard.

It is fantastic how easy it is to make modifications to the parser.
Great Job!
Reply all
Reply to author
Forward
0 new messages