Results 1 to 4 of 4
  1. #1
    Egrimm86 is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default math expression parser

    Hi all!
    I'm an italian student, and I have to program a parser wich have to reconize if a string is an algebrical expressione and pass his result to an evaluatoer that have to clculate the result of the expression.

    Mine idea is to divide the expression in OpExp. An OpExp is composed of an operator and what he have on his right and his left.

    Java Code:
    public class OpExp {
    
    	private String operator;
    	private IExp l_IExp;
    	private IExp r_IExp;
    
    	public OpExp(String op, IExp left, IExp right){
    		operator = op;
    		l_IExp = left;
    		r_IExp = right;
    	}
    
    	public void finalize() throws Throwable {
    		super.finalize();
    	}
    
    	public IExp getLeft(){
    		return l_IExp;
    	}
    
    	public String getOpName(){
    		return operator;
    		
    	}
    
    	public IExp getRight(){
    		return r_IExp;
    	}
    }
    We choose to use a stack stack, but, if the order of the operation is good, the order of wich is on left or right is often wrong.

    For example, in the expression (3+2)+2*(5+6), when he have to calculate the product of "*" on the left he see (3+2) and not the "2" wich is at the bottom of the stack... so probably the problem is that we use only a stack... maybe with 2 stack or a list...

    If it's useful, I could post the code of ours parser

    Any suggestion?

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    1.) Please get rid of that finalize method.
    2.) Are you restricting yourself to binary operators only?
    3.) There is obviously something wrong your implementation if you are not seeing some of the input.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Egrimm86 View Post
    Java Code:
    public class OpExp {
    
    	private String operator;
    	private IExp l_IExp;
    	private IExp r_IExp;
    
    	public OpExp(String op, IExp left, IExp right){
    		operator = op;
    		l_IExp = left;
    		r_IExp = right;
    	}
    
    	public void finalize() throws Throwable {
    		super.finalize();
    	}
    
    	public IExp getLeft(){
    		return l_IExp;
    	}
    
    	public String getOpName(){
    		return operator;
    		
    	}
    
    	public IExp getRight(){
    		return r_IExp;
    	}
    }
    Can an IExp be an OPExp? If not your types are incorrect, e.g. the right side of the OPExp 2*(5+6) needs to be an OPExp itself (5+6).

    kind regards,

    Jos

  4. #4
    Egrimm86 is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 11-13-2009, 09:51 PM
  2. DNS name parser 1.2.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-14-2007, 08:21 PM
  3. XML Parser
    By samfuerte in forum XML
    Replies: 1
    Last Post: 07-14-2007, 04:14 PM
  4. Parser
    By Peter in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2007, 07:08 AM
  5. DKP Log Parser 1.4.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 06-25-2007, 08:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •