Results 1 to 2 of 2
  1. #1
    chris1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default Need help with setting operator precedence.

    Java Code:
    import java.util.Scanner;
    
    public class InFixEvaluator
    {
      public static int evaluate(String expression)
      {
        BoundedStackInterface<String>  opStack    = new ArrayStack<String>(50);  
        BoundedStackInterface<Integer> valueStack = new ArrayStack<Integer>(50);
        
        String thisOp;
        int value;
        int operand1;
        int operand2;
    
        int result = 0;
    
        Scanner tokenizer = new Scanner(expression);
    
        while (tokenizer.hasNext()){
          if (tokenizer.hasNextInt()){
            value = tokenizer.nextInt();
            if (valueStack.isFull())
            	throw new InFixException("Too many operands - stack overflow");
            valueStack.push(value);
          }
          else{
            thisOp = tokenizer.next();
            if (opStack.isFull())
            	throw new InFixException("Too many operators - stack overflow");
            opStack.push(thisOp);
          }
        }
        while(!opStack.isEmpty()){
            thisOp = opStack.top();
            if (thisOp.equals("("))
            	throw new InFixException("Unbalanced expression");
            opStack.pop();
          if (thisOp.equals(")")){
            while(!thisOp.equals ("(")){
            	if (opStack.isEmpty())
            		throw new InFixException("Not enough operands - stack underflow");
            	thisOp = opStack.top();
                opStack.pop();
                if (thisOp.equals("("))
                	break;
                if (valueStack.isEmpty())
                	throw new InFixException("Not enough operands - stack underflow");
                operand1 = valueStack.top();
                valueStack.pop();
                if (valueStack.isEmpty())
                	throw new InFixException("Not enough operands - stack underflow");
                operand2 = valueStack.top();
                valueStack.pop();
                
            if(thisOp.equals("*")){
              result = operand1 * operand2;
            valueStack.push(result);
            }
            else
            if(thisOp.equals("+")){
              result = operand1 + operand2;
              valueStack.push(result);
            }
            else
            if(thisOp.equals("-")){
              result = operand1 - operand2;
              valueStack.push(result);
            }
            else
            	throw new InFixException("Illegal symbol: " + thisOp);
          }
          }
          else {
        	  if (valueStack.isEmpty())
        		  throw new InFixException("Not enough operands - stack underflow");
              operand2 = valueStack.top();
              valueStack.pop();
              if (valueStack.isEmpty())
            	  throw new InFixException("Not enough operands - stack underflow");
              operand1 = valueStack.top();
              valueStack.pop();
            if(thisOp.equals("*"))
                 result = operand1 * operand2;
            else
            if(thisOp.equals("+"))
                 result = operand1 + operand2;
            else
            if(thisOp.equals("-"))
                 result = operand1 - operand2;
            else
            	throw new InFixException("Illegal symbol: " + thisOp);
            
            valueStack.push(result);
           }
          result = valueStack.top();
        }
        return result;
      }
    }
    There is the code for my Infix evaluator and the only thing I have left to do is set the operator precedence. I know that I have to compare the operator that I pop off to the next one but I am confused about how to go about and then solve the expression in the right order. As you can see in my code the only valid operators are *,+,-. I am stuck on this and any help would be greatly appreciated.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need help with setting operator precedence.

    Operator precedence parsing is a well known technology; google gives you many links; one of them is this one: Operator Precedence Parsing

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Java OR operator || help!
    By BeardedCamel in forum New To Java
    Replies: 2
    Last Post: 09-19-2011, 08:55 PM
  2. About precedence of methods
    By RichersooN in forum New To Java
    Replies: 12
    Last Post: 06-22-2011, 08:30 AM
  3. question about the operator ++
    By dardar in forum New To Java
    Replies: 10
    Last Post: 08-16-2010, 02:39 PM
  4. help with not equal to operator !=
    By manowar689 in forum New To Java
    Replies: 9
    Last Post: 06-15-2010, 12:10 AM
  5. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 07:50 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
  •