/* Autor: Dieter Schultschik * MatNr: 0125258 * email: e0125258@student.tuwien.ac.at */ package Wider; import eprog.*; import java.util.*; class WidParser extends EprogIO { private static int iEprogCounter = 0; static private final int EPROG_LIMIT = 15; static private final char OPEN_SEQUENCE = '('; static private final char CLOSE_SEQUENCE = ')'; static private final char OP_SERIAL = '+'; static private final char OP_PARALLEL = '/'; static private final int MAX_INTEGER_VALUE = Integer.MAX_VALUE; static float parse(String sInit) throws ParseErrorException { Vector vWidValues = new Vector(); final char OPERATOR_NOT_SET = 'x'; char cOperator = OPERATOR_NOT_SET; float result = 0; int iOpPos = 0; if(FLAGS.DEBUG) println("New ParserCall()"); //solange unnötige Klammern im String sind while(sInit != (sInit = killRedundantDelimiters(sInit))); if(FLAGS.DEBUG) println("After 'killRedundantDelimiters':\t" + sInit); //wenn kein Operator mehr vorhanden ist, wird versucht den wert zu bekommen if(sInit.indexOf(OP_SERIAL)== -1 && sInit.indexOf(OP_PARALLEL)== -1) { try{ if(FLAGS.DEBUG) println("Only 1 Expression in Sequence... returning it!"); return getParsedValue(sInit); } catch(ParseErrorException e){ throw e; } } while(sInit.length() > 0) { if(FLAGS.DEBUG) println("in main while loop:\t\t\t" +sInit); //wenn wir auf eine Klammer stossen if(sInit.charAt(0) == OPEN_SEQUENCE) { try { iOpPos = getSubSequence(sInit); } catch(ParseErrorException e){ throw e; } //(A+B+C)/D -> A+B+C vWidValues.add(new Float(parse(sInit.substring(1,iOpPos)))); if(++iOpPos == sInit.length()) break; else //gleicher Operator oder noch keiner gesetzt if(cOperator == sInit.charAt(iOpPos) || cOperator == OPERATOR_NOT_SET) cOperator = sInit.charAt(iOpPos); else throw new ParseErrorException("Operator mismatch"); } else { iOpPos = getPosOfNextOperator(sInit); if(iOpPos != -1 && iOpPos != sInit.length()-1) if(cOperator == sInit.charAt(iOpPos) || cOperator == OPERATOR_NOT_SET) cOperator = sInit.charAt(iOpPos); else throw new ParseErrorException("Operator mismatch"); else iOpPos = sInit.length(); try{ vWidValues.add(new Float(getParsedValue(sInit.substring(0,iOpPos)))); } catch(ParseErrorException e){ throw e; } //nicht mehr als 15 widerstände erlaubt. if(++iEprogCounter > EPROG_LIMIT) throw new ParseErrorException("more than 15"); } sInit = sInit.substring((iOpPos == sInit.length() ? iOpPos:++iOpPos),sInit.length()); } switch(cOperator){ case OP_SERIAL: for(int j=0; j < vWidValues.size(); j++) result += ((Float)(vWidValues.get(j))).floatValue(); break; case OP_PARALLEL: for(int j=0; j < vWidValues.size(); j++) result += 1/((Float)(vWidValues.get(j))).floatValue(); result = 1 / result; break; default: throw new ParseErrorException("Syntax fehler"); } if(FLAGS.DEBUG) println("End of Parser; result =\t\t\t" + result); return result; } static private int getSubSequence(String sSequence) throws ParseErrorException { if(FLAGS.DEBUG) println("start of 'getSubSequence':\t\t" + sSequence); int iCounter=0; for(int i=0; i < sSequence.length(); i++) { if(sSequence.charAt(i)== OPEN_SEQUENCE) iCounter++; if(sSequence.charAt(i)== CLOSE_SEQUENCE)iCounter--; if(iCounter == 0) return i; } throw new ParseErrorException("Nesting level"); } static private int getPosOfNextOperator(String sSequence) { if(FLAGS.DEBUG) println("start of 'getPosOfNextOperator':\t" + sSequence); for(int i=0; i 0 && sSequence.charAt(0)== OPEN_SEQUENCE && sSequence.charAt(sSequence.length()-1)== CLOSE_SEQUENCE) return sSequence.substring(1,sSequence.length()-1); else return sSequence; } static private float getParsedValue(String sVal) throws ParseErrorException { double dTmp; if(FLAGS.DEBUG) println("start of 'getParsedValue':\t\t" + sVal); try{ dTmp = Double.parseDouble(sVal); } catch(NumberFormatException e){ throw new ParseErrorException("Number Format"); } if(dTmp > MAX_INTEGER_VALUE) throw new ParseErrorException("max integer"); return (float)dTmp; } }