1 package ro.infoiasi.donald.compiler.lexer; 2 3 import ro.infoiasi.donald.compiler.lexer.exceptions.*; 4 import ro.infoiasi.donald.compiler.simple.*; 5 import java.util.*; 6 7 public class ExpTree extends BinTree { 8 private Alphabet alpha = new Alphabet(); 9 10 public ExpTree() { 11 super(); 12 } 13 14 public ExpTree(ExpList el) throws ExpParseError { 15 super(); 16 alpha = el.getAlphabet(); 17 addRootSubTree(create(el)); 18 } 19 20 private static ExpTree create(AbstractList el) throws ExpParseError { 21 el = removeParenthesis(el); 22 ExpTree et = new ExpTree(); 23 if (!el.isEmpty()) { 24 OperatorToken minOpToken = null; 25 int minI = 0; 26 for (int i = 0; i<el.size(); i++) { 27 Class c = el.get(i).getClass(); 28 if (c == OperatorToken.class) { 29 OperatorToken opToken = (OperatorToken)el.get(i); 30 if (minOpToken == null || 31 (minOpToken.compareTo(opToken)>0)) { 32 minOpToken = opToken; 33 minI = i; 34 } 35 } else if (c == ParenthesisLeft.class) { 36 ParenthesisLeft paraLeft = (ParenthesisLeft)el.get(i); 38 ParenthesisRight paraRight = paraLeft.pair(); 39 i += paraRight.tokenNo() - paraLeft.tokenNo(); 40 } 41 } 42 if (minOpToken == null) { 44 if (el.size()>1 || el.get(0).getClass() != SymbolToken.class) { 45 throw new ExpParseError("No operator found", 46 ((ExpToken)el.get(0)).strIndex(), 47 ((ExpToken)el.get(el.size()-1)).strIndex()); 48 } else { 49 et.addRoot(el.get(0)); 50 } 51 } else { 52 et.addRoot(minOpToken); 53 if (minOpToken.operator().isUnaryLeft()) { 54 if (minI != el.size()-1) { 55 throw new ExpParseError("Operator is unary and binds to the left only", 56 minOpToken.strIndex()); 57 } 58 ExpTree et1 = create((AbstractList)(el.subList(0,minI))); 59 if (et1.IsEmpty()) { 60 throw new ExpParseError("Operator is unary and binds to the left only", 61 minOpToken.strIndex()); 62 } 63 et.addLeftSubTree(et1, et.root()); 64 } else if (minOpToken.operator().isUnaryRight()) { 65 66 } else if (minOpToken.operator().isBinary()) { 67 ExpTree et1 = create((AbstractList)(el.subList(0,minI))); 68 ExpTree et2 = create((AbstractList)(el.subList(minI+1,el.size()))); 69 if (et1.IsEmpty() || et2.IsEmpty()) { 70 throw new ExpParseError("Operator is binary", minOpToken.strIndex()); 72 } 73 et.addLeftSubTree(et1, et.root()); 74 et.addRightSubTree(et2, et.root()); 75 } else { 76 throw new ExpParseError("Operator is not unary or binary", 77 ((ExpToken)el.get(0)).strIndex()); 78 } 79 } 80 } 81 return et; 82 } 83 84 private static AbstractList removeParenthesis(AbstractList el) { 85 boolean over = false; 86 while (!el.isEmpty() && !over) { 87 over = true; 88 if (el.get(0).getClass() == ParenthesisLeft.class && 89 el.get(el.size()-1).getClass() == ParenthesisRight.class) { 90 ParenthesisLeft paraLeft = (ParenthesisLeft)el.get(0); 91 ParenthesisRight paraRight = (ParenthesisRight)el.get(el.size()-1); 92 if (paraLeft.pair() == paraRight) { 93 el = (AbstractList)el.subList(1,el.size()-1); 94 over = false; 95 } 96 } 97 } 98 return el; 99 } 100 101 public Alphabet getAlphabet() { 102 return alpha; 103 } 104 } 105 | Popular Tags |