Beispiel #1
0
 protected <S, P> void filterUnambiguousPaths(
     final Pda<S, P> pda, S state, Map<S, Integer> dist, Map<S, List<S>> followers) {
   if (followers.containsKey(state)) return;
   List<S> f = Lists.newArrayList(pda.getFollowers(state));
   if (f.size() <= 1) {
     followers.put(state, f);
     if (f.size() == 1) filterUnambiguousPaths(pda, f.get(0), dist, followers);
     return;
   }
   int closestDist = dist.get(f.get(0));
   S closest = f.get(0);
   for (int i = 1; i < f.size(); i++) {
     int d = dist.get(f.get(i));
     if (d < closestDist) {
       closestDist = d;
       closest = f.get(i);
     }
   }
   IsPop<S, P> isPop = new IsPop<S, P>(pda);
   Set<S> closestPops = nfaUtil.findFirst(pda, Collections.singleton(closest), isPop);
   Iterator<S> it = f.iterator();
   while (it.hasNext()) {
     S next = it.next();
     if (next != closest) {
       Set<S> nextPops = nfaUtil.findFirst(pda, Collections.singleton(next), isPop);
       if (!closestPops.equals(nextPops)) it.remove();
     }
   }
   followers.put(state, f);
   for (S follower : f) filterUnambiguousPaths(pda, follower, dist, followers);
 }
Beispiel #2
0
 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();
   Multimap<S, S> followers = LinkedHashMultimap.create();
   for (S s_old : nfaUtil.collect(pda)) {
     S s_new = idstates.get(s_old);
     if (s_new == null) {
       Pda<S, P> sub = expand.apply(s_old);
       if (sub != null) {
         S s_start = identity.get(fact.createPush(result, tokens.apply(s_old)));
         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));
             }
           }
       } else {
         idstates.put(s_old, s_new = clone(s_old, pda, result, tokens, fact, identity));
         followers.putAll(s_new, pda.getFollowers(s_old));
       }
     }
   }
   for (Map.Entry<S, Collection<S>> entry : followers.asMap().entrySet()) {
     Set<S> f = Sets.newLinkedHashSet();
     for (S s : entry.getValue()) f.add(idstates.get(s));
     fact.setFollowers(result, entry.getKey(), f);
   }
   return result;
 }
Beispiel #3
0
 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;
 }
Beispiel #4
0
 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);
 }