import java.io.*; import java.util.*; import syntaxTree.*; import globalEnv.*; public class StatementParser { private static StreamTokenizer in; private static char lParen = '('; private static char rParen = ')'; public static void main(String args[]) throws IOException { if (args.length < 1) throw new IOException("Supply a filename on command line"); FileReader fileIn = new FileReader(args[0]); echoFile(fileIn); fileIn = new FileReader(args[0]); in = new StreamTokenizer(fileIn); in.ordinaryChar('/'); in.ordinaryChar('-'); try { parseStatements();} catch (IOException e) { System.out.println(e);}} private static void echoFile(FileReader fileIn) throws IOException { int b; while ((b = fileIn.read()) != -1) System.out.write(b); System.out.println(); System.out.println(); fileIn.close();} private static void parseStatements() throws IOException { BinNonTermNd root; int i = 1; while (in.nextToken() != StreamTokenizer.TT_EOF) { in.pushBack(); System.out.println("Processing statement " + i++ + "..."); root = parseStatement(); root.printTree(); System.out.println("Value is: " + root.evaluate()); System.out.println();} System.out.println("The symbols and their final values are:"); Enumeration e = GlobalEnv.theSymbols(); while (e.hasMoreElements()) { String name = (String)e.nextElement(); System.out.println(name + " = " + GlobalEnv.valueInEnv(name));}} private static AbstractNode parseExpression() throws IOException { // System.out.println("Expression called"); AbstractNode root = parseTerm(); in.nextToken(); char op; while ((in.ttype == '+') || (in.ttype == '-')) { op = (char)in.ttype; // System.out.println(String.valueOf(op)); BinNonTermNd temp = new BinNonTermNd(); temp.setLeft(root); temp.setRight(parseTerm()); temp.setOp(op); root = temp; in.nextToken();} in.pushBack(); // System.out.println("Expression finished"); return root;} private static NumericTermNode parseUnsignedNumber() throws IOException { // System.out.println("Number called"); in.nextToken(); if (in.ttype != StreamTokenizer.TT_NUMBER) throw new IOException("Number expected"); return new NumericTermNode(in.nval);} }