1 22 23 package org.javacc.parser; 24 25 import java.util.Vector ; 26 27 public class LookaheadWalk { 28 29 public static boolean considerSemanticLA; 30 31 public static Vector sizeLimitedMatches; 32 33 public static void vectorAppend(Vector vToAppendTo, Vector vToAppend) { 34 for (int i = 0; i < vToAppend.size(); i++) { 35 vToAppendTo.addElement(vToAppend.elementAt(i)); 36 } 37 } 38 39 public static Vector genFirstSet(Vector partialMatches, Expansion exp) { 40 if (exp instanceof RegularExpression) { 41 Vector retval = new Vector (); 42 for (int i = 0; i < partialMatches.size(); i++) { 43 MatchInfo m = (MatchInfo)partialMatches.elementAt(i); 44 MatchInfo mnew = new MatchInfo(); 45 for (int j = 0; j < m.firstFreeLoc; j++) { 46 mnew.match[j] = m.match[j]; 47 } 48 mnew.firstFreeLoc = m.firstFreeLoc; 49 mnew.match[mnew.firstFreeLoc++] = ((RegularExpression)exp).ordinal; 50 if (mnew.firstFreeLoc == MatchInfo.laLimit) { 51 sizeLimitedMatches.addElement(mnew); 52 } else { 53 retval.addElement(mnew); 54 } 55 } 56 return retval; 57 } else if (exp instanceof NonTerminal) { 58 NormalProduction prod = ((NonTerminal)exp).prod; 59 if (prod instanceof JavaCodeProduction) { 60 return new Vector (); 61 } else { 62 return genFirstSet(partialMatches, prod.expansion); 63 } 64 } else if (exp instanceof Choice) { 65 Vector retval = new Vector (); 66 Choice ch = (Choice)exp; 67 for (int i = 0; i < ch.choices.size(); i++) { 68 Vector v = genFirstSet(partialMatches, (Expansion)ch.choices.elementAt(i)); 69 vectorAppend(retval, v); 70 } 71 return retval; 72 } else if (exp instanceof Sequence) { 73 Vector v = partialMatches; 74 Sequence seq = (Sequence)exp; 75 for (int i = 0; i < seq.units.size(); i++) { 76 v = genFirstSet(v, (Expansion)seq.units.elementAt(i)); 77 if (v.size() == 0) break; 78 } 79 return v; 80 } else if (exp instanceof OneOrMore) { 81 Vector retval = new Vector (); 82 Vector v = partialMatches; 83 OneOrMore om = (OneOrMore)exp; 84 while (true) { 85 v = genFirstSet(v, om.expansion); 86 if (v.size() == 0) break; 87 vectorAppend(retval, v); 88 } 89 return retval; 90 } else if (exp instanceof ZeroOrMore) { 91 Vector retval = new Vector (); 92 vectorAppend(retval, partialMatches); 93 Vector v = partialMatches; 94 ZeroOrMore zm = (ZeroOrMore)exp; 95 while (true) { 96 v = genFirstSet(v, zm.expansion); 97 if (v.size() == 0) break; 98 vectorAppend(retval, v); 99 } 100 return retval; 101 } else if (exp instanceof ZeroOrOne) { 102 Vector retval = new Vector (); 103 vectorAppend(retval, partialMatches); 104 vectorAppend(retval, genFirstSet(partialMatches, ((ZeroOrOne)exp).expansion)); 105 return retval; 106 } else if (exp instanceof TryBlock) { 107 return genFirstSet(partialMatches, ((TryBlock)exp).exp); 108 } else if (considerSemanticLA && 109 exp instanceof Lookahead && 110 ((Lookahead)exp).action_tokens.size() != 0 111 ) { 112 return new Vector (); 113 } else { 114 Vector retval = new Vector (); 115 vectorAppend(retval, partialMatches); 116 return retval; 117 } 118 } 119 120 public static void vectorSplit(Vector toSplit, Vector mask, Vector partInMask, Vector rest) { 121 OuterLoop: 122 for (int i = 0; i < toSplit.size(); i++) { 123 for (int j = 0; j < mask.size(); j++) { 124 if (toSplit.elementAt(i) == mask.elementAt(j)) { 125 partInMask.addElement(toSplit.elementAt(i)); 126 continue OuterLoop; 127 } 128 } 129 rest.addElement(toSplit.elementAt(i)); 130 } 131 } 132 133 public static Vector genFollowSet(Vector partialMatches, Expansion exp, long generation) { 134 if (exp.myGeneration == generation) { 135 return new Vector (); 136 } 137 exp.myGeneration = generation; 139 if (exp.parent == null) { 140 Vector retval = new Vector (); 141 vectorAppend(retval, partialMatches); 142 return retval; 143 } else if (exp.parent instanceof NormalProduction) { 144 Vector parents = ((NormalProduction)exp.parent).parents; 145 Vector retval = new Vector (); 146 for (int i = 0; i < parents.size(); i++) { 148 Vector v = genFollowSet(partialMatches, (Expansion)parents.elementAt(i), generation); 149 vectorAppend(retval, v); 150 } 151 return retval; 152 } else if (exp.parent instanceof Sequence) { 153 Sequence seq = (Sequence)exp.parent; 154 Vector v = partialMatches; 155 for (int i = exp.ordinal+1; i < seq.units.size(); i++) { 156 v = genFirstSet(v, (Expansion)seq.units.elementAt(i)); 157 if (v.size() == 0) return v; 158 } 159 Vector v1 = new Vector (); 160 Vector v2 = new Vector (); 161 vectorSplit(v, partialMatches, v1, v2); 162 if (v1.size() != 0) { 163 v1 = genFollowSet(v1, seq, generation); 165 } 166 if (v2.size() != 0) { 167 v2 = genFollowSet(v2, seq, Expansion.nextGenerationIndex++); 169 } 170 vectorAppend(v2, v1); 171 return v2; 172 } else if (exp.parent instanceof OneOrMore || exp.parent instanceof ZeroOrMore) { 173 Vector moreMatches = new Vector (); 174 vectorAppend(moreMatches, partialMatches); 175 Vector v = partialMatches; 176 while (true) { 177 v = genFirstSet(v, exp); 178 if (v.size() == 0) break; 179 vectorAppend(moreMatches, v); 180 } 181 Vector v1 = new Vector (); 182 Vector v2 = new Vector (); 183 vectorSplit(moreMatches, partialMatches, v1, v2); 184 if (v1.size() != 0) { 185 v1 = genFollowSet(v1, (Expansion)exp.parent, generation); 187 } 188 if (v2.size() != 0) { 189 v2 = genFollowSet(v2, (Expansion)exp.parent, Expansion.nextGenerationIndex++); 191 } 192 vectorAppend(v2, v1); 193 return v2; 194 } else { 195 return genFollowSet(partialMatches, (Expansion)exp.parent, generation); 197 } 198 } 199 200 public static void reInit() 201 { 202 considerSemanticLA = false; 203 sizeLimitedMatches = null; 204 } 205 206 } 207 | Popular Tags |