Advertisement
Vita_Harvey

Parser_A

Mar 9th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. package csc143.data_structures;
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5. import java.awt.event.*;
  6. import java.util.*;
  7.  
  8. /**
  9.  * @author Vita Wiebe
  10.  * @version PA9: Stack/Queue Use, Parser
  11.  * This program takes a mathematical expression, user-input in
  12.  * the standard, infix notation, and produces an equivalent
  13.  * expression written in postfix notation.
  14.  */
  15. public class Parser extends UnboundedArrayQueue implements ParserInterface {
  16.  
  17.    // Fields
  18.    
  19.    // String s, the input string from user.
  20.    // Pre-condition: non-empty.
  21.    private String s;
  22.    
  23.    // Our data structures, for handling the input, reordering,
  24.    // and eventual output of the postfix form of input.
  25.    private UnboundedArrayQueue inputQ;
  26.    private UnboundedArrayQueue outputQ;
  27.    private UnboundedArrayStack transformStack;
  28.    
  29.    // For comparison when parsing the input string.
  30.    public final String operators = "+-/*%";
  31.    
  32.    /**
  33.     * Our class constructor.
  34.     */
  35.    public Parser() {      
  36.    }
  37.    
  38.    /**
  39.     * This method parses the input string into individual tokens.
  40.     * <p>
  41.     * For the minimal version of the assignment, this means breaking  
  42.     * the input string into single-character tokens, discarding all
  43.     * white space.
  44.     * For the standard version of the assignment, this entails
  45.     * breaking the input string into contiguous "runs" of alphanumeric
  46.     * characters (identifiers and integers) and punctuation (operators).
  47.     * <p>
  48.     * Negative numbers are not handled by this tokenizer.
  49.     *
  50.     * @param input The input string.
  51.     * @return A queue object containing the individual tokens.
  52.     */
  53.    public UnboundedArrayQueue<String> tokenizeInput(String input) {
  54.    
  55.       s = input;
  56.       String sChar;
  57.       inputQ = new UnboundedArrayQueue();
  58.      
  59.       // Iterate over input string, s.
  60.       // If not whitespace or operator.      
  61.       for(int i = 0; i < s.length(); ) {                
  62.          char c = s.charAt(i);
  63.          if (c == ' ') {
  64.             i++;            
  65.          } else if (operators.contains(Character.toString(c))) {          
  66.             i++;
  67.          } else {
  68.             // will i need to convert to String here too to add to
  69.             // string queue?
  70.             inputQ.add(c);
  71.             i++;
  72.          }    
  73.       }
  74.      
  75.       // Iterate over input string again to find operators.
  76.       // Add these to the inputQ next.
  77.       for(int i = 0; i < s.length(); i++) {
  78.          char c = s.charAt(i);
  79.          if(operators.contains(Character.toString(c))) {
  80.             inputQ.add(c);
  81.          }        
  82.       }
  83.    
  84.    }
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement