nfastate.java
来自「java 编译器java复杂编译器,可以编译java文件的类库」· Java 代码 · 共 2,249 行 · 第 1/5 页
JAVA
2,249 行
continue; needUpdate = false; for (j = 0; j < nameSet.length; j++) { if (nameSet[j] == -1) continue; if (live[j] && ElemOccurs(nameSet[j], tmpSet) >= 0) { if (!needUpdate) { needUpdate = true; commonFreq++; } count[freq[j]]--; count[commonFreq]++; freq[j] = commonFreq; } } if (needUpdate) { int foundFreq = -1; blockLen = 0; for (j = 0; j <= commonFreq; j++) if (count[j] > blockLen) { foundFreq = j; blockLen = count[j]; } if (blockLen <= 1) return false; for (j = 0; j < nameSet.length; j++) if (nameSet[j] != -1 && freq[j] != foundFreq) { live[j] = false; count[freq[j]]--; } } } if (blockLen <= 1) return false; int[] commonBlock = new int[blockLen]; int cnt = 0; //System.out.println("Common Block for " + set + " : "); for (i = 0; i < nameSet.length; i++) { if (live[i]) { if (((NfaState)indexedAllStates.elementAt(nameSet[i])).isComposite) return false; stateDone[nameSet[i]] = true; commonBlock[cnt++] = nameSet[i]; //System.out.print(nameSet[i] + ", "); } } //System.out.println(""); String s = GetStateSetString(commonBlock); e = allNextStates.keys(); Outer : while (e.hasMoreElements()) { int at; boolean firstOne = true; String stringToFix; int[] setToFix = (int[])allNextStates.get(stringToFix = (String)e.nextElement()); if (setToFix == commonBlock) continue; for (int k = 0; k < cnt; k++) { if ((at = ElemOccurs(commonBlock[k], setToFix)) >= 0) { if (!firstOne) setToFix[at] = -1; firstOne = false; } else continue Outer; } if (stateSetsToFix.get(stringToFix) == null) stateSetsToFix.put(stringToFix, setToFix); } next.usefulEpsilonMoves -= blockLen - 1; AddCompositeStateSet(s, false); return true; } private boolean CheckNextOccursTogether() { if (next == null || next.usefulEpsilonMoves <= 1) return true; String set = next.epsilonMovesString; int[] nameSet = (int[])allNextStates.get(set); if (nameSet.length == 1 || compositeStateTable.get(set) != null || stateSetsToFix.get(set) != null) return false; int i; Hashtable occursIn = new Hashtable(); NfaState tmp = (NfaState)allStates.elementAt(nameSet[0]); for (i = 1; i < nameSet.length; i++) { NfaState tmp1 = (NfaState)allStates.elementAt(nameSet[i]); if (tmp.inNextOf != tmp1.inNextOf) return false; } int isPresent, j; Enumeration e = allNextStates.keys(); while (e.hasMoreElements()) { String s; int[] tmpSet = (int[])allNextStates.get(s = (String)e.nextElement()); if (tmpSet == nameSet) continue; isPresent = 0; Outer: for (j = 0; j < nameSet.length; j++) { if (ElemOccurs(nameSet[j], tmpSet) >= 0) isPresent++; else if (isPresent > 0) return false; } if (isPresent == j) { if (tmpSet.length > nameSet.length) occursIn.put(s, tmpSet); /* May not need. But safe. */ if (compositeStateTable.get(s) != null || stateSetsToFix.get(s) != null) return false; } else if (isPresent != 0) return false; } e = occursIn.keys(); while (e.hasMoreElements()) { String s; int[] setToFix = (int[])occursIn.get(s = (String)e.nextElement()); if (stateSetsToFix.get(s) == null) stateSetsToFix.put(s, setToFix); for (int k = 0; k < setToFix.length; k++) if (ElemOccurs(setToFix[k], nameSet) > 0) // Not >= since need the first one (0) setToFix[k] = -1; } next.usefulEpsilonMoves = 1; AddCompositeStateSet(next.epsilonMovesString, false); return true; } private static void FixStateSets() { Hashtable fixedSets = new Hashtable(); Enumeration e = stateSetsToFix.keys(); int[] tmp = new int[generatedStates]; int i; while (e.hasMoreElements()) { String s; int[] toFix = (int[])stateSetsToFix.get(s = (String)e.nextElement()); int cnt = 0; //System.out.print("Fixing : "); for (i = 0; i < toFix.length; i++) { //System.out.print(toFix[i] + ", "); if (toFix[i] != -1) tmp[cnt++] = toFix[i]; } int[] fixed = new int[cnt]; System.arraycopy(tmp, 0, fixed, 0, cnt); fixedSets.put(s, fixed); allNextStates.put(s, fixed); //System.out.println(" as " + GetStateSetString(fixed)); } for (i = 0; i < allStates.size(); i++) { NfaState tmpState = (NfaState)allStates.elementAt(i); int[] newSet; if (tmpState.next == null || tmpState.next.usefulEpsilonMoves == 0) continue; /*if (compositeStateTable.get(tmpState.next.epsilonMovesString) != null) tmpState.next.usefulEpsilonMoves = 1; else*/ if ((newSet = (int[])fixedSets.get(tmpState.next.epsilonMovesString)) != null) tmpState.FixNextStates(newSet); } } private final void FixNextStates(int[] newSet) { next.usefulEpsilonMoves = newSet.length; //next.epsilonMovesString = GetStateSetString(newSet); } private static boolean Intersect(String set1, String set2) { if (set1 == null || set2 == null) return false; int[] nameSet1 = (int[])allNextStates.get(set1); int[] nameSet2 = (int[])allNextStates.get(set2); if (nameSet1 == null || nameSet2 == null) return false; if (nameSet1 == nameSet2) return true; for (int i = nameSet1.length; i-- > 0; ) for (int j = nameSet2.length; j-- > 0; ) if (nameSet1[i] == nameSet2[j]) return true; return false; } private static void DumpHeadForCase(java.io.PrintWriter ostr, int byteNum) { if (byteNum == 0) ostr.println(" long l = 1L << curChar;"); else if (byteNum == 1) ostr.println(" long l = 1L << (curChar & 077);"); else { if (Options.B("JAVA_UNICODE_ESCAPE") || unicodeWarningGiven) { ostr.println(" int hiByte = (int)(curChar >> 8);"); ostr.println(" int i1 = hiByte >> 6;"); ostr.println(" long l1 = 1L << (hiByte & 077);"); } ostr.println(" int i2 = (curChar & 0xff) >> 6;"); ostr.println(" long l2 = 1L << (curChar & 077);"); } ostr.println(" MatchLoop: do"); ostr.println(" {"); ostr.println(" switch(jjstateSet[--i])"); ostr.println(" {"); } private static Vector PartitionStatesSetForAscii(int[] states, int byteNum) { int[] cardinalities = new int[states.length]; Vector original = new Vector(); Vector partition = new Vector(); NfaState tmp; original.setSize(states.length); int cnt = 0; for (int i = 0; i < states.length; i++) { tmp = (NfaState)allStates.elementAt(states[i]); if (tmp.asciiMoves[byteNum] != 0L) { int j; int p = NumberOfBitsSet(tmp.asciiMoves[byteNum]); for (j = 0; j < i; j++) if (cardinalities[j] <= p) break; for (int k = i; k > j; k--) cardinalities[k] = cardinalities[k - 1]; cardinalities[j] = p; original.insertElementAt(tmp, j); cnt++; } } original.setSize(cnt); while (original.size() > 0) { tmp = (NfaState)original.elementAt(0); original.removeElement(tmp); long bitVec = tmp.asciiMoves[byteNum]; Vector subSet = new Vector(); subSet.addElement(tmp); for (int j = 0; j < original.size(); j++) { NfaState tmp1 = (NfaState)original.elementAt(j); if ((tmp1.asciiMoves[byteNum] & bitVec) == 0L) { bitVec |= tmp1.asciiMoves[byteNum]; subSet.addElement(tmp1); original.removeElementAt(j--); } } partition.addElement(subSet); } return partition; } private String PrintNoBreak(java.io.PrintWriter ostr, int byteNum, boolean[] dumped) { if (inNextOf != 1) throw new Error("JavaCC Bug: Please send mail to sankar@cs.stanford.edu"); dumped[stateName] = true; if (byteNum >= 0) { if (asciiMoves[byteNum] != 0L) { ostr.println(" case " + stateName + ":"); DumpAsciiMoveForCompositeState(ostr, byteNum, false); return ""; } } else if (nonAsciiMethod != -1) { ostr.println(" case " + stateName + ":"); DumpNonAsciiMoveForCompositeState(ostr); return ""; } return (" case " + stateName + ":\n"); } private static void DumpCompositeStatesAsciiMoves(java.io.PrintWriter ostr, String key, int byteNum, boolean[] dumped) { int i; int[] nameSet = (int[])allNextStates.get(key); if (nameSet.length == 1 || dumped[StateNameForComposite(key)]) return; NfaState toBePrinted = null; int neededStates = 0; NfaState tmp; NfaState stateForCase = null; String toPrint = ""; boolean stateBlock = (stateBlockTable.get(key) != null); for (i = 0; i < nameSet.length; i++) { tmp = (NfaState)allStates.elementAt(nameSet[i]); if (tmp.asciiMoves[byteNum] != 0L) { if (neededStates++ == 1) break; else toBePrinted = tmp; } else dumped[tmp.stateName] = true; if (tmp.stateForCase != null) { if (stateForCase != null) throw new Error("JavaCC Bug: Please send mail to sankar@cs.stanford.edu : "); stateForCase = tmp.stateForCase; } } if (stateForCase != null) toPrint = stateForCase.PrintNoBreak(ostr, byteNum, dumped); if (neededStates == 0) { if (stateForCase != null && toPrint.equals("")) ostr.println(" break;"); return; } if (neededStates == 1) { //if (byteNum == 1) //System.out.println(toBePrinted.stateName + " is the only state for " //+ key + " ; and key is : " + StateNameForComposite(key)); if (!toPrint.equals("")) ostr.print(toPrint); ostr.println(" case " + StateNameForComposite(key) + ":"); if (!dumped[toBePrinted.stateName] && !stateBlock && toBePrinted.inNextOf > 1) ostr.println(" case " + toBePrinted.stateName + ":"); dumped[toBePrinted.stateName] = true; toBePrinted.DumpAsciiMove(ostr, byteNum, dumped); return; } Vector partition = PartitionStatesSetForAscii(nameSet, byteNum); if (!toPrint.equals("")) ostr.print(toPrint); int keyState = StateNameForComposite(key); ostr.println(" case " + keyState + ":"); if (keyState < generatedStates) dumped[keyState] = true; for (i = 0; i < partition.size(); i++) { Vector subSet = (Vector)partition.elementAt(i); for (int j = 0; j < subSet.size(); j++) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?