Advertisement
Luninariel

StackParser - WIP

Mar 4th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.21 KB | None | 0 0
  1. import java.io.*;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.lang.*;
  5. import java.util.ArrayList;
  6. import java.util.Scanner;
  7.  
  8. /**
  9.  * This program will perform the following:
  10.  * Uses a scanner to read expressions from Expressions.txt
  11.  * Each expression in Expressions.txt uses a character # to signal the end of the equation.
  12.  * As the Expressions are read in they're placed into a Character Array express
  13.  * Several Array's are established for purposes explained below:
  14.  * Char [] Vart - A character array of variables used within the Equations. Values Range from A - F and then again 0-9.
  15.  * Int [] ivalue - An Int Array to hold the values of each Equation Variable. A - F are given their CHARACTERS value, as are 0-9.
  16.  * Char [] Opert - A character Array of the symbols used within each equation including @ - Exponent, % - Modulus, * - Times, / - Divide, + - Addition, - - Subtraction. ) - Right Paren, and ( - Left Paren
  17.  * Int [] IntValP - An Int Array that holds the priority of each operator from our Operator Equation.
  18.  * As each character from the express array is read, it is assigned as either an Operator, or Operand, given a value or priority, or placed within their relevant stack
  19.  * Math is performed in order of operations on each expression, as read from left to right. Parenthesis with highest priority, followed by Exponent, then Modulus having the same priority as Multiplication and Division and finally Addition and Subtraction with the lowest priority.
  20.  * The entire process is printed to make this appear as a character by character parser.
  21.  */
  22.  
  23. public class SinglePassParser {
  24.     public static void main(String[] args) throws Exception {
  25.  
  26.         //Create a scanner to read the file Expressions.text
  27.         try{
  28.             String Test;
  29.             Scanner input = new Scanner(new File("src/main/Expressions.txt"));
  30.  
  31.                 int i, num, ivalu;
  32.                 //A Character Array for the variables we'll be using A-F, and 0-9
  33.                 char[] vart = {'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
  34.  
  35.                 //A int array used to provide values for the Character Array vart, A=8, B=12, C=2, D=3, E=15, F=4, and 0-9 are exactly that.
  36.                 int[] ivalue = {8, 12, 2, 3, 15, 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  37.  
  38.                 //A Character array used to provide the Symbols used for Operators in our equations.
  39.                 char[] opert = {'@', '%', '*', '/', '+', '-', ')', '(', '#'};
  40.  
  41.                 //A int array used to provide priority for the Operator's used in our formulas.
  42.                 int[] intvalp = {3, 2, 2, 2, 1, 1, 99, -99, -100};
  43.             while(input.hasNextLine()){
  44.                 Test= input.next();
  45.  
  46.                 //A new instance of GenericManagerStacks meant to hold Integers, our Operands.
  47.                 GenericManagerStacks<Integer> opnd = new GenericManagerStacks<Integer>();
  48.  
  49.                 //A new instance of GenericManagerStacks meant to hold Operator Objects, our operators.
  50.                 GenericManagerStacks<Opertobj> oper = new GenericManagerStacks<Opertobj>();
  51.  
  52.                 System.out.println("pushing Operator #with priority -100");
  53.  
  54.                 //Our end character being added, the symbol we use to mark the end of an equation.
  55.                 Opertobj pnode1 = new Opertobj('#', -100);
  56.                 oper.pushnode(pnode1);
  57.                 int oprior, exvalue;
  58.  
  59.                 //Character array's for the formulas we are parsing.
  60.  
  61.                 char[] express = Test.toCharArray();
  62.  
  63.                 //Loops through each read of the equation, evaluating each caracter, peeking at the top of its particular stack, and placing it in the correct place it will continue this action until a # is found.
  64.                 i = 0;
  65.                 while (express[i] != '#') {
  66.                     System.out.println("Parsing " + express[i]);
  67.  
  68.                     if (((express[i] >= '0') && (express[i] <= '9')) || ((express[i] >= 'A') && (express[i] <= 'Z'))) {
  69.                         System.out.println("This is an operand " + express[i]);
  70.  
  71.                         ivalu = findval(express[i], vart, ivalue, 15);
  72.                         if (ivalu == -99) System.out.println("No value in table for" + express[i]);
  73.  
  74.                         System.out.println("We're pushing it on the operand stack " + ivalu);
  75.                         opnd.pushnode(ivalu);
  76.                     } else {
  77.                         System.out.println("This is an operator " + express[i]);
  78.  
  79.                         if (express[i] == '(') {
  80.                             System.out.println("Pushing on operator stack " + express[i]);
  81.                             Opertobj pnodeo = new Opertobj(express[i], -99);
  82.                             oper.pushnode(pnodeo);
  83.                         } else if (express[i] == ')') {
  84.                             while ((oper.peeknode()).operator != '(') {
  85.                                 popevalandpush(oper, opnd);
  86.                             }
  87.                             oper.popnode();
  88.                         } else {
  89.                             oprior = findval(express[i], opert, intvalp, 7);
  90.                             System.out.println("Peeking at top of stack " + (oper.peeknode()).priority);
  91.  
  92.                             while (oprior <= (oper.peeknode()).priority) popevalandpush(oper, opnd);
  93.                             System.out.println("pushing Operator " + express[i] + "with priority " + oprior);
  94.                             Opertobj pnodeo = new Opertobj(express[i], oprior);
  95.                             oper.pushnode(pnodeo);
  96.                         }
  97.                     }
  98.                     i++;
  99.                 }
  100.                 while ((oper.peeknode()).operator != '#') {
  101.                     popevalandpush(oper, opnd);
  102.                 }
  103.                 exvalue = opnd.popnode();
  104.                 System.out.println("the value for this expression is " + exvalue);
  105.  
  106.             }
  107.  
  108.         }
  109.         catch (FileNotFoundException e) {
  110.             System.err.println("File  was not found");
  111.         }
  112.  
  113.  
  114.     }
  115.  
  116.     //A method to check each operand and assign it a function.
  117.     public static int IntEval(int oper1, char oper, int oper2) {
  118.         int result = 0;
  119.  
  120.         switch (oper) {
  121.             case '+':
  122.                 result = oper1 + oper2;
  123.  
  124.                 return result;
  125.  
  126.             case '-':
  127.                 result = oper1 - oper2;
  128.                 System.out.println("***eval " + oper1 + oper + oper2 + "*result* " + result);
  129.                 return result;
  130.  
  131.             case '*':
  132.                 result = oper1 * oper2;
  133.                 System.out.println("***eval " + oper1 + oper + oper2 + "*result* " + result);
  134.                 return result;
  135.  
  136.             case '/':
  137.                 if (oper2 != 0) {
  138.                     result = oper1 / oper2;
  139.  
  140.                     System.out.println("***eval " + oper1 + oper + oper2 + "*result* " + result);
  141.                     return result;
  142.                 } else {
  143.                     System.out.println("attempted divide but zero not allowed");
  144.                     return -99;
  145.                 }
  146.             case '@':
  147.                 if (oper2 >= 1) {
  148.                     result = (int)Math.pow(oper1, oper2);
  149.                     System.out.println("***eval " + oper1 + oper + oper2 + "*result* " + result);
  150.                     return result;
  151.                 } else {
  152.                     System.out.println("Attempted to raise to negative or zero power");
  153.                     return 1;
  154.                 }
  155.             case '%':
  156.                 result = oper1 % oper2;
  157.                 System.out.println("***eval " + oper1 + oper + oper2 + "*result* " + result);
  158.                 return result;
  159.  
  160.             default:
  161.                 System.out.println("bad operator " + oper);
  162.                 return -99;
  163.         }
  164.     }
  165.  
  166.     //Finds the value of each character and returns its value
  167.     public static int findval(char x, char[] vtab, int[] valtb, int last) {
  168.         int i, vreturn = -99;
  169.  
  170.         for (i = 0; i <= last; i++)
  171.             if (vtab[i] == x) vreturn = valtb[i];
  172.         System.out.println("Found this char: " + x + " value is: " + vreturn);
  173.         return vreturn;
  174.     }
  175.     //Pops, evaluates, and Pushes the stacks as needed.
  176.     public static void popevalandpush(GenericManagerStacks<Opertobj> x, GenericManagerStacks<Integer> y) {
  177.         int a, b, c;
  178.         char operandx;
  179.         operandx = (x.popnode()).GetOpert();
  180.         a = y.popnode();
  181.         b = y.popnode();
  182.         System.out.println("in popeval " + b + operandx + a);
  183.         c = IntEval(b, operandx, a);
  184.         y.pushnode(c);
  185.         return;
  186.     }
  187. }
  188.  
  189. //The Generic Manager Stacks class
  190. class GenericManagerStacks<T> {
  191.     protected ArrayList<T> mystack;
  192.     protected int number;
  193.  
  194.     //Constructor
  195.     public GenericManagerStacks() {
  196.         number = 0;
  197.  
  198.         mystack = new ArrayList<T>(100);
  199.     }
  200.  
  201.     //Getter for Number
  202.     public int getnumber() {
  203.         return number;
  204.     }
  205.  
  206.     //Pushes the node
  207.     public int pushnode(T x) {
  208.         System.out.println("in pushnode " + number + " x is " + x);
  209.  
  210.         mystack.add(number, x);
  211.         number++;
  212.         System.out.println("Leaving pushnode");
  213.         return number;
  214.     }
  215.  
  216.     // Pops the next node
  217.     public T popnode() {
  218.         T nodeval;
  219.  
  220.         nodeval = mystack.get(number - 1);
  221.         mystack.remove(number - 1);
  222.         number--;
  223.         return nodeval;
  224.     }
  225.  
  226.     //Peeks at the top of the stack
  227.     public T peeknode() {
  228.         T nodeval;
  229.  
  230.         nodeval = mystack.get(number - 1);
  231.         return nodeval;
  232.     }
  233.  
  234.     //Checks if the stack is empty
  235.     boolean stackempty() {
  236.         if (number == 0) return true;
  237.         else return false;
  238.     }
  239.  
  240. }
  241.  
  242. //The class that holds the operator objects and their priority
  243. class Opertobj {
  244.     protected char operator;
  245.     protected int priority;
  246.  
  247.     //Constructor
  248.     public Opertobj(char opert, int pri) {
  249.         operator = opert;
  250.         priority = pri;
  251.     }
  252.  
  253.     //Getter for priority
  254.     public int GetPrior() {
  255.         return priority;
  256.     }
  257.  
  258.     //Getter for Operator
  259.     public char GetOpert() {
  260.         return operator;
  261.     }
  262.  
  263.  
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement