PostFix notation

31 views
Skip to first unread message

nibe

unread,
Jul 20, 2009, 4:25:30 AM7/20/09
to Jep Java Users
Hi,
I'm looking for a lib to translate math formula in a post-fix
notation.
I use Jep lib to calculate math formula, and I see some reference in
JavaDoc about PostFix notation, but I'm not able to understand if it
can translate from normal to post-fix, or just calculate a formula in
postfix notation.

Thanks you very much

Cesare

Nathan Funk

unread,
Jul 21, 2009, 12:58:08 AM7/21/09
to Jep Java Users
If I understand correctly you want to print a formula in postfix
notation. That is possible with Jep. You can parse an equation
formatted as "1+2" and then print it to result in "1 2 +".

This is most easily done by extending
com.singularsys.jep.walkers.PostFixTreeWalker. Hope this gets you on
the right track.

Nathan

nibe

unread,
Jul 21, 2009, 5:39:41 AM7/21/09
to Jep Java Users
Hi Nathan,
thank you for your reply.
What I would like to have is a function that take as parameter an
equation formatted as "1+2" and gives as result an equation formatted
as "1 2 +".
The evaluation of the equation will be done in prefix notation, what I
need is just the conversion from prefix to postfix.
Seems that postFixTreeWalker just set a method for the tree walker
strategy.
Is there a function to do that?

thank you a lot

Cesare

Nathan Funk

unread,
Jul 21, 2009, 11:14:33 AM7/21/09
to Jep Java Users
There is no built-in method to accomplish what you want, but
implementing it would be straightforward. The code below is for
PrefixTreeDumper. The implementation of a postfix printing class would
be very similar to this.


public class PrefixTreeDumper extends PrefixTreeWalker {
PrintStream out=null;

public PrefixTreeDumper() {out = System.out;}
public PrefixTreeDumper(PrintStream stream) {out=stream;}

public void dump(Node node) throws JepException {
walk(node);
}
@Override
protected void visit(ASTConstant node, int nchildren, int depth) {
output(node,depth);
}
@Override
protected void visit(ASTFunNode node, int nchildren, int depth) {
output(node,depth);
}
@Override
protected void visit(ASTOpNode node, int nchildren, int depth) {
output(node,depth);
}
@Override
protected void visit(ASTVarNode node, int nchildren, int depth) {
output(node,depth);
}

protected void output(Node n, int depth) {

// create the indentation with 'depth' number of spaces
for(int i=0;i<depth;++i)
out.print(' ');

// print the node info
out.println(n);

Richard Morris

unread,
Jul 24, 2009, 5:54:28 AM7/24/09
to jep-...@googlegroups.com
Following on from Nathan the following code prints an expression in
postfix notation

/*
Created 24 Jul 2009 - Richard Morris
*/
package com.singularsys.jep.walkers;

import java.io.PrintStream;

import com.singularsys.jep.JepException;
import com.singularsys.jep.parser.ASTConstant;
import com.singularsys.jep.parser.ASTFunNode;
import com.singularsys.jep.parser.ASTOpNode;
import com.singularsys.jep.parser.ASTVarNode;
import com.singularsys.jep.parser.Node;

public class PostfixTreeDumper extends PostfixTreeWalker {
public static final boolean DETAILED=false;
public static final boolean PRINT_DEPTH=false;
PrintStream out=null;

public PostfixTreeDumper() {out = System.out;}
public PostfixTreeDumper(PrintStream stream) {out=stream;}

public void dump(Node node) throws JepException {
walk(node);
}

@Override


protected void visit(ASTFunNode node, int nchildren, int depth)

throws JepException {
output(node,depth);
}

@Override
protected void visit(ASTOpNode node, int nchildren, int depth)

throws JepException {
output(node,depth);
}

@Override
protected void visit(ASTVarNode node, int nchildren, int depth)

throws JepException {
output(node,depth);
}

@Override
protected void visit(ASTConstant node, int nchildren, int depth)

throws JepException {
output(node,depth);
}

protected void output(Node n, int depth) {
// create the indentation with 'depth' number of spaces

if(PRINT_DEPTH)


for(int i=0;i<depth;++i)
out.print(' ');

// print the node info

if(DETAILED)
{
out.println(n);
}
else {
if(n instanceof ASTConstant) {
out.println(n.getValue());
}
else
out.println(n.getName());
}
}

}

--
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

Reply all
Reply to author
Forward
0 new messages