Advertisement
tombenko

Parser.java

Jun 15th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. import java.util.Stack;
  2. import java.util.StringTokenizer;
  3.  
  4. public class Parser{
  5.     /*
  6.      * This class offers methods to handle the calculators input stream
  7.      * strings. The constructor is to tokenize the stream and the method
  8.      * is to convert it to reverse polish notation.
  9.      */
  10.      
  11.     StringTokenizer st;
  12.    
  13.     public Parser(String input){
  14.         /*
  15.          * The possible tokens are the operators and numbers. All other
  16.          * character is dropped out, assuming them being typos.
  17.          */
  18.        
  19.         st = new StringTokenizer(input, Operator.getOperatorString()+"()", true); //The delimiters are the operators and the brackets. They must be in the token stream also.
  20.        
  21.     }
  22.    
  23.     public String[] convertToRPN(){
  24.         /*
  25.          * Turning the tokenized stream into a reverse polish notationed
  26.          * array of strings using the shunting-yard algorithm.
  27.          */
  28.          
  29.          Stack<String> opStack = new Stack<String>();
  30.          String[] rpn = new String[st.countTokens()];
  31.          boolean wasOperator = true;
  32.          int index = 0;
  33.          
  34.          while( st.hasMoreTokens() ){
  35.              
  36.              String temporary = st.nextToken();
  37.              
  38.              if( temporary.equals("(")){ //The brackets are unneeded, but for the negative numbers the opening side is works as an operator.
  39.                  opStack.push(temporary);
  40.                  wasOperator = true;
  41.              } else if( (temporary.equals("-") ) && (wasOperator)){ //This is it. When the previous token was an operator, the "-" is just signs a negative number.
  42.                  //Yet the possibilitiy of too much minuses aren't examined.
  43.                  rpn[index] = Double.toString( -1 * Double.parseDouble(st.nextToken()) );
  44.                  index++;
  45.              } else if( temporary.equals(")")){ //The closing bracket means we have to empty the operators stack.
  46.                  while( !(opStack.peek().equals("(")) ){
  47.                      rpn[index] = opStack.pop();
  48.                      index++;
  49.                      wasOperator = false; //And, of course this means that the next tokan must be an operator.
  50.                  }
  51.                  opStack.pop();
  52.              }else if( Operator.isOperator(temporary) ){ //Here we push the operators to the stack, until they have a lower precedence than the actual.
  53.                  while( ( !(opStack.empty()) ) && ( !(opStack.peek().equals("(")) ) && (Operator.getPrecedence(opStack.peek()).byteValue() > Operator.getPrecedence(temporary).byteValue() ) ){
  54.                      rpn[index] = opStack.pop();
  55.                      index++;
  56.                  }
  57.                  opStack.push(temporary);
  58.                  wasOperator = true;
  59.              } else {
  60.                  rpn[index] = temporary;
  61.                  index++;
  62.                  wasOperator = false;
  63.              }
  64.              
  65.          }
  66.          while( !(opStack.empty()) ){ //We need emptying the stack. Just popping everything out from it.
  67.              rpn[index] = opStack.pop();
  68.              index++;
  69.          }
  70.          return rpn;
  71.        
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement