protected <S, P, T, D extends Pda<S, P>> S clone(S state, Pda<S, P> src, D target, Function<S, T> tokens, PdaFactory<D, S, P, T> fact, Identity<S> identity) { if (state == src.getStart()) return target.getStart(); if (state == src.getStop()) return target.getStop(); P push = src.getPush(state); if (push != null) return identity.get(fact.createPush(target, tokens.apply(state))); P pop = src.getPop(state); if (pop != null) return identity.get(fact.createPop(target, tokens.apply(state))); return identity.get(fact.createState(target, tokens.apply(state))); }
protected <S, R, P> TraversalItem<S, R> newItem(Pda<S, P> pda, MappedComparator<S, Integer> comp, Map<S, Integer> distances, S next, R item) { List<S> followers = Lists.newArrayList(); for (S f : pda.getFollowers(next)) if (distances.containsKey(f)) followers.add(f); Collections.sort(followers, comp); return new TraversalItem<S, R>(next, followers, item); }
protected String stateToString(Pda<STATE, STACKITEM> pda, STATE state) { STACKITEM push = pda.getPush(state); STACKITEM pop = pda.getPop(state); if (pushFormatter != null && push != null) return pushFormatter.apply(push); if (popFormatter != null && pop != null) return popFormatter.apply(pop); String prefix = push != null && pop != null ? "<>" : push != null ? ">>" : pop != null ? "<<" : ""; if (stateFormatter != null) { String fmt = stateFormatter.apply(state); return fmt.startsWith(prefix) ? fmt : prefix + fmt; } if (state == null) return prefix + "null"; String fmt = state.toString(); return fmt.startsWith(prefix) ? fmt : prefix + fmt; } }
public <S, P> List<S> shortestStackpruningPathTo(Pda<S, P> pda, Iterator<P> stack, Predicate<S> matches) { return shortestStackpruningPathTo(pda, pda.getStart(), stack, matches, Predicates.<S> alwaysTrue()); }
List<TraceItem<S, P>> newCurrent = Lists.newArrayList(); for (TraceItem<S, P> trace : current) for (S follower : pda.getFollowers(trace.state)) { if (matches.apply(follower)) return new TraceItem<S, P>(trace, follower, trace.stackitem); if (canPass.apply(follower)) { P push = pda.getPush(follower); visited.add(follower); if (push != null) { newCurrent.add(new TraceItem<S, P>(trace, follower, pushed)); } else { P pop = pda.getPop(follower); if (pop != null) { if (trace.stackitem != null && pop == trace.stackitem.peek()) {
public <S, P, T, D extends Pda<S, P>> D expand(Pda<S, P> pda, Function<S, Pda<S, P>> expand, Function<S, T> tokens, PdaFactory<D, S, P, T> fact) { D result = fact.create(tokens.apply(pda.getStart()), tokens.apply(pda.getStop())); Identity<S> identity = new Identity<S>(); Map<S, S> idstates = Maps.newIdentityHashMap(); S s_stop = identity.get(fact.createPop(result, tokens.apply(s_old))); idstates.put(s_old, s_start); idstates.put(sub.getStart(), s_start); idstates.put(sub.getStop(), s_stop); followers.putAll(s_start, sub.getFollowers(sub.getStart())); followers.putAll(s_stop, pda.getFollowers(s_old)); for (S f_old : nfaUtil.collect(sub)) if (f_old != sub.getStart() && f_old != sub.getStop()) { S f_new = idstates.get(f_old); if (f_new == null) { idstates.put(f_old, f_new = clone(f_old, sub, result, tokens, fact, identity)); followers.putAll(f_new, pda.getFollowers(f_old)); followers.putAll(s_new, pda.getFollowers(s_old));
@Override public boolean apply(S input) { return pda.getPop(input) != null; } }
E root = new ProductionUtil().getRoot(cfg, ele); if (root == cfg.getRoot()) followerStates.add(pda.getStop()); for (E c : callers.get(root)) { S s = stops.get(c);
public <S, P> List<S> shortestPathTo(Pda<S, P> pda, Iterator<P> stack, S match) { return shortestPathTo(pda, pda.getStart(), stack, Predicates.equalTo(match), Predicates.<S> alwaysTrue()); }
List<TraceItem<S, P>> newCurrent = Lists.newArrayList(); for (TraceItem<S, P> trace : current) for (S follower : pda.getFollowers(trace.state)) { if (matches.apply(follower)) return new TraceItem<S, P>(trace, follower, trace.stackitem); if (canPass.apply(follower)) { P push = pda.getPush(follower); visited.add(follower); if (push != null) { newCurrent.add(new TraceItem<S, P>(trace, follower, pushed)); } else { P pop = pda.getPop(follower); if (pop != null) { if (trace.stackitem != null && pop == trace.stackitem.peek()) {
protected String stateToString(Pda<STATE, STACKITEM> pda, STATE state) { STACKITEM push = pda.getPush(state); STACKITEM pop = pda.getPop(state); if (pushFormatter != null && push != null) return pushFormatter.apply(push); if (popFormatter != null && pop != null) return popFormatter.apply(pop); String prefix = push != null && pop != null ? "<>" : push != null ? ">>" : pop != null ? "<<" : ""; if (stateFormatter != null) { String fmt = stateFormatter.apply(state); return fmt.startsWith(prefix) ? fmt : prefix + fmt; } if (state == null) return prefix + "null"; String fmt = state.toString(); return fmt.startsWith(prefix) ? fmt : prefix + fmt; } }
public <S, P, T, D extends Pda<S, P>> D expand(Pda<S, P> pda, Function<S, Pda<S, P>> expand, Function<S, T> tokens, PdaFactory<D, S, P, T> fact) { D result = fact.create(tokens.apply(pda.getStart()), tokens.apply(pda.getStop())); Identity<S> identity = new Identity<S>(); Map<S, S> idstates = Maps.newIdentityHashMap(); S s_stop = identity.get(fact.createPop(result, tokens.apply(s_old))); idstates.put(s_old, s_start); idstates.put(sub.getStart(), s_start); idstates.put(sub.getStop(), s_stop); followers.putAll(s_start, sub.getFollowers(sub.getStart())); followers.putAll(s_stop, pda.getFollowers(s_old)); for (S f_old : nfaUtil.collect(sub)) if (f_old != sub.getStart() && f_old != sub.getStop()) { S f_new = idstates.get(f_old); if (f_new == null) { idstates.put(f_old, f_new = clone(f_old, sub, result, tokens, fact, identity)); followers.putAll(f_new, pda.getFollowers(f_old)); followers.putAll(s_new, pda.getFollowers(s_old));
public boolean apply(S input) { return pda.getPop(input) != null; } }
E root = new ProductionUtil().getRoot(cfg, ele); if (root == cfg.getRoot()) followerStates.add(pda.getStop()); for (E c : callers.get(root)) { S s = stops.get(c);
protected <S, P, T, D extends Pda<S, P>> S clone(S state, Pda<S, P> src, D target, Function<S, T> tokens, PdaFactory<D, S, P, T> fact, Identity<S> identity) { if (state == src.getStart()) return target.getStart(); if (state == src.getStop()) return target.getStop(); P push = src.getPush(state); if (push != null) return identity.get(fact.createPush(target, tokens.apply(state))); P pop = src.getPop(state); if (pop != null) return identity.get(fact.createPop(target, tokens.apply(state))); return identity.get(fact.createState(target, tokens.apply(state))); }
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); }
public <S, P> List<S> shortestPathTo(Pda<S, P> pda, Iterator<P> stack, Predicate<S> matches, Predicate<S> canPass) { return shortestPathTo(pda, pda.getStart(), stack, matches, canPass); }
List<TraceItem<S, P>> newCurrent = Lists.newArrayList(); for (TraceItem<S, P> trace : current) for (S follower : pda.getFollowers(trace.state)) { if (matches.apply(follower)) { TraceItem<S, P> found = new TraceItem<S, P>(trace, follower, trace.stackitem); P push = pda.getPush(follower); visited.add(follower); if (push != null) { newCurrent.add(new TraceItem<S, P>(trace, follower, pushed)); } else { P pop = pda.getPop(follower); if (pop != null) { if (trace.stackitem != null && pop == trace.stackitem.peek()) {