import easyIO.*; /** * ?velse i INF5110 v?ren 2006 * @author Arne Maus ************************/ class Tre{ //type-koder final static int NULL =0, NUMBER =1, EXP = 2, EXP1 = 3, EXP2=4; // Op-koder final static int ADD = 5, SUBTR = 6; // Error final static int ERROR = 7; static String getName(int type){ String [] n = {"NULL","NUMBER","EXP","EXP1","EXP2","ADD","SUBTR","ERROR"}; return n[type]; } public static void main(String [] args) { In in = new In(args[0]); String setning = in.inLine(); System.out.println("Test av setning:" + setning +"\n"); in.close(); Parser p = new Parser (new Scanner(args[0])); Node tre = p.parser(); tre.print(); System.out.println("\n"); // test av treet med utregneren Eval e = new Eval(); int v = e.exp(tre); System.out.println("Evaluering av venstre-tre av: " + setning +" er: " + v); } } class Scanner { In in; String token; boolean eof = false; Scanner(String fil) { in = new In(fil); } String nextToken() { if (in.endOfFile()) { eof =true; token = "$"; } else { token = in.inWord(); } return token; } boolean EOF () { return eof; } String getToken() { return token; } int getTokenValue(){ if (getTokenType() == Tre.NUMBER) return new Integer(token).intValue(); else return 0; } int getTokenType() { if (EOF()) return Tre.NULL ; else { char c = token.charAt(0); if (Character.isDigit(c) ) return Tre.NUMBER; else if (c== '+') return Tre.ADD; else if (c== '-') return Tre.SUBTR; else return Tre.ERROR; } } } class Parser { Scanner s; Parser (Scanner s) { this.s = s; } Node exp1(){ String t = s.nextToken(); Node ret = new Node (Tre.NUMBER,Tre.NULL,Tre.NULL); switch(s.getTokenType()) { case Tre.NUMBER: ret.verdi = s.getTokenValue(); ret= exp2(ret); break; default: ret.type = Tre.ERROR; } return ret; } Node exp2(Node v){ String t = s.nextToken(); Node ret = new Node (Tre.EXP2,Tre.NULL,Tre.NULL); switch(s.getTokenType()) { case Tre.NULL: ret = v; break; case Tre.ADD: case Tre.SUBTR: ret.op= s.getTokenType(); ret.sub[0] = v; s.nextToken(); if (s.getTokenType() == Tre.NUMBER) { ret.sub[1] = new Node(Tre.NUMBER, s.getTokenValue(),Tre.NULL); if (!s.EOF() )ret = exp2(ret); } } return ret; } Node parser() { return exp1(); } // lag metoder her } class Eval { int number (Node t) { if (t.type == Tre.NUMBER) return t.verdi; else return Tre.ERROR; } int exp (Node t) { int ret = 0; switch(t.type) { case Tre.NUMBER: ret = number(t); break; case Tre.EXP1: case Tre.EXP2: if (t.op == Tre.ADD) ret= exp (t.sub[0]) + t.sub[1].verdi; else if(t.op == Tre.SUBTR) ret = exp(t.sub[0]) - t.sub[1].verdi; } return ret; } // end exp } // end evalHTre class Node{ int type,verdi, op; Node [] sub = new Node[2]; void print () { if (type == Tre.NUMBER) { System.out.print("(NUMBER:" + verdi+")"); } else { System.out.print("(" + Tre.getName(op) +":"); if (sub[0] != null) sub[0].print(); System.out.print(","); if (sub[1] != null) sub[1].print(); System.out.print(")"); } } // end print Node (int type, int verdi, int op) { this.verdi = verdi; this.type = type; this.op = op; } }