protected <S> void collect(Nfa<S> nfa, S state, Set<S> visited) { if (!visited.add(state)) return; for (S s : nfa.getFollowers(state)) collect(nfa, s, visited); }
public <S> Set<S> collect(Nfa<S> nfa) { Set<S> result = Sets.newLinkedHashSet(); collect(nfa, nfa.getStart(), result); return result; }
protected <S> void collect(Nfa<S> nfa, S state, Set<S> visited) { if (!visited.add(state)) return; for (S s : nfa.getFollowers(state)) collect(nfa, s, visited); }
public <S> Set<S> collect(Nfa<S> nfa) { Set<S> result = Sets.newHashSet(); collect(nfa, nfa.getStart(), result); return result; }
public <S> void removeOrphans(Nfa<S> nfa) { Map<S, Integer> distances = distanceToFinalStateMap(nfa); for (S s : collect(nfa)) { Iterator<S> i = nfa.getFollowers(s).iterator(); while (i.hasNext()) if (!distances.containsKey(i.next())) i.remove(); } }
public <S> void removeOrphans(Nfa<S> nfa) { Map<S, Integer> distances = distanceToFinalStateMap(nfa); for (S s : collect(nfa)) { Iterator<S> i = nfa.getFollowers(s).iterator(); while (i.hasNext()) if (!distances.containsKey(i.next())) i.remove(); } }
public String format(Pda<STATE, STACKITEM> pda) { STATE start = pda.getStart(); List<String> result = Lists.newArrayList(); for (STATE s : new NfaUtil().collect(pda)) if (s != start) { String str = format(pda, s); if (str != null) result.add(str); } Collections.sort(result); String startstring = format(pda, start); if (startstring != null) result.add(0, startstring); return Joiner.on('\n').join(result); }
public String format(Pda<STATE, STACKITEM> pda) { STATE start = pda.getStart(); List<String> result = Lists.newArrayList(); for (STATE s : new NfaUtil().collect(pda)) if (s != start) { String str = format(pda, s); if (str != null) result.add(str); } Collections.sort(result); String startstring = format(pda, start); if (startstring != null) result.add(0, startstring); return Joiner.on('\n').join(result); }
public <S> Nfa<S> sort(Nfa<S> nfa, Comparator<S> comparator) { Map<S, List<S>> followerMap = Maps.newHashMap(); for (S state : new NfaUtil().collect(nfa)) { ArrayList<S> followers = Lists.newArrayList(nfa.getFollowers(state)); Collections.sort(followers, comparator); followerMap.put(state, followers); } return new NFAImpl<S>(nfa.getStart(), nfa.getStop(), followerMap); }
public <S extends Comparable<S>> Nfa<S> sort(Nfa<S> nfa) { Map<S, List<S>> followerMap = Maps.newLinkedHashMap(); for (S state : new NfaUtil().collect(nfa)) { ArrayList<S> followers = Lists.newArrayList(nfa.getFollowers(state)); Collections.sort(followers); followerMap.put(state, followers); } return new NFAImpl<S>(nfa.getStart(), nfa.getStop(), followerMap); }
public <S> Nfa<S> sort(Nfa<S> nfa, Comparator<S> comparator) { Map<S, List<S>> followerMap = Maps.newLinkedHashMap(); for (S state : new NfaUtil().collect(nfa)) { ArrayList<S> followers = Lists.newArrayList(nfa.getFollowers(state)); Collections.sort(followers, comparator); followerMap.put(state, followers); } return new NFAImpl<S>(nfa.getStart(), nfa.getStop(), followerMap); }
public <S extends Comparable<S>> Nfa<S> sort(Nfa<S> nfa) { Map<S, List<S>> followerMap = Maps.newHashMap(); for (S state : new NfaUtil().collect(nfa)) { ArrayList<S> followers = Lists.newArrayList(nfa.getFollowers(state)); Collections.sort(followers); followerMap.put(state, followers); } return new NFAImpl<S>(nfa.getStart(), nfa.getStop(), followerMap); }
public <S, P, D extends Pda<S, P>> Map<P, Pair<S, S>> collectPopsAndPushs(Pda<S, P> pda) { Map<P, Pair<S, S>> result = Maps.newLinkedHashMap(); for (S s : nfaUtil.collect(pda)) { P push = pda.getPush(s); if (push != null) { Pair<S, S> o = result.get(push); Pair<S, S> n = Tuples.create(s, o == null ? null : o.getSecond()); result.put(push, n); } P pop = pda.getPop(s); if (pop != null) { Pair<S, S> o = result.get(pop); Pair<S, S> n = Tuples.create(o == null ? null : o.getFirst(), s); result.put(pop, n); } } return result; }
protected <T> boolean createOptional(StateAliasNfa<T> states) { List<StateAlias<T>> opt = Lists.newArrayList(); L: for (StateAlias<T> cand : new NfaUtil().collect(states)) { if (cand.getIncoming().isEmpty() || cand.getOutgoing().isEmpty()) continue L; for (StateAlias<T> in : cand.getIncoming()) if (!in.getOutgoing().containsAll(cand.getOutgoing())) continue L; opt.add(cand); } for (StateAlias<T> o : opt) { o.getElement().setOptional(true); for (StateAlias<T> in : Lists.newArrayList(o.getIncoming())) if (in != o) for (StateAlias<T> out : Lists.newArrayList(o.getOutgoing())) if (out != o) { out.getIncoming().remove(in); in.getOutgoing().remove(out); } } return !opt.isEmpty(); }
protected void initOrderIDs(Nfa<ISemState> nfa) { for (ISemState state : new NfaUtil().collect(nfa)) if (state.getAssignedGrammarElement() != null) ((SemState) state).orderID = getElementID(state.getAssignedGrammarElement()); }
protected void initContentValidationNeeded(EClass clazz, Nfa<ISemState> nfa) { Multimap<EStructuralFeature, AbstractElement> assignments = HashMultimap.create(); Set<ISemState> states = new NfaUtil().collect(nfa); for (ISemState state : states) if (state.getFeature() != null) assignments.put(state.getFeature(), state.getAssignedGrammarElement()); boolean[] validationNeeded = new boolean[clazz.getFeatureCount()]; for (EStructuralFeature feature : clazz.getEAllStructuralFeatures()) validationNeeded[clazz.getFeatureID(feature)] = isContentValidationNeeded(assignments.get(feature)); for (ISemState state : states) if (state.getFeature() != null && validationNeeded[state.getFeatureID()]) ((SemState) state).contentValidationNeeded = Lists.newArrayList(assignments.get(state.getFeature())); else ((SemState) state).contentValidationNeeded = Collections.emptyList(); }
protected void draw(Digraph result, Pda<STATE, STACKITEM> pda) { for (STATE s : new NfaUtil().collect(pda)) { result.add(create(result, pda, s)); for (STATE f : pda.getFollowers(s)) result.add(create(result, pda, s, f)); } }
protected void draw(Digraph result, Nfa<STATE> nfa) { for (STATE s : new NfaUtil().collect(nfa)) { result.add(create(result, nfa, s)); for (STATE f : nfa.getFollowers(s)) result.add(create(result, nfa, s, f)); } }
protected void draw(Digraph result, Nfa<STATE> nfa) { for (STATE s : new NfaUtil().collect(nfa)) { result.add(create(result, nfa, s)); for (STATE f : nfa.getFollowers(s)) result.add(create(result, nfa, s, f)); } }
protected void draw(Digraph result, Pda<STATE, STACKITEM> pda) { for (STATE s : new NfaUtil().collect(pda)) { result.add(create(result, pda, s)); for (STATE f : pda.getFollowers(s)) result.add(create(result, pda, s, f)); } }