1 package fri.patterns.interpreter.parsergenerator.parsertables; 2 3 import java.util.*; 4 import fri.patterns.interpreter.parsergenerator.syntax.*; 5 6 14 15 class LALRSyntaxNode extends LRSyntaxNode 16 { 17 18 public LALRSyntaxNode(Nullable nullable, FirstSets firstSets) { 19 super(nullable, firstSets); 20 } 21 22 23 24 protected SLRSyntaxNode createSyntaxNode() { 25 return new LALRSyntaxNode(nullable, firstSets); 26 } 27 28 32 protected RuleStateItem createRuleStateItem(int ruleIndex, Rule rule) { 33 LALRRuleStateItem item = new LALRRuleStateItem(ruleIndex, rule); 34 addStartLookahead(item, ruleIndex); 35 return item; 36 } 37 38 39 public List build(Syntax syntax, List syntaxNodes, Hashtable kernels) { 40 syntaxNodes = super.build(syntax, syntaxNodes, kernels); 41 42 for (int i = 0; i < syntaxNodes.size(); i++) { 44 LALRSyntaxNode node = (LALRSyntaxNode) syntaxNodes.get(i); 45 for (Enumeration e = node.entries.elements(); e.hasMoreElements(); ) { 46 LALRRuleStateItem item = (LALRRuleStateItem) e.nextElement(); 47 item.propagateLookaheads(null); 48 } 49 } 50 return syntaxNodes; 51 } 52 53 58 protected void addRulesDerivingPendingNonTerminal(RuleStateItem itm, String nonterm, Syntax syntax, List newItems) { 59 62 LALRRuleStateItem item = (LALRRuleStateItem) itm; 63 boolean needsPropagation = false; 64 List lookahead = null; 65 66 for (int i = 0; i < syntax.size(); i++) { 67 Rule rule = syntax.getRule(i); 68 69 if (rule.getNonterminal().equals(nonterm)) { 70 LALRRuleStateItem rsi = (LALRRuleStateItem) createRuleStateItem(i, rule); 71 72 LALRRuleStateItem existing = (LALRRuleStateItem)entries.get(rsi); 74 if (existing != null) { 75 rsi = existing; 76 } 77 else { entries.put(rsi, rsi); newItems.add(rsi); } 81 82 if (lookahead == null) 83 needsPropagation = item.calculateLookahead( 86 lookahead = new ArrayList(), 87 nullable, 88 firstSets); 89 90 rsi.addLookahead(lookahead.iterator()); 92 if (needsPropagation) item.addPropagate(rsi); } 95 } 96 } 97 98 99 103 protected void linkParentItemToChild(RuleStateItem parent, int newIndex, List syntaxNodes, RuleStateItem child) { 104 LALRRuleStateItem pnt = (LALRRuleStateItem) parent; 105 pnt.followNodeIndex = newIndex; 106 107 LALRSyntaxNode node = (LALRSyntaxNode) syntaxNodes.get(newIndex); 108 109 LALRRuleStateItem rsi = (LALRRuleStateItem) node.entries.get(child); 111 112 pnt.addPropagate(rsi); 114 } 115 116 117 118 119 120 124 protected class LALRRuleStateItem extends LRRuleStateItem 125 { 126 boolean needsPropagation = false; 127 Stack propagateItems = new Stack(); 128 129 public LALRRuleStateItem(int ruleIndex, Rule rule) { 130 super(ruleIndex, rule); 131 } 132 133 protected LALRRuleStateItem(RuleStateItem orig) { 134 super(orig); 135 } 136 137 138 protected RuleStateItem createRuleStateItem(RuleStateItem orig) { 139 return new LALRRuleStateItem(orig); 140 } 141 142 143 void addPropagate(RuleStateItem item) { 144 propagateItems.push(item); 145 needsPropagation = true; 146 } 147 148 149 void propagateLookaheads(Iterator originatorLookahead) { 150 boolean change = false; 151 152 if (needsPropagation == false && (originatorLookahead == null || originatorLookahead.hasNext() == false)) 154 return; 155 156 if (originatorLookahead != null) 157 change = addLookahead(originatorLookahead); 159 if (change || needsPropagation) { 161 needsPropagation = false; 162 163 for (int i = 0; i < propagateItems.size(); i++) { 164 LALRRuleStateItem item = (LALRRuleStateItem) propagateItems.get(i); 165 item.propagateLookaheads(lookahead.keySet().iterator()); 166 } 167 } 168 } 169 170 171 public boolean equals(Object o) { 172 RuleStateItem item = (RuleStateItem)o; 173 return ruleIndex == item.ruleIndex && pointerPosition == item.pointerPosition; 174 } 175 176 177 public int hashCode() { 178 if (hashCache == null) 179 hashCache = new Integer (ruleIndex * 13 + pointerPosition); 180 return hashCache.intValue(); 181 } 182 183 } 185 } | Popular Tags |