@Override public Set<S> getFollowers(final S node) { final S start = nfa.getStart(); final S stop = nfa.getStop(); return filterFollowers(nfa, nfa.getFollowers(node), new Predicate<S>() { @Override public boolean apply(S input) { return input == start || input == stop || filter.apply(input); } }); }
public <S, P, R, D extends Pda<S, P>> D filterEdges(Pda<S, P> pda, Traverser<? super Pda<S, P>, S, R> traverser, PdaFactory<D, S, P, S> factory) { Map<S, Integer> distances = new NfaUtil().distanceToFinalStateMap(pda); return filterEdges(pda, traverser, distances, factory); }
public <S, E, T1, T2, NFA extends Nfa<S>> NFA create(Production<E, T1> production, FollowerFunction<E> ff, Function<E, T2> tokenFunc, NfaFactory<NFA, S, ? super T2> factory, T2 start, T2 stop) { Map<E, S> states = Maps.newHashMap(); NFA nfa = factory.create(start, stop); states.put(null, nfa.getStop()); create(production, nfa, nfa.getStart(), ff.getStarts(production.getRoot()), ff, tokenFunc, factory, states); return nfa; }
public <S, P, E, T1, T2, D extends Pda<S, P>> D create(Cfg<E, T1> cfg, FollowerFunction<E> ff, Function<E, T2> element2token, PdaFactory<D, S, P, ? super T2> fact) { D pda = fact.create(null, null); Map<E, S> states = Maps.newLinkedHashMap(); Map<E, S> stops = Maps.newLinkedHashMap(); Multimap<E, E> callers = new CfgUtil().getCallers(cfg); create(cfg, pda, pda.getStart(), cfg.getRoot(), ff.getStarts(cfg.getRoot()), ff, element2token, fact, states, stops, callers); return pda; }
public <S, P> Nfa<S> filterUnambiguousPaths(Pda<S, P> pda) { Map<S, List<S>> followers = Maps.newLinkedHashMap(); Map<S, Integer> distanceMap = nfaUtil.distanceToFinalStateMap(pda); filterUnambiguousPaths(pda, pda.getStart(), distanceMap, followers); return new NfaUtil.NFAImpl<S>(pda.getStart(), pda.getStop(), followers); }
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)); } }
@Override public CyclicStackItem<P> enter(Pda<S, P> pda, S state, CyclicStackItem<P> previous) { P item; if ((item = pda.getPush(state)) != null) return previous.push(item); if ((item = pda.getPop(state)) != null) return previous.pop(item); if (previous == null) return new CyclicStackItem<P>(); return previous; }
protected <E, T> void getCallers(Cfg<E, T> cfg, E root, Multimap<E, E> callers) { for (E call : new ProductionUtil().getAllChildren(cfg, root)) { E called = cfg.getCall(call); if (called != null) { boolean contained = callers.containsKey(called) || called == cfg.getRoot(); callers.put(called, call); if (!contained) getCallers(cfg, called, callers); } } } }
protected void collectChildrenUnorderedAlt(E element, Iterable<E> alternativeChildren, Set<E> result, Set<E> visited) { boolean hasMandatory = false; for (E child : orderedIterable(alternativeChildren)) { hasMandatory |= !production.isOptional(child); collectElement(child, result, visited); } if (!hasMandatory || production.isOptional(element)) collectByParent(element, result, visited); }
public <S> Map<S, Integer> distanceToFinalStateMap(Nfa<S> nfa) { final S stop = nfa.getStop(); return distanceToStateMap(nfa, new Predicate<S>() { @Override public boolean apply(S input) { return stop == input; } }); }
public String createPush(StringPda pda, T token) { String push = tokenToStr(token, nullState); String s = push.startsWith(">>") ? push : ">>" + push; pda.state(s).push(push); return s; }
public <S, P, D extends Pda<S, P>> D filterOrphans(Pda<S, P> pda, PdaFactory<D, S, P, S> factory) { CyclicStackTraverser<S, P> traverser = new CyclicStackTraverser<S, P>(); return filterEdges(pda, traverser, factory); }
@Override public StringPda create(T start, T stop) { return new StringPda(tokenToStr(start, nullStart), tokenToStr(stop, nullStop)); }
public <S, P, E, T1, T2, D extends Pda<S, P>> D create(Cfg<E, T1> cfg, FollowerFunction<E> ff, Function<E, T2> element2token, PdaFactory<D, S, P, ? super T2> fact) { D pda = fact.create(null, null); Map<E, S> states = Maps.newHashMap(); Map<E, S> stops = Maps.newHashMap(); Multimap<E, E> callers = new CfgUtil().getCallers(cfg); create(cfg, pda, pda.getStart(), cfg.getRoot(), ff.getStarts(cfg.getRoot()), true, ff, element2token, fact, states, stops, callers); return pda; }
public CyclicStackItem<P> enter(Pda<S, P> pda, S state, CyclicStackItem<P> previous) { P item; if ((item = pda.getPush(state)) != null) return previous.push(item); if ((item = pda.getPop(state)) != null) return previous.pop(item); if (previous == null) return new CyclicStackItem<P>(); return previous; }
protected void collectChildrenAlternative(E element, Iterable<E> alternativeChildren, Set<E> result, Set<E> visited) { boolean optional = production.isOptional(element); for (E child : orderedIterable(alternativeChildren)) { optional |= production.isOptional(child); collectElement(child, result, visited); } if (optional) collectByParent(element, result, visited); }