/** DFS-based topological sort. A valid sort is the reverse of * the post-order DFA traversal. Amazingly simple but true. * For sorting, I'm not following convention here since ANTLR * needs the opposite. Here's what I assume for sorting: * * If there exists an edge u → v then u depends on v and v * must happen before u. * * So if this gives nonreversed postorder traversal, I get the order * I want. */ public List<T> sort() { Set<Node<T>> visited = new OrderedHashSet<Node<T>>(); ArrayList<T> sorted = new ArrayList<T>(); while ( visited.size() < nodes.size() ) { // pick any unvisited node, n Node<T> n = null; for (Node<T> tNode : nodes.values()) { n = tNode; if ( !visited.contains(n) ) break; } DFS(n, visited, sorted); } return sorted; }
/** Two DFAStates are equal if their NFA configuration sets are the * same. This method is used to see if a DFA state already exists. * * Because the number of alternatives and number of NFA configurations are * finite, there is a finite number of DFA states that can be processed. * This is necessary to show that the algorithm terminates. * * Cannot test the DFA state numbers here because in DFA.addState we need * to know if any other state exists that has this exact set of NFA * configurations. The DFAState state number is irrelevant. */ @Override public boolean equals(Object o) { // compare set of NFA configurations in this set with other DFAState other = (DFAState)o; return this.nfaConfigurations.equals(other.nfaConfigurations); }
/** Return the uniquely mentioned alt from the NFA configurations; * Ignore the resolved bit etc... Return INVALID_ALT_NUMBER * if there is more than one alt mentioned. */ public int getUniqueAlt() { int alt = NFA.INVALID_ALT_NUMBER; int numConfigs = nfaConfigurations.size(); for (int i = 0; i < numConfigs; i++) { NFAConfiguration configuration = (NFAConfiguration) nfaConfigurations.get(i); if ( alt==NFA.INVALID_ALT_NUMBER ) { alt = configuration.alt; // found first alt } else if ( configuration.alt!=alt ) { return NFA.INVALID_ALT_NUMBER; } } return alt; }
protected List<IntervalSet> makeEdgeSetsDisjoint(List<IntervalSet> edges) { OrderedHashSet<IntervalSet> disjointSets = new OrderedHashSet<IntervalSet>(); if ( disjointSets.contains(t) ) { // exact set present continue; int numDisjointElements = disjointSets.size(); for (int i = 0; i < numDisjointElements; i++) { IntervalSet s_i = disjointSets.get(i); disjointSets.set(i, intersection); disjointSets.add(existingMinusNewElements); disjointSets.add(remainder); return disjointSets.elements();
int numConfigs = d.nfaConfigurations.size(); for (int i = 0; i < numConfigs; i++) { NFAConfiguration c = d.nfaConfigurations.get(i); if ( c.resolved || c.resolveWithPredicate ) { continue; // the conflict resolver indicates we must leave alone d.nfaConfigurations.clear(); d.addNFAConfiguration(p,c.alt,c.context,c.semanticContext);
for (Iterator it = nondeterministicAlts.iterator(); it.hasNext();) { Integer altI = (Integer) it.next(); altToSetOfContextsMap.put(altI, new OrderedHashSet<SemanticContext>()); int numConfigs = d.nfaConfigurations.size(); for (int i = 0; i < numConfigs; i++) { NFAConfiguration configuration = (NFAConfiguration)d.nfaConfigurations.get(i); Integer altI = Utils.integer(configuration.alt); NFAConfiguration configuration = (NFAConfiguration)d.nfaConfigurations.get(i); Integer altI = Utils.integer(configuration.alt); if ( incompletelyCoveredAlts.contains(altI) &&
if ( dfa.nfa.grammar.composite.watchNFAConversion ) { System.out.println("convert DFA state "+d.stateNumber+ " ("+d.nfaConfigurations.size()+" nfa states)");
NFAConfiguration anyConfig = d.nfaConfigurations.get(0); NFAState anyState = dfa.nfa.getState(anyConfig.state);
protected List<IntervalSet> makeEdgeSetsDisjoint(List<IntervalSet> edges) { OrderedHashSet<IntervalSet> disjointSets = new OrderedHashSet<IntervalSet>(); if ( disjointSets.contains(t) ) { // exact set present continue; int numDisjointElements = disjointSets.size(); for (int i = 0; i < numDisjointElements; i++) { IntervalSet s_i = disjointSets.get(i); disjointSets.set(i, intersection); disjointSets.add(existingMinusNewElements); disjointSets.add(remainder); return disjointSets.elements();
int numConfigs = d.nfaConfigurations.size(); for (int i = 0; i < numConfigs; i++) { NFAConfiguration c = d.nfaConfigurations.get(i); if ( c.resolved || c.resolveWithPredicate ) { continue; // the conflict resolver indicates we must leave alone d.nfaConfigurations.clear(); d.addNFAConfiguration(p,c.alt,c.context,c.semanticContext);
new HashMap<Integer, OrderedHashSet<SemanticContext>>(); for (Integer altI : nondeterministicAlts) { altToSetOfContextsMap.put(altI, new OrderedHashSet<SemanticContext>()); int numConfigs = d.nfaConfigurations.size(); for (int i = 0; i < numConfigs; i++) { NFAConfiguration configuration = d.nfaConfigurations.get(i); Integer altI = Utils.integer(configuration.alt); NFAConfiguration configuration = d.nfaConfigurations.get(i); Integer altI = Utils.integer(configuration.alt); if ( incompletelyCoveredAlts.contains(altI) &&
if ( labelDFATarget.nfaConfigurations.size()==0 ) {
NFAConfiguration anyConfig = d.nfaConfigurations.get(0); NFAState anyState = dfa.nfa.getState(anyConfig.state);
protected List<IntervalSet> makeEdgeSetsDisjoint(List<IntervalSet> edges) { OrderedHashSet<IntervalSet> disjointSets = new OrderedHashSet<IntervalSet>(); if ( disjointSets.contains(t) ) { // exact set present continue; int numDisjointElements = disjointSets.size(); for (int i = 0; i < numDisjointElements; i++) { IntervalSet s_i = (IntervalSet)disjointSets.get(i); disjointSets.set(i, intersection); disjointSets.add(existingMinusNewElements); disjointSets.add(remainder); return disjointSets.elements();
/** Return the uniquely mentioned alt from the NFA configurations; * Ignore the resolved bit etc... Return INVALID_ALT_NUMBER * if there is more than one alt mentioned. */ public int getUniqueAlt() { int alt = NFA.INVALID_ALT_NUMBER; int numConfigs = nfaConfigurations.size(); for (int i = 0; i < numConfigs; i++) { NFAConfiguration configuration = nfaConfigurations.get(i); if ( alt==NFA.INVALID_ALT_NUMBER ) { alt = configuration.alt; // found first alt } else if ( configuration.alt!=alt ) { return NFA.INVALID_ALT_NUMBER; } } return alt; }
int numConfigs = d.nfaConfigurations.size(); for (int i = 0; i < numConfigs; i++) { NFAConfiguration c = (NFAConfiguration)d.nfaConfigurations.get(i); if ( c.resolved || c.resolveWithPredicate ) { continue; // the conflict resolver indicates we must leave alone d.nfaConfigurations.clear(); d.addNFAConfiguration(p,c.alt,c.context,c.semanticContext);