/*********************************************************************************** * Freeman Lo * * This Compiler reads from a math equation and generates an output of * assembler code with push, add, mul, div, sub, and prtpop * the interpreter stack machine then interpretes the code and generates a math number * as a result. It uses recursive decent to create the formula and pop onto a * temporary stack machine for processing. * *************************************************************************************/ import java.io.*; import java.util.*; class Overflow extends Exception { } class Underflow extends Exception { } class Compiler { private static String line="", temp="", word=""; private static StringTokenizer token = null; private static int numTok=0; private static short a=0; private static short b=0; private static Stack stak=null; public static void expr() { term(); while ( word.charAt(0) == '+' || word.charAt(0) == '-' ) { if ( word.charAt(0) == '+') word = "add"; else if ( word.charAt(0) == '-' ) word = "sub"; try { stak.push(word); } catch(Overflow of){ } getToken(); term(); System.out.println(stak.topAndPop()); } // end while }// end expr public static void term() { try { factor(); } catch( Overflow of) { System.out.println(of); } while ( word.charAt(0) == '*' || word.charAt(0) == '/' ) { if ( word.charAt(0) == '*') word = "mul"; else if ( word.charAt(0) == '/' ) word = "div"; try { stak.push(word); // * } catch (Overflow of){ } getToken(); // num try { factor(); } catch (Overflow of){ } System.out.println(stak.topAndPop()); } // end while }// end term public static void factor() throws Overflow{ // this checks for DIGITS, (, ) // perhaps i should add a if statement here to // get the precedence of the stuff. if ( Character.isDigit(word.charAt(0)) ) { System.out.println("push " + word); // push 2, 3 getToken(); } else if ( word.charAt(0) == '(' ) { getToken(); expr(); if ( word.charAt(0) == ')' ) getToken(); else error(); } // end if else error(); } // end factor public static void getToken() { word = token.nextToken(); word = word.trim(); }// end getToken public static void writeFile() throws IOException{ // this allows the use of the redirection symbol > BufferedWriter bw = null; bw = new BufferedWriter( new OutputStreamWriter(System.out)); // bufferedwriter inheritates methods from class Writer // therefore i can insert strings into the write method } // end writeFile public static void readFile() throws IOException{ // this allows the use of the redirection symbol < BufferedReader br=null; br = new BufferedReader( new InputStreamReader(System.in)); // this unfortunately works and works well for all cases which is good // i don't want it to be foobar after a full day of ISSUES. while ( (temp=br.readLine()) !=null ) { line = line + temp + " "; } // end while token = new StringTokenizer(line); } // end readFile public static void initStack() { numTok = token.countTokens(); stak = new Stack(numTok); } public static void error() { System.out.println("You have screwed it up, dude"); } public static void main ( String [] args ) throws IOException { readFile(); initStack(); getToken(); expr(); System.out.println("prtpop"); } // end main } // end class